Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.
Merged
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
17 changes: 17 additions & 0 deletions lib/src/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import 'package:path/path.dart' as path;
import 'analyzer.dart';
import 'utils.dart';

List<String>? _reservedWords;

List<String> get reservedWords => _reservedWords ??= _collectReservedWords();

/// Returns direct children of [parent].
List<Element> getChildren(Element parent, [String? name]) {
var children = <Element>[];
Expand Down Expand Up @@ -173,6 +177,9 @@ bool isPublicMethod(ClassMember m) {
return declaredElement != null && isMethod(m) && declaredElement.isPublic;
}

/// Check if the given word is a Dart reserved word.
bool isReservedWord(String word) => reservedWords.contains(word);

/// Returns `true` if the given method [declaration] is a "simple getter".
///
/// A simple getter takes one of these basic forms:
Expand Down Expand Up @@ -330,6 +337,16 @@ bool _checkForSimpleSetter(MethodDeclaration setter, Expression expression) {
return false;
}

List<String> _collectReservedWords() {
var reserved = <String>[];
for (var entry in Keyword.keywords.entries) {
if (entry.value.isReservedWord) {
reserved.add(entry.key);
}
}
return reserved;
}

int? _getIntValue(Expression expression, LinterContext? context,
{bool negated = false}) {
int? value;
Expand Down
6 changes: 5 additions & 1 deletion lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// 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 'ast.dart';

// An identifier here is defined as:
// * A sequence of `_`, `$`, letters or digits,
// * where no `$` comes after a digit.
Expand Down Expand Up @@ -86,7 +88,9 @@ bool isValidLibraryPrefix(String libraryPrefix) =>

/// Returns true if this [id] is a valid package name.
bool isValidPackageName(String id) =>
_lowerCaseUnderScoreWithLeadingUnderscores.hasMatch(id) && isIdentifier(id);
_lowerCaseUnderScoreWithLeadingUnderscores.hasMatch(id) &&
isIdentifier(id) &&
!isReservedWord(id);

class CamelCaseString {
static final _camelCaseMatcher = RegExp(r'[A-Z][a-z]*');
Expand Down
1 change: 1 addition & 0 deletions test/utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void main() {
'async',
], isValidPackageName, isTrue);
testEach([
'break', // reserved word
'fOO',
'foo_',
'foo_Bar',
Expand Down