Skip to content

Commit 2722aed

Browse files
author
John Messerly
committed
fixes #201, add support for custom URL mapping
[email protected] Review URL: https://codereview.chromium.org/1150143006
1 parent 8524217 commit 2722aed

File tree

7 files changed

+57
-10
lines changed

7 files changed

+57
-10
lines changed

pkg/dev_compiler/lib/src/analysis_context.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ AnalysisContext createAnalysisContext(CompilerOptions options,
3434
? createMockSdkResolver(mockSdkSources)
3535
: createSdkPathResolver(options.dartSdkPath);
3636

37-
var resolvers = [sdkResolver];
37+
var resolvers = [];
38+
if (options.customUrlMappings.isNotEmpty) {
39+
resolvers.add(new CustomUriResolver(options.customUrlMappings));
40+
}
41+
resolvers.add(sdkResolver);
3842
if (options.useImplicitHtml) {
3943
resolvers.add(_createImplicitEntryResolver(options));
4044
}

pkg/dev_compiler/lib/src/dependency_graph.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,6 @@ class DartSourceNode extends SourceNode {
301301
: uri.resolve(directiveUri.stringValue);
302302
var target =
303303
ParseDartTask.resolveDirective(graph._context, _source, d, null);
304-
if (target != null) {
305-
if (targetUri != target.uri) print(">> ${target.uri} $targetUri");
306-
}
307304
var node = graph.nodes.putIfAbsent(
308305
targetUri, () => new DartSourceNode(graph, targetUri, target));
309306
//var node = graph.nodeFromUri(targetUri);

pkg/dev_compiler/lib/src/options.dart

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ class CompilerOptions implements RulesOptions, ResolverOptions, JSCodeOptions {
236236
/// package (if we can infer where that is located).
237237
final String runtimeDir;
238238

239+
/// Custom URI mappings, such as "dart:foo" -> "path/to/foo.dart"
240+
final Map<String, String> customUrlMappings;
241+
239242
CompilerOptions({this.allowConstCasts: true, this.checkSdk: false,
240243
this.dumpInfo: false, this.dumpInfoFile, this.dumpSrcDir,
241244
this.forceCompile: false, this.formatOutput: false,
@@ -254,12 +257,14 @@ class CompilerOptions implements RulesOptions, ResolverOptions, JSCodeOptions {
254257
this.emitSourceMaps: true, this.entryPointFile: null,
255258
this.serverMode: false, this.useImplicitHtml: false,
256259
this.enableHashing: false, this.host: 'localhost', this.port: 8080,
257-
this.runtimeDir});
260+
this.runtimeDir, this.customUrlMappings: const {}});
258261
}
259262

260263
/// Parses options from the command-line
261264
CompilerOptions parseOptions(List<String> argv) {
262265
ArgResults args = argParser.parse(argv);
266+
bool showUsage = args['help'];
267+
263268
var serverMode = args['server'];
264269
var enableHashing = args['hashing'];
265270
if (enableHashing == null) {
@@ -289,6 +294,16 @@ CompilerOptions parseOptions(List<String> argv) {
289294
var dumpInfo = args['dump-info'];
290295
if (dumpInfo == null) dumpInfo = serverMode;
291296

297+
var customUrlMappings = <String, String>{};
298+
for (var mapping in args['url-mapping']) {
299+
var splitMapping = mapping.split(',');
300+
if (splitMapping.length != 2) {
301+
showUsage = true;
302+
continue;
303+
}
304+
customUrlMappings[splitMapping[0]] = splitMapping[1];
305+
}
306+
292307
var entryPointFile = args.rest.length == 0 ? null : args.rest.first;
293308

294309
return new CompilerOptions(
@@ -306,6 +321,7 @@ CompilerOptions parseOptions(List<String> argv) {
306321
covariantGenerics: args['covariant-generics'],
307322
relaxedCasts: args['relaxed-casts'],
308323
useColors: useColors,
324+
customUrlMappings: customUrlMappings,
309325
useMultiPackage: args['use-multi-package'],
310326
packageRoot: args['package-root'],
311327
packagePaths: args['package-paths'].split(','),
@@ -319,7 +335,7 @@ CompilerOptions parseOptions(List<String> argv) {
319335
onlyInferConstsAndFinalFields: args['infer-only-finals'],
320336
nonnullableTypes: optionsToList(args['nonnullable'],
321337
defaultValue: TypeOptions.NONNULLABLE_TYPES),
322-
help: args['help'],
338+
help: showUsage,
323339
useMockSdk: args['mock-sdk'],
324340
dartSdkPath: sdkPath,
325341
logLevel: logLevel,
@@ -380,6 +396,11 @@ final ArgParser argParser = new ArgParser()
380396
abbr: 'p',
381397
help: 'Package root to resolve "package:" imports',
382398
defaultsTo: 'packages/')
399+
..addOption('url-mapping',
400+
help: '--url-mapping=libraryUri,/path/to/library.dart uses library.dart\n'
401+
'as the source for an import of of "libraryUri".',
402+
allowMultiple: true,
403+
splitCommas: false)
383404
..addFlag('use-multi-package',
384405
help: 'Whether to use the multi-package resolver for "package:" imports',
385406
defaultsTo: false)

pkg/dev_compiler/lib/src/testing.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import 'utils.dart';
4747
/// });
4848
///
4949
CheckerResults testChecker(Map<String, String> testFiles,
50-
{bool allowConstCasts: true, String sdkDir,
50+
{bool allowConstCasts: true, String sdkDir, customUrlMappings: const {},
5151
CheckerReporter createReporter(AnalysisContext context),
5252
covariantGenerics: true, relaxedCasts: true,
5353
inferDownwards: RulesOptions.inferDownwardsDefault,
@@ -73,7 +73,8 @@ CheckerResults testChecker(Map<String, String> testFiles,
7373
useMockSdk: sdkDir == null,
7474
dartSdkPath: sdkDir,
7575
runtimeDir: '/dev_compiler_runtime/',
76-
entryPointFile: '/main.dart');
76+
entryPointFile: '/main.dart',
77+
customUrlMappings: customUrlMappings);
7778

7879
var context = createAnalysisContext(options, fileResolvers: [uriResolver]);
7980

pkg/dev_compiler/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author: Dart Dev Compiler team <[email protected]>
77
homepage: https://github.com/dart-lang/dev_compiler
88
dependencies:
99
analyzer: ^0.25.0
10-
args: ">=0.12.1 <0.14.0"
10+
args: ^0.13.0
1111
cli_util: ^0.0.1
1212
crypto: ^0.9.0
1313
html: ^0.12.0

pkg/dev_compiler/test/checker/checker_test.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ library dev_compiler.test.checker_test;
88
import 'package:test/test.dart';
99

1010
import 'package:dev_compiler/src/testing.dart';
11+
import '../test_util.dart' show testDirectory;
1112

1213
void main() {
1314
test('ternary operator', () {
@@ -2915,7 +2916,7 @@ void main() {
29152916
29162917
int foo(int x) => x;
29172918
int bar(int x, int y) => x + y;
2918-
2919+
29192920
void main() {
29202921
bool b;
29212922
b = /*severe:InvalidRuntimeCheckError*/foo is I2I;
@@ -2951,4 +2952,15 @@ void main() {
29512952
'''
29522953
});
29532954
});
2955+
2956+
test('custom URL mappings', () => testChecker({
2957+
'/main.dart': '''
2958+
import 'dart:foobar' show Baz;
2959+
main() {
2960+
print(Baz.quux);
2961+
}'''
2962+
},
2963+
customUrlMappings: {
2964+
'dart:foobar': '$testDirectory/checker/dart_foobar.dart'
2965+
}));
29542966
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
/// This is a test for --url-mapping option.
6+
/// Analyzer's CustomUriResolver only supports on-disk resources, so we make
7+
/// this file accessible.
8+
library dart.foobar;
9+
10+
class Baz {
11+
static String quux = "hello world";
12+
}

0 commit comments

Comments
 (0)