From 712195b5592c561faf1edda81330b060781f6893 Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Mon, 1 Apr 2019 17:27:29 -0700 Subject: [PATCH] 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. --- dev/bots/analyze-sample-code.dart | 4 +- .../known_broken_documentation.dart | 42 +++++++++++++++++++ dev/bots/test/analyze-sample-code_test.dart | 6 +-- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/dev/bots/analyze-sample-code.dart b/dev/bots/analyze-sample-code.dart index 6af14703f9..23a97584e5 100644 --- a/dev/bots/analyze-sample-code.dart +++ b/dev/bots/analyze-sample-code.dart @@ -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 ')) { diff --git a/dev/bots/test/analyze-sample-code-test-input/known_broken_documentation.dart b/dev/bots/test/analyze-sample-code-test-input/known_broken_documentation.dart index 6a00c15f9a..fd3c78ed3f 100644 --- a/dev/bots/test/analyze-sample-code-test-input/known_broken_documentation.dart +++ b/dev/bots/test/analyze-sample-code-test-input/known_broken_documentation.dart @@ -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} diff --git a/dev/bots/test/analyze-sample-code_test.dart b/dev/bots/test/analyze-sample-code_test.dart index 0d46585a6d..8e0f347a15 100644 --- a/dev/bots/test/analyze-sample-code_test.dart +++ b/dev/bots/test/analyze-sample-code_test.dart @@ -17,14 +17,14 @@ void main() { ..removeWhere((String line) => line.startsWith('Analyzer output:')); expect(process.exitCode, isNot(equals(0))); expect(stderrLines, [ - '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, ['Found 2 sample code sections.', 'Starting analysis of samples.', '']); + expect(stdoutLines, ['Found 7 sample code sections.', 'Starting analysis of samples.', '']); }, skip: Platform.isWindows); }