Index: pkg/analyzer/lib/src/task/dart.dart |
diff --git a/pkg/analyzer/lib/src/task/dart.dart b/pkg/analyzer/lib/src/task/dart.dart |
index 2de68f1957d9172236a914bee75a231d41491313..797d8f736aca11ba0e885d816076e67917ccae85 100644 |
--- a/pkg/analyzer/lib/src/task/dart.dart |
+++ b/pkg/analyzer/lib/src/task/dart.dart |
@@ -3005,7 +3005,8 @@ class GenerateLintsTask extends SourceBasedAnalysisTask { |
} |
/** |
- * Information about analysis `//ignore:` comments within a source file. |
+ * Information about analysis `//ignore:` and `//ignore_for_file` comments |
+ * within a source file. |
*/ |
class IgnoreInfo { |
/** |
@@ -3024,12 +3025,25 @@ class IgnoreInfo { |
static final RegExp _IGNORE_MATCHER = |
new RegExp(r'//[ ]*ignore:(.*)$', multiLine: true); |
+ /** |
+ * A regular expression for matching 'ignore_for_file' comments. Produces |
+ * matches containing 2 groups. For example: |
+ * |
+ * * ['//ignore_for_file: error_code', 'error_code'] |
+ * |
+ * Resulting codes may be in a list ('error_code_1,error_code2'). |
+ */ |
+ static final RegExp _IGNORE_FOR_FILE_MATCHER = |
+ new RegExp(r'//[ ]*ignore_for_file:(.*)$', multiLine: true); |
+ |
final Map<int, List<String>> _ignoreMap = new HashMap<int, List<String>>(); |
+ final Set<String> _ignoreForFileSet = new HashSet<String>(); |
+ |
/** |
* Whether this info object defines any ignores. |
*/ |
- bool get hasIgnores => ignores.isNotEmpty; |
+ bool get hasIgnores => ignores.isNotEmpty || _ignoreForFileSet.isNotEmpty; |
/** |
* Map of line numbers to associated ignored error codes. |
@@ -3037,6 +3051,11 @@ class IgnoreInfo { |
Map<int, Iterable<String>> get ignores => _ignoreMap; |
/** |
+ * Iterable of error codes ignored for the whole file. |
+ */ |
+ Iterable<String> get ignoreForFiles => _ignoreForFileSet; |
+ |
+ /** |
* Ignore this [errorCode] at [line]. |
*/ |
void add(int line, String errorCode) { |
@@ -3051,9 +3070,17 @@ class IgnoreInfo { |
} |
/** |
+ * Ignore these [errorCodes] in the whole file. |
+ */ |
+ void addAllForFile(Iterable<String> errorCodes) { |
+ _ignoreForFileSet.addAll(errorCodes); |
+ } |
+ |
+ /** |
* Test whether this [errorCode] is ignored at the given [line]. |
*/ |
bool ignoredAt(String errorCode, int line) => |
+ _ignoreForFileSet.contains(errorCode) || |
_ignoreMap[line]?.contains(errorCode) == true; |
/** |
@@ -3061,7 +3088,8 @@ class IgnoreInfo { |
*/ |
static IgnoreInfo calculateIgnores(String content, LineInfo info) { |
Iterable<Match> matches = _IGNORE_MATCHER.allMatches(content); |
- if (matches.isEmpty) { |
+ Iterable<Match> fileMatches = _IGNORE_FOR_FILE_MATCHER.allMatches(content); |
+ if (matches.isEmpty && fileMatches.isEmpty) { |
return _EMPTY_INFO; |
} |
@@ -3074,6 +3102,13 @@ class IgnoreInfo { |
.map((String code) => code.trim().toLowerCase()); |
ignoreInfo.addAll(info.getLocation(match.start).lineNumber, codes); |
} |
+ for (Match match in fileMatches) { |
+ Iterable<String> codes = match |
+ .group(1) |
+ .split(',') |
+ .map((String code) => code.trim().toLowerCase()); |
+ ignoreInfo.addAllForFile(codes); |
+ } |
return ignoreInfo; |
} |
} |