Skip to content

Commit

Permalink
feat: allow non alphanumeric characters in enum class names (#1297)
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
Co-authored-by: Nikolas Rimikis <leptopoda@users.noreply.github.com>
  • Loading branch information
Leptopoda and Leptopoda authored Jan 12, 2024
1 parent 861fadb commit c575020
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# 8.9.0-wip

- Add ignoring types that the StandardJsonPlugin should leave as a List.
- Allow non alphanumeric characters in enum class names.

# 8.8.1

Expand Down
15 changes: 8 additions & 7 deletions built_value_generator/lib/src/enum_source_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ abstract class EnumSourceClass
if (getter == null) return null;
var source = parsedLibrary.getElementDeclaration(getter)!.node.toSource();
var matches = RegExp(r'static BuiltSet<' +
element.displayName +
r'> get values => (_\$\w+)\;')
RegExp.escape(element.displayName) +
r'> get values => (_\$[\w$]+)\;')
.allMatches(source);
return matches.isEmpty ? null : matches.first.group(1);
}
Expand All @@ -93,8 +93,8 @@ abstract class EnumSourceClass
if (getter == null) return null;
var source = parsedLibrary.getElementDeclaration(getter)!.node.toSource();
var matches = RegExp(r'static ' +
element.displayName +
r' valueOf\((?:final )?String name\) \=\> (\_\$\w+)\(name\)\;')
RegExp.escape(element.displayName) +
r' valueOf\((?:final )?String name\) \=\> (\_\$[\w$]+)\(name\)\;')
.allMatches(source);
return matches.isEmpty ? null : matches.first.group(1);
}
Expand Down Expand Up @@ -153,9 +153,10 @@ abstract class EnumSourceClass
}

Iterable<String> _checkConstructor() {
var expectedCode =
RegExp('const $name._\\((?:final )?String name\\) : super\\(name\\);');
var expectedCode217 = RegExp('const $name._\\(super.name\\);');
var expectedCode = RegExp(
'const ${RegExp.escape(name)}._\\((?:final )?String name\\) : super\\(name\\);');
var expectedCode217 =
RegExp('const ${RegExp.escape(name)}._\\(super.name\\);');
return constructors.length == 1 &&
(constructors.single.contains(expectedCode) ||
constructors.single.contains(expectedCode217))
Expand Down
20 changes: 20 additions & 0 deletions end_to_end_test/lib/enums.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,23 @@ class FallbackNumberEnum extends EnumClass {
static BuiltSet<FallbackNumberEnum> get values => _$fbNumberValues;
static FallbackNumberEnum valueOf(String name) => _$fbNumberValueOf(name);
}

class EnumWith$Dollar_UnderScore extends EnumClass {
static Serializer<EnumWith$Dollar_UnderScore> get serializer =>
_$enumWith$DollarUnderScoreSerializer;

@BuiltValueEnumConst(wireNumber: 0)
static const EnumWith$Dollar_UnderScore $value =
_$dollar_UnderScoreEnum$Value;

@BuiltValueEnumConst(wireNumber: -1, fallback: true)
static const EnumWith$Dollar_UnderScore value$ =
_$dollar_UnderScoreEnumValue$;

const EnumWith$Dollar_UnderScore._(String name) : super(name);

static BuiltSet<EnumWith$Dollar_UnderScore> get values =>
_$enum$Dollar_UnderScoreValues;
static EnumWith$Dollar_UnderScore valueOf(String name) =>
_$enum$Dollar_UnderScoreValueOf(name);
}
53 changes: 53 additions & 0 deletions end_to_end_test/lib/enums.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c575020

Please sign in to comment.