Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: pkg/analyzer/lib/src/task/dart.dart

Issue 2888953002: Analyzer: add ignore_for_file comment that ignores a type of problem for the whole file; add tests. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/error_suppression_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 analyzer.src.task.dart; 5 library analyzer.src.task.dart;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 import 'package:analyzer/dart/ast/ast.dart'; 9 import 'package:analyzer/dart/ast/ast.dart';
10 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; 10 import 'package:analyzer/dart/ast/standard_resolution_map.dart';
(...skipping 2987 matching lines...) Expand 10 before | Expand all | Expand 10 after
2998 * Create a [GenerateLintsTask] based on the given [target] in 2998 * Create a [GenerateLintsTask] based on the given [target] in
2999 * the given [context]. 2999 * the given [context].
3000 */ 3000 */
3001 static GenerateLintsTask createTask( 3001 static GenerateLintsTask createTask(
3002 AnalysisContext context, AnalysisTarget target) { 3002 AnalysisContext context, AnalysisTarget target) {
3003 return new GenerateLintsTask(context, target); 3003 return new GenerateLintsTask(context, target);
3004 } 3004 }
3005 } 3005 }
3006 3006
3007 /** 3007 /**
3008 * Information about analysis `//ignore:` comments within a source file. 3008 * Information about analysis `//ignore:` and `//ignore_for_file` comments
3009 * within a source file.
3009 */ 3010 */
3010 class IgnoreInfo { 3011 class IgnoreInfo {
3011 /** 3012 /**
3012 * Instance shared by all cases without matches. 3013 * Instance shared by all cases without matches.
3013 */ 3014 */
3014 static final IgnoreInfo _EMPTY_INFO = new IgnoreInfo(); 3015 static final IgnoreInfo _EMPTY_INFO = new IgnoreInfo();
3015 3016
3016 /** 3017 /**
3017 * A regular expression for matching 'ignore' comments. Produces matches 3018 * A regular expression for matching 'ignore' comments. Produces matches
3018 * containing 2 groups. For example: 3019 * containing 2 groups. For example:
3019 * 3020 *
3020 * * ['//ignore: error_code', 'error_code'] 3021 * * ['//ignore: error_code', 'error_code']
3021 * 3022 *
3022 * Resulting codes may be in a list ('error_code_1,error_code2'). 3023 * Resulting codes may be in a list ('error_code_1,error_code2').
3023 */ 3024 */
3024 static final RegExp _IGNORE_MATCHER = 3025 static final RegExp _IGNORE_MATCHER =
3025 new RegExp(r'//[ ]*ignore:(.*)$', multiLine: true); 3026 new RegExp(r'//[ ]*ignore:(.*)$', multiLine: true);
3026 3027
3028 /**
3029 * A regular expression for matching 'ignore_for_file' comments. Produces
3030 * matches containing 2 groups. For example:
3031 *
3032 * * ['//ignore_for_file: error_code', 'error_code']
3033 *
3034 * Resulting codes may be in a list ('error_code_1,error_code2').
3035 */
3036 static final RegExp _IGNORE_FOR_FILE_MATCHER =
3037 new RegExp(r'//[ ]*ignore_for_file:(.*)$', multiLine: true);
3038
3027 final Map<int, List<String>> _ignoreMap = new HashMap<int, List<String>>(); 3039 final Map<int, List<String>> _ignoreMap = new HashMap<int, List<String>>();
3028 3040
3041 final Set<String> _ignoreForFileSet = new HashSet<String>();
3042
3029 /** 3043 /**
3030 * Whether this info object defines any ignores. 3044 * Whether this info object defines any ignores.
3031 */ 3045 */
3032 bool get hasIgnores => ignores.isNotEmpty; 3046 bool get hasIgnores => ignores.isNotEmpty || _ignoreForFileSet.isNotEmpty;
3033 3047
3034 /** 3048 /**
3035 * Map of line numbers to associated ignored error codes. 3049 * Map of line numbers to associated ignored error codes.
3036 */ 3050 */
3037 Map<int, Iterable<String>> get ignores => _ignoreMap; 3051 Map<int, Iterable<String>> get ignores => _ignoreMap;
3038 3052
3039 /** 3053 /**
3054 * Iterable of error codes ignored for the whole file.
3055 */
3056 Iterable<String> get ignoreForFiles => _ignoreForFileSet;
3057
3058 /**
3040 * Ignore this [errorCode] at [line]. 3059 * Ignore this [errorCode] at [line].
3041 */ 3060 */
3042 void add(int line, String errorCode) { 3061 void add(int line, String errorCode) {
3043 _ignoreMap.putIfAbsent(line, () => new List<String>()).add(errorCode); 3062 _ignoreMap.putIfAbsent(line, () => new List<String>()).add(errorCode);
3044 } 3063 }
3045 3064
3046 /** 3065 /**
3047 * Ignore these [errorCodes] at [line]. 3066 * Ignore these [errorCodes] at [line].
3048 */ 3067 */
3049 void addAll(int line, Iterable<String> errorCodes) { 3068 void addAll(int line, Iterable<String> errorCodes) {
3050 _ignoreMap.putIfAbsent(line, () => new List<String>()).addAll(errorCodes); 3069 _ignoreMap.putIfAbsent(line, () => new List<String>()).addAll(errorCodes);
3051 } 3070 }
3052 3071
3053 /** 3072 /**
3073 * Ignore these [errorCodes] in the whole file.
3074 */
3075 void addAllForFile(Iterable<String> errorCodes) {
3076 _ignoreForFileSet.addAll(errorCodes);
3077 }
3078
3079 /**
3054 * Test whether this [errorCode] is ignored at the given [line]. 3080 * Test whether this [errorCode] is ignored at the given [line].
3055 */ 3081 */
3056 bool ignoredAt(String errorCode, int line) => 3082 bool ignoredAt(String errorCode, int line) =>
3083 _ignoreForFileSet.contains(errorCode) ||
3057 _ignoreMap[line]?.contains(errorCode) == true; 3084 _ignoreMap[line]?.contains(errorCode) == true;
3058 3085
3059 /** 3086 /**
3060 * Calculate ignores for the given [content] with line [info]. 3087 * Calculate ignores for the given [content] with line [info].
3061 */ 3088 */
3062 static IgnoreInfo calculateIgnores(String content, LineInfo info) { 3089 static IgnoreInfo calculateIgnores(String content, LineInfo info) {
3063 Iterable<Match> matches = _IGNORE_MATCHER.allMatches(content); 3090 Iterable<Match> matches = _IGNORE_MATCHER.allMatches(content);
3064 if (matches.isEmpty) { 3091 Iterable<Match> fileMatches = _IGNORE_FOR_FILE_MATCHER.allMatches(content);
3092 if (matches.isEmpty && fileMatches.isEmpty) {
3065 return _EMPTY_INFO; 3093 return _EMPTY_INFO;
3066 } 3094 }
3067 3095
3068 IgnoreInfo ignoreInfo = new IgnoreInfo(); 3096 IgnoreInfo ignoreInfo = new IgnoreInfo();
3069 for (Match match in matches) { 3097 for (Match match in matches) {
3070 // See _IGNORE_MATCHER for format --- note the possibility of error lists. 3098 // See _IGNORE_MATCHER for format --- note the possibility of error lists.
3071 Iterable<String> codes = match 3099 Iterable<String> codes = match
3072 .group(1) 3100 .group(1)
3073 .split(',') 3101 .split(',')
3074 .map((String code) => code.trim().toLowerCase()); 3102 .map((String code) => code.trim().toLowerCase());
3075 ignoreInfo.addAll(info.getLocation(match.start).lineNumber, codes); 3103 ignoreInfo.addAll(info.getLocation(match.start).lineNumber, codes);
3076 } 3104 }
3105 for (Match match in fileMatches) {
3106 Iterable<String> codes = match
3107 .group(1)
3108 .split(',')
3109 .map((String code) => code.trim().toLowerCase());
3110 ignoreInfo.addAllForFile(codes);
3111 }
3077 return ignoreInfo; 3112 return ignoreInfo;
3078 } 3113 }
3079 } 3114 }
3080 3115
3081 /** 3116 /**
3082 * A task that ensures that all of the inferable instance members in a 3117 * A task that ensures that all of the inferable instance members in a
3083 * compilation unit have had their type inferred. 3118 * compilation unit have had their type inferred.
3084 */ 3119 */
3085 class InferInstanceMembersInUnitTask extends SourceBasedAnalysisTask { 3120 class InferInstanceMembersInUnitTask extends SourceBasedAnalysisTask {
3086 /** 3121 /**
(...skipping 2749 matching lines...) Expand 10 before | Expand all | Expand 10 after
5836 5871
5837 @override 5872 @override
5838 bool moveNext() { 5873 bool moveNext() {
5839 if (_newSources.isEmpty) { 5874 if (_newSources.isEmpty) {
5840 return false; 5875 return false;
5841 } 5876 }
5842 currentTarget = _newSources.removeLast(); 5877 currentTarget = _newSources.removeLast();
5843 return true; 5878 return true;
5844 } 5879 }
5845 } 5880 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/generated/error_suppression_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698