Skip to content

Commit fc3ff62

Browse files
iimironchukyurii-prykhodko-solidYurii Prykhodko
authored
Add extensions support for avoid_using_api (#208)
* lint is working for extensions * test case for avoid_using_api * avoid_using_api works with getters + cleared test case * Update CHANGELOG.md * Update CHANGELOG.md * simplify * fmt --------- Co-authored-by: Yurii Prykhodko <[email protected]> Co-authored-by: Yurii Prykhodko <[email protected]>
1 parent 7770a13 commit fc3ff62

File tree

6 files changed

+87
-0
lines changed

6 files changed

+87
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 0.4.0
22

33
- Added `allow_with_comments` parameter for `no_empty_block` lint.
4+
- Added extension support for `avoid_using_api`
45
- Added `exclude_entity` parameter for `prefer_match_file_name` lint.
56
It is now possible to configure this lint to ignore `enum`,
67
`extension` and `mixin` declarations via `analysis_options.yaml`.

lib/src/lints/avoid_using_api/avoid_using_api_linter.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,5 +245,39 @@ class AvoidUsingApiLinter {
245245
);
246246
}
247247
});
248+
249+
context.registry.addMethodInvocation((node) {
250+
final methodName = node.methodName.name;
251+
if (methodName != identifier) return;
252+
253+
final enclosingElement = node.methodName.staticElement?.enclosingElement3;
254+
if (enclosingElement is! ExtensionElement ||
255+
enclosingElement.name != className) {
256+
return;
257+
}
258+
259+
final sourcePath = enclosingElement.librarySource.uri.toString();
260+
if (!_matchesSource(sourcePath, source)) {
261+
return;
262+
}
263+
264+
reporter.atNode(node.methodName, entryCode);
265+
});
266+
267+
context.registry.addPrefixedIdentifier((node) {
268+
final propertyName = node.identifier.name;
269+
if (propertyName != identifier) return;
270+
271+
final enclosingElement = node.identifier.staticElement?.enclosingElement3;
272+
if (enclosingElement is! ExtensionElement ||
273+
enclosingElement.name != className) {
274+
return;
275+
}
276+
277+
final sourcePath = enclosingElement.librarySource.uri.toString();
278+
if (!_matchesSource(sourcePath, source)) return;
279+
280+
reporter.atNode(node.identifier, entryCode);
281+
});
248282
}
249283
}

lint_test/avoid_using_api/external_source/lib/external_source.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ const test2 = 'Hello World';
2222
void test() {}
2323

2424
int banned = 5;
25+
26+
extension BannedExtension on int {
27+
int banned() => this + 10;
28+
29+
int get bannedGetter => 10;
30+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
analyzer:
2+
plugins:
3+
- ../../custom_lint
4+
5+
custom_lint:
6+
rules:
7+
- avoid_using_api:
8+
severity: warning
9+
entries:
10+
- class_name: BannedExtension
11+
identifier: banned
12+
source: package:external_source
13+
reason: "Banned identifier from BannedExtension from package:external_source is not allowed"
14+
- class_name: BannedExtension
15+
identifier: bannedGetter
16+
source: package:external_source
17+
reason: "bannedGetter identifier from BannedExtension from package:external_source is not allowed"
18+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import 'package:external_source/external_source.dart';
2+
3+
void extensionIdentifierBanTesting() {
4+
const int a = 10;
5+
6+
// expect_lint: avoid_using_api
7+
a.banned();
8+
9+
// expect_lint: avoid_using_api
10+
a.bannedGetter;
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: identifier_extension_source_ban
2+
description: A sample command-line application.
3+
version: 1.0.0
4+
publish_to: none
5+
6+
environment:
7+
sdk: ^3.7.2
8+
9+
dependencies:
10+
external_source:
11+
path: ../external_source
12+
13+
dev_dependencies:
14+
lints: ^3.0.0
15+
test: ^1.21.0
16+
solid_lints:
17+
path: ../../../

0 commit comments

Comments
 (0)