Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## 0.4.0

- Added `allow_with_comments` parameter for `no_empty_block` lint.

- Added extension support for `avoid_using_api`
Comment thread
yurii-prykhodko-solid marked this conversation as resolved.
## 0.3.0

- Added `exclude` parameter for the following lints:
Expand Down
43 changes: 43 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,48 @@ class AvoidUsingApiLinter {
);
}
});

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

final element = node.methodName.staticElement;
if (element == null || element.enclosingElement3 is! ExtensionElement) {
return;
}

final extensionElement = element.enclosingElement3! as ExtensionElement;

if (extensionElement.name != className) {
return;
}

final sourcePath = extensionElement.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 element = node.identifier.staticElement;
if (element == null || element.enclosingElement3 is! ExtensionElement) {
return;
}

final extensionElement = element.enclosingElement3! as ExtensionElement;
if (extensionElement.name != className) return;

final sourcePath = extensionElement.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;
Comment thread
yurii-prykhodko-solid marked this conversation as resolved.

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: ../../../