2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
- // @dart = 2.9
6
-
7
5
import 'dart:async' ;
8
6
import 'dart:convert' show json;
9
7
import 'dart:io' ;
@@ -46,8 +44,8 @@ const _binaryName = 'dartdevc -k';
46
44
/// The result may also contain a [previousResult] , which can be passed back in
47
45
/// for batch/worker executions to attempt to existing state.
48
46
Future <CompilerResult > compile (ParsedArguments args,
49
- {fe.InitializedCompilerState compilerState,
50
- Map <Uri , List <int >> inputDigests}) {
47
+ {fe.InitializedCompilerState ? compilerState,
48
+ Map <Uri , List <int >>? inputDigests}) {
51
49
if (compilerState != null && ! args.isBatchOrWorker) {
52
50
throw ArgumentError (
53
51
'previousResult requires --batch or --bazel_worker mode/' );
@@ -83,10 +81,10 @@ String _usageMessage(ArgParser ddcArgParser) =>
83
81
'${ddcArgParser .usage }' ;
84
82
85
83
Future <CompilerResult > _compile (List <String > args,
86
- {fe.InitializedCompilerState compilerState,
84
+ {fe.InitializedCompilerState ? compilerState,
87
85
bool isWorker = false ,
88
86
bool useIncrementalCompiler = false ,
89
- Map <Uri , List <int >> inputDigests}) async {
87
+ Map <Uri , List <int >>? inputDigests}) async {
90
88
// TODO(jmesserly): refactor options to share code with dartdevc CLI.
91
89
var argParser = ArgParser (allowTrailingOptions: true )
92
90
..addFlag ('help' ,
@@ -98,7 +96,9 @@ Future<CompilerResult> _compile(List<String> args,
98
96
defaultsTo: false ,
99
97
hide: true )
100
98
..addFlag ('track-widget-creation' ,
101
- help: 'Enable inspecting of Flutter widgets.' , hide: true )
99
+ help: 'Enable inspecting of Flutter widgets.' ,
100
+ defaultsTo: false ,
101
+ hide: true )
102
102
// TODO(jmesserly): add verbose help to show hidden options
103
103
..addOption ('dart-sdk-summary' ,
104
104
help: 'The path to the Dart SDK summary file.' , hide: true )
@@ -198,9 +198,10 @@ Future<CompilerResult> _compile(List<String> args,
198
198
199
199
var summaryPaths = options.summaryModules.keys.toList ();
200
200
var summaryModules = Map .fromIterables (
201
- summaryPaths.map (sourcePathToUri), options.summaryModules.values);
202
- var sdkSummaryPath = argResults['dart-sdk-summary' ] as String ;
203
- var librarySpecPath = argResults['libraries-file' ] as String ;
201
+ summaryPaths.map (sourcePathToUri).cast <Uri >(),
202
+ options.summaryModules.values);
203
+ var sdkSummaryPath = argResults['dart-sdk-summary' ] as String ? ;
204
+ var librarySpecPath = argResults['libraries-file' ] as String ? ;
204
205
if (sdkSummaryPath == null ) {
205
206
sdkSummaryPath =
206
207
defaultSdkSummaryPath (soundNullSafety: options.soundNullSafety);
@@ -246,7 +247,8 @@ Future<CompilerResult> _compile(List<String> args,
246
247
// TODO(jmesserly): conceptually CFE should not need a .packages file to
247
248
// resolve package URIs that are in the input summaries, but it seems to.
248
249
// This needs further investigation.
249
- var packageFile = argResults['packages' ] as String ?? _findPackagesFilePath ();
250
+ var packageFile =
251
+ argResults['packages' ] as String ? ?? _findPackagesFilePath ();
250
252
251
253
var succeeded = true ;
252
254
void diagnosticMessageHandler (fe.DiagnosticMessage message) {
@@ -259,14 +261,13 @@ Future<CompilerResult> _compile(List<String> args,
259
261
var explicitExperimentalFlags = fe.parseExperimentalFlags (options.experiments,
260
262
onError: stderr.writeln, onWarning: print);
261
263
262
- var trackWidgetCreation =
263
- argResults['track-widget-creation' ] as bool ?? false ;
264
+ var trackWidgetCreation = argResults['track-widget-creation' ] as bool ;
264
265
265
266
var compileSdk = argResults['compile-sdk' ] == true ;
266
267
var oldCompilerState = compilerState;
267
- List <Component > doneAdditionalDills ;
268
- fe.IncrementalCompiler incrementalCompiler;
269
- fe.WorkerInputComponent cachedSdkInput;
268
+ var doneAdditionalDills = < Component > [] ;
269
+ fe.IncrementalCompiler ? incrementalCompiler;
270
+ fe.WorkerInputComponent ? cachedSdkInput;
270
271
var recordUsedInputs = argResults['used-inputs-file' ] != null ;
271
272
var additionalDills = summaryModules.keys.toList ();
272
273
@@ -291,18 +292,17 @@ Future<CompilerResult> _compile(List<String> args,
291
292
// If digests weren't given and if not in worker mode, create fake data and
292
293
// ensure we don't have a previous state (as that wouldn't be safe with
293
294
// fake input digests).
294
- if (! isWorker && (inputDigests == null || inputDigests.isEmpty)) {
295
+ inputDigests ?? = {};
296
+ if (! isWorker && inputDigests.isEmpty) {
295
297
oldCompilerState = null ;
296
- inputDigests ?? = {};
298
+
297
299
if (! compileSdk) {
298
300
inputDigests[sourcePathToUri (sdkSummaryPath)] = const [0 ];
299
301
}
300
302
for (var uri in summaryModules.keys) {
301
303
inputDigests[uri] = const [0 ];
302
304
}
303
305
}
304
-
305
- doneAdditionalDills = List <Component >(summaryModules.length);
306
306
compilerState = await fe.initializeIncrementalCompiler (
307
307
oldCompilerState,
308
308
{
@@ -329,7 +329,7 @@ Future<CompilerResult> _compile(List<String> args,
329
329
options.soundNullSafety ? fe.NnbdMode .Strong : fe.NnbdMode .Weak );
330
330
incrementalCompiler = compilerState.incrementalCompiler;
331
331
cachedSdkInput =
332
- compilerState.workerInputCache[sourcePathToUri (sdkSummaryPath)];
332
+ compilerState.workerInputCache! [sourcePathToUri (sdkSummaryPath)];
333
333
}
334
334
335
335
// TODO(jmesserly): is there a cleaner way to do this?
@@ -338,20 +338,20 @@ Future<CompilerResult> _compile(List<String> args,
338
338
// `initializeCompiler`. Also we should be able to pass down Components for
339
339
// SDK and summaries.
340
340
//
341
- fe.DdcResult result;
341
+ fe.DdcResult ? result;
342
342
if (! useIncrementalCompiler) {
343
343
result = await fe.compile (compilerState, inputs, diagnosticMessageHandler);
344
344
} else {
345
345
compilerState.options.onDiagnostic = diagnosticMessageHandler;
346
- var incrementalCompilerResult = await incrementalCompiler.computeDelta (
346
+ var incrementalCompilerResult = await incrementalCompiler! .computeDelta (
347
347
entryPoints: inputs,
348
348
fullComponent: true ,
349
349
trackNeededDillLibraries: recordUsedInputs);
350
350
result = fe.DdcResult (
351
351
incrementalCompilerResult.component,
352
- cachedSdkInput.component,
352
+ cachedSdkInput! .component,
353
353
doneAdditionalDills,
354
- incrementalCompilerResult.classHierarchy,
354
+ incrementalCompilerResult.classHierarchy! ,
355
355
incrementalCompilerResult.neededDillLibraries);
356
356
}
357
357
compilerState.options.onDiagnostic = null ; // See http://dartbug.com/36983.
@@ -393,7 +393,7 @@ Future<CompilerResult> _compile(List<String> args,
393
393
kernel.BinaryPrinter (sink).writeComponentFile (component);
394
394
outFiles.add (sink.flush ().then ((_) => sink.close ()));
395
395
}
396
- String fullDillUri;
396
+ String ? fullDillUri;
397
397
if (argResults['experimental-output-compiled-kernel' ] as bool ) {
398
398
if (outPaths.length > 1 ) {
399
399
print (
@@ -433,7 +433,7 @@ Future<CompilerResult> _compile(List<String> args,
433
433
final summaryToModule = Map <Component , String >.identity ();
434
434
for (var i = 0 ; i < result.additionalDills.length; i++ ) {
435
435
var additionalDill = result.additionalDills[i];
436
- var moduleImport = summaryModules[additionalDills[i]];
436
+ var moduleImport = summaryModules[additionalDills[i]]! ;
437
437
for (var l in additionalDill.libraries) {
438
438
assert (! importToSummary.containsKey (l));
439
439
importToSummary[l] = additionalDill;
@@ -487,12 +487,12 @@ Future<CompilerResult> _compile(List<String> args,
487
487
if (recordUsedInputs) {
488
488
var usedOutlines = < Uri > {};
489
489
if (useIncrementalCompiler) {
490
- var neededDillLibraries = result.neededDillLibraries;
491
- compilerState.incrementalCompiler.updateNeededDillLibrariesWithHierarchy (
490
+ var neededDillLibraries = result.neededDillLibraries! ;
491
+ compilerState.incrementalCompiler! .updateNeededDillLibrariesWithHierarchy (
492
492
neededDillLibraries, result.classHierarchy);
493
493
for (var lib in neededDillLibraries) {
494
494
if (lib.importUri.isScheme ('dart' )) continue ;
495
- var uri = compilerState.libraryToInputDill[lib.importUri];
495
+ var uri = compilerState.libraryToInputDill! [lib.importUri];
496
496
if (uri == null ) {
497
497
throw StateError ('Library ${lib .importUri } was recorded as used, '
498
498
'but was not in the list of known libraries.' );
@@ -650,20 +650,20 @@ class JSCode {
650
650
///
651
651
/// The source paths will initially be absolute paths. They can be adjusted
652
652
/// using [placeSourceMap] .
653
- final Map sourceMap;
653
+ final Map ? sourceMap;
654
654
655
655
/// Module and library information
656
656
///
657
657
/// The [metadata] is a contract between compiler and the debugger,
658
658
/// helping the debugger map between libraries, modules, source paths.
659
659
/// see: https://goto.google.com/dart-web-debugger-metadata
660
- final ModuleMetadata metadata;
660
+ final ModuleMetadata ? metadata;
661
661
662
662
/// Module debug symbols.
663
663
///
664
664
/// The [symbols] is a contract between compiler and the debugger,
665
665
/// helping the debugger map between dart and JS objects.
666
- final ModuleSymbols symbols;
666
+ final ModuleSymbols ? symbols;
667
667
668
668
JSCode (this .code, this .sourceMap, {this .symbols, this .metadata});
669
669
}
@@ -677,18 +677,18 @@ JSCode jsProgramToCode(js_ast.Program moduleTree, ModuleFormat format,
677
677
bool inlineSourceMap = false ,
678
678
bool emitDebugMetadata = false ,
679
679
bool emitDebugSymbols = false ,
680
- String jsUrl,
681
- String mapUrl,
682
- String fullDillUri,
683
- String sourceMapBase,
684
- String customScheme,
685
- String multiRootOutputPath,
686
- ProgramCompiler compiler,
687
- Component component}) {
680
+ String ? jsUrl,
681
+ String ? mapUrl,
682
+ String ? fullDillUri,
683
+ String ? sourceMapBase,
684
+ String ? customScheme,
685
+ String ? multiRootOutputPath,
686
+ ProgramCompiler ? compiler,
687
+ Component ? component}) {
688
688
var opts = js_ast.JavaScriptPrintingOptions (
689
689
allowKeywordsInProperties: true , allowSingleLineIfStatements: true );
690
690
js_ast.SimpleJavaScriptPrintingContext printer;
691
- SourceMapBuilder sourceMap;
691
+ SourceMapBuilder ? sourceMap;
692
692
if (buildSourceMap) {
693
693
var sourceMapContext = SourceMapPrintingContext ();
694
694
sourceMap = sourceMapContext.sourceMap;
@@ -702,9 +702,9 @@ JSCode jsProgramToCode(js_ast.Program moduleTree, ModuleFormat format,
702
702
tree.accept (js_ast.Printer (opts, printer,
703
703
localNamer: js_ast.TemporaryNamer (tree, nameListener)));
704
704
705
- Map builtMap;
705
+ Map ? builtMap;
706
706
if (buildSourceMap && sourceMap != null ) {
707
- builtMap = placeSourceMap (sourceMap.build (jsUrl), mapUrl, customScheme,
707
+ builtMap = placeSourceMap (sourceMap.build (jsUrl! ), mapUrl! , customScheme,
708
708
multiRootOutputPath: multiRootOutputPath, sourceMapBase: sourceMapBase);
709
709
var jsDir = p.dirname (p.fromUri (jsUrl));
710
710
var relative = p.relative (p.fromUri (mapUrl), from: jsDir);
@@ -733,19 +733,19 @@ JSCode jsProgramToCode(js_ast.Program moduleTree, ModuleFormat format,
733
733
// our runtime metrics gathering would obtain this information from the
734
734
// compilation server, not the browser. We don't yet have the infra for that.
735
735
var compileTimeStatistics = {
736
- 'dartSize' : component != null ? _computeDartSize (component) : null ,
736
+ 'dartSize' : _computeDartSize (component! ) ,
737
737
'sourceMapSize' : encodedMap.length
738
738
};
739
739
text = text.replaceFirst (
740
740
SharedCompiler .metricsLocationID, '$compileTimeStatistics ' );
741
741
742
742
var debugMetadata = emitDebugMetadata
743
- ? _emitMetadata (moduleTree, component, mapUrl, jsUrl, fullDillUri)
743
+ ? _emitMetadata (moduleTree, component, mapUrl! , jsUrl! , fullDillUri)
744
744
: null ;
745
745
746
746
var debugSymbols = emitDebugSymbols
747
747
? _emitSymbols (
748
- compiler, moduleTree.name, nameListener.identifierNames, component)
748
+ compiler! , moduleTree.name! , nameListener! .identifierNames, component)
749
749
: null ;
750
750
751
751
return JSCode (text, builtMap, symbols: debugSymbols, metadata: debugMetadata);
@@ -759,17 +759,26 @@ JSCode jsProgramToCode(js_ast.Program moduleTree, ModuleFormat format,
759
759
/// names used when outputting the JavaScript.
760
760
ModuleSymbols _emitSymbols (ProgramCompiler compiler, String moduleName,
761
761
Map <js_ast.Identifier , String > identifierNames, Component component) {
762
+ /// Returns the name selected in the final Javascript for [id] .
763
+ String lookupName (js_ast.Identifier id) {
764
+ var name = identifierNames[id];
765
+ if (name == null ) {
766
+ throw Exception ('No recorded naming decision found for Identifier with '
767
+ 'name: ${id .name }' );
768
+ }
769
+ return name;
770
+ }
771
+
762
772
var classJsNames = < Class , String > {
763
- for (var e in compiler.classIdentifiers.entries)
764
- e.key: identifierNames[e.value],
773
+ for (var e in compiler.classIdentifiers.entries) e.key: lookupName (e.value),
765
774
};
766
775
var procedureJsNames = < Procedure , String > {
767
776
for (var e in compiler.procedureIdentifiers.entries)
768
- e.key: identifierNames[ e.value] ,
777
+ e.key: lookupName ( e.value) ,
769
778
};
770
779
var variableJsNames = < VariableDeclaration , String > {
771
780
for (var e in compiler.variableIdentifiers.entries)
772
- e.key: identifierNames[ e.value] ,
781
+ e.key: lookupName ( e.value) ,
773
782
};
774
783
775
784
return ModuleSymbolsCollector (moduleName, classJsNames, compiler.memberNames,
@@ -778,10 +787,10 @@ ModuleSymbols _emitSymbols(ProgramCompiler compiler, String moduleName,
778
787
}
779
788
780
789
ModuleMetadata _emitMetadata (js_ast.Program program, Component component,
781
- String sourceMapUri, String moduleUri, String fullDillUri) {
790
+ String sourceMapUri, String moduleUri, String ? fullDillUri) {
782
791
var metadata = ModuleMetadata (
783
- program.name,
784
- loadFunctionName (program.name),
792
+ program.name! ,
793
+ loadFunctionName (program.name! ),
785
794
sourceMapUri,
786
795
moduleUri,
787
796
fullDillUri,
@@ -803,7 +812,7 @@ Map<String, String> parseAndRemoveDeclaredVariables(List<String> args) {
803
812
var declaredVariables = < String , String > {};
804
813
for (var i = 0 ; i < args.length;) {
805
814
var arg = args[i];
806
- String rest;
815
+ String ? rest;
807
816
const defineFlag = '--define' ;
808
817
if (arg.startsWith ('-D' ) && arg.length > 2 ) {
809
818
rest = arg.substring (2 );
@@ -835,7 +844,7 @@ Map<String, String> parseAndRemoveDeclaredVariables(List<String> args) {
835
844
836
845
/// The default path of the kernel summary for the Dart SDK given the
837
846
/// [soundNullSafety] mode.
838
- String defaultSdkSummaryPath ({bool soundNullSafety}) {
847
+ String defaultSdkSummaryPath ({required bool soundNullSafety}) {
839
848
var outlineDill = soundNullSafety ? 'ddc_outline_sound.dill' : 'ddc_sdk.dill' ;
840
849
return p.join (getSdkPath (), 'lib' , '_internal' , outlineDill);
841
850
}
@@ -850,7 +859,7 @@ String getSdkPath() => p.dirname(p.dirname(Platform.resolvedExecutable));
850
859
///
851
860
/// Checks for a `.dart_tool/package_config.json` file in the current working
852
861
/// directory, or in any parent directory.
853
- String _findPackagesFilePath () {
862
+ String ? _findPackagesFilePath () {
854
863
// TODO(jmesserly): this was copied from package:package_config/discovery.dart
855
864
// Unfortunately the relevant function is not public. CFE APIs require a URI
856
865
// to the .packages file, rather than letting us provide the package map data.
@@ -872,9 +881,9 @@ String _findPackagesFilePath() {
872
881
}
873
882
874
883
/// Inputs must be absolute paths. Returns null if no prefixing path is found.
875
- String _longestPrefixingPath (Uri baseUri, List <Uri > prefixingPaths) {
884
+ String ? _longestPrefixingPath (Uri baseUri, List <Uri > prefixingPaths) {
876
885
var basePath = baseUri.path;
877
- return prefixingPaths.fold (null , (String previousValue, Uri element) {
886
+ return prefixingPaths.fold (null , (String ? previousValue, Uri element) {
878
887
if (basePath.startsWith (element.path) &&
879
888
(previousValue == null || previousValue.length < element.path.length)) {
880
889
return element.path;
0 commit comments