Skip to content

Commit 268c098

Browse files
committed
feat: lint default constructor only
1 parent 330eb89 commit 268c098

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

lib/src/lints/avoid_using_api/avoid_using_api_linter.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class AvoidUsingApiLinter {
1919
required this.config,
2020
});
2121

22+
/// The identifier for the default constructor
23+
static const String defaultConstructorIdentifier = '()';
24+
2225
/// Access to the resolver for this lint context
2326
final CustomLintResolver resolver;
2427

@@ -190,6 +193,11 @@ class AvoidUsingApiLinter {
190193
String className,
191194
String source,
192195
) {
196+
if (identifier == defaultConstructorIdentifier) {
197+
_banDefaultConstructor(className, source, entryCode);
198+
return;
199+
}
200+
193201
context.registry.addSimpleIdentifier((node) {
194202
final name = node.name;
195203
if (name != identifier) {
@@ -280,4 +288,25 @@ class AvoidUsingApiLinter {
280288
reporter.atNode(node.identifier, entryCode);
281289
});
282290
}
291+
292+
void _banDefaultConstructor(
293+
String className,
294+
String source,
295+
LintCode entryCode,
296+
) {
297+
context.registry.addInstanceCreationExpression((node) {
298+
final constructorName = node.constructorName.type.name2.lexeme;
299+
if (constructorName != className || node.constructorName.name != null) {
300+
return;
301+
}
302+
303+
final sourcePath =
304+
node.constructorName.type.element2?.library2?.uri.toString();
305+
if (sourcePath == null || !_matchesSource(sourcePath, source)) {
306+
return;
307+
}
308+
309+
reporter.atNode(node, entryCode);
310+
});
311+
}
283312
}

lint_test/avoid_using_api/identifier_class_source_ban/analysis_options.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ custom_lint:
77
- avoid_using_api:
88
severity: warning
99
entries:
10+
- identifier: ()
11+
class_name: BannedCodeUsage
12+
source: package:external_source
13+
reason: "BannedCodeUsage() from package:external_source is not allowed"
1014
- identifier: test4
1115
class_name: BannedCodeUsage
1216
source: package:external_source

lint_test/avoid_using_api/identifier_class_source_ban/lib/identifier_class_source_ban_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'dart:collection';
55
import 'package:external_source/external_source.dart';
66

77
void testingBannedCodeLint() async {
8+
// expect_lint: avoid_using_api
89
final bannedCodeUsage = BannedCodeUsage();
910
// expect_lint: avoid_using_api
1011
BannedCodeUsage.test2();

0 commit comments

Comments
 (0)