Skip to content
Draft
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
5 changes: 5 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
plugins:
native_analyzer_plugin:
path: pkgs/native_analyzer_plugin
diagnostics:
avoid_import_outside_src: true
10 changes: 5 additions & 5 deletions pkgs/code_assets/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ include: package:dart_flutter_team_lints/analysis_options.yaml
analyzer:
language:
strict-raw-types: true
plugins:
# - custom_lint # https://github.com/dart-lang/sdk/issues/60784

linter:
rules:
Expand All @@ -15,6 +13,8 @@ linter:
- prefer_final_in_for_each
- prefer_final_locals

custom_lint:
rules:
- avoid_import_outside_src
plugins:
native_analyzer_plugin:
path: ../native_analyzer_plugin
diagnostics:
avoid_import_outside_src: true
5 changes: 2 additions & 3 deletions pkgs/code_assets/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ dependencies:
hooks: ^0.20.1

dev_dependencies:
custom_lint: ^0.7.5
dart_flutter_team_lints: ^3.5.2
json_schema: ^5.2.0 # May only be used in tool/ and test/json_schema/.
native_analyzer_plugin:
path: ../native_analyzer_plugin/
native_test_helpers:
path: ../native_test_helpers/
repo_lint_rules:
path: ../repo_lint_rules/
test: ^1.25.15
10 changes: 5 additions & 5 deletions pkgs/data_assets/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ include: package:dart_flutter_team_lints/analysis_options.yaml
analyzer:
language:
strict-raw-types: true
plugins:
# - custom_lint # https://github.com/dart-lang/sdk/issues/60784

linter:
rules:
Expand All @@ -14,6 +12,8 @@ linter:
- prefer_final_in_for_each
- prefer_final_locals

custom_lint:
rules:
- avoid_import_outside_src
plugins:
native_analyzer_plugin:
path: ../native_analyzer_plugin
diagnostics:
avoid_import_outside_src: true
3 changes: 0 additions & 3 deletions pkgs/data_assets/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ dependencies:
hooks: ^0.20.1

dev_dependencies:
custom_lint: ^0.7.5
dart_flutter_team_lints: ^3.5.2
json_schema: ^5.2.0 # May only be used in tool/ and test/json_schema/.
native_test_helpers:
path: ../native_test_helpers
repo_lint_rules:
path: ../repo_lint_rules/
test: ^1.25.15
10 changes: 5 additions & 5 deletions pkgs/hooks/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ include: package:dart_flutter_team_lints/analysis_options.yaml
analyzer:
language:
strict-raw-types: true
plugins:
# - custom_lint # https://github.com/dart-lang/sdk/issues/60784

linter:
rules:
Expand All @@ -15,6 +13,8 @@ linter:
- prefer_final_in_for_each
- prefer_final_locals

custom_lint:
rules:
- avoid_import_outside_src
plugins:
native_analyzer_plugin:
path: ../native_analyzer_plugin
diagnostics:
avoid_import_outside_src: true
2 changes: 1 addition & 1 deletion pkgs/hooks/lib/src/avoid_import_outside_src_rule_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// expect_lint: avoid_import_outside_src
// ignore: native_analyzer_plugin/avoid_import_outside_src
import '../hooks.dart';

/// A random use of a type.
Expand Down
5 changes: 2 additions & 3 deletions pkgs/hooks/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ dependencies:
dev_dependencies:
args: ^2.6.0
code_assets: ^0.19.7 # Used for running tests with real asset types.
custom_lint: ^0.7.5
dart_flutter_team_lints: ^3.5.2
data_assets: any # Used for running tests with real asset types.
file_testing: ^3.0.2
glob: any
json_schema: ^5.2.0 # May only be used in tool/ and test/json_schema/.
json_syntax_generator:
path: ../json_syntax_generator/
native_analyzer_plugin:
path: ../native_analyzer_plugin/
native_test_helpers:
path: ../native_test_helpers/
path: ^1.9.1
repo_lint_rules:
path: ../repo_lint_rules/
test: ^1.25.15
10 changes: 5 additions & 5 deletions pkgs/hooks_runner/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ include: package:dart_flutter_team_lints/analysis_options.yaml
analyzer:
language:
strict-raw-types: true
plugins:
# - custom_lint # https://github.com/dart-lang/sdk/issues/60784

linter:
rules:
Expand All @@ -14,6 +12,8 @@ linter:
- prefer_final_in_for_each
- prefer_final_locals

custom_lint:
rules:
- avoid_import_outside_src
plugins:
native_analyzer_plugin:
path: ../native_analyzer_plugin
diagnostics:
avoid_import_outside_src: true
5 changes: 2 additions & 3 deletions pkgs/hooks_runner/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ dependencies:

dev_dependencies:
args: any
custom_lint: ^0.7.5
dart_flutter_team_lints: ^3.5.2
data_assets: any # Used in tests.
file_testing: ^3.0.2
native_analyzer_plugin:
path: ../native_analyzer_plugin/
native_test_helpers:
path: ../native_test_helpers/
pub_formats: # Used in tests.
path: ../pub_formats/
repo_lint_rules:
path: ../repo_lint_rules/
test: ^1.25.15
10 changes: 5 additions & 5 deletions pkgs/json_syntax_generator/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ include: package:dart_flutter_team_lints/analysis_options.yaml
analyzer:
language:
strict-raw-types: true
plugins:
# - custom_lint # https://github.com/dart-lang/sdk/issues/60784

linter:
rules:
Expand All @@ -14,6 +12,8 @@ linter:
- prefer_final_in_for_each
- prefer_final_locals

custom_lint:
rules:
- avoid_import_outside_src
plugins:
native_analyzer_plugin:
path: ../native_analyzer_plugin
diagnostics:
avoid_import_outside_src: true
5 changes: 2 additions & 3 deletions pkgs/json_syntax_generator/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ dependencies:
json_schema: ^5.2.0

dev_dependencies:
custom_lint: ^0.7.5
dart_flutter_team_lints: ^3.5.2
native_analyzer_plugin:
path: ../native_analyzer_plugin/
native_test_helpers:
path: ../native_test_helpers/
path: ^1.9.1
repo_lint_rules:
path: ../repo_lint_rules/
test: ^1.25.15
17 changes: 17 additions & 0 deletions pkgs/native_analyzer_plugin/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analysis_server_plugin/plugin.dart';
import 'package:analysis_server_plugin/registry.dart';

import 'src/rules/avoid_import_outside_src_rule.dart';

final plugin = NativeAnalyzerPlugin();

class NativeAnalyzerPlugin extends Plugin {
@override
void register(PluginRegistry registry) {
registry.registerLintRule(AvoidImportOutsideSrcRule());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/analysis_rule/analysis_rule.dart';
import 'package:analyzer/analysis_rule/rule_context.dart';
import 'package:analyzer/analysis_rule/rule_visitor_registry.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/error/error.dart';

class AvoidImportOutsideSrcRule extends AnalysisRule {
static const LintCode code = LintCode(
'avoid_import_outside_src',
"Avoid importing files outside 'lib/src/' from files within 'lib/src/'.",
correctionMessage:
"Files outside 'lib/src/' likely only export definitions from inside 'lib/src/'. "
'Import the file with the definition directly.',
);

AvoidImportOutsideSrcRule()
: super(
name: 'avoid_import_outside_src',
description:
"Avoid importing files outside 'lib/src/' from files within 'lib/src/'.",
);

@override
LintCode get diagnosticCode => code;

@override
void registerNodeProcessors(
RuleVisitorRegistry registry,
RuleContext context,
) {
var visitor = _Visitor(rule: this, context: context);
registry.addImportDirective(this, visitor);
}
}

class _Visitor extends SimpleAstVisitor<void> {
final RuleContext context;
final AnalysisRule rule;

_Visitor({required this.rule, required this.context});

@override
void visitImportDirective(ImportDirective node) {
final importString = node.uri.stringValue;
final importedUri = node.libraryImport?.importedLibrary?.uri;
final importingFile = context.currentUnit?.file.toUri();

if (importedUri == null || importingFile == null || importString == null) {
return;
}

if (importString.startsWith('package')) {
// Package imports are of no interest. Use prefer_relative_imports to
// prevent package imports of the same package.
return;
}

if (importString.startsWith('dart')) {
return;
}

if (_isInSrcDirectory(importingFile)) {
if (!_isInSrcDirectory(importedUri)) {
rule.reportAtNode(node);
}
}
}

bool _isInSrcDirectory(Uri uri) {
if (uri.scheme == 'package') {
if (uri.path.contains('src/')) {
return true;
}
return false;
} else if (uri.scheme == 'file') {
if (uri.toFilePath(windows: false).contains('lib/src/')) {
return true;
}
if (uri.toFilePath(windows: false).contains('lib/')) {
return false;
}
}
return false;
}
}
18 changes: 18 additions & 0 deletions pkgs/native_analyzer_plugin/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: native_analyzer_plugin
version: 0.1.0
publish_to: none

resolution: workspace

environment:
sdk: '>=3.9.0 <4.0.0'

dependencies:
analysis_server_plugin: ^0.2.2
analyzer: ^8.0.0
analyzer_plugin: ^0.13.6
analyzer_testing: ^0.1.2

dev_dependencies:
test: any
test_reflective_loader: any
10 changes: 5 additions & 5 deletions pkgs/native_test_helpers/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ include: package:dart_flutter_team_lints/analysis_options.yaml
analyzer:
language:
strict-raw-types: true
plugins:
# - custom_lint # https://github.com/dart-lang/sdk/issues/60784

linter:
rules:
Expand All @@ -14,6 +12,8 @@ linter:
- prefer_final_in_for_each
- prefer_final_locals

custom_lint:
rules:
- avoid_import_outside_src
plugins:
native_analyzer_plugin:
path: ../native_analyzer_plugin
diagnostics:
avoid_import_outside_src: true
10 changes: 5 additions & 5 deletions pkgs/native_toolchain_c/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ include: package:dart_flutter_team_lints/analysis_options.yaml
analyzer:
language:
strict-raw-types: true
plugins:
# - custom_lint # https://github.com/dart-lang/sdk/issues/60784

linter:
rules:
Expand All @@ -14,6 +12,8 @@ linter:
- prefer_final_in_for_each
- prefer_final_locals

custom_lint:
rules:
- avoid_import_outside_src
plugins:
native_analyzer_plugin:
path: ../native_analyzer_plugin
diagnostics:
avoid_import_outside_src: true
5 changes: 2 additions & 3 deletions pkgs/native_toolchain_c/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ dependencies:

dev_dependencies:
collection: ^1.19.1
custom_lint: ^0.7.5
dart_flutter_team_lints: ^3.5.2
native_analyzer_plugin:
path: ../native_analyzer_plugin/
native_test_helpers:
path: ../native_test_helpers/
repo_lint_rules:
path: ../repo_lint_rules/
test: ^1.25.15
2 changes: 2 additions & 0 deletions pkgs/objective_c/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ dev_dependencies:
ffigen: ^20.0.0-dev.0
flutter_test:
sdk: flutter
native_analyzer_plugin:
path: ../native_analyzer_plugin/
native_test_helpers:
path: ../native_test_helpers/
path: ^1.9.0
Expand Down
2 changes: 2 additions & 0 deletions pkgs/pub_formats/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ dev_dependencies:
json_schema: ^5.2.0 # May only be used in tool/ and test/json_schema/.
json_syntax_generator:
path: ../json_syntax_generator/
native_analyzer_plugin:
path: ../native_analyzer_plugin/
native_test_helpers:
path: ../native_test_helpers/
test: ^1.25.15
Expand Down
Loading
Loading