Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit 3deae6e

Browse files
authored
disallow reserved words in package names (#2889)
* disallow reserved words in package names * fixed import
1 parent 5c85d71 commit 3deae6e

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

lib/src/ast.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import 'package:path/path.dart' as path;
1414
import 'analyzer.dart';
1515
import 'utils.dart';
1616

17+
List<String>? _reservedWords;
18+
19+
List<String> get reservedWords => _reservedWords ??= _collectReservedWords();
20+
1721
/// Returns direct children of [parent].
1822
List<Element> getChildren(Element parent, [String? name]) {
1923
var children = <Element>[];
@@ -173,6 +177,9 @@ bool isPublicMethod(ClassMember m) {
173177
return declaredElement != null && isMethod(m) && declaredElement.isPublic;
174178
}
175179

180+
/// Check if the given word is a Dart reserved word.
181+
bool isReservedWord(String word) => reservedWords.contains(word);
182+
176183
/// Returns `true` if the given method [declaration] is a "simple getter".
177184
///
178185
/// A simple getter takes one of these basic forms:
@@ -330,6 +337,16 @@ bool _checkForSimpleSetter(MethodDeclaration setter, Expression expression) {
330337
return false;
331338
}
332339

340+
List<String> _collectReservedWords() {
341+
var reserved = <String>[];
342+
for (var entry in Keyword.keywords.entries) {
343+
if (entry.value.isReservedWord) {
344+
reserved.add(entry.key);
345+
}
346+
}
347+
return reserved;
348+
}
349+
333350
int? _getIntValue(Expression expression, LinterContext? context,
334351
{bool negated = false}) {
335352
int? value;

lib/src/utils.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'ast.dart';
6+
57
// An identifier here is defined as:
68
// * A sequence of `_`, `$`, letters or digits,
79
// * where no `$` comes after a digit.
@@ -86,7 +88,9 @@ bool isValidLibraryPrefix(String libraryPrefix) =>
8688

8789
/// Returns true if this [id] is a valid package name.
8890
bool isValidPackageName(String id) =>
89-
_lowerCaseUnderScoreWithLeadingUnderscores.hasMatch(id) && isIdentifier(id);
91+
_lowerCaseUnderScoreWithLeadingUnderscores.hasMatch(id) &&
92+
isIdentifier(id) &&
93+
!isReservedWord(id);
9094

9195
class CamelCaseString {
9296
static final _camelCaseMatcher = RegExp(r'[A-Z][a-z]*');

test/utils_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ void main() {
2525
'async',
2626
], isValidPackageName, isTrue);
2727
testEach([
28+
'break', // reserved word
2829
'fOO',
2930
'foo_',
3031
'foo_Bar',

0 commit comments

Comments
 (0)