forked from dart-lang/linter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
file_name check improvements; benchmarking (dart-lang#1829)
* file_name check improvements; benchmarking * inlining and cleanup * tidy
- Loading branch information
Showing
8 changed files
with
265 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) 2019, 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. | ||
|
||
/// String utilities that use underlying ASCII codes for improved performance. | ||
/// Ultimately we should consider carefully when we use RegExps where a simple | ||
/// loop would do (and would do so far more performantly). | ||
/// See: https://github.com/dart-lang/linter/issues/1828 | ||
library ascii_utils; | ||
|
||
import 'package:charcode/ascii.dart'; | ||
|
||
/// Return `true` if the given [character] is the ASCII '.' character. | ||
bool isDot(int character) => character == $dot; | ||
|
||
/// Return `true` if the given [character] is a lowercase ASCII character. | ||
bool isLowerCase(int character) => character >= $a && character <= $z; | ||
|
||
/// Return `true` if the given [character] an ASCII number character. | ||
bool isNumber(int character) => character >= 48 && character <= 57; | ||
|
||
/// Return `true` if the given [character] is the ASCII '_' character. | ||
bool isUnderScore(int character) => character == $_; | ||
|
||
/// Check if the given [name] is a valid Dart filename. | ||
/// | ||
/// Files with a strict `.dart` extension are required to use: | ||
/// * `lower_snake_case` and are | ||
/// * limited to valid Dart identifiers | ||
/// | ||
/// (Files without a strict `.dart` extension are considered valid.) | ||
bool isValidDartFileName(String name) { | ||
if (name.length < 6 || !name.endsWith('.dart')) { | ||
return true; | ||
} | ||
|
||
final length = name.length - 5; | ||
for (int i = 1; i < length - 1; ++i) { | ||
final character = name.codeUnitAt(i); | ||
// Indicates a prefixed suffix (like `.g.dart`) which is considered a | ||
// non-strict Dart filename. | ||
if (isDot(character)) { | ||
return true; | ||
} | ||
} | ||
|
||
for (int i = 0; i < length; ++i) { | ||
final character = name.codeUnitAt(i); | ||
if (!isLowerCase(character) && !isUnderScore(character)) { | ||
if (isNumber(character)) { | ||
if (i == 0) { | ||
return false; | ||
} | ||
continue; | ||
} | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) 2019, 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:linter/src/util/ascii_utils.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
main() { | ||
group('fileNames', () { | ||
group('good', () { | ||
for (var name in goodFileNames) { | ||
test(name, () { | ||
expect(isValidDartFileName(name), isTrue); | ||
}); | ||
} | ||
}); | ||
|
||
group('bad', () { | ||
for (var name in badFileNames) { | ||
test(name, () { | ||
expect(isValidDartFileName(name), isFalse); | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
final badFileNames = [ | ||
'Foo.dart', | ||
'fooBar.dart', | ||
'.foo_Bar.dart', | ||
'F_B.dart', | ||
'JS.dart', | ||
'JSON.dart', | ||
]; | ||
|
||
final goodFileNames = [ | ||
// Generated files. | ||
'file-system.g.dart', | ||
'SliderMenu.css.dart', | ||
'_file.dart', | ||
'_file.g.dart', | ||
// Non-strict Dart. | ||
'bwu_server.shared.datastore.some_file', | ||
'foo_bar.baz', | ||
'foo_bar.dart', | ||
'foo_bar.g.dart', | ||
'foo_bar', | ||
'foo.bar', | ||
'foo_bar_baz', | ||
'foo', | ||
'foo_', | ||
'foo.bar_baz.bang', | ||
//See: https://github.com/flutter/flutter/pull/1996 | ||
'pointycastle.impl.ec_domain_parameters.gostr3410_2001_cryptopro_a', | ||
'a.b', | ||
'a.b.c', | ||
'p2.src.acme', | ||
//See: https://github.com/dart-lang/linter/issues/1803 | ||
'_', | ||
'_f', | ||
'__f', | ||
'___f', | ||
'Foo', | ||
'fooBar.', | ||
'.foo_Bar', | ||
'_.', | ||
'.', | ||
'F_B', | ||
'JS', | ||
'JSON', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright (c) 2019, 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:benchmark_harness/benchmark_harness.dart'; | ||
import 'package:linter/src/util/ascii_utils.dart'; | ||
import 'package:linter/src/utils.dart'; | ||
|
||
import '../test/ascii_utils_test.dart' as utils_test; | ||
|
||
main() { | ||
FileNameRegexpTestBenchmarkGood().report(); | ||
FileNameCharLoopTestBenchmarkGood().report(); | ||
FileNameRegexpTestBenchmarkBad().report(); | ||
FileNameCharLoopTestBenchmarkBad().report(); | ||
AllMatchesBenchmark().report(); | ||
DotScanBenchmark().report(); | ||
} | ||
|
||
final badFileNames = utils_test.badFileNames; | ||
|
||
final goodFileNames = utils_test.goodFileNames; | ||
|
||
class AllMatchesBenchmark extends BaseBenchmark { | ||
const AllMatchesBenchmark() : super('AllMatches'); | ||
|
||
@override | ||
void run() { | ||
for (var name in ['foo.dart', 'a-b.dart', 'a-b.css.dart', 'foo']) { | ||
//Test. | ||
'\.'.allMatches(name).length; | ||
} | ||
} | ||
} | ||
|
||
class BaseBenchmark extends BenchmarkBase { | ||
const BaseBenchmark(String name) : super(name); | ||
|
||
@override | ||
void exercise() { | ||
for (int i = 0; i < 100; i++) { | ||
run(); | ||
} | ||
} | ||
} | ||
|
||
class DotScanBenchmark extends BaseBenchmark { | ||
const DotScanBenchmark() : super('DotScan'); | ||
|
||
bool hasOneDot(String name) { | ||
int count = 0; | ||
for (int i = 0; i < name.length; ++i) { | ||
final character = name.codeUnitAt(i); | ||
count += isDot(character) ? 1 : 0; | ||
if (count > 1) { | ||
return false; | ||
} | ||
} | ||
return count == 1; | ||
} | ||
|
||
@override | ||
void run() { | ||
// ignore: prefer_foreach | ||
for (var name in ['foo.dart', 'a-b.dart', 'a-b.css.dart', 'foo']) { | ||
hasOneDot(name); | ||
} | ||
} | ||
} | ||
|
||
class FileNameCharLoopTestBenchmarkBad extends BenchmarkBase { | ||
const FileNameCharLoopTestBenchmarkBad() : super('Loop:Bad'); | ||
|
||
@override | ||
void run() { | ||
badFileNames.forEach(isValidDartFileName); | ||
} | ||
} | ||
|
||
class FileNameCharLoopTestBenchmarkGood extends BenchmarkBase { | ||
const FileNameCharLoopTestBenchmarkGood() : super('Loop:Good'); | ||
|
||
@override | ||
void run() { | ||
goodFileNames.forEach(isValidDartFileName); | ||
} | ||
} | ||
|
||
class FileNameRegexpTestBenchmarkBad extends BenchmarkBase { | ||
const FileNameRegexpTestBenchmarkBad() : super('Regexp:Bad'); | ||
|
||
@override | ||
void run() { | ||
badFileNames.forEach(isLowerCaseUnderScoreWithDots); | ||
} | ||
} | ||
|
||
class FileNameRegexpTestBenchmarkGood extends BaseBenchmark { | ||
const FileNameRegexpTestBenchmarkGood() : super('Regexp:Good'); | ||
|
||
@override | ||
void run() { | ||
goodFileNames.forEach(isLowerCaseUnderScoreWithDots); | ||
} | ||
} |