Skip to content

Commit

Permalink
fix: Read Dart reserved keyword from analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Mar 19, 2023
1 parent 5f1f28b commit f6a2b45
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 127 deletions.
145 changes: 25 additions & 120 deletions bin/src/utils.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
import 'package:code_builder/code_builder.dart';
import 'package:meta/meta.dart' show visibleForTesting;
import 'package:recase/recase.dart';
import 'package:analyzer/dart/ast/token.dart' show Keyword;

extension DartStyleName on String {
/// These Dart reserved keywords are not allowed as field names
@visibleForTesting
static final Iterable<String> dartReservedPropertyKeywords = Keyword.values
.where((element) => element.isReservedWord)
.map((e) => e.lexeme.toLowerCase());

/// Prisma where reversed keywords
@visibleForTesting
static const Iterable<String> prismaWhereReservedKeywords = [
'and',
'or',
'not',
];

/// Convert to dart class name
String toDartClassName() {
if (whereReservedKeywords.map((e) => e.toUpperCase()).contains(this)) {
return pascalCase;
}
if (reservedClassKeywords.contains(toLowerCase())) {
return r'$' + pascalCase;
}
return _toDartPublicName().pascalCase;
}
String toDartClassName() => _toDartPublicName().pascalCase;

/// Convert to dart property name
String toDartPropertyName() {
if (whereReservedKeywords.map((e) => e.toUpperCase()).contains(this)) {
if (prismaWhereReservedKeywords
.map((e) => e.toUpperCase())
.contains(this)) {
return this;
}
if (reservedPropertyKeywords.contains(toLowerCase())) {
return r'$' + camelCase;

final property = _toDartPublicName().camelCase;
if (dartReservedPropertyKeywords.contains(property.toLowerCase())) {
return r'$' + property;
}
return _toDartPublicName().camelCase;

return property;
}

/// To dart public property/class name
Expand All @@ -48,110 +60,3 @@ extension NullableReferenceExtension on Reference {
}
});
}

/// Prisma where reversed keywords
@visibleForTesting
const Iterable<String> whereReservedKeywords = [
'and',
'or',
'not',
];

/// These Dart reserved keywords are not allowed as field names
@visibleForTesting
const Iterable<String> reservedPropertyKeywords = [
'abstract',
'as',
'async',
'await',
'covariant',
'deferred',
"default",
'dynamic',
'export',
'extension',
'external',
'factory',
'function',
'get',
'hide',
'in',
'is',
'implements',
'import',
'interface',
'late',
'library',
'mixin',
'on',
'operator',
'part',
'required',
'set',
'show',
'static',
'sync',
'typedef',
'yield',
];

/// These Dart reserved keywords are not allowed as class or type names
@visibleForTesting
const Iterable<String> reservedClassKeywords = [
'abstract',
'as',
'assert',
'break',
'case',
'catch',
'class',
'const',
'continue',
'covariant',
'default',
'deferred',
'do',
'dynamic',
'else',
'enum',
'export',
'extends',
'extension',
'external',
'factory',
'false',
'final',
'finally',
'for',
'function',
'get',
'if',
'implements',
'import',
'in',
'interface',
'is',
'late',
'library',
'mixin',
'new',
'null',
'operator',
'part',
'required',
'rethrow',
'return',
'set',
'static',
'super',
'switch',
'this',
'throw',
'true',
'try',
'typedef',
'var',
'void',
'while',
'with',
];
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ executables:
prisma: orm

dependencies:
analyzer: ^5.8.0
args: ^2.3.1
code_builder: ^4.4.0
crypto: ^3.0.2
Expand Down
12 changes: 5 additions & 7 deletions test/generator/utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ void main() {
expect('a_b_c'.toDartClassName(), 'ABC');
expect('_a'.toDartClassName(), r'$a');

for (final keyword in reservedClassKeywords) {
expect(keyword.toDartClassName(), '\$${keyword.pascalCase}');
}
for (final keyword in whereReservedKeywords) {
for (final keyword in DartStyleName.prismaWhereReservedKeywords) {
expect(keyword.toDartClassName(), keyword.pascalCase);
}
});
Expand All @@ -28,11 +25,12 @@ void main() {
expect('a_b_c'.toDartPropertyName(), 'aBC');
expect('_a'.toDartPropertyName(), r'$a');

for (final keyword in reservedPropertyKeywords) {
for (final keyword in DartStyleName.dartReservedPropertyKeywords) {
expect(keyword.toDartPropertyName(), '\$${keyword.camelCase}');
}
for (final keyword in whereReservedKeywords) {
expect(keyword.toDartPropertyName(), keyword.camelCase);

for (final keyword in DartStyleName.prismaWhereReservedKeywords) {
expect(keyword.toDartPropertyName(), keyword);
}
});
});
Expand Down

0 comments on commit f6a2b45

Please sign in to comment.