-
Notifications
You must be signed in to change notification settings - Fork 0
fix: v0.5.5 — parser brace-counting, implicit relations, FK ordering #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -422,11 +422,11 @@ class PrismaParser { | |
| datasourceProvider = datasourceMatch.group(1)!; | ||
| } | ||
|
|
||
| // Extract enums | ||
| final enumPattern = RegExp(r'enum\s+(\w+)\s*\{([^}]+)\}', multiLine: true); | ||
| for (final match in enumPattern.allMatches(schemaContent)) { | ||
| final name = match.group(1)!; | ||
| final body = match.group(2)!; | ||
| // Extract enums (use brace-counting to handle comments with braces) | ||
| final enumBlocks = _extractBlocks(schemaContent, 'enum'); | ||
| for (final block in enumBlocks) { | ||
| final name = block.name; | ||
| final body = block.body; | ||
| final values = body | ||
| .split('\n') | ||
| .map((line) { | ||
|
|
@@ -447,18 +447,16 @@ class PrismaParser { | |
| final modelNameMap = <String, String>{}; // originalName -> dartName | ||
|
|
||
| // First pass: collect all model name mappings | ||
| final modelPattern = | ||
| RegExp(r'model\s+(\w+)\s*\{([^}]+)\}', multiLine: true); | ||
| for (final match in modelPattern.allMatches(schemaContent)) { | ||
| final originalModelName = match.group(1)!; | ||
| final modelResult = _handleReservedKeyword(originalModelName, 'model'); | ||
| modelNameMap[originalModelName] = modelResult.dartName; | ||
| final modelBlocks = _extractBlocks(schemaContent, 'model'); | ||
| for (final block in modelBlocks) { | ||
| final modelResult = _handleReservedKeyword(block.name, 'model'); | ||
| modelNameMap[block.name] = modelResult.dartName; | ||
| } | ||
|
|
||
| // Second pass: parse models with resolved type names | ||
| for (final match in modelPattern.allMatches(schemaContent)) { | ||
| final originalModelName = match.group(1)!; | ||
| final modelBody = match.group(2)!; | ||
| for (final block in modelBlocks) { | ||
| final originalModelName = block.name; | ||
| final modelBody = block.body; | ||
|
|
||
| // Handle reserved keywords - auto-rename if needed | ||
| final modelResult = _handleReservedKeyword(originalModelName, 'model'); | ||
|
|
@@ -527,8 +525,11 @@ class PrismaParser { | |
| } | ||
| } | ||
|
|
||
| // Check if it's a relation | ||
| final isRelation = attributes.contains('@relation'); | ||
| // Check if it's a relation — explicit (@relation) or implicit (type is a model) | ||
| final baseFieldType = | ||
| fieldType.replaceAll('?', '').replaceAll('[]', ''); | ||
|
Comment on lines
+529
to
+530
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| final isRelation = attributes.contains('@relation') || | ||
| modelNameMap.containsKey(baseFieldType); | ||
| String? relationName; | ||
| List<String>? relationFromFields; | ||
| List<String>? relationToFields; | ||
|
|
@@ -637,6 +638,29 @@ class PrismaParser { | |
| ); | ||
| } | ||
|
|
||
| /// Extract model/enum blocks using brace counting instead of [^}] regex. | ||
| /// | ||
| /// Handles inline comments containing `{` or `}` which break simple regex. | ||
| List<_Block> _extractBlocks(String content, String keyword) { | ||
| final blocks = <_Block>[]; | ||
| final pattern = RegExp('$keyword\\s+(\\w+)\\s*\\{'); | ||
| for (final match in pattern.allMatches(content)) { | ||
| final name = match.group(1)!; | ||
| final start = match.end; // position after the opening { | ||
| var depth = 1; | ||
| var i = start; | ||
| while (i < content.length && depth > 0) { | ||
| final ch = content[i]; | ||
| if (ch == '{') depth++; | ||
| if (ch == '}') depth--; | ||
| i++; | ||
| } | ||
| // i is now past the closing }, body is between start and i-1 | ||
| blocks.add(_Block(name: name, body: content.substring(start, i - 1))); | ||
|
Comment on lines
+652
to
+659
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The brace-counting logic is still susceptible to the same truncation issue if a comment contains an unbalanced closing brace (e.g., while (i < content.length && depth > 0) {
final ch = content[i];
// Skip inline comments to avoid false depth triggers from braces in comments
if (ch == '/' && i + 1 < content.length && content[i + 1] == '/') {
while (i < content.length && content[i] != '\n') i++;
continue;
}
if (ch == '{') depth++;
if (ch == '}') depth--;
i++;
}
if (depth == 0) {
blocks.add(_Block(name: name, body: content.substring(start, i - 1)));
} |
||
| } | ||
| return blocks; | ||
| } | ||
|
|
||
| /// Extracts content inside @default(...) handling nested parentheses. | ||
| /// | ||
| /// For example: | ||
|
|
@@ -670,3 +694,10 @@ class PrismaParser { | |
| return attributes.substring(contentStart, i - 1); | ||
| } | ||
| } | ||
|
|
||
| /// A named block extracted from a Prisma schema (model or enum). | ||
| class _Block { | ||
| final String name; | ||
| final String body; | ||
| const _Block({required this.name, required this.body}); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The call to
_findForeignKeyOnModelis currently unreachable because_findForeignKeyFromFieldalways returns a non-null string (it falls back to a default convention if no explicit relation is found). This logic should be restructured to prioritize explicit relations on the field, then check for explicit relations elsewhere in the model, and finally fall back to the naming convention.