Skip to content

Commit

Permalink
fix: Enum constructor clause (#12)
Browse files Browse the repository at this point in the history
Co-authored-by: Lukas Klingsbo <[email protected]>
  • Loading branch information
bdlukaa and spydon authored Oct 30, 2023
1 parent 9f0cbf8 commit 52c8cfb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/src/constructor_declaration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ Map<String, dynamic>? serializeConstructorDeclaration(
hasPrivateAnnotation(annotations)) {
return null;
}
final className = (constructor.parent! as ClassDeclaration).name.lexeme;
final String className;

if (constructor.parent is ClassDeclaration) {
className = (constructor.parent! as ClassDeclaration).name.lexeme;
} else if (constructor.parent is EnumDeclaration) {
className = (constructor.parent! as EnumDeclaration).name.lexeme;
} else {
throw UnsupportedError(
'Constructor parent is neither ClassDeclaration nor EnumDeclaration. '
'It is ${constructor.parent.runtimeType}',
);
}

final constructorName = constructor.name == null
? className
: '$className.${constructor.name!.lexeme}';
Expand Down
22 changes: 22 additions & 0 deletions test/enum_declaration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,26 @@ void main() {
);
});
});

test('enum with constructor', () {
expect(
parseAsJson('''
enum Abc {
a;
const Abc();
}
'''),
{
'kind': 'enum',
'name': 'Abc',
'values': [
{'name': 'a'},
],
'members': [
{'kind': 'constructor', 'name': 'Abc', 'const': true},
],
},
);
});
}

0 comments on commit 52c8cfb

Please sign in to comment.