• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dart-lang / linter / 3690449303

pending completion
3690449303

push

github

GitHub
Fix `unnecessary_parenthesis` with postfix bang operator. (#3904)

2 of 2 new or added lines in 1 file covered. (100.0%)

8263 of 8640 relevant lines covered (95.64%)

1.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.83
/lib/src/rules/implementation_imports.dart
1
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2
// for details. All rights reserved. Use of this source code is governed by a
3
// BSD-style license that can be found in the LICENSE file.
4

5
import 'package:analyzer/dart/ast/ast.dart';
6
import 'package:analyzer/dart/ast/visitor.dart';
7

8
import '../analyzer.dart';
9

10
const _desc = r"Don't import implementation files from another package.";
11

12
const _details = r'''
13
From the the [pub package layout doc](https://dart.dev/tools/pub/package-layout#implementation-files):
14

15
**DON'T** import implementation files from another package.
16

17
The libraries inside `lib` are publicly visible: other packages are free to
18
import them.  But much of a package's code is internal implementation libraries
19
that should only be imported and used by the package itself.  Those go inside a
20
subdirectory of `lib` called `src`.  You can create subdirectories in there if
21
it helps you organize things.
22

23
You are free to import libraries that live in `lib/src` from within other Dart
24
code in the same package (like other libraries in `lib`, scripts in `bin`,
25
and tests) but you should never import from another package's `lib/src`
26
directory.  Those files are not part of the package's public API, and they
27
might change in ways that could break your code.
28

29
**BAD:**
30
```dart
31
// In 'road_runner'
32
import 'package:acme/src/internals.dart';
33
```
34

35
''';
36

37
bool isImplementation(Uri? uri) {
1✔
38
  var segments = uri?.pathSegments ?? const <String>[];
1✔
39
  if (segments.length > 2) {
2✔
40
    if (segments[1] == 'src') {
2✔
41
      return true;
42
    }
43
  }
44
  return false;
45
}
46

47
bool isPackage(Uri? uri) => uri?.scheme == 'package';
3✔
48

49
bool samePackage(Uri? uri1, Uri? uri2) {
1✔
50
  if (uri1 == null || uri2 == null) {
51
    return false;
52
  }
53
  var segments1 = uri1.pathSegments;
1✔
54
  var segments2 = uri2.pathSegments;
1✔
55
  if (segments1.isEmpty || segments2.isEmpty) {
2✔
56
    return false;
57
  }
58
  return segments1.first == segments2.first;
3✔
59
}
60

61
class ImplementationImports extends LintRule {
62
  static const LintCode code = LintCode('implementation_imports',
63
      "Import of a library in the 'lib/src' directory of another package.",
64
      correctionMessage:
65
          'Try importing a public library that exports this library, or '
66
          'removing the import.');
67

68
  ImplementationImports()
1✔
69
      : super(
1✔
70
            name: 'implementation_imports',
71
            description: _desc,
72
            details: _details,
73
            group: Group.style);
74

75
  @override
1✔
76
  LintCode get lintCode => code;
77

78
  @override
1✔
79
  void registerNodeProcessors(
80
      NodeLintRegistry registry, LinterContext context) {
81
    var visitor = _Visitor(this);
1✔
82
    registry.addImportDirective(this, visitor);
1✔
83
  }
84
}
85

86
class _Visitor extends SimpleAstVisitor<void> {
87
  final LintRule rule;
88

89
  _Visitor(this.rule);
1✔
90

91
  @override
×
92
  void visitImportDirective(ImportDirective node) {
93
    var importUri = node.element?.importedLibrary?.source.uri;
×
94
    var sourceUri = node.element?.source.uri;
×
95

96
    // Test for 'package:*/src/'.
97
    if (!isImplementation(importUri)) {
×
98
      return;
99
    }
100

101
    // If the source URI is not a `package` URI bail out.
102
    if (!isPackage(sourceUri)) {
×
103
      return;
104
    }
105

106
    if (!samePackage(importUri, sourceUri)) {
×
107
      rule.reportLint(node.uri);
×
108
    }
109
  }
110
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc