mirror of
https://github.com/flutter/packages.git
synced 2025-07-01 15:23:25 +08:00
[pigeon] added error for static fields (#392)
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
## NEXT
|
## NEXT
|
||||||
|
|
||||||
* [front-end] Added a more explicit error if generic fields are used.
|
* [front-end] Added a more explicit error if generic fields are used.
|
||||||
|
* [front-end] Added a more explicit error for static fields.
|
||||||
|
|
||||||
## 0.3.0
|
## 0.3.0
|
||||||
|
|
||||||
|
@ -683,7 +683,12 @@ class _RootBuilder extends dart_ast_visitor.RecursiveAstVisitor<Object?> {
|
|||||||
Object? visitFieldDeclaration(dart_ast.FieldDeclaration node) {
|
Object? visitFieldDeclaration(dart_ast.FieldDeclaration node) {
|
||||||
if (_currentClass != null) {
|
if (_currentClass != null) {
|
||||||
final dart_ast.TypeAnnotation? type = node.fields.type;
|
final dart_ast.TypeAnnotation? type = node.fields.type;
|
||||||
if (type is dart_ast.NamedType) {
|
if (node.isStatic) {
|
||||||
|
_errors.add(Error(
|
||||||
|
message:
|
||||||
|
'Pigeon doesn\'t support static fields ("${node.toString()}"), consider using enums.',
|
||||||
|
lineNumber: _calculateLineNumber(source, node.offset)));
|
||||||
|
} else if (type is dart_ast.NamedType) {
|
||||||
_currentClass!.fields.add(Field(
|
_currentClass!.fields.add(Field(
|
||||||
name: node.fields.variables[0].name.name,
|
name: node.fields.variables[0].name.name,
|
||||||
dataType: type.toString(),
|
dataType: type.toString(),
|
||||||
|
@ -416,4 +416,26 @@ void main() {
|
|||||||
expect(parseResult.errors[0].message, contains('Generic fields'));
|
expect(parseResult.errors[0].message, contains('Generic fields'));
|
||||||
expect(parseResult.errors[0].lineNumber, isNotNull);
|
expect(parseResult.errors[0].lineNumber, isNotNull);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('error with static field', () {
|
||||||
|
final Pigeon dartle = Pigeon.setup();
|
||||||
|
const String code = '''
|
||||||
|
class WithStaticField {
|
||||||
|
static int? x;
|
||||||
|
int? y;
|
||||||
|
}
|
||||||
|
|
||||||
|
@HostApi()
|
||||||
|
abstract class WithStaticFieldApi {
|
||||||
|
void doit(WithStaticField withTemplate);
|
||||||
|
}
|
||||||
|
''';
|
||||||
|
_withTempFile('compilationError.dart', (File file) {
|
||||||
|
file.writeAsStringSync(code);
|
||||||
|
final ParseResults parseResult = dartle.parseFile(file.path);
|
||||||
|
expect(parseResult.errors.length, equals(1));
|
||||||
|
expect(parseResult.errors[0].message, contains('static field'));
|
||||||
|
expect(parseResult.errors[0].lineNumber, isNotNull);
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user