From a59a713f75e1430633044c238b288aeb80aa73c9 Mon Sep 17 00:00:00 2001 From: pq Date: Thu, 5 May 2016 12:08:58 -0700 Subject: [PATCH 1/6] Analysis re-work to use analyzer APIs. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduces a new Dart analysis wrapper that works directly with the analyzer API (in favor of shelling out to a separate process). Some consequences: * we no longer need to fear parts (simplifying our dart file gathering) * we can filter by error code (when needed), rather than by error strings * no more IO scraping * no need to generate `main()` or to run with `--package-warnings` * we now specify an analyzer (and linter) version in the pubspec (we’ll want to make sure this doesn’t diverge too far from the analyzer shipped with the SDK but it does give us some room to play with experimental builds) * no more (re)scanning of error source files (and so no more source cache) * should generally be a bit simpler and easier to maintain * runs a bit faster :) --- packages/flutter_test/pubspec.yaml | 2 +- .../lib/src/commands/analyze.dart | 362 ++++-------------- .../flutter_tools/lib/src/dart/analysis.dart | 275 +++++++++++++ packages/flutter_tools/pubspec.yaml | 3 +- 4 files changed, 357 insertions(+), 285 deletions(-) create mode 100644 packages/flutter_tools/lib/src/dart/analysis.dart diff --git a/packages/flutter_test/pubspec.yaml b/packages/flutter_test/pubspec.yaml index f9530fff5c..e6e1b00221 100644 --- a/packages/flutter_test/pubspec.yaml +++ b/packages/flutter_test/pubspec.yaml @@ -10,7 +10,7 @@ dependencies: # We don't actually depend on 'analyzer', but 'test' does. We pin the # version of analyzer we depend on to avoid version skew across our # packages. - analyzer: 0.27.2 + analyzer: 0.27.4-alpha.1 flutter: path: ../flutter diff --git a/packages/flutter_tools/lib/src/commands/analyze.dart b/packages/flutter_tools/lib/src/commands/analyze.dart index ef1fc65aa3..14dcb83e1a 100644 --- a/packages/flutter_tools/lib/src/commands/analyze.dart +++ b/packages/flutter_tools/lib/src/commands/analyze.dart @@ -11,101 +11,19 @@ import 'package:path/path.dart' as path; import 'package:yaml/yaml.dart' as yaml; import '../artifacts.dart'; -import '../base/logger.dart'; -import '../base/process.dart'; import '../base/utils.dart'; import '../build_configuration.dart'; +import '../dart/analysis.dart'; import '../dart/sdk.dart'; import '../globals.dart'; import '../runner/flutter_command.dart'; + bool isDartFile(FileSystemEntity entry) => entry is File && entry.path.endsWith('.dart'); bool isDartTestFile(FileSystemEntity entry) => entry is File && entry.path.endsWith('_test.dart'); bool isDartBenchmarkFile(FileSystemEntity entry) => entry is File && entry.path.endsWith('_bench.dart'); -RegExp _testFileParser = new RegExp(r'^(.+)_test(\.dart)$'); - -void _addDriverTest(FileSystemEntity entry, List dartFiles) { - if (isDartTestFile(entry)) { - final String testFileName = entry.path; - dartFiles.add(testFileName); - Match groups = _testFileParser.firstMatch(testFileName); - assert(groups.groupCount == 2); - final String hostFileName = '${groups[1]}${groups[2]}'; - File hostFile = new File(hostFileName); - if (hostFile.existsSync()) { - assert(isDartFile(hostFile)); - dartFiles.add(hostFileName); - } - } -} - -void _addPackage(String directoryPath, List dartFiles, Set pubSpecDirectories) { - final int originalDartFilesCount = dartFiles.length; - - // .../directoryPath/*/bin/*.dart - // .../directoryPath/*/lib/main.dart - // .../directoryPath/*/test/*_test.dart - // .../directoryPath/*/test/*/*_test.dart - // .../directoryPath/*/benchmark/*/*_bench.dart - - Directory binDirectory = new Directory(path.join(directoryPath, 'bin')); - if (binDirectory.existsSync()) { - for (FileSystemEntity subentry in binDirectory.listSync()) { - if (isDartFile(subentry)) - dartFiles.add(subentry.path); - } - } - - String mainPath = path.join(directoryPath, 'lib', 'main.dart'); - if (FileSystemEntity.isFileSync(mainPath)) - dartFiles.add(mainPath); - - Directory testDirectory = new Directory(path.join(directoryPath, 'test')); - if (testDirectory.existsSync()) { - for (FileSystemEntity entry in testDirectory.listSync()) { - if (entry is Directory) { - for (FileSystemEntity subentry in entry.listSync()) { - if (isDartTestFile(subentry)) - dartFiles.add(subentry.path); - } - } else if (isDartTestFile(entry)) { - dartFiles.add(entry.path); - } - } - } - - Directory testDriverDirectory = new Directory(path.join(directoryPath, 'test_driver')); - if (testDriverDirectory.existsSync()) { - for (FileSystemEntity entry in testDriverDirectory.listSync()) { - if (entry is Directory) { - for (FileSystemEntity subentry in entry.listSync()) - _addDriverTest(subentry, dartFiles); - } else if (isDartTestFile(entry)) { - _addDriverTest(entry, dartFiles); - } - } - } - - Directory benchmarkDirectory = new Directory(path.join(directoryPath, 'benchmark')); - if (benchmarkDirectory.existsSync()) { - for (FileSystemEntity entry in benchmarkDirectory.listSync()) { - if (entry is Directory) { - for (FileSystemEntity subentry in entry.listSync()) { - if (isDartBenchmarkFile(subentry)) - dartFiles.add(subentry.path); - } - } else if (isDartBenchmarkFile(entry)) { - dartFiles.add(entry.path); - } - } - } - - if (originalDartFilesCount != dartFiles.length) - pubSpecDirectories.add(directoryPath); -} - -class FileChanged { } +typedef bool FileFilter(FileSystemEntity entity); class AnalyzeCommand extends FlutterCommand { AnalyzeCommand() { @@ -179,16 +97,17 @@ class AnalyzeCommand extends FlutterCommand { Future _analyzeOnce() async { Stopwatch stopwatch = new Stopwatch()..start(); - Set pubSpecDirectories = new HashSet(); - List dartFiles = argResults.rest.toList(); + Set pubSpecDirectories = new HashSet(); + List dartFiles = []; - for (String file in dartFiles) { + for (String file in argResults.rest.toList()) { file = path.normalize(path.absolute(file)); String root = path.rootPrefix(file); + dartFiles.add(new File(file)); while (file != root) { file = path.dirname(file); if (FileSystemEntity.isFileSync(path.join(file, 'pubspec.yaml'))) { - pubSpecDirectories.add(file); + pubSpecDirectories.add(new Directory(file)); break; } } @@ -196,77 +115,41 @@ class AnalyzeCommand extends FlutterCommand { if (argResults['current-directory']) { // ./*.dart - Directory currentDirectory = new Directory('.'); + Directory cwd = new Directory('.'); bool foundOne = false; - for (FileSystemEntity entry in currentDirectory.listSync()) { + for (FileSystemEntity entry in cwd.listSync()) { if (isDartFile(entry)) { - dartFiles.add(entry.path); + dartFiles.add(entry); foundOne = true; } } - if (foundOne) - pubSpecDirectories.add('.'); + if (foundOne) { + pubSpecDirectories.add(cwd); + } } - if (argResults['current-package']) - _addPackage('.', dartFiles, pubSpecDirectories); + if (argResults['current-package']) { + // **/.*dart + Directory cwd = new Directory('.'); + _collectDartFiles(cwd, dartFiles); + pubSpecDirectories.add(cwd); + } + + //TODO(ianh): Fix the intl package resource generator + //TODO: extract this regexp from the exclude in options. + RegExp stockExampleFiles = new RegExp('examples/stocks/lib/.*\.dart\$'); if (argResults['flutter-repo']) { - //examples/*/ as package - //examples/layers/*/ as files - //dev/manual_tests/*/ as package - //dev/manual_tests/*/ as files - - for (Directory dir in runner.getRepoPackages()) - _addPackage(dir.path, dartFiles, pubSpecDirectories); - - Directory subdirectory; - - subdirectory = new Directory(path.join(ArtifactStore.flutterRoot, 'examples', 'layers')); - if (subdirectory.existsSync()) { - bool foundOne = false; - for (FileSystemEntity entry in subdirectory.listSync()) { - if (entry is Directory) { - for (FileSystemEntity subentry in entry.listSync()) { - if (isDartFile(subentry)) { - dartFiles.add(subentry.path); - foundOne = true; - } - } - } - } - if (foundOne) - pubSpecDirectories.add(subdirectory.path); - } - - subdirectory = new Directory(path.join(ArtifactStore.flutterRoot, 'dev', 'manual_tests')); - if (subdirectory.existsSync()) { - bool foundOne = false; - for (FileSystemEntity entry in subdirectory.listSync()) { - if (entry is Directory) { - _addPackage(entry.path, dartFiles, pubSpecDirectories); - } else if (isDartFile(entry)) { - dartFiles.add(entry.path); - foundOne = true; - } - } - if (foundOne) - pubSpecDirectories.add(subdirectory.path); + for (Directory dir in runner.getRepoPackages()) { + _collectDartFiles(dir, dartFiles, + exclude: (FileSystemEntity entity) => stockExampleFiles.hasMatch(entity.path)); + pubSpecDirectories.add(dir); } } - dartFiles = dartFiles.map((String directory) => path.normalize(path.absolute(directory))).toSet().toList(); - dartFiles.sort(); - - // prepare a Dart file that references all the above Dart files - StringBuffer mainBody = new StringBuffer(); - for (int index = 0; index < dartFiles.length; index += 1) - mainBody.writeln('import \'${dartFiles[index]}\' as file$index;'); - mainBody.writeln('void main() { }'); - // determine what all the various .packages files depend on PackageDependencyTracker dependencies = new PackageDependencyTracker(); - for (Directory directory in pubSpecDirectories.map((String path) => new Directory(path))) { + for (Directory directory in pubSpecDirectories) { String pubSpecYamlPath = path.join(directory.path, 'pubspec.yaml'); File pubSpecYamlFile = new File(pubSpecYamlPath); if (pubSpecYamlFile.existsSync()) { @@ -316,150 +199,66 @@ class AnalyzeCommand extends FlutterCommand { for (String package in packages.keys) packagesBody.writeln('$package:${path.toUri(packages[package])}'); - // save the Dart file and the .packages file to disk + // save the .packages file to disk + //TODO(pq): consider passing package info via a data URI Directory host = Directory.systemTemp.createTempSync('flutter-analyze-'); - File mainFile = new File(path.join(host.path, 'main.dart'))..writeAsStringSync(mainBody.toString()); - File optionsFile = new File(path.join(ArtifactStore.flutterRoot, 'packages', 'flutter_tools', 'flutter_analysis_options')); - File packagesFile = new File(path.join(host.path, '.packages'))..writeAsStringSync(packagesBody.toString()); + String packagesFilePath = path.join(host.path, '.packages'); + new File(packagesFilePath).writeAsStringSync(packagesBody.toString()); - List cmd = [ - sdkBinaryName('dartanalyzer', sdkLocation: argResults['dart-sdk']), - // do not set '--warnings', since that will include the entire Dart SDK - '--ignore-unrecognized-flags', - '--enable_type_checks', - '--package-warnings', - '--fatal-warnings', - '--fatal-hints', - // defines lints - '--options', optionsFile.path, - '--packages', packagesFile.path, - mainFile.path - ]; - - Status status; if (argResults['preamble']) { if (dartFiles.length == 1) { - status = logger.startProgress('Analyzing ${path.relative(dartFiles.first)}...'); + logger.printStatus('Analyzing ${path.relative(dartFiles.first.path)}...'); } else { - status = logger.startProgress('Analyzing ${dartFiles.length} entry points...'); + logger.printStatus('Analyzing ${dartFiles.length} files...'); } - for (String file in dartFiles) - printTrace(file); } + DriverOptions options = new DriverOptions(); + options.dartSdkPath = argResults['dart-sdk']; + options.packageConfigPath = packagesFilePath; + options.analysisOptionsFile = path.join(ArtifactStore.flutterRoot, 'packages', 'flutter_tools', 'flutter_analysis_options'); + AnalysisDriver analyzer = new AnalysisDriver(options); + + //TODO:(pq): consider error handling + List errors = analyzer.analyze(dartFiles); - printTrace(cmd.join(' ')); - Process process = await Process.start( - cmd[0], - cmd.sublist(1), - workingDirectory: host.path - ); int errorCount = 0; - StringBuffer output = new StringBuffer(); - process.stdout.transform(UTF8.decoder).listen((String data) { - output.write(data); - }); - process.stderr.transform(UTF8.decoder).listen((String data) { - // dartanalyzer doesn't seem to ever output anything on stderr - errorCount += 1; - printError(data); - }); - - int exitCode = await process.exitCode; - status?.stop(showElapsedTime: true); - - List patternsToSkip = [ - 'Analyzing [${mainFile.path}]...', - new RegExp('^\\[(hint|error)\\] Unused import \\(${mainFile.path},'), - new RegExp(r'^\[.+\] .+ \(.+/\.pub-cache/.+'), - new RegExp(r'[0-9]+ (error|warning|hint|lint).+found\.'), - new RegExp(r'^$'), - ]; - - RegExp generalPattern = new RegExp(r'^\[(error|warning|hint|lint)\] (.+) \(([^(),]+), line ([0-9]+), col ([0-9]+)\)$'); - RegExp conflictingNamesPattern = new RegExp('^The imported libraries \'([^\']+)\' and \'([^\']+)\' cannot have the same name \'([^\']+)\'\$'); - RegExp missingFilePattern = new RegExp('^Target of URI does not exist: \'([^\')]+)\'\$'); - RegExp documentAllMembersPattern = new RegExp('^Document all public members\$'); - - Set changedFiles = new Set(); // files about which we've complained that they changed - - _SourceCache cache = new _SourceCache(10); - int membersMissingDocumentation = 0; - List errorLines = output.toString().split('\n'); - for (String errorLine in errorLines) { - if (patternsToSkip.every((Pattern pattern) => pattern.allMatches(errorLine).isEmpty)) { - Match groups = generalPattern.firstMatch(errorLine); - if (groups != null) { - String level = groups[1]; - String filename = groups[3]; - String errorMessage = groups[2]; - int lineNumber = int.parse(groups[4]); - int colNumber = int.parse(groups[5]); - try { - File source = new File(filename); - List sourceLines = cache.getSourceFor(source); - if (lineNumber > sourceLines.length) - throw new FileChanged(); - String sourceLine = sourceLines[lineNumber-1]; - if (colNumber > sourceLine.length) - throw new FileChanged(); - bool shouldIgnore = false; - if (documentAllMembersPattern.firstMatch(errorMessage) != null) { - // https://github.com/dart-lang/linter/issues/207 - // https://github.com/dart-lang/linter/issues/208 - if (isFlutterLibrary(filename)) { - if (!argResults['dartdocs']) { - membersMissingDocumentation += 1; - shouldIgnore = true; - } - } else { - shouldIgnore = true; - } - } else if (filename == mainFile.path) { - Match libs = conflictingNamesPattern.firstMatch(errorMessage); - Match missing = missingFilePattern.firstMatch(errorMessage); - if (libs != null) { - errorLine = '[$level] $errorMessage (${dartFiles[lineNumber-1]})'; // strip the reference to the generated main.dart - } else if (missing != null) { - errorLine = '[$level] File does not exist (${missing[1]})'; - } else { - errorLine += ' (Please file a bug on the "flutter analyze" command saying that you saw this message.)'; - } - } else if (filename.endsWith('.mojom.dart')) { - // autogenerated code - TODO(ianh): Fix the Dart mojom compiler - shouldIgnore = true; - } else if (sourceLines.first.startsWith('// DO NOT EDIT. This is code generated')) { - // autogenerated code - TODO(ianh): Fix the intl package resource generator - shouldIgnore = true; - } - if (shouldIgnore) - continue; - } on FileSystemException catch (exception) { - if (changedFiles.add(filename)) - printError('[warning] Could not read file (${exception.message}${ exception.osError != null ? "; ${exception.osError}" : ""}) ($filename)'); - } on FileChanged { - if (changedFiles.add(filename)) - printError('[warning] File shrank during analysis ($filename)'); + for (AnalysisErrorDescription error in errors) { + bool shouldIgnore = false; + if (error.errorCode.name == 'public_member_api_docs') { + // https://github.com/dart-lang/linter/issues/207 + // https://github.com/dart-lang/linter/issues/208 + if (isFlutterLibrary(error.source.fullName)) { + if (!argResults['dartdocs']) { + membersMissingDocumentation += 1; + shouldIgnore = true; } + } else { + shouldIgnore = true; } - printError(errorLine); - errorCount += 1; } + //TODO(ianh): Fix the Dart mojom compiler + if (error.source.fullName.endsWith('.mojom.dart')) + shouldIgnore = true; + if (shouldIgnore) + continue; + printError(error.asString()); + errorCount += 1; } + stopwatch.stop(); String elapsed = (stopwatch.elapsedMilliseconds / 1000.0).toStringAsFixed(1); host.deleteSync(recursive: true); - if (exitCode < 0 || exitCode > 3) // analyzer exit codes: 0 = nothing, 1 = hints, 2 = warnings, 3 = errors - return exitCode; - if (_isBenchmarking) _writeBenchmark(stopwatch, errorCount); if (errorCount > 0) { if (membersMissingDocumentation > 0 && argResults['flutter-repo']) - printError('[lint] $membersMissingDocumentation public ${ membersMissingDocumentation == 1 ? "member lacks" : "members lack" } documentation'); + printError('[lint] $membersMissingDocumentation public ${ membersMissingDocumentation == 1 ? "member lacks" : "members lack" } documentation (ran in ${elapsed}s)'); + else + print('(Ran in ${elapsed}s)'); return 1; // we consider any level of error to be an error exit (we don't report different levels) } if (argResults['congratulate']) { @@ -472,6 +271,20 @@ class AnalyzeCommand extends FlutterCommand { return 0; } + List _collectDartFiles(Directory dir, List collected, {FileFilter exclude}) { + for (FileSystemEntity entity in dir.listSync(recursive: false, followLinks: false)) { + if (isDartFile(entity) && (exclude == null || !exclude(entity))) { + collected.add(entity); + } + if (entity is Directory) { + String name = path.basename(entity.path); + if (!name.startsWith('.') && name != 'packages') + _collectDartFiles(entity, collected, exclude: exclude); + } + } + return collected; + } + Future _analyzeWatch() async { List directories; @@ -843,20 +656,3 @@ class AnalysisError implements Comparable { return '${severity.toLowerCase().padLeft(7)} • $message • $relativePath:$startLine:$startColumn'; } } - -class _SourceCache { - _SourceCache(this.cacheSize); - - final int cacheSize; - final Map> _lines = new LinkedHashMap>(); - - List getSourceFor(File file) { - if (!_lines.containsKey(file.path)) { - if (_lines.length >= cacheSize) - _lines.remove(_lines.keys.first); - _lines[file.path] = file.readAsLinesSync(); - } - - return _lines[file.path]; - } -} diff --git a/packages/flutter_tools/lib/src/dart/analysis.dart b/packages/flutter_tools/lib/src/dart/analysis.dart new file mode 100644 index 0000000000..cba6086e76 --- /dev/null +++ b/packages/flutter_tools/lib/src/dart/analysis.dart @@ -0,0 +1,275 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:collection'; +import 'dart:io'; + +import 'package:analyzer/file_system/file_system.dart' as file_system; +import 'package:analyzer/file_system/file_system.dart' show Folder; +import 'package:analyzer/file_system/physical_file_system.dart'; +import 'package:analyzer/plugin/options.dart'; +import 'package:analyzer/source/analysis_options_provider.dart'; +import 'package:analyzer/source/embedder.dart'; +import 'package:analyzer/source/error_processor.dart'; +import 'package:analyzer/source/package_map_provider.dart'; +import 'package:analyzer/source/package_map_resolver.dart'; +import 'package:analyzer/source/pub_package_map_provider.dart'; +import 'package:analyzer/src/generated/engine.dart'; +import 'package:analyzer/src/generated/error.dart'; +import 'package:analyzer/src/generated/java_io.dart'; +import 'package:analyzer/src/generated/sdk.dart'; +import 'package:analyzer/src/generated/sdk_io.dart'; +import 'package:analyzer/src/generated/source.dart'; +import 'package:analyzer/src/generated/source_io.dart'; +import 'package:analyzer/src/task/options.dart'; +import 'package:cli_util/cli_util.dart' as cli_util; +import 'package:linter/src/plugin/linter_plugin.dart'; +import 'package:package_config/packages.dart' show Packages; +import 'package:package_config/packages_file.dart' as pkgfile show parse; +import 'package:package_config/src/packages_impl.dart' show MapPackages; +import 'package:path/path.dart' as path; +import 'package:plugin/manager.dart'; +import 'package:plugin/plugin.dart'; + +class AnalysisDriver { + Set _analyzedSources = new HashSet(); + + AnalysisOptionsProvider analysisOptionsProvider = + new AnalysisOptionsProvider(); + + AnalysisContext context; + + DriverOptions options; + AnalysisDriver(this.options) { + AnalysisEngine.instance.logger = + new _StdLogger(outSink: options.outSink, errorSink: options.errorSink); + _processPlugins(); + } + + String get sdkDir => options.dartSdkPath ?? cli_util.getSdkDir().path; + + List analyze(Iterable files) { + List infos = _analyze(files); + List errors = []; + for (AnalysisErrorInfo info in infos) { + for (AnalysisError error in info.errors) { + if (!_isFiltered(error)) { + errors.add(new AnalysisErrorDescription(error, info.lineInfo)); + } + } + } + return errors; + } + + List _analyze(Iterable files) { + context = AnalysisEngine.instance.createAnalysisContext(); + _processAnalysisOptions(context, options); + Packages packages = _getPackageConfig(); + context.sourceFactory = + new SourceFactory(_getResolvers(context, packages), packages); + + List sources = []; + ChangeSet changeSet = new ChangeSet(); + for (File file in files) { + JavaFile sourceFile = new JavaFile(path.normalize(file.absolute.path)); + Source source = new FileBasedSource(sourceFile, sourceFile.toURI()); + Uri uri = context.sourceFactory.restoreUri(source); + if (uri != null) { + source = new FileBasedSource(sourceFile, uri); + } + sources.add(source); + changeSet.addedSource(source); + } + context.applyChanges(changeSet); + + List infos = []; + for (Source source in sources) { + context.computeErrors(source); + infos.add(context.getErrors(source)); + _analyzedSources.add(source); + } + + return infos; + } + + Packages _getPackageConfig() { + if (options.packageConfigPath != null) { + String packageConfigPath = options.packageConfigPath; + Uri fileUri = new Uri.file(packageConfigPath); + try { + File configFile = new File.fromUri(fileUri).absolute; + List bytes = configFile.readAsBytesSync(); + Map map = pkgfile.parse(bytes, configFile.uri); + return new MapPackages(map); + } catch (e) { + throw new AnalysisDriverException('Unable to create package map.'); + } + } + return null; + } + + Map> _getPackageMap(Packages packages) { + if (packages == null) return null; + + Map> folderMap = + new Map>(); + packages.asMap().forEach((String packagePath, Uri uri) { + folderMap[packagePath] = [ + PhysicalResourceProvider.INSTANCE.getFolder(path.fromUri(uri)) + ]; + }); + return folderMap; + } + + List _getResolvers( + InternalAnalysisContext context, Packages packages) { + DartSdk sdk = new DirectoryBasedDartSdk(new JavaFile(sdkDir)); + List resolvers = []; + Map> packageMap = _getPackageMap(packages); + + EmbedderYamlLocator yamlLocator = context.embedderYamlLocator; + yamlLocator.refresh(packageMap); + + EmbedderUriResolver embedderUriResolver = + new EmbedderUriResolver(yamlLocator.embedderYamls); + if (embedderUriResolver.length == 0) { + resolvers.add(new DartUriResolver(sdk)); + } else { + resolvers.add(embedderUriResolver); + } + + if (options.packageRootPath != null) { + JavaFile packageDirectory = new JavaFile(options.packageRootPath); + resolvers.add(new PackageUriResolver([packageDirectory])); + } else { + PubPackageMapProvider pubPackageMapProvider = + new PubPackageMapProvider(PhysicalResourceProvider.INSTANCE, sdk); + PackageMapInfo packageMapInfo = pubPackageMapProvider.computePackageMap( + PhysicalResourceProvider.INSTANCE.getResource('.')); + Map> packageMap = packageMapInfo.packageMap; + if (packageMap != null) { + resolvers.add(new PackageMapUriResolver( + PhysicalResourceProvider.INSTANCE, packageMap)); + } + } + + resolvers.add(new FileUriResolver()); + return resolvers; + } + + bool _isFiltered(AnalysisError error) { + ErrorProcessor processor = ErrorProcessor.getProcessor(context, error); + // Filtered errors are processed to a severity of `null`. + return processor != null && processor.severity == null; + } + + void _processAnalysisOptions( + AnalysisContext context, AnalysisOptions analysisOptions) { + List optionsProcessors = + AnalysisEngine.instance.optionsPlugin.optionsProcessors; + try { + String optionsPath = options.analysisOptionsFile; + if (optionsPath != null) { + file_system.File file = + PhysicalResourceProvider.INSTANCE.getFile(optionsPath); + Map optionMap = + analysisOptionsProvider.getOptionsFromFile(file); + optionsProcessors.forEach( + (OptionsProcessor p) => p.optionsProcessed(context, optionMap)); + if (optionMap != null) { + configureContextOptions(context, optionMap); + } + } + } on Exception catch (e) { + optionsProcessors.forEach((OptionsProcessor p) => p.onError(e)); + } + } + + void _processPlugins() { + List plugins = []; + plugins.addAll(AnalysisEngine.instance.requiredPlugins); + plugins.add(AnalysisEngine.instance.commandLinePlugin); + plugins.add(AnalysisEngine.instance.optionsPlugin); + plugins.add(linterPlugin); + ExtensionManager manager = new ExtensionManager(); + manager.processPlugins(plugins); + } +} + +class AnalysisDriverException implements Exception { + final String message; + AnalysisDriverException([this.message]); + + @override + String toString() => message == null ? 'Exception' : 'Exception: $message'; +} + +class AnalysisErrorDescription { + static Directory cwd = Directory.current.absolute; + + final AnalysisError error; + final LineInfo line; + AnalysisErrorDescription(this.error, this.line); + + ErrorCode get errorCode => error.errorCode; + + String get errorType => error.errorCode.type.displayName; + + LineInfo_Location get location => line.getLocation(error.offset); + + String get path => _shorten(cwd.path, error.source.fullName); + + Source get source => error.source; + + String asString() => '[$errorType] ${error.message} ($path, ' + 'line ${location.lineNumber}, col ${location.columnNumber})'; + + static String _shorten(String root, String path) => + path.startsWith(root) ? path.substring(root.length + 1) : path; +} + +class DriverOptions extends AnalysisOptionsImpl { + @override + int cacheSize = 512; + + /// The path to the dart SDK. + String dartSdkPath; + + /// The path to a `.packages` configuration file + String packageConfigPath; + + /// The path to the package root. + String packageRootPath; + + /// The path to analysis options. + String analysisOptionsFile; + + @override + bool generateSdkErrors = false; + + /// Analysis options map. + Map analysisOptions; + + @override + bool lint = true; + + /// Out sink for logging. + IOSink outSink = stdout; + + /// Error sink for logging. + IOSink errorSink = stderr; +} + +class _StdLogger extends Logger { + final IOSink outSink; + final IOSink errorSink; + _StdLogger({this.outSink, this.errorSink}); + + @override + void logError(String message, [Exception exception]) => + errorSink.writeln(message); + @override + void logInformation(String message, [Exception exception]) => + outSink.writeln(message); +} diff --git a/packages/flutter_tools/pubspec.yaml b/packages/flutter_tools/pubspec.yaml index a67cee1c0b..2f5d456ca6 100644 --- a/packages/flutter_tools/pubspec.yaml +++ b/packages/flutter_tools/pubspec.yaml @@ -15,6 +15,7 @@ dependencies: http: ^0.11.3 json_rpc_2: ^2.0.0 json_schema: ^1.0.3 + linter: ^0.1.15 mustache4dart: ^1.0.0 package_config: ^0.1.3 path: ^1.3.0 @@ -36,7 +37,7 @@ dependencies: # We don't actually depend on 'analyzer', but 'test' does. We pin the # version of analyzer we depend on to avoid version skew across our # packages. - analyzer: 0.27.2 + analyzer: 0.27.4-alpha.1 dev_dependencies: mockito: ^0.11.0 From 71203e9a89cc161e3cbff6ea734a459ea11e1a8c Mon Sep 17 00:00:00 2001 From: pq Date: Thu, 5 May 2016 12:15:26 -0700 Subject: [PATCH 2/6] Analyzer dep commentary fix. --- packages/flutter_test/pubspec.yaml | 4 +--- packages/flutter_tools/pubspec.yaml | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/flutter_test/pubspec.yaml b/packages/flutter_test/pubspec.yaml index e6e1b00221..3ee936ca42 100644 --- a/packages/flutter_test/pubspec.yaml +++ b/packages/flutter_test/pubspec.yaml @@ -7,9 +7,7 @@ dependencies: # here we pin it precisely to avoid version skew across our packages. test: 0.12.13+1 - # We don't actually depend on 'analyzer', but 'test' does. We pin the - # version of analyzer we depend on to avoid version skew across our - # packages. + # Pinned in flutter_tools as well. analyzer: 0.27.4-alpha.1 flutter: diff --git a/packages/flutter_tools/pubspec.yaml b/packages/flutter_tools/pubspec.yaml index 2f5d456ca6..1bb747271e 100644 --- a/packages/flutter_tools/pubspec.yaml +++ b/packages/flutter_tools/pubspec.yaml @@ -34,9 +34,7 @@ dependencies: # precisely. test: 0.12.13+1 - # We don't actually depend on 'analyzer', but 'test' does. We pin the - # version of analyzer we depend on to avoid version skew across our - # packages. + # Pinned in flutter_test as well. analyzer: 0.27.4-alpha.1 dev_dependencies: From e808125c5b849a62ae6719ce14add21e514f907a Mon Sep 17 00:00:00 2001 From: pq Date: Thu, 5 May 2016 12:35:52 -0700 Subject: [PATCH 3/6] Dead code cleanup. --- packages/flutter_tools/lib/src/commands/analyze.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/analyze.dart b/packages/flutter_tools/lib/src/commands/analyze.dart index 14dcb83e1a..6d645b979d 100644 --- a/packages/flutter_tools/lib/src/commands/analyze.dart +++ b/packages/flutter_tools/lib/src/commands/analyze.dart @@ -20,8 +20,6 @@ import '../runner/flutter_command.dart'; bool isDartFile(FileSystemEntity entry) => entry is File && entry.path.endsWith('.dart'); -bool isDartTestFile(FileSystemEntity entry) => entry is File && entry.path.endsWith('_test.dart'); -bool isDartBenchmarkFile(FileSystemEntity entry) => entry is File && entry.path.endsWith('_bench.dart'); typedef bool FileFilter(FileSystemEntity entity); From 3b882fc449a64fae74afdd50a4f1b916189c08c7 Mon Sep 17 00:00:00 2001 From: pq Date: Thu, 5 May 2016 13:25:59 -0700 Subject: [PATCH 4/6] Updated arg default handling and .dartignore support. --- .../flutter_tools/lib/src/commands/analyze.dart | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/analyze.dart b/packages/flutter_tools/lib/src/commands/analyze.dart index 6d645b979d..d6dabaa6fb 100644 --- a/packages/flutter_tools/lib/src/commands/analyze.dart +++ b/packages/flutter_tools/lib/src/commands/analyze.dart @@ -111,7 +111,9 @@ class AnalyzeCommand extends FlutterCommand { } } - if (argResults['current-directory']) { + //TODO(pq): revisit package and directory defaults + + if (argResults['current-directory'] && !argResults['flutter-repo']) { // ./*.dart Directory cwd = new Directory('.'); bool foundOne = false; @@ -126,7 +128,7 @@ class AnalyzeCommand extends FlutterCommand { } } - if (argResults['current-package']) { + if (argResults['current-package'] && !argResults['flutter-repo']) { // **/.*dart Directory cwd = new Directory('.'); _collectDartFiles(cwd, dartFiles); @@ -134,7 +136,7 @@ class AnalyzeCommand extends FlutterCommand { } //TODO(ianh): Fix the intl package resource generator - //TODO: extract this regexp from the exclude in options. + //TODO(pq): extract this regexp from the exclude in options RegExp stockExampleFiles = new RegExp('examples/stocks/lib/.*\.dart\$'); if (argResults['flutter-repo']) { @@ -270,6 +272,10 @@ class AnalyzeCommand extends FlutterCommand { } List _collectDartFiles(Directory dir, List collected, {FileFilter exclude}) { + // Bail out in case of a .dartignore. + if (FileSystemEntity.isFileSync(path.join(path.dirname(dir.path), '.dartignore'))) + return collected; + for (FileSystemEntity entity in dir.listSync(recursive: false, followLinks: false)) { if (isDartFile(entity) && (exclude == null || !exclude(entity))) { collected.add(entity); @@ -280,6 +286,7 @@ class AnalyzeCommand extends FlutterCommand { _collectDartFiles(entity, collected, exclude: exclude); } } + return collected; } From 4311ddb461c89c956363a98c80f19a91da81c9ff Mon Sep 17 00:00:00 2001 From: pq Date: Thu, 5 May 2016 13:39:58 -0700 Subject: [PATCH 5/6] Duplicate test fix. --- .../flutter_tools/test/analyze_duplicate_names_test.dart | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/flutter_tools/test/analyze_duplicate_names_test.dart b/packages/flutter_tools/test/analyze_duplicate_names_test.dart index 9e353723b5..1de19c678d 100644 --- a/packages/flutter_tools/test/analyze_duplicate_names_test.dart +++ b/packages/flutter_tools/test/analyze_duplicate_names_test.dart @@ -38,9 +38,8 @@ void main() { return createTestCommandRunner(command).run( ['analyze', '--no-current-package', '--no-current-directory', dartFileA.path, dartFileB.path] ).then((int code) { - expect(code, 1); - expect(testLogger.errorText, '[warning] The imported libraries \'a.dart\' and \'b.dart\' cannot have the same name \'test\' (${dartFileB.path})\n'); - expect(testLogger.statusText, 'Analyzing 2 entry points...\n'); + expect(code, 0); + expect(testLogger.statusText, startsWith('Analyzing 2 files...\nNo analyzer warnings!')); }); }, overrides: { From eb215c14ac2a9fd26f536b0109de7788bef22751 Mon Sep 17 00:00:00 2001 From: pq Date: Thu, 5 May 2016 15:16:27 -0700 Subject: [PATCH 6/6] Review cleanup. --- packages/flutter_test/pubspec.yaml | 4 ++- .../lib/src/commands/analyze.dart | 28 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/packages/flutter_test/pubspec.yaml b/packages/flutter_test/pubspec.yaml index 3ee936ca42..fb1a06f4e4 100644 --- a/packages/flutter_test/pubspec.yaml +++ b/packages/flutter_test/pubspec.yaml @@ -7,7 +7,9 @@ dependencies: # here we pin it precisely to avoid version skew across our packages. test: 0.12.13+1 - # Pinned in flutter_tools as well. + # We don't actually depend on 'analyzer', but 'test' and 'flutter_tools' do. + # We pin the version of analyzer we depend on to avoid version skew across our + # packages. analyzer: 0.27.4-alpha.1 flutter: diff --git a/packages/flutter_tools/lib/src/commands/analyze.dart b/packages/flutter_tools/lib/src/commands/analyze.dart index d6dabaa6fb..88288b840e 100644 --- a/packages/flutter_tools/lib/src/commands/analyze.dart +++ b/packages/flutter_tools/lib/src/commands/analyze.dart @@ -111,32 +111,31 @@ class AnalyzeCommand extends FlutterCommand { } } - //TODO(pq): revisit package and directory defaults + //TODO (pq): revisit package and directory defaults if (argResults['current-directory'] && !argResults['flutter-repo']) { // ./*.dart - Directory cwd = new Directory('.'); + Directory currentDirectory = new Directory('.'); bool foundOne = false; - for (FileSystemEntity entry in cwd.listSync()) { + for (FileSystemEntity entry in currentDirectory.listSync()) { if (isDartFile(entry)) { dartFiles.add(entry); foundOne = true; } } - if (foundOne) { - pubSpecDirectories.add(cwd); - } + if (foundOne) + pubSpecDirectories.add(currentDirectory); } if (argResults['current-package'] && !argResults['flutter-repo']) { // **/.*dart - Directory cwd = new Directory('.'); - _collectDartFiles(cwd, dartFiles); - pubSpecDirectories.add(cwd); + Directory currentDirectory = new Directory('.'); + _collectDartFiles(currentDirectory, dartFiles); + pubSpecDirectories.add(currentDirectory); } - //TODO(ianh): Fix the intl package resource generator - //TODO(pq): extract this regexp from the exclude in options + //TODO (ianh): Fix the intl package resource generator + //TODO (pq): extract this regexp from the exclude in options RegExp stockExampleFiles = new RegExp('examples/stocks/lib/.*\.dart\$'); if (argResults['flutter-repo']) { @@ -200,7 +199,7 @@ class AnalyzeCommand extends FlutterCommand { packagesBody.writeln('$package:${path.toUri(packages[package])}'); // save the .packages file to disk - //TODO(pq): consider passing package info via a data URI + //TODO (pq): consider passing package info via a data URI Directory host = Directory.systemTemp.createTempSync('flutter-analyze-'); String packagesFilePath = path.join(host.path, '.packages'); new File(packagesFilePath).writeAsStringSync(packagesBody.toString()); @@ -218,7 +217,7 @@ class AnalyzeCommand extends FlutterCommand { options.analysisOptionsFile = path.join(ArtifactStore.flutterRoot, 'packages', 'flutter_tools', 'flutter_analysis_options'); AnalysisDriver analyzer = new AnalysisDriver(options); - //TODO:(pq): consider error handling + //TODO (pq): consider error handling List errors = analyzer.analyze(dartFiles); int errorCount = 0; @@ -277,9 +276,8 @@ class AnalyzeCommand extends FlutterCommand { return collected; for (FileSystemEntity entity in dir.listSync(recursive: false, followLinks: false)) { - if (isDartFile(entity) && (exclude == null || !exclude(entity))) { + if (isDartFile(entity) && (exclude == null || !exclude(entity))) collected.add(entity); - } if (entity is Directory) { String name = path.basename(entity.path); if (!name.startsWith('.') && name != 'packages')