|
| 1 | +// Copyright (c) 2022, 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 'dart:collection'; |
| 6 | + |
| 7 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 8 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 9 | +import 'package:analyzer/dart/element/element.dart'; |
| 10 | +import 'package:analyzer/dart/element/type.dart'; |
| 11 | +import 'package:collection/collection.dart'; |
| 12 | + |
| 13 | +import '../analyzer.dart'; |
| 14 | +import '../util/dart_type_utilities.dart'; |
| 15 | + |
| 16 | +const _desc = 'Unreachable top-level members in executable libraries.'; |
| 17 | + |
| 18 | +const _details = r''' |
| 19 | +
|
| 20 | +Top-level members in an executable library should be used directly inside this |
| 21 | +library. An executable library is a library that contains a `main` top-level |
| 22 | +function or that contains a top-level function annotated with |
| 23 | +`@pragma('vm:entry-point')`). Executable libraries are not usually imported |
| 24 | +and it's better to avoid defining unused members. |
| 25 | +
|
| 26 | +This rule assumes that an executable library isn't imported by other files |
| 27 | +except to execute its `main` function. |
| 28 | +
|
| 29 | +**BAD:** |
| 30 | +
|
| 31 | +```dart |
| 32 | +main() {} |
| 33 | +void f() {} |
| 34 | +``` |
| 35 | +
|
| 36 | +**GOOD:** |
| 37 | +
|
| 38 | +```dart |
| 39 | +main() { |
| 40 | + f(); |
| 41 | +} |
| 42 | +void f() {} |
| 43 | +``` |
| 44 | +
|
| 45 | +'''; |
| 46 | + |
| 47 | +class UnreachableFromMain extends LintRule { |
| 48 | + UnreachableFromMain() |
| 49 | + : super( |
| 50 | + name: 'unreachable_from_main', |
| 51 | + description: _desc, |
| 52 | + details: _details, |
| 53 | + group: Group.style, |
| 54 | + maturity: Maturity.experimental, |
| 55 | + ); |
| 56 | + |
| 57 | + @override |
| 58 | + void registerNodeProcessors( |
| 59 | + NodeLintRegistry registry, |
| 60 | + LinterContext context, |
| 61 | + ) { |
| 62 | + var visitor = _Visitor(this); |
| 63 | + registry.addCompilationUnit(this, visitor); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +class _Visitor extends SimpleAstVisitor<void> { |
| 68 | + _Visitor(this.rule); |
| 69 | + |
| 70 | + final LintRule rule; |
| 71 | + |
| 72 | + @override |
| 73 | + void visitCompilationUnit(CompilationUnit node) { |
| 74 | + // TODO(a14n): add support of libs with parts |
| 75 | + if (node.directives.whereType<PartOfDirective>().isNotEmpty) return; |
| 76 | + if (node.directives.whereType<PartDirective>().isNotEmpty) return; |
| 77 | + |
| 78 | + var topDeclarations = node.declarations |
| 79 | + .expand((e) => [ |
| 80 | + if (e is TopLevelVariableDeclaration) |
| 81 | + ...e.variables.variables |
| 82 | + else |
| 83 | + e, |
| 84 | + ]) |
| 85 | + .toSet(); |
| 86 | + |
| 87 | + var entryPoints = topDeclarations.where(_isEntryPoint).toList(); |
| 88 | + if (entryPoints.isEmpty) return; |
| 89 | + |
| 90 | + var declarationByElement = <Element, Declaration>{}; |
| 91 | + for (var declaration in topDeclarations) { |
| 92 | + var element = declaration.declaredElement; |
| 93 | + if (element != null) { |
| 94 | + if (element is TopLevelVariableElement) { |
| 95 | + declarationByElement[element] = declaration; |
| 96 | + var getter = element.getter; |
| 97 | + if (getter != null) declarationByElement[getter] = declaration; |
| 98 | + var setter = element.setter; |
| 99 | + if (setter != null) declarationByElement[setter] = declaration; |
| 100 | + } else { |
| 101 | + declarationByElement[element] = declaration; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // The following map contains for every declaration the set of the |
| 107 | + // declarations it references. |
| 108 | + var dependencies = Map<Declaration, Set<Declaration>>.fromIterable( |
| 109 | + topDeclarations, |
| 110 | + value: (declaration) => |
| 111 | + DartTypeUtilities.traverseNodesInDFS(declaration as Declaration) |
| 112 | + .expand((e) => [ |
| 113 | + if (e is SimpleIdentifier) e.staticElement, |
| 114 | + // with `id++` staticElement of `id` is null |
| 115 | + if (e is CompoundAssignmentExpression) ...[ |
| 116 | + e.readElement, |
| 117 | + e.writeElement, |
| 118 | + ], |
| 119 | + ]) |
| 120 | + .whereNotNull() |
| 121 | + .map((e) => e.thisOrAncestorMatching((a) => |
| 122 | + a.enclosingElement3 == null || |
| 123 | + a.enclosingElement3 is CompilationUnitElement)) |
| 124 | + .map((e) => declarationByElement[e]) |
| 125 | + .whereNotNull() |
| 126 | + .where((e) => e != declaration) |
| 127 | + .toSet(), |
| 128 | + ); |
| 129 | + |
| 130 | + var usedMembers = entryPoints.toSet(); |
| 131 | + // The following variable will be used to visit every reachable declaration |
| 132 | + // starting from entry-points. At every loop an element is removed. This |
| 133 | + // element is marked as used and we add its dependencies in the declaration |
| 134 | + // list to traverse. Once this list is empty `usedMembers` contains every |
| 135 | + // declarations reachable from an entry-point. |
| 136 | + var toTraverse = Queue.of(usedMembers); |
| 137 | + while (toTraverse.isNotEmpty) { |
| 138 | + var declaration = toTraverse.removeLast(); |
| 139 | + for (var dep in dependencies[declaration]!) { |
| 140 | + if (usedMembers.add(dep)) { |
| 141 | + toTraverse.add(dep); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + var unusedMembers = topDeclarations.difference(usedMembers).where((e) { |
| 147 | + var element = e.declaredElement; |
| 148 | + return element != null && |
| 149 | + element.isPublic && |
| 150 | + !element.hasVisibleForTesting; |
| 151 | + }); |
| 152 | + |
| 153 | + for (var member in unusedMembers) { |
| 154 | + if (member is NamedCompilationUnitMember) { |
| 155 | + rule.reportLintForToken(member.name2); |
| 156 | + } else if (member is VariableDeclaration) { |
| 157 | + rule.reportLintForToken(member.name2); |
| 158 | + } else if (member is ExtensionDeclaration) { |
| 159 | + rule.reportLintForToken( |
| 160 | + member.name2 ?? member.firstTokenAfterCommentAndMetadata); |
| 161 | + } else { |
| 162 | + rule.reportLintForToken(member.firstTokenAfterCommentAndMetadata); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + bool _isEntryPoint(Declaration e) => |
| 168 | + e is FunctionDeclaration && |
| 169 | + (e.name2.lexeme == 'main' || e.metadata.any(_isPragmaVmEntry)); |
| 170 | + |
| 171 | + bool _isPragmaVmEntry(Annotation annotation) { |
| 172 | + if (!annotation.isPragma) return false; |
| 173 | + var value = annotation.elementAnnotation?.computeConstantValue(); |
| 174 | + if (value == null) return false; |
| 175 | + var name = value.getField('name'); |
| 176 | + return name != null && |
| 177 | + name.hasKnownValue && |
| 178 | + name.toStringValue() == 'vm:entry-point'; |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +extension on Element { |
| 183 | + bool get isPragma => (library?.isDartCore ?? false) && name == 'pragma'; |
| 184 | +} |
| 185 | + |
| 186 | +extension on Annotation { |
| 187 | + bool get isPragma { |
| 188 | + var element = elementAnnotation?.element; |
| 189 | + DartType type; |
| 190 | + if (element is ConstructorElement) { |
| 191 | + type = element.returnType; |
| 192 | + } else if (element is PropertyAccessorElement && element.isGetter) { |
| 193 | + type = element.returnType; |
| 194 | + } else { |
| 195 | + // Dunno what this is. |
| 196 | + return false; |
| 197 | + } |
| 198 | + return type is InterfaceType && type.element2.isPragma; |
| 199 | + } |
| 200 | +} |
0 commit comments