dart-sdk/pkg/analyzer_plugin/tool/spec/codegen_dart.dart
Devon Carew 3b0b1bd863 Bump package:lints to the latest; address instances of new lints.
Changes:
```
> git log --format="%C(auto) %h %s" 8d5f750..b044aca
 https://dart.googlesource.com/lints.git/+/b044aca add several rules to core and recommended (150)
 https://dart.googlesource.com/lints.git/+/81100a2 fix a dangling table link (146)
```

Diff: https://dart.googlesource.com/lints.git/+/8d5f7500024320654adb1e799e49fc10c5304ae7..b044acab9f6669b3d8e781923a8ff86877801177/
Change-Id: I031333ade99af700a7009b14a36d3aadba12fc94
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/327321
Reviewed-by: Nicholas Shahan <nshahan@google.com>
Commit-Queue: Devon Carew <devoncarew@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
2023-09-25 21:15:15 +00:00

50 lines
1.6 KiB
Dart

// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'api.dart';
/// Visitor specialized for generating Dart code.
class DartCodegenVisitor extends HierarchicalApiVisitor {
/// Type references in the spec that are named something else in Dart.
static const Map<String, String> _typeRenames = {
'long': 'int',
'object': 'Map',
};
DartCodegenVisitor(super.api);
/// Convert the given [TypeDecl] to a Dart type.
String dartType(TypeDecl type) {
if (type is TypeReference) {
var typeName = type.typeName;
var referencedDefinition = api.types[typeName];
if (_typeRenames.containsKey(typeName)) {
return _typeRenames[typeName]!;
}
if (referencedDefinition == null) {
return typeName;
}
var referencedType = referencedDefinition.type;
if (referencedType is TypeObject || referencedType is TypeEnum) {
return typeName;
}
return dartType(referencedType);
} else if (type is TypeList) {
return 'List<${dartType(type.itemType)}>';
} else if (type is TypeMap) {
return 'Map<${dartType(type.keyType)}, ${dartType(type.valueType)}>';
} else if (type is TypeUnion) {
return 'Object';
} else {
throw Exception("Can't convert to a dart type");
}
}
/// Return the Dart type for [field], nullable if the field is optional.
String fieldDartType(TypeObjectField field) {
var typeStr = dartType(field.type);
return field.optional ? '$typeStr?' : typeStr;
}
}