Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.4.0

- Added `allow_with_comments` parameter for `no_empty_block` lint.
- Added extension support for `avoid_using_api`
- Added `exclude_entity` parameter for `prefer_match_file_name` lint.
It is now possible to configure this lint to ignore `enum`,
`extension` and `mixin` declarations via `analysis_options.yaml`.
Expand Down
34 changes: 34 additions & 0 deletions lib/src/lints/avoid_using_api/avoid_using_api_linter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,39 @@ class AvoidUsingApiLinter {
);
}
});

context.registry.addMethodInvocation((node) {
final methodName = node.methodName.name;
if (methodName != identifier) return;

final enclosingElement = node.methodName.staticElement?.enclosingElement3;
if (enclosingElement is! ExtensionElement ||
enclosingElement.name != className) {
return;
}

final sourcePath = enclosingElement.librarySource.uri.toString();
if (!_matchesSource(sourcePath, source)) {
return;
}

reporter.atNode(node.methodName, entryCode);
});

context.registry.addPrefixedIdentifier((node) {
final propertyName = node.identifier.name;
if (propertyName != identifier) return;

final enclosingElement = node.identifier.staticElement?.enclosingElement3;
if (enclosingElement is! ExtensionElement ||
enclosingElement.name != className) {
return;
}

final sourcePath = enclosingElement.librarySource.uri.toString();
if (!_matchesSource(sourcePath, source)) return;

reporter.atNode(node.identifier, entryCode);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ const test2 = 'Hello World';
void test() {}

int banned = 5;

extension BannedExtension on int {
int banned() => this + 10;

int get bannedGetter => 10;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
analyzer:
plugins:
- ../../custom_lint

custom_lint:
rules:
- avoid_using_api:
severity: warning
entries:
- class_name: BannedExtension
identifier: banned
source: package:external_source
reason: "Banned identifier from BannedExtension from package:external_source is not allowed"
- class_name: BannedExtension
identifier: bannedGetter
source: package:external_source
reason: "bannedGetter identifier from BannedExtension from package:external_source is not allowed"

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:external_source/external_source.dart';

void extensionIdentifierBanTesting() {
const int a = 10;

// expect_lint: avoid_using_api
a.banned();

// expect_lint: avoid_using_api
a.bannedGetter;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: identifier_extension_source_ban
description: A sample command-line application.
version: 1.0.0
publish_to: none

environment:
sdk: ^3.7.2

dependencies:
external_source:
path: ../external_source

dev_dependencies:
lints: ^3.0.0
test: ^1.21.0
solid_lints:
path: ../../../