Unverified Commit 712195b5 authored by LongCatIsLooong's avatar LongCatIsLooong Committed by GitHub

update sample code analyzer regexp & test case (#30201)

updates the regex the analyzer uses, so it should be able to recognize expressions such as
const Foo a = Foo(); as "other code" rather than a constructor call.
parent e3619d7a
......@@ -165,7 +165,7 @@ class SampleChecker {
static final RegExp _codeBlockEndRegex = RegExp(r'///\s+```\s*$');
/// A RegExp that matches a Dart constructor.
static final RegExp _constructorRegExp = RegExp(r'[A-Z][a-zA-Z0-9<>.]*\(');
static final RegExp _constructorRegExp = RegExp(r'(const\s+)?_*[A-Z][a-zA-Z0-9<>._]*\(');
/// Whether or not to keep the temp directory around after running.
///
......@@ -742,7 +742,7 @@ linter:
if (block.isEmpty) {
throw SampleCheckerException('$line: Empty ```dart block in sample code.');
}
if (block.first.startsWith('new ') || block.first.startsWith('const ') || block.first.startsWith(_constructorRegExp)) {
if (block.first.startsWith('new ') || block.first.startsWith(_constructorRegExp)) {
_expressionId += 1;
return Section.surround(line, 'dynamic expression$_expressionId = ', block.toList(), ';');
} else if (block.first.startsWith('await ')) {
......
......@@ -7,6 +7,10 @@
// Examples can assume:
// bool _visible = true;
// class _Text extends Text {
// const _Text(String text) : super(text);
// const _Text.__(String text) : super(text);
// }
/// A blabla that blabla its blabla blabla blabla.
///
......@@ -41,3 +45,41 @@
/// )
/// ```
/// {@end-tool}
///
/// {@tool sample}
/// regular const constructor
///
/// ```dart
/// const Text('Poor wandering ones!')
/// ```
/// {@end-tool}
///
/// {@tool sample}
/// const private constructor
/// ```dart
/// const _Text('Poor wandering ones!')
/// ```
/// {@end-tool}
///
/// {@tool sample}
/// yet another const private constructor
/// ```dart
/// const _Text.__('Poor wandering ones!')
/// ```
/// {@end-tool}
///
/// {@tool sample}
/// const variable
///
/// ```dart
/// const text0 = Text('Poor wandering ones!');
/// ```
/// {@end-tool}
///
/// {@tool sample}
/// more const variables
///
/// ```dart
/// const text1 = _Text('Poor wandering ones!');
/// ```
/// {@end-tool}
......@@ -17,14 +17,14 @@ void main() {
..removeWhere((String line) => line.startsWith('Analyzer output:'));
expect(process.exitCode, isNot(equals(0)));
expect(stderrLines, <String>[
'known_broken_documentation.dart:26:9: new Opacity(',
'known_broken_documentation.dart:30:9: new Opacity(',
'>>> Unnecessary new keyword (unnecessary_new)',
'known_broken_documentation.dart:38:9: new Opacity(',
'known_broken_documentation.dart:42:9: new Opacity(',
'>>> Unnecessary new keyword (unnecessary_new)',
'',
'Found 1 sample code errors.',
'',
]);
expect(stdoutLines, <String>['Found 2 sample code sections.', 'Starting analysis of samples.', '']);
expect(stdoutLines, <String>['Found 7 sample code sections.', 'Starting analysis of samples.', '']);
}, skip: Platform.isWindows);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment