Skip to content

Commit

Permalink
Use explicit generics for collections in build script (#3301)
Browse files Browse the repository at this point in the history
Towards #3300

Avoid inference to improve consistency between getting configuration in
from default options or from user provided options which are a
`YamlList` or `YamlMap` and never have a generic other than `dynamic`.
The generic is the most user visible difference, the type of the
concrete collection is not as likely to cause problems. Use `String` keys
for maps since the `BuilderOptions` class requires it for the outer
collection.

Avoid `toExpression` for the `appliesBuilders` key to keep using
inference for that list. Now `toExpression` is only for the default
options. Remove now unused support for const in `toExpression`.
  • Loading branch information
natebosch committed May 10, 2022
1 parent 8ebdc5a commit de4f74f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
15 changes: 8 additions & 7 deletions _test/test/goldens/generated_build_script.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ final _builders = <_i1.BuilderApplication>[
r'test/**.node_test.dart',
r'test/**.vm_test.dart'
]),
defaultOptions: const _i8.BuilderOptions({
r'dart2js_args': [r'--minify']
defaultOptions: const _i8.BuilderOptions(<String, dynamic>{
r'dart2js_args': <dynamic>[r'--minify']
}),
defaultDevOptions: const _i8.BuilderOptions({
r'dart2js_args': [r'--enable-asserts']
defaultDevOptions: const _i8.BuilderOptions(<String, dynamic>{
r'dart2js_args': <dynamic>[r'--enable-asserts']
}),
defaultReleaseOptions:
const _i8.BuilderOptions({r'compiler': r'dart2js'}),
const _i8.BuilderOptions(<String, dynamic>{r'compiler': r'dart2js'}),
appliesBuilders: const [
r'build_web_compilers:dart2js_archive_extractor'
]),
Expand All @@ -128,10 +128,11 @@ final _builders = <_i1.BuilderApplication>[
_i1.applyPostProcess(r'build_web_compilers:dart2js_archive_extractor',
_i7.dart2jsArchiveExtractor,
defaultReleaseOptions:
const _i8.BuilderOptions({r'filter_outputs': true})),
const _i8.BuilderOptions(<String, dynamic>{r'filter_outputs': true})),
_i1.applyPostProcess(
r'build_web_compilers:dart_source_cleanup', _i7.dartSourceCleanup,
defaultReleaseOptions: const _i8.BuilderOptions({r'enabled': true})),
defaultReleaseOptions:
const _i8.BuilderOptions(<String, dynamic>{r'enabled': true})),
_i1.applyPostProcess(
r'provides_builder:some_post_process_builder', _i4.somePostProcessBuilder)
];
Expand Down
6 changes: 6 additions & 0 deletions build_runner/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.1.11-dev

- Use an explicit `dynamic` generic type for collections in default builder
options to reduce behavior differences between reading default options and
user provided options.

## 2.1.10

- Allow build version 2.3.x.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ Expression _applyPostProcessBuilder(PostProcessBuilderDefinition definition) {
}

Expression _rawStringList(List<String> strings) =>
strings.toExpression(constant: true);
literalConstList([for (var s in strings) literalString(s, raw: true)]);

/// Returns the actual import to put in the generated script based on an import
/// found in the build.yaml.
Expand Down Expand Up @@ -363,19 +363,18 @@ extension on LanguageVersion {
/// This is similar to [literal] from `package:code_builder`, except that it
/// always writes raw string literals.
extension ConvertToExpression on Object? {
Expression toExpression({bool constant = false}) {
Expression toExpression() {
final $this = this;

if ($this is Map) {
final create = constant ? literalConstMap : literalMap;
return create({
return literalMap({
for (final entry in $this.cast<Object?, Object?>().entries)
entry.key.toExpression(): entry.value.toExpression()
});
}, refer('String'), refer('dynamic'));
} else if ($this is List) {
final create = constant ? literalConstList : literalList;
return create(
[for (final entry in $this.cast<Object?>()) entry.toExpression()]);
return literalList(
[for (final entry in $this.cast<Object?>()) entry.toExpression()],
refer('dynamic'));
} else if ($this is String) {
return literalString($this, raw: true);
} else {
Expand Down
2 changes: 1 addition & 1 deletion build_runner/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: build_runner
version: 2.1.10
version: 2.1.11-dev
description: A build system for Dart code generation and modular compilation.
repository: https://github.com/dart-lang/build/tree/master/build_runner

Expand Down

0 comments on commit de4f74f

Please sign in to comment.