diff --git a/.analysis_options b/.analysis_options index 02fb831577..5c545109ec 100644 --- a/.analysis_options +++ b/.analysis_options @@ -1,9 +1,9 @@ # Specify analysis options. -# This file is the .analysis_options file used by Flutter editors, such as -# Atom. It is very similar to flutter_tools/flutter_analysis_options; the only -# difference (currently) is the public_member_api_docs option, which triggers -# too many messages to be used in editors. +# This file contains the analysis options used by Flutter editors, such as Atom. +# It is very similar to .analysis_options_flutter_analyze; +# the only difference (currently) is the public_member_api_docs option, +# which triggers too many messages to be used in editors. analyzer: language: diff --git a/.analysis_options_flutter_analyze b/.analysis_options_flutter_analyze new file mode 100644 index 0000000000..10d63e3f89 --- /dev/null +++ b/.analysis_options_flutter_analyze @@ -0,0 +1,73 @@ +# Specify analysis options. + +# This file contains the analysis options used by 'flutter analyze'. +# It is very similar to .analysis_options; +# the only difference (currently) is the public_member_api_docs option, +# which is turned on and programmatically reduced to a single output line +# indicating the # of violations for that rule. + +analyzer: + language: + enableStrictCallChecks: true + enableSuperMixins: true + strong-mode: true + errors: + # we allow overriding fields (if they use super, ideally...) + strong_mode_invalid_field_override: ignore + # we allow type narrowing + strong_mode_invalid_method_override: ignore + strong_mode_static_type_error: ignore + strong_mode_down_cast_composite: ignore + # we allow having TODOs in the code + todo: ignore + exclude: + - 'bin/**' + +linter: + rules: + # these are in the same order as http://dart-lang.github.io/linter/lints/ + # to make maintenance easier + + # error rules + - avoid_empty_else + # - comment_references + - control_flow_in_finally + - hash_and_equals + # - iterable_contains_unrelated_type + - test_types_in_equals + - throw_in_finally + - unrelated_type_equality_checks + + # style rules + - always_declare_return_types + - always_specify_types + - annotate_overrides + - avoid_as + - avoid_init_to_null + - avoid_return_types_on_setters + - await_only_futures + - camel_case_types + # - constant_identifier_names + - control_flow_in_finally + - empty_constructor_bodies + - implementation_imports + - library_names + - library_prefixes + - non_constant_identifier_names + - one_member_abstracts + # - overriden_field + - package_api_docs + - package_prefixed_library_names + - prefer_is_not_empty + - public_member_api_docs + - slash_for_doc_comments + - sort_constructors_first + - sort_unnamed_constructors_first + - super_goes_last + # - type_annotate_public_apis # subset of always_specify_types + - type_init_formals + - unnecessary_brace_in_string_interp + - unnecessary_getters_setters + + # pub rules + - package_names diff --git a/packages/flutter_tools/lib/src/commands/analyze.dart b/packages/flutter_tools/lib/src/commands/analyze.dart index a4f980c97b..11dd127a75 100644 --- a/packages/flutter_tools/lib/src/commands/analyze.dart +++ b/packages/flutter_tools/lib/src/commands/analyze.dart @@ -84,6 +84,21 @@ class AnalyzeCommand extends FlutterCommand { return true; } + /// Return `true` if [fileList] contains a path that resides inside the Flutter repository. + /// If [fileList] is empty, then return `true` if the current directory resides inside the Flutter repository. + bool inRepo(List fileList) { + if (fileList == null || fileList.isEmpty) + fileList = [path.current]; + String root = path.normalize(path.absolute(Cache.flutterRoot)); + String prefix = root + Platform.pathSeparator; + for (String file in fileList) { + file = path.normalize(path.absolute(file)); + if (file == root || file.startsWith(prefix)) + return true; + } + return false; + } + bool get _isBenchmarking => argResults['benchmark']; Future _analyzeOnce() async { @@ -106,7 +121,7 @@ class AnalyzeCommand extends FlutterCommand { bool currentDirectory = argResults['current-directory'] && (argResults.wasParsed('current-directory') || dartFiles.isEmpty); bool currentPackage = argResults['current-package'] && (argResults.wasParsed('current-package') || dartFiles.isEmpty); - bool flutterRepo = argResults['flutter-repo']; + bool flutterRepo = argResults['flutter-repo'] || inRepo(argResults.rest); //TODO (pq): revisit package and directory defaults @@ -202,7 +217,9 @@ class AnalyzeCommand extends FlutterCommand { DriverOptions options = new DriverOptions(); options.dartSdkPath = argResults['dart-sdk']; options.packageMap = packages; - options.analysisOptionsFile = path.join(Cache.flutterRoot, 'packages', 'flutter_tools', 'flutter_analysis_options'); + options.analysisOptionsFile = flutterRepo + ? path.join(Cache.flutterRoot, '.analysis_options_flutter_analyze') + : path.join(Cache.flutterRoot, 'packages', 'flutter_tools', 'flutter_analysis_options'); AnalysisDriver analyzer = new AnalysisDriver(options); // TODO(pq): consider error handling @@ -241,14 +258,14 @@ class AnalyzeCommand extends FlutterCommand { _writeBenchmark(stopwatch, errorCount, membersMissingDocumentation); if (errorCount > 0) { - if (membersMissingDocumentation > 0 && argResults['flutter-repo']) + if (membersMissingDocumentation > 0 && flutterRepo) 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']) { - if (membersMissingDocumentation > 0 && argResults['flutter-repo']) { + if (membersMissingDocumentation > 0 && flutterRepo) { printStatus('No analyzer warnings! (ran in ${elapsed}s; $membersMissingDocumentation public ${ membersMissingDocumentation == 1 ? "member lacks" : "members lack" } documentation)'); } else { printStatus('No analyzer warnings! (ran in ${elapsed}s)'); diff --git a/packages/flutter_tools/test/analyze_test.dart b/packages/flutter_tools/test/analyze_test.dart index 5eb1668cdc..95100a4bc8 100644 --- a/packages/flutter_tools/test/analyze_test.dart +++ b/packages/flutter_tools/test/analyze_test.dart @@ -6,6 +6,7 @@ import 'dart:async'; import 'dart:io'; import 'package:flutter_tools/src/base/os.dart'; +import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/commands/analyze.dart'; import 'package:flutter_tools/src/dart/pub.dart'; import 'package:flutter_tools/src/dart/sdk.dart'; @@ -67,6 +68,30 @@ void main() { }, overrides: { OperatingSystemUtils: os }); + + testUsingContext('inRepo', () { + AnalyzeCommand cmd = new AnalyzeCommand(); + // Absolute paths + expect(cmd.inRepo([tempDir.path]), isFalse); + expect(cmd.inRepo([path.join(tempDir.path, 'foo')]), isFalse); + expect(cmd.inRepo([Cache.flutterRoot]), isTrue); + expect(cmd.inRepo([path.join(Cache.flutterRoot, 'foo')]), isTrue); + // Relative paths + String oldWorkingDirectory = path.current; + try { + Directory.current = Cache.flutterRoot; + expect(cmd.inRepo(['.']), isTrue); + expect(cmd.inRepo(['foo']), isTrue); + Directory.current = tempDir.path; + expect(cmd.inRepo(['.']), isFalse); + expect(cmd.inRepo(['foo']), isFalse); + } finally { + Directory.current = oldWorkingDirectory; + } + // Ensure no exceptions + cmd.inRepo(null); + cmd.inRepo([]); + }); }); }