| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library linter.src.analysis; | 5 library linter.src.analysis; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 import 'package:analyzer/file_system/file_system.dart' show Folder; | 10 import 'package:analyzer/file_system/file_system.dart' show Folder; |
| 11 import 'package:analyzer/file_system/physical_file_system.dart'; | 11 import 'package:analyzer/file_system/physical_file_system.dart'; |
| 12 import 'package:analyzer/source/package_map_provider.dart'; | 12 import 'package:analyzer/source/package_map_provider.dart'; |
| 13 import 'package:analyzer/source/package_map_resolver.dart'; | 13 import 'package:analyzer/source/package_map_resolver.dart'; |
| 14 import 'package:analyzer/source/pub_package_map_provider.dart'; | 14 import 'package:analyzer/source/pub_package_map_provider.dart'; |
| 15 import 'package:analyzer/src/generated/element.dart'; | 15 import 'package:analyzer/src/generated/element.dart'; |
| 16 import 'package:analyzer/src/generated/engine.dart'; | 16 import 'package:analyzer/src/generated/engine.dart'; |
| 17 import 'package:analyzer/src/generated/java_io.dart'; | 17 import 'package:analyzer/src/generated/java_io.dart'; |
| 18 import 'package:analyzer/src/generated/sdk.dart'; | 18 import 'package:analyzer/src/generated/sdk.dart'; |
| 19 import 'package:analyzer/src/generated/sdk_io.dart'; | 19 import 'package:analyzer/src/generated/sdk_io.dart'; |
| 20 import 'package:analyzer/src/generated/source.dart'; | 20 import 'package:analyzer/src/generated/source.dart'; |
| 21 import 'package:analyzer/src/generated/source_io.dart'; | 21 import 'package:analyzer/src/generated/source_io.dart'; |
| 22 import 'package:analyzer/src/services/lint.dart'; | 22 import 'package:analyzer/src/services/lint.dart'; |
| 23 import 'package:cli_util/cli_util.dart' as cli_util; | 23 import 'package:cli_util/cli_util.dart' as cli_util; |
| 24 import 'package:linter/src/io.dart'; | 24 import 'package:linter/src/io.dart'; |
| 25 import 'package:linter/src/linter.dart'; | 25 import 'package:linter/src/linter.dart'; |
| 26 import 'package:linter/src/project.dart'; | 26 import 'package:linter/src/project.dart'; |
| 27 import 'package:linter/src/rules.dart'; | 27 import 'package:linter/src/rules.dart'; |
| 28 import 'package:linter/src/sdk.dart'; |
| 28 import 'package:package_config/packages.dart' show Packages; | 29 import 'package:package_config/packages.dart' show Packages; |
| 29 import 'package:package_config/packages_file.dart' as pkgfile show parse; | 30 import 'package:package_config/packages_file.dart' as pkgfile show parse; |
| 30 import 'package:package_config/src/packages_impl.dart' show MapPackages; | 31 import 'package:package_config/src/packages_impl.dart' show MapPackages; |
| 31 import 'package:path/path.dart' as p; | 32 import 'package:path/path.dart' as p; |
| 32 | 33 |
| 33 Source createSource(Uri sourceUri) => | 34 Source createSource(Uri sourceUri) => |
| 34 new FileBasedSource(new JavaFile(sourceUri.toFilePath())); | 35 new FileBasedSource(new JavaFile(sourceUri.toFilePath())); |
| 35 | 36 |
| 36 /// Print the given message and exit with the given [exitCode] | 37 /// Print the given message and exit with the given [exitCode] |
| 37 void printAndFail(String message, {int exitCode: 15}) { | 38 void printAndFail(String message, {int exitCode: 15}) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 55 Set<Source> _sourcesAnalyzed = new HashSet<Source>(); | 56 Set<Source> _sourcesAnalyzed = new HashSet<Source>(); |
| 56 | 57 |
| 57 final LinterOptions options; | 58 final LinterOptions options; |
| 58 | 59 |
| 59 AnalysisDriver(this.options); | 60 AnalysisDriver(this.options); |
| 60 | 61 |
| 61 /// Return the number of sources that have been analyzed so far. | 62 /// Return the number of sources that have been analyzed so far. |
| 62 int get numSourcesAnalyzed => _sourcesAnalyzed.length; | 63 int get numSourcesAnalyzed => _sourcesAnalyzed.length; |
| 63 | 64 |
| 64 List<UriResolver> get resolvers { | 65 List<UriResolver> get resolvers { |
| 65 DartSdk sdk = new DirectoryBasedDartSdk(new JavaFile(sdkDir)); | 66 DartSdk sdk = options.useMockSdk |
| 67 ? new MockSdk() |
| 68 : new DirectoryBasedDartSdk(new JavaFile(sdkDir)); |
| 69 |
| 66 List<UriResolver> resolvers = [new DartUriResolver(sdk)]; | 70 List<UriResolver> resolvers = [new DartUriResolver(sdk)]; |
| 71 |
| 67 if (options.packageRootPath != null) { | 72 if (options.packageRootPath != null) { |
| 68 JavaFile packageDirectory = new JavaFile(options.packageRootPath); | 73 JavaFile packageDirectory = new JavaFile(options.packageRootPath); |
| 69 resolvers.add(new PackageUriResolver([packageDirectory])); | 74 resolvers.add(new PackageUriResolver([packageDirectory])); |
| 70 } else { | 75 } else { |
| 71 PubPackageMapProvider pubPackageMapProvider = new PubPackageMapProvider( | 76 PubPackageMapProvider pubPackageMapProvider = new PubPackageMapProvider( |
| 72 PhysicalResourceProvider.INSTANCE, sdk, options.runPubList); | 77 PhysicalResourceProvider.INSTANCE, sdk, options.runPubList); |
| 73 PackageMapInfo packageMapInfo = pubPackageMapProvider.computePackageMap( | 78 PackageMapInfo packageMapInfo = pubPackageMapProvider.computePackageMap( |
| 74 PhysicalResourceProvider.INSTANCE.getResource('.')); | 79 PhysicalResourceProvider.INSTANCE.getResource('.')); |
| 75 Map<String, List<Folder>> packageMap = packageMapInfo.packageMap; | 80 Map<String, List<Folder>> packageMap = packageMapInfo.packageMap; |
| 76 if (packageMap != null) { | 81 if (packageMap != null) { |
| 77 resolvers.add(new PackageMapUriResolver( | 82 resolvers.add(new PackageMapUriResolver( |
| 78 PhysicalResourceProvider.INSTANCE, packageMap)); | 83 PhysicalResourceProvider.INSTANCE, packageMap)); |
| 79 } | 84 } |
| 80 } | 85 } |
| 86 |
| 81 // File URI resolver must come last so that files inside "/lib" are | 87 // File URI resolver must come last so that files inside "/lib" are |
| 82 // are analyzed via "package:" URI's. | 88 // are analyzed via "package:" URI's. |
| 83 resolvers.add(new FileUriResolver()); | 89 resolvers.add(new FileUriResolver()); |
| 84 return resolvers; | 90 return resolvers; |
| 85 } | 91 } |
| 86 | 92 |
| 87 String get sdkDir { | 93 String get sdkDir { |
| 88 if (options.dartSdkPath != null) { | 94 if (options.dartSdkPath != null) { |
| 89 return options.dartSdkPath; | 95 return options.dartSdkPath; |
| 90 } | 96 } |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 | 210 |
| 205 /// Whether to show lint warnings. | 211 /// Whether to show lint warnings. |
| 206 bool enableLints = true; | 212 bool enableLints = true; |
| 207 | 213 |
| 208 /// The path to a `.packages` configuration file | 214 /// The path to a `.packages` configuration file |
| 209 String packageConfigPath; | 215 String packageConfigPath; |
| 210 | 216 |
| 211 /// The path to the package root. | 217 /// The path to the package root. |
| 212 String packageRootPath; | 218 String packageRootPath; |
| 213 | 219 |
| 220 /// If non-null, the function to use to run pub list. This is used to mock |
| 221 /// out executions of pub list when testing the linter. |
| 222 RunPubList runPubList = null; |
| 223 |
| 214 /// Whether to show SDK warnings. | 224 /// Whether to show SDK warnings. |
| 215 bool showSdkWarnings = false; | 225 bool showSdkWarnings = false; |
| 216 | 226 |
| 227 /// Whether to use a mock SDK (to speed up testing). |
| 228 bool useMockSdk = false; |
| 229 |
| 217 /// Whether to show lints for the transitive closure of imported and exported | 230 /// Whether to show lints for the transitive closure of imported and exported |
| 218 /// libraries. | 231 /// libraries. |
| 219 bool visitTransitiveClosure = false; | 232 bool visitTransitiveClosure = false; |
| 220 | |
| 221 /// If non-null, the function to use to run pub list. This is used to mock | |
| 222 /// out executions of pub list when testing the linter. | |
| 223 RunPubList runPubList = null; | |
| 224 } | 233 } |
| 225 | 234 |
| 226 /// Prints logging information comments to the [outSink] and error messages to | 235 /// Prints logging information comments to the [outSink] and error messages to |
| 227 /// [errorSink]. | 236 /// [errorSink]. |
| 228 class StdLogger extends Logger { | 237 class StdLogger extends Logger { |
| 229 @override | 238 @override |
| 230 void logError(String message, [exception]) => errorSink.writeln(message); | 239 void logError(String message, [exception]) => errorSink.writeln(message); |
| 231 @override | 240 @override |
| 232 void logError2(String message, dynamic exception) => | 241 void logError2(String message, dynamic exception) => |
| 233 errorSink.writeln(message); | 242 errorSink.writeln(message); |
| 234 @override | 243 @override |
| 235 void logInformation(String message, [exception]) => outSink.writeln(message); | 244 void logInformation(String message, [exception]) => outSink.writeln(message); |
| 236 @override | 245 @override |
| 237 void logInformation2(String message, dynamic exception) => | 246 void logInformation2(String message, dynamic exception) => |
| 238 outSink.writeln(message); | 247 outSink.writeln(message); |
| 239 } | 248 } |
| OLD | NEW |