Skip to content

Commit 2564c15

Browse files
committed
Merge pull request #68 from stereotype441/analyze-lib-files-by-package-uri
Analyze lib files by package uri
2 parents 36e9209 + 5e76dd7 commit 2564c15

File tree

6 files changed

+110
-32
lines changed

6 files changed

+110
-32
lines changed

bin/linter.dart

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,34 @@ import 'package:linter/src/io.dart';
1212
import 'package:linter/src/linter.dart';
1313

1414
void main(List<String> args) {
15+
runLinter(args, new LinterOptions());
16+
}
17+
18+
const processFileFailedExitCode = 65;
19+
20+
const unableToProcessExitCode = 64;
21+
22+
String getRoot(List<String> paths) =>
23+
paths.length == 1 && new Directory(paths[0]).existsSync() ? paths[0] : null;
24+
25+
isLinterErrorCode(int code) =>
26+
code == unableToProcessExitCode || code == processFileFailedExitCode;
27+
28+
void printUsage(ArgParser parser, IOSink out, [String error]) {
29+
var message = "Lints Dart source files and pubspecs.";
30+
if (error != null) {
31+
message = error;
32+
}
33+
34+
out.writeln('''$message
35+
Usage: linter <file>
36+
${parser.usage}
37+
38+
For more information, see https://github.com/dart-lang/linter
39+
''');
40+
}
41+
42+
void runLinter(List<String> args, LinterOptions initialLintOptions) {
1543
var parser = new ArgParser(allowTrailingOptions: true);
1644

1745
parser
@@ -49,7 +77,7 @@ void main(List<String> args) {
4977
return;
5078
}
5179

52-
var lintOptions = new LinterOptions();
80+
var lintOptions = initialLintOptions;
5381

5482
var configFile = options["config"];
5583
if (configFile != null) {
@@ -95,27 +123,3 @@ $err
95123
$stack''');
96124
}
97125
}
98-
99-
const processFileFailedExitCode = 65;
100-
101-
const unableToProcessExitCode = 64;
102-
103-
String getRoot(List<String> paths) =>
104-
paths.length == 1 && new Directory(paths[0]).existsSync() ? paths[0] : null;
105-
106-
isLinterErrorCode(int code) =>
107-
code == unableToProcessExitCode || code == processFileFailedExitCode;
108-
109-
void printUsage(ArgParser parser, IOSink out, [String error]) {
110-
var message = "Lints Dart source files and pubspecs.";
111-
if (error != null) {
112-
message = error;
113-
}
114-
115-
out.writeln('''$message
116-
Usage: linter <file>
117-
${parser.usage}
118-
119-
For more information, see https://github.com/dart-lang/linter
120-
''');
121-
}

lib/src/analysis.dart

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,13 @@ class AnalysisDriver {
4949

5050
List<UriResolver> get resolvers {
5151
DartSdk sdk = new DirectoryBasedDartSdk(new JavaFile(sdkDir));
52-
List<UriResolver> resolvers = [
53-
new DartUriResolver(sdk),
54-
new FileUriResolver()
55-
];
52+
List<UriResolver> resolvers = [new DartUriResolver(sdk)];
5653
if (options.packageRootPath != null) {
5754
JavaFile packageDirectory = new JavaFile(options.packageRootPath);
5855
resolvers.add(new PackageUriResolver([packageDirectory]));
5956
} else {
60-
PubPackageMapProvider pubPackageMapProvider =
61-
new PubPackageMapProvider(PhysicalResourceProvider.INSTANCE, sdk);
57+
PubPackageMapProvider pubPackageMapProvider = new PubPackageMapProvider(
58+
PhysicalResourceProvider.INSTANCE, sdk, options.runPubList);
6259
PackageMapInfo packageMapInfo = pubPackageMapProvider.computePackageMap(
6360
PhysicalResourceProvider.INSTANCE.getResource('.'));
6461
Map<String, List<Folder>> packageMap = packageMapInfo.packageMap;
@@ -67,6 +64,9 @@ class AnalysisDriver {
6764
PhysicalResourceProvider.INSTANCE, packageMap));
6865
}
6966
}
67+
// File URI resolver must come last so that files inside "/lib" are
68+
// are analyzed via "package:" URI's.
69+
resolvers.add(new FileUriResolver());
7070
return resolvers;
7171
}
7272

@@ -90,6 +90,12 @@ class AnalysisDriver {
9090
for (File file in files) {
9191
JavaFile sourceFile = new JavaFile(file.path);
9292
Source source = new FileBasedSource.con2(sourceFile.toURI(), sourceFile);
93+
Uri uri = context.sourceFactory.restoreUri(source);
94+
if (uri != null) {
95+
// Ensure that we analyze the file using its canonical URI (e.g. if
96+
// it's in "/lib", analyze it using a "package:" URI).
97+
source = new FileBasedSource.con2(uri, sourceFile);
98+
}
9399
sources.add(source);
94100
changeSet.addedSource(source);
95101
}
@@ -155,6 +161,10 @@ class DriverOptions {
155161
/// Whether to show lints for the transitive closure of imported and exported
156162
/// libraries.
157163
bool visitTransitiveClosure = false;
164+
165+
/// If non-null, the function to use to run pub list. This is used to mock
166+
/// out executions of pub list when testing the linter.
167+
RunPubList runPubList = null;
158168
}
159169

160170
/// Prints logging information comments to the [outSink] and error messages to

test/_data/p4/lib/lib1.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
library lib1;
6+
7+
import 'package:p4/lib2.dart';
8+
9+
import 'lib3.dart';
10+
11+
void test() {
12+
f(g());
13+
}

test/_data/p4/lib/lib2.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
library lib2;
6+
7+
import 'lib3.dart';
8+
9+
void f(C c) {}

test/_data/p4/lib/lib3.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
library lib3;
6+
7+
C g() => new C();
8+
9+
class C {}

test/integration_test.dart

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
library linter.test.integration;
66

7+
import 'dart:convert';
78
import 'dart:io';
89

910
import 'package:linter/src/config.dart';
1011
import 'package:linter/src/io.dart';
12+
import 'package:linter/src/linter.dart';
13+
import 'package:mockito/mockito.dart';
14+
import 'package:path/path.dart';
1115
import 'package:unittest/unittest.dart';
1216

1317
import '../bin/linter.dart' as dartlint;
@@ -54,8 +58,32 @@ defineTests() {
5458
});
5559
test('bad pubspec', () {
5660
dartlint.main(['test/_data/p3', 'test/_data/p3/_pubpspec.yaml']);
61+
expect(
62+
collectingOut.trim(), endsWith('1 file analyzed, 0 issues found.'));
63+
});
64+
});
65+
group('p4', () {
66+
IOSink currentOut = outSink;
67+
CollectingSink collectingOut = new CollectingSink();
68+
setUp(() => outSink = collectingOut);
69+
tearDown(() {
70+
collectingOut.buffer.clear();
71+
outSink = currentOut;
72+
});
73+
test('no warnings due to bad canonicalization', () {
74+
var libPath = new Directory('test/_data/p4/lib').absolute.path;
75+
var options = new LinterOptions([]);
76+
options.runPubList = (_) {
77+
var processResult = new MockProcessResult();
78+
when(processResult.exitCode).thenReturn(0);
79+
when(processResult.stderr).thenReturn('');
80+
when(processResult.stdout).thenReturn(
81+
JSON.encode({'packages': {'p4': libPath}, 'input_files': []}));
82+
return processResult;
83+
};
84+
dartlint.runLinter(['test/_data/p4'], options);
5785
expect(collectingOut.trim(),
58-
endsWith('1 file analyzed, 0 issues found.'));
86+
endsWith('3 files analyzed, 0 issues found.'));
5987
});
6088
});
6189

@@ -75,3 +103,8 @@ defineTests() {
75103
});
76104
});
77105
}
106+
107+
class MockProcessResult extends Mock implements ProcessResult {
108+
@override
109+
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
110+
}

0 commit comments

Comments
 (0)