Skip to content
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

feat: allow non alphanumeric characters in enum class names #1297

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.