lint unnecessary_new on samples (#21539) * lint unnecessary_new on samples * fix tests
diff --git a/dev/bots/analyze-sample-code.dart b/dev/bots/analyze-sample-code.dart index a3087b7..8662d33 100644 --- a/dev/bots/analyze-sample-code.dart +++ b/dev/bots/analyze-sample-code.dart
@@ -218,6 +218,7 @@ linter: rules: - unnecessary_const + - unnecessary_new '''); print('Found $sampleCodeSections sample code sections.'); final Process process = await Process.start( @@ -314,18 +315,20 @@ exit(exitCode); } +final RegExp _constructorRegExp = new RegExp(r'[A-Z][a-zA-Z0-9<>.]*\('); + int _expressionId = 0; void processBlock(Line line, List<String> block, List<Section> sections) { if (block.isEmpty) throw '$line: Empty ```dart block in sample code.'; - if (block.first.startsWith('new ') || block.first.startsWith('const ')) { + if (block.first.startsWith('new ') || block.first.startsWith('const ') || block.first.startsWith(_constructorRegExp)) { _expressionId += 1; sections.add(new Section(line, 'dynamic expression$_expressionId = ', block.toList(), ';')); } else if (block.first.startsWith('await ')) { _expressionId += 1; sections.add(new Section(line, 'Future<Null> expression$_expressionId() async { ', block.toList(), ' }')); - } else if (block.first.startsWith('class ')) { + } else if (block.first.startsWith('class ') || block.first.startsWith('enum ')) { sections.add(new Section(line, null, block.toList(), null)); } else { final List<String> buffer = <String>[];
diff --git a/dev/bots/test/analyze-sample-code_test.dart b/dev/bots/test/analyze-sample-code_test.dart index 0774eea..8eb1f11 100644 --- a/dev/bots/test/analyze-sample-code_test.dart +++ b/dev/bots/test/analyze-sample-code_test.dart
@@ -30,8 +30,10 @@ "$directory/main.dart:5:8: Unused import: 'dart:ui'", "$directory/main.dart:6:8: Unused import: 'package:flutter_test/flutter_test.dart'", "$directory/main.dart:9:8: Target of URI doesn't exist: 'package:flutter/known_broken_documentation.dart'", + 'test/analyze-sample-code-test-input/known_broken_documentation.dart:27:5: Unnecessary new keyword (unnecessary_new)', "test/analyze-sample-code-test-input/known_broken_documentation.dart:27:9: Undefined class 'Opacity' (undefined_class)", "test/analyze-sample-code-test-input/known_broken_documentation.dart:29:20: Undefined class 'Text' (undefined_class)", + 'test/analyze-sample-code-test-input/known_broken_documentation.dart:39:5: Unnecessary new keyword (unnecessary_new)', "test/analyze-sample-code-test-input/known_broken_documentation.dart:39:9: Undefined class 'Opacity' (undefined_class)", "test/analyze-sample-code-test-input/known_broken_documentation.dart:41:20: Undefined class 'Text' (undefined_class)", 'test/analyze-sample-code-test-input/known_broken_documentation.dart:42:5: unexpected comma at end of sample code',