Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit ab95f02

Browse files
authored
feat: support type=lint suppression (#1156)
* feat: support type=lint suppression * fix: disable type=lint check for other commands
1 parent 4affa64 commit ab95f02

File tree

5 files changed

+61
-9
lines changed

5 files changed

+61
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* feat: add `allow-initialized` option to [`avoid-late-keyword`](https://dcm.dev/docs/individuals/rules/common/avoid-late-keyword).
1414
* feat: add `ignored-types` option to [`avoid-late-keyword`](https://dcm.dev/docs/individuals/rules/common/avoid-late-keyword).
1515
* fix: support tear-off methods for `check-unnecessary-nullable`.
16+
* feat: support type=lint suppression.
1617

1718
## 5.4.0
1819

lib/src/analyzers/lint_analyzer/lint_analyzer.dart

+12-6
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ class LintAnalyzer {
196196
);
197197
}
198198

199+
// ignore: long-method
199200
LintFileReport? _analyzeFile(
200201
ResolvedUnitResult result,
201202
LintAnalysisConfig config,
@@ -206,7 +207,11 @@ class LintAnalyzer {
206207
return null;
207208
}
208209

209-
final ignores = Suppression(result.content, result.lineInfo);
210+
final ignores = Suppression(
211+
result.content,
212+
result.lineInfo,
213+
supportsTypeLintIgnore: true,
214+
);
210215
final internalResult = InternalResolvedUnitResult(
211216
filePath,
212217
result.content,
@@ -240,11 +245,12 @@ class LintAnalyzer {
240245
path: filePath,
241246
relativePath: relativePath,
242247
file: fileMetrics,
243-
classes: Map.unmodifiable(classMetrics
244-
.map<String, Report>((key, value) => MapEntry(key.name, value))),
245-
functions: Map.unmodifiable(functionMetrics.map<String, Report>(
246-
(key, value) => MapEntry(key.fullName, value),
247-
)),
248+
classes: Map.unmodifiable(
249+
classMetrics.map((key, value) => MapEntry(key.name, value)),
250+
),
251+
functions: Map.unmodifiable(
252+
functionMetrics.map((key, value) => MapEntry(key.fullName, value)),
253+
),
248254
issues: issues,
249255
antiPatternCases: antiPatterns,
250256
);

lib/src/utils/suppression.dart

+18-3
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,30 @@ class Suppression {
77
static final _ignoreForFileMatcher =
88
RegExp('//[ ]*ignore_for_file:(.*)', multiLine: true);
99

10+
static const _typeLint = 'type=lint';
11+
1012
final _ignoreMap = <int, List<String>>{};
1113
final _ignoreForFileSet = <String>{};
14+
15+
bool _hasAllLintsSuppressed = false;
16+
1217
final LineInfo lineInfo;
1318

1419
/// Checks that the [id] is globally suppressed.
15-
bool isSuppressed(String id) => _ignoreForFileSet.contains(_canonicalize(id));
20+
bool isSuppressed(String id) =>
21+
_hasAllLintsSuppressed || _ignoreForFileSet.contains(_canonicalize(id));
1622

1723
/// Checks that the [id] is suppressed for the [lineIndex].
1824
bool isSuppressedAt(String id, int lineIndex) =>
1925
isSuppressed(id) ||
2026
(_ignoreMap[lineIndex]?.contains(_canonicalize(id)) ?? false);
2127

2228
/// Initialize a newly created [Suppression] with the given [content] and [lineInfo].
23-
Suppression(String content, this.lineInfo) {
29+
Suppression(
30+
String content,
31+
this.lineInfo, {
32+
bool supportsTypeLintIgnore = false,
33+
}) {
2434
for (final match in _ignoreMatchers.allMatches(content)) {
2535
final ids = match.group(1)!.split(',').map(_canonicalize);
2636
final location = lineInfo.getLocation(match.start);
@@ -40,7 +50,12 @@ class Suppression {
4050
}
4151

4252
for (final match in _ignoreForFileMatcher.allMatches(content)) {
43-
_ignoreForFileSet.addAll(match.group(1)!.split(',').map(_canonicalize));
53+
final suppressed = match.group(1)!.split(',').map(_canonicalize);
54+
if (supportsTypeLintIgnore && suppressed.contains(_typeLint)) {
55+
_hasAllLintsSuppressed = true;
56+
} else {
57+
_ignoreForFileSet.addAll(suppressed);
58+
}
4459
}
4560
}
4661

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// ignore_for_file: type=lint
2+
3+
void main() {
4+
const a = 5;
5+
const b = a + 5;
6+
}

test/src/analyzers/lint_analyzer/models/suppression_test.dart

+24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:test/test.dart';
44
import '../../../helpers/file_resolver.dart';
55

66
const _examplePath = 'test/resources/suppression_example.dart';
7+
const _exampleAllPath = 'test/resources/suppression_all_example.dart';
78

89
void main() {
910
test('suppression in content', () async {
@@ -12,6 +13,7 @@ void main() {
1213
final suppression = Suppression(
1314
parseResult.content,
1415
parseResult.lineInfo,
16+
supportsTypeLintIgnore: true,
1517
);
1618

1719
expect(suppression.isSuppressed('rule_id1'), isTrue);
@@ -26,4 +28,26 @@ void main() {
2628
expect(suppression.isSuppressedAt('rule_id8', 9), isTrue);
2729
expect(suppression.isSuppressedAt('rule_id9', 9), isTrue);
2830
});
31+
32+
test('suppression with type=lint', () async {
33+
final parseResult = await FileResolver.resolve(_exampleAllPath);
34+
35+
final suppression = Suppression(
36+
parseResult.content,
37+
parseResult.lineInfo,
38+
supportsTypeLintIgnore: true,
39+
);
40+
41+
expect(suppression.isSuppressed('rule_id1'), isTrue);
42+
expect(suppression.isSuppressed('rule_id2'), isTrue);
43+
expect(suppression.isSuppressed('rule_id3'), isTrue);
44+
expect(suppression.isSuppressed('rule_id4'), isTrue);
45+
46+
expect(suppression.isSuppressedAt('rule_id4', 5), isTrue);
47+
expect(suppression.isSuppressedAt('rule_id5', 5), isTrue);
48+
expect(suppression.isSuppressedAt('rule_id6', 9), isTrue);
49+
expect(suppression.isSuppressedAt('rule_id7', 9), isTrue);
50+
expect(suppression.isSuppressedAt('rule_id8', 9), isTrue);
51+
expect(suppression.isSuppressedAt('rule_id9', 9), isTrue);
52+
});
2953
}

0 commit comments

Comments
 (0)