diff --git a/pkg/analyzer/test/dart/ast/visitor_test.dart b/pkg/analyzer/test/dart/ast/visitor_test.dart index a0f5d1d8017..8a42c5d2fc1 100644 --- a/pkg/analyzer/test/dart/ast/visitor_test.dart +++ b/pkg/analyzer/test/dart/ast/visitor_test.dart @@ -8,7 +8,6 @@ import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../../src/diagnostics/parser_diagnostics.dart'; -import '../../util/ast_type_matchers.dart'; main() { defineReflectiveSuite(() { @@ -19,7 +18,7 @@ main() { @reflectiveTest class BreadthFirstVisitorTest extends ParserDiagnosticsTest { void test_it() { - String source = r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A { bool get g => true; } @@ -37,19 +36,19 @@ A f(p) { } else { return null; } -}'''; - CompilationUnit unit = parseStringWithErrors(source).unit; - List nodes = []; - _BreadthFirstVisitorTestHelper visitor = _BreadthFirstVisitorTestHelper( - nodes, - ); - visitor.visitAllNodes(unit); +}'''); + var findNode = parseResult.findNode; + + var nodes = []; + var visitor = _BreadthFirstVisitorTestHelper(nodes); + visitor.visitAllNodes(parseResult.unit); + expect(nodes, hasLength(51)); - expect(nodes[0], isCompilationUnit); - expect(nodes[2], isClassDeclaration); - expect(nodes[3], isFunctionDeclaration); - expect(nodes[24], isFunctionDeclarationStatement); - expect(nodes[50], isIntegerLiteral); // 3 + expect(nodes[0], parseResult.unit); + expect(nodes[2], findNode.classDeclaration('class B')); + expect(nodes[3], findNode.functionDeclaration('A f')); + expect(nodes[24], findNode.functionDeclarationStatement('num q')); + expect(nodes[50], findNode.integerLiteral('3')); } } diff --git a/pkg/analyzer/test/generated/expression_parser_test.dart b/pkg/analyzer/test/generated/expression_parser_test.dart index d1e2857f961..dee3bae48af 100644 --- a/pkg/analyzer/test/generated/expression_parser_test.dart +++ b/pkg/analyzer/test/generated/expression_parser_test.dart @@ -4,7 +4,6 @@ import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/ast/token.dart'; -import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; import '../src/dart/resolution/node_text_expectations.dart'; @@ -21,16 +20,11 @@ main() { class ExpressionParserTest extends ParserDiagnosticsTest { void test_binaryExpression_allOperators() { // https://github.com/dart-lang/sdk/issues/36255 - for (TokenType type in TokenType.all) { - if (type.precedence > 0) { - var source = 'a ${type.lexeme} b'; - try { - parseStringWithErrors('var x = $source;'); - } on TestFailure { - // Ensure that there are no infinite loops or exceptions thrown - // by the parser. Test failures are fine. - } + for (var type in TokenType.all) { + if (type.precedence <= 0) { + continue; } + parseTestCodeIgnoringDiagnostics('var x = a ${type.lexeme} b;'); } } diff --git a/pkg/analyzer/test/generated/utilities_test.dart b/pkg/analyzer/test/generated/utilities_test.dart index 58afe99b563..b72806b5e8f 100644 --- a/pkg/analyzer/test/generated/utilities_test.dart +++ b/pkg/analyzer/test/generated/utilities_test.dart @@ -145,7 +145,7 @@ class LineInfoTest { @reflectiveTest class NodeReplacerTest extends ParserDiagnosticsTest { void test_adjacentStrings() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { 'aaa' 'bbb'; } @@ -159,7 +159,7 @@ void f() { } void test_annotation() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' @prefix.A.named(args) @prefix.B.named(args) void f() {} @@ -177,7 +177,7 @@ void f() {} } void test_argumentList() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { g(0, 1); } @@ -191,7 +191,7 @@ void f() { } void test_asExpression() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { 0 as int; 1 as int; @@ -205,7 +205,7 @@ void f() { } void test_assertStatement() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { assert(true, 'first'); assert(true, 'second'); @@ -219,7 +219,7 @@ void f() { } void test_assignmentExpression() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { a = 0; b = 1; @@ -236,7 +236,7 @@ void f() { } void test_awaitExpression() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() async { await 0; await 1; @@ -250,7 +250,7 @@ void f() async { } void test_binaryExpression() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { 0 + 1; 1 + 2; @@ -264,7 +264,7 @@ void f() { } void test_block() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { print(0); print(1); @@ -279,7 +279,7 @@ void f() { } void test_blockFunctionBody() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { print('fff'); } @@ -296,7 +296,7 @@ void g() { } void test_breakStatement() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { while (true) { break first; @@ -312,7 +312,7 @@ void f() { } void test_cascadeExpression() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { 0..foo..bar; 1..foo; @@ -333,7 +333,7 @@ void f() { } void test_catchClause() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { try {} on E catch (e, st) {} try {} on E2 catch (e2, st2) {} @@ -352,7 +352,7 @@ void f() { } void test_classTypeAlias() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' /// Comment A. @myA1 @myA2 @@ -381,7 +381,7 @@ class B = B0 with N implements J; } void test_comment() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' /// Has [foo] and [bar]. void f() {} '''); @@ -394,7 +394,7 @@ void f() {} } void test_commentReference() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' /// Has [foo] and [bar]. void f() {} '''); @@ -406,7 +406,7 @@ void f() {} } void test_compilationUnit() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' import 'a.dart'; import 'b.dart'; class A {} @@ -426,7 +426,7 @@ class B {} } void test_conditionalExpression() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { true ? 0 : 1; false ? 2 : 3; @@ -444,7 +444,7 @@ void f() { } void test_constantPattern() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f(x) async { if (x case 0) {} if (x case 1) {} @@ -462,7 +462,7 @@ void f(x) async { } void test_constructorDeclaration() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A { @myA1 @myA2 @@ -482,7 +482,7 @@ class B { } void test_constructorDeclaration_redirectedConstructor() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A { factory A() = R; } @@ -499,7 +499,7 @@ class B { } void test_constructorFieldInitializer() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A { A() : a = 0, b = 1; } @@ -512,7 +512,7 @@ class A { } void test_constructorName() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { new prefix.A.foo(); new prefix.B.bar(); @@ -526,7 +526,7 @@ void f() { } void test_continueStatement() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { while (true) { continue first; @@ -542,7 +542,7 @@ void f() { } void test_declaredIdentifier() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { for (int i in []) {} for (double j in []) {} @@ -556,7 +556,7 @@ void f() { } void test_defaultFormalParameter() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f({int a = 0, double b = 1}) {} '''); _assertReplacementForChildren( @@ -567,18 +567,25 @@ void f({int a = 0, double b = 1}) {} } void test_doStatement() { - var parseResult = parseStringWithErrors(r''' -void f({int a = 0, double b = 1}) {} + var parseResult = parseTestCodeWithDiagnostics(r''' +void f() { + do { + 0; + } while (true); + do { + 1; + } while (false); +} '''); - _assertReplacementForChildren( - destination: parseResult.findNode.formalParameter('a ='), - source: parseResult.findNode.formalParameter('b ='), - childAccessors: [(node) => node.type!, (node) => node.defaultClause!], + _assertReplacementForChildren( + destination: parseResult.findNode.doStatement('true'), + source: parseResult.findNode.doStatement('false'), + childAccessors: [(node) => node.body, (node) => node.condition], ); } void test_enumBody_constants() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' enum E1 {one} enum E2 {two} '''); @@ -590,7 +597,7 @@ enum E2 {two} } void test_enumBody_members() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' enum E1 {one; void foo() {}} enum E2 {two; void bar() {}} '''); @@ -602,7 +609,7 @@ enum E2 {two; void bar() {}} } void test_enumConstantDeclaration() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' enum E { @myA1 @myA2 @@ -614,7 +621,7 @@ enum E { } void test_enumDeclaration() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' enum E1 with M1 implements I1 {one, two} enum E2 with M2 implements I2 {one, two} '''); @@ -629,7 +636,7 @@ enum E2 with M2 implements I2 {one, two} } void test_exportDirective() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' @myA1 @myA2 export 'a.dart' hide A show B; @@ -650,7 +657,7 @@ export 'b.dart'; } void test_expressionFunctionBody() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() => 0; void g() => 1; '''); @@ -662,7 +669,7 @@ void g() => 1; } void test_expressionStatement() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { 0; 1; @@ -676,7 +683,7 @@ void f() { } void test_extendsClause() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A extends A0 {} class B extends B0 {} '''); @@ -688,7 +695,7 @@ class B extends B0 {} } void test_extensionDeclaration() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' /// Comment A. @myA1 @myA2 @@ -714,7 +721,7 @@ extension F on double {} } void test_fieldDeclaration() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A { @myA1 @myA2 @@ -732,7 +739,7 @@ class B extends B0 {} } void test_fieldFormalParameter() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A { A([int this.foo = 0, double this.bar = 1]); } @@ -745,7 +752,7 @@ class A { } void test_fieldFormalParameter_functionTyped() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A { A( @myA1 @@ -768,7 +775,7 @@ class A { } void test_forEachPartsWithDeclaration() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { for (int a in []) {} for (int b in []) {} @@ -782,7 +789,7 @@ void f() { } void test_forEachPartsWithIdentifier() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { for (a in []) {} for (b in []) {} @@ -796,7 +803,7 @@ void f() { } void test_forEachPartsWithPattern() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { for (var (a) in []) {} for (var (b) in []) {} @@ -810,7 +817,7 @@ void f() { } void test_forEachStatement_withIdentifier() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f(int a) { for (a in []) {} for (b in []) {} @@ -824,7 +831,7 @@ void f(int a) { } void test_formalParameterList() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f(int a, int b) {} '''); _assertReplaceInList( @@ -835,7 +842,7 @@ void f(int a, int b) {} } void test_forPartsWithDeclarations() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { for (int i = 0; i < 8; i++, i += 2) {} for (int j = 0; j < 8; j++) {} @@ -855,7 +862,7 @@ void f() { } void test_forPartsWithExpression() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { for (i = 0; i < 8; i++, i += 2) {} for (j = 0; j < 8; j++) {} @@ -878,7 +885,7 @@ void f() { } void test_functionDeclaration() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' @myA1 @myA2 int f() => 0; @@ -896,7 +903,7 @@ double g() => 0; } void test_functionDeclarationStatement() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { void g() {} void h() {} @@ -910,7 +917,7 @@ void f() { } void test_functionExpression() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f(int a) { 0; } @@ -935,7 +942,7 @@ void g(double b) { } void test_functionExpressionInvocation() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { (g)(0); (h)(1); @@ -953,7 +960,7 @@ void f() { } void test_functionTypeAlias() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' @myA1 @myA2 typedef int F(int a); @@ -977,7 +984,7 @@ typedef double G(double b); } void test_functionTypedFormalParameterSuffix() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f( int a(int a1), double b(double b2), @@ -1001,7 +1008,7 @@ void f( } void test_genericFunctionType() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' typedef A = int Function(int a); typedef B = double Function(double b); '''); @@ -1022,7 +1029,7 @@ typedef B = double Function(double b); } void test_genericTypeAlias() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' @myA1 @myA2 typedef A = int; @@ -1042,7 +1049,7 @@ typedef B = double; } void test_hideCombinator() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' import '' hide A, B; '''); var node = parseResult.findNode.hideCombinator('hide'); @@ -1054,7 +1061,7 @@ import '' hide A, B; } void test_ifStatement() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { if (true) { 0; @@ -1080,7 +1087,7 @@ void f() { } void test_implementsClause() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A implements I, J {} '''); var node = parseResult.findNode.implementsClause('implements'); @@ -1092,7 +1099,7 @@ class A implements I, J {} } void test_importDirective() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' @myA1 @myA2 import 'a.dart' hide A show B; @@ -1113,7 +1120,7 @@ import 'b.dart'; } void test_indexExpression() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { a[0]; b[1]; @@ -1127,7 +1134,7 @@ void f() { } void test_instanceCreationExpression() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { new A(0); new B(1); @@ -1144,7 +1151,7 @@ void f() { } void test_interpolationExpression() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { '$foo $bar'; } @@ -1157,7 +1164,7 @@ void f() { } void test_isExpression() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { 0 is int; 1 is double; @@ -1171,7 +1178,7 @@ void f() { } void test_labeledStatement() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { foo: bar: 0; baz: 1; @@ -1191,7 +1198,7 @@ void f() { } void test_libraryDirective() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' @myA1 @myA2 library foo; @@ -1206,7 +1213,7 @@ library foo; } void test_listLiteral() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { [0, 1]; []; @@ -1226,7 +1233,7 @@ void f() { } void test_mapLiteralEntry() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { {0: 1, 2: 3}; } @@ -1239,7 +1246,7 @@ void f() { } void test_methodDeclaration() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A { @myA1 @myA2 @@ -1271,7 +1278,7 @@ class A { } void test_methodInvocation() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { a.foo(0); b.bar(1); @@ -1289,7 +1296,7 @@ void f() { } void test_mixinDeclaration() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' @myA1 @myA2 mixin A on A0 implements I { @@ -1318,7 +1325,7 @@ mixin B on B0 implements J { } void test_namedArgument() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { g(foo: 0, bar: 1); } @@ -1331,7 +1338,7 @@ void f() { } void test_nameWithTypeParameters() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A {} class B {} '''); @@ -1348,7 +1355,7 @@ class B {} } void test_nativeFunctionBody() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() native 'foo'; void g() native 'bar'; '''); @@ -1360,7 +1367,7 @@ void g() native 'bar'; } void test_parenthesizedExpression() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { (0); (1); @@ -1374,7 +1381,7 @@ void f() { } void test_partDirective() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' @myA1 @myA2 part 'a.dart'; @@ -1390,7 +1397,7 @@ part 'b.dart'; } void test_partOfDirective() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' @myA1 @myA2 part of 'a.dart'; @@ -1405,7 +1412,7 @@ part of 'a.dart'; } void test_patternAssignment() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { int a; int b; @@ -1421,7 +1428,7 @@ void f() { } void test_patternVariableDeclaration() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { var (a) = 0; var (b) = 1; @@ -1435,7 +1442,7 @@ void f() { } void test_postfixExpression() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { a++; b++; @@ -1449,7 +1456,7 @@ void f() { } void test_prefixedIdentifier() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { a.foo; b.bar; @@ -1463,7 +1470,7 @@ void f() { } void test_prefixExpression() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { ++a; ++b; @@ -1477,7 +1484,7 @@ void f() { } void test_primaryConstructorDeclaration() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A.a(int a) {} class B.b(double b) {} '''); @@ -1500,7 +1507,7 @@ class B.b(double b) {} } void test_propertyAccess() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { (a).foo; (b).bar; @@ -1514,7 +1521,7 @@ void f() { } void test_recordLiteral() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { (1, 2); } @@ -1528,7 +1535,7 @@ void f() { } void test_redirectingConstructorInvocation() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A { A.named(); A.foo() : this.named(0); @@ -1546,7 +1553,7 @@ class A { } void test_regularFormalParameter() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f([int a = 0, double b = 1]) {} '''); _assertReplacementForChildren( @@ -1559,7 +1566,7 @@ void f([int a = 0, double b = 1]) {} } void test_regularFormalParameter_functionTyped() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f( @myA1 @myA2 @@ -1584,7 +1591,7 @@ void f( } void test_relationalPattern() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f(x) { if (x case > 0) {} if (x case > 1) {} @@ -1598,7 +1605,7 @@ void f(x) { } void test_returnStatement() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { return 0; return 1; @@ -1612,7 +1619,7 @@ void f() { } void test_setOrMapLiteral() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { {0: 1, 2: 3}; {}; @@ -1632,7 +1639,7 @@ void f() { } void test_showCombinator() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' import '' show A, B; '''); var node = parseResult.findNode.showCombinator('show'); @@ -1644,7 +1651,7 @@ import '' show A, B; } void test_simpleFormalParameter() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f( @myA1 @myA2 @@ -1662,7 +1669,7 @@ void f( } void test_stringInterpolation() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { 'my $foo other $bar'; } @@ -1676,7 +1683,7 @@ void f() { } void test_superConstructorInvocation() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A { A.foo() : super.first(0); A.bar() : super.second(0); @@ -1693,7 +1700,7 @@ class A { } void test_superFormalParameter() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A { A([num a = 0]); } @@ -1711,7 +1718,7 @@ class B extends A { } void test_superFormalParameter_functionTyped() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A { A(int foo(int a)); } @@ -1732,7 +1739,7 @@ class B extends A { } void test_switchCase_language219() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' // @dart = 2.19 void f() { switch (x) { @@ -1751,7 +1758,7 @@ void f() { } void test_switchDefault() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { switch (x) { foo: bar: @@ -1763,7 +1770,7 @@ void f() { } void test_switchStatement_language219() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' // @dart = 2.19 void f() { switch (0) { @@ -1786,7 +1793,7 @@ void f() { } void test_throwExpression() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { throw 0; throw 1; @@ -1800,7 +1807,7 @@ void f() { } void test_topLevelVariableDeclaration() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' @myA1 @myA2 var a = 0; @@ -1817,7 +1824,7 @@ var b = 1; } void test_tryStatement() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { try { // 0 0; @@ -1846,7 +1853,7 @@ void f() { } void test_typeArgumentList() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { g(); } @@ -1859,7 +1866,7 @@ void f() { } void test_typeParameter() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A {} '''); _assertReplacementForChildren( @@ -1870,7 +1877,7 @@ class A {} } void test_typeParameterList() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A {} '''); // Find from the offset after the `<` because NodeLocator usually picks @@ -1884,7 +1891,7 @@ class A {} } void test_variableDeclaration() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { var a = 0; var b = 1; @@ -1898,7 +1905,7 @@ void f() { } void test_variableDeclarationList() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { int a = 0, b = 1; double c = 2; @@ -1917,7 +1924,7 @@ void f() { } void test_variableDeclarationStatement() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { int a = 0; double b = 1; @@ -1931,7 +1938,7 @@ void f() { } void test_whenClause() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { if (x case 0 when 1) {} if (x case 0 when 2) {} @@ -1945,7 +1952,7 @@ void f() { } void test_whileStatement() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() { while (true) { 0; @@ -1963,7 +1970,7 @@ void f() { } void test_withClause() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' class A with M, N {} '''); var node = parseResult.findNode.withClause('with'); @@ -1975,7 +1982,7 @@ class A with M, N {} } void test_yieldStatement() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void f() sync* { yield 0; yield 1; diff --git a/pkg/analyzer/test/src/clients/dart_style/rewrite_cascade_test.dart b/pkg/analyzer/test/src/clients/dart_style/rewrite_cascade_test.dart index 8fb12fd7ca7..154ffd823e5 100644 --- a/pkg/analyzer/test/src/clients/dart_style/rewrite_cascade_test.dart +++ b/pkg/analyzer/test/src/clients/dart_style/rewrite_cascade_test.dart @@ -2,7 +2,6 @@ // 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:analyzer/dart/ast/ast.dart'; import 'package:analyzer/src/clients/dart_style/rewrite_cascade.dart'; import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; @@ -46,15 +45,16 @@ class RewriteCascadeTest extends ParserDiagnosticsTest { }; void assertSingle({required String input, required String expected}) { - var parseResult = parseStringWithErrors(''' + var parseResult = parseTestCodeWithDiagnostics(''' void f() { $input } '''); - var statement = parseResult.findNode.expressionStatement(input); + var findNode = parseResult.findNode; + var statement = findNode.singleExpressionStatement; var result = fixCascadeByParenthesizingTarget( expressionStatement: statement, - cascadeExpression: statement.expression as CascadeExpression, + cascadeExpression: findNode.singleCascadeExpression, ); expect(result.toSource(), expected); expect(result.semicolon, same(statement.semicolon)); @@ -67,13 +67,12 @@ void f() { test_insertCascadeTargetIntoExpression() { void assertSingle({required String input, required String expected}) { - var parseResult = parseStringWithErrors(''' + var parseResult = parseTestCodeWithDiagnostics(''' void f() { $input; - } - '''); - var statement = parseResult.findNode.expressionStatement(input); - var cascadeExpression = statement.expression as CascadeExpression; +} +'''); + var cascadeExpression = parseResult.findNode.singleCascadeExpression; var result = insertCascadeTargetIntoExpression( expression: cascadeExpression.cascadeSections.single, cascadeTarget: cascadeExpression.target, diff --git a/pkg/analyzer/test/src/dart/analysis/defined_names_test.dart b/pkg/analyzer/test/src/dart/analysis/defined_names_test.dart index f0918d347ad..a6688eb3a33 100644 --- a/pkg/analyzer/test/src/dart/analysis/defined_names_test.dart +++ b/pkg/analyzer/test/src/dart/analysis/defined_names_test.dart @@ -2,7 +2,6 @@ // 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:analyzer/dart/analysis/features.dart'; import 'package:analyzer/src/dart/analysis/defined_names.dart'; import 'package:analyzer/src/dart/ast/ast.dart'; import 'package:test/test.dart'; @@ -19,7 +18,7 @@ main() { @reflectiveTest class DefinedNamesTest extends ParserDiagnosticsTest { test_classMemberNames_class() { - DefinedNames names = _computeDefinedNames(''' + var names = _computeDefinedNames(''' class A { int a, b; A(); @@ -40,7 +39,7 @@ class B { } test_classMemberNames_class_primaryConstructor_named() { - DefinedNames names = _computeDefinedNames(''' + var names = _computeDefinedNames(''' class A.named(final int a1, int a2, var a3); '''); expect(names.topLevelNames, unorderedEquals(['A'])); @@ -48,7 +47,7 @@ class A.named(final int a1, int a2, var a3); } test_classMemberNames_class_primaryConstructor_unnamed() { - DefinedNames names = _computeDefinedNames(''' + var names = _computeDefinedNames(''' class A(final int a1, int a2, var a3); '''); expect(names.topLevelNames, unorderedEquals(['A'])); @@ -56,7 +55,7 @@ class A(final int a1, int a2, var a3); } test_classMemberNames_enum() { - DefinedNames names = _computeDefinedNames(''' + var names = _computeDefinedNames(''' enum E { v1, v2; final int d = 0; @@ -68,7 +67,7 @@ enum E { } test_classMemberNames_enum_empty() { - DefinedNames names = _computeDefinedNames(''' + var names = _computeDefinedNames(''' enum E; '''); expect(names.topLevelNames, unorderedEquals(['E'])); @@ -76,7 +75,7 @@ enum E; } test_classMemberNames_enum_primaryConstructor_named() { - DefinedNames names = _computeDefinedNames(''' + var names = _computeDefinedNames(''' enum E.named(final int a1, int a2, var a3) { v1.named(1, 2, 3), v2.named(4, 5, 6); } @@ -86,7 +85,7 @@ enum E.named(final int a1, int a2, var a3) { } test_classMemberNames_enum_primaryConstructor_unnamed() { - DefinedNames names = _computeDefinedNames(''' + var names = _computeDefinedNames(''' enum E(final int a1, int a2, var a3) { v1(1, 2, 3), v2(4, 5, 6); } @@ -96,7 +95,7 @@ enum E(final int a1, int a2, var a3) { } test_classMemberNames_extension() { - DefinedNames names = _computeDefinedNames(''' + var names = _computeDefinedNames(''' extension E on int { int a; void b() {} @@ -109,7 +108,7 @@ extension E on int { } test_classMemberNames_extension_empty() { - DefinedNames names = _computeDefinedNames(''' + var names = _computeDefinedNames(''' extension E on int; '''); expect(names.topLevelNames, unorderedEquals(['E'])); @@ -117,7 +116,7 @@ extension E on int; } test_classMemberNames_extensionType_primaryConstructor_multipleFormalParameters() { - DefinedNames names = _computeDefinedNames(''' + var names = _computeDefinedNames(''' extension type A(int it1, final int it2, int it3) { void foo() {} } @@ -127,7 +126,7 @@ extension type A(int it1, final int it2, int it3) { } test_classMemberNames_extensionType_primaryConstructor_named() { - DefinedNames names = _computeDefinedNames(''' + var names = _computeDefinedNames(''' extension type A.named(int it) { int a, b; A(); @@ -145,7 +144,7 @@ extension type A.named(int it) { } test_classMemberNames_extensionType_primaryConstructor_unnamed() { - DefinedNames names = _computeDefinedNames(''' + var names = _computeDefinedNames(''' extension type A(int it) { void foo() {} } @@ -155,7 +154,7 @@ extension type A(int it) { } test_classMemberNames_mixin() { - DefinedNames names = _computeDefinedNames(''' + var names = _computeDefinedNames(''' mixin A { int a, b; d() {} @@ -174,7 +173,7 @@ mixin B { } test_classMemberNames_mixin_empty() { - DefinedNames names = _computeDefinedNames(''' + var names = _computeDefinedNames(''' mixin M; '''); expect(names.topLevelNames, unorderedEquals(['M'])); @@ -182,7 +181,7 @@ mixin M; } test_topLevelNames() { - DefinedNames names = _computeDefinedNames(''' + var names = _computeDefinedNames(''' class A {} class B = Object with A; typedef C(); @@ -199,8 +198,8 @@ mixin M {} expect(names.classMemberNames, isEmpty); } - DefinedNames _computeDefinedNames(String code, {FeatureSet? featureSet}) { - var parseResult = parseStringWithErrors(code, featureSet: featureSet); + DefinedNames _computeDefinedNames(String code) { + var parseResult = parseTestCodeWithDiagnostics(code); var unit = parseResult.unit as CompilationUnitImpl; return computeDefinedNames(unit); } diff --git a/pkg/analyzer/test/src/dart/analysis/referenced_names_test.dart b/pkg/analyzer/test/src/dart/analysis/referenced_names_test.dart index 299dd9fe917..2060b8872c8 100644 --- a/pkg/analyzer/test/src/dart/analysis/referenced_names_test.dart +++ b/pkg/analyzer/test/src/dart/analysis/referenced_names_test.dart @@ -2,7 +2,6 @@ // 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:analyzer/dart/analysis/features.dart'; import 'package:analyzer/src/dart/analysis/referenced_names.dart'; import 'package:test/test.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; @@ -19,7 +18,7 @@ main() { @reflectiveTest class ComputeReferencedNamesTest extends ParserDiagnosticsTest { test_class_constructor() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' class U { U.named(A a, B b) { C c = null; @@ -30,7 +29,7 @@ class U { } test_class_constructor_invocation() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' f() { const A.foo(); } @@ -39,7 +38,7 @@ f() { } test_class_constructor_invocation_prefixed() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' import 'a.dart' as p; f() { @@ -50,7 +49,7 @@ f() { } test_class_constructor_parameters() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' class U { U(A a) { a; @@ -62,7 +61,7 @@ class U { } test_class_constructor_superFormalParameter() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' class A { A({x}); } @@ -74,7 +73,7 @@ class B extends A { } test_class_extends_sameName_importPrefix() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' import 'a.dart' as p; class A extends p.A {} '''); @@ -82,7 +81,7 @@ class A extends p.A {} } test_class_field() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' class U { A f = new B(); } @@ -91,7 +90,7 @@ class U { } test_class_getter() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' class U { A get a => new B(); } @@ -100,7 +99,7 @@ class U { } test_class_members() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' class U { int a; int get b; @@ -117,7 +116,7 @@ class U { } test_class_members_dontHideQualified() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' class U { int a; int get b; @@ -133,7 +132,7 @@ class U { } test_class_method() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' class U { A m(B p) { C v = 0; @@ -144,7 +143,7 @@ class U { } test_class_method_localVariables() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' class U { A m() { B b = null; @@ -162,7 +161,7 @@ class U { } test_class_method_parameters() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' class U { m(A a) { a; @@ -174,7 +173,7 @@ class U { } test_class_method_parameters_dontHideNamedExpressionName() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' main() { var p; new C(p: p); @@ -184,7 +183,7 @@ main() { } test_class_method_typeParameters() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' class U { A m(B b, T t) { C c = 0; @@ -195,7 +194,7 @@ class U { } test_class_setter() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' class U { set a(A a) { B b = null; @@ -206,7 +205,7 @@ class U { } test_class_typeParameters() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' class U { T f = new A(); } @@ -215,7 +214,7 @@ class U { } test_extensionType_typeParameters() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' extension type Z(int it) { A m(B b, T t, Z z) { C c = 0; @@ -226,7 +225,7 @@ extension type Z(int it) { } test_instantiatedNames_importPrefix() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' import 'a.dart' as p1; import 'b.dart' as p2; main() { @@ -242,7 +241,7 @@ main() { } test_localFunction() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' f(A a) { g(B b) {} } @@ -251,7 +250,7 @@ f(A a) { } test_superToSubs_importPrefix() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' import 'a.dart' as p1; import 'b.dart' as p2; class U extends p1.A with p2.B implements p2.C {} @@ -260,35 +259,35 @@ class U extends p1.A with p2.B implements p2.C {} } test_topLevelVariable() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' A v = new B(c); '''); expect(names, unorderedEquals(['A', 'B', 'c'])); } test_topLevelVariable_multiple() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' A v1 = new B(c), v2 = new D(f); '''); expect(names, unorderedEquals(['A', 'B', 'c', 'D', 'E', 'f'])); } test_unit_classTypeAlias() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' class U = A with B implements C; '''); expect(names, unorderedEquals(['A', 'B', 'C'])); } test_unit_classTypeAlias_typeParameters() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' class U = A with B implements C; '''); expect(names, unorderedEquals(['A', 'B', 'C', 'D'])); } test_unit_extension() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' extension E on int {} f() { E; @@ -298,7 +297,7 @@ f() { } test_unit_function() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' A f(B b) { C c = 0; } @@ -307,7 +306,7 @@ A f(B b) { } test_unit_function_doc() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' /** * Documentation [C.d] reference. */ @@ -317,7 +316,7 @@ A f(B b) {} } test_unit_function_dontHideQualified() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' class U { int a; int get b; @@ -333,7 +332,7 @@ class U { } test_unit_function_localFunction_parameter() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' A f() { B g(x) { x; @@ -346,7 +345,7 @@ A f() { } test_unit_function_localFunctions() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' A f() { B b = null; C g() {} @@ -357,7 +356,7 @@ A f() { } test_unit_function_localsDontHideQualified() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' f(A a, B b) { var v = 0; a.v; @@ -368,7 +367,7 @@ f(A a, B b) { } test_unit_function_localVariables() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' A f() { B b = null; b; @@ -384,7 +383,7 @@ A f() { } test_unit_function_parameters() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' A f(B b) { C c = 0; b; @@ -394,7 +393,7 @@ A f(B b) { } test_unit_function_parameters_dontHideQualified() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' f(x, C g()) { g().x; } @@ -403,7 +402,7 @@ f(x, C g()) { } test_unit_function_typeParameters() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' A f(B b, T t) { C c = 0; } @@ -412,21 +411,21 @@ A f(B b, T t) { } test_unit_functionTypeAlias() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' typedef A F(B B, C c(D d)); '''); expect(names, unorderedEquals(['A', 'B', 'C', 'D'])); } test_unit_functionTypeAlias_typeParameters() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' typedef A F(B b, T t); '''); expect(names, unorderedEquals(['A', 'B'])); } test_unit_getter() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' A get aaa { return new B(); } @@ -435,7 +434,7 @@ A get aaa { } test_unit_setter() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' set aaa(A a) { B b = null; } @@ -444,7 +443,7 @@ set aaa(A a) { } test_unit_topLevelDeclarations() { - Set names = _computeReferencedNames(''' + var names = _computeReferencedNames(''' class L1 {} class L2 = A with B implements C; A L3() => null; @@ -466,8 +465,8 @@ main() { expect(names, unorderedEquals(['A', 'B', 'C'])); } - Set _computeReferencedNames(String code, {FeatureSet? featureSet}) { - var parseResult = parseStringWithErrors(code, featureSet: featureSet); + Set _computeReferencedNames(String code) { + var parseResult = parseTestCodeWithDiagnostics(code); var unit = parseResult.unit; return computeReferencedNames(unit); } @@ -476,7 +475,7 @@ main() { @reflectiveTest class ComputeSubtypedNamesTest extends ParserDiagnosticsTest { void test_classDeclaration() { - Set names = _computeSubtypedNames(''' + var names = _computeSubtypedNames(''' import 'lib.dart'; class X extends A {} class Y extends A with B {} @@ -486,7 +485,7 @@ class Z implements A, B, C {} } void test_classTypeAlias() { - Set names = _computeSubtypedNames(''' + var names = _computeSubtypedNames(''' import 'lib.dart'; class X = A with B implements C, D, E; '''); @@ -494,7 +493,7 @@ class X = A with B implements C, D, E; } void test_extensionTypeDeclaration() { - Set names = _computeSubtypedNames(''' + var names = _computeSubtypedNames(''' extension type E1(X it) implements A {} extension type E2(X it) implements B {} '''); @@ -502,7 +501,7 @@ extension type E2(X it) implements B {} } void test_mixinDeclaration() { - Set names = _computeSubtypedNames(''' + var names = _computeSubtypedNames(''' import 'lib.dart'; mixin M on A, B implements C, D {} '''); @@ -510,7 +509,7 @@ mixin M on A, B implements C, D {} } void test_prefixed() { - Set names = _computeSubtypedNames(''' + var names = _computeSubtypedNames(''' import 'lib.dart' as p; class X extends p.A with p.B implements p.C {} '''); @@ -518,7 +517,7 @@ class X extends p.A with p.B implements p.C {} } void test_typeArguments() { - Set names = _computeSubtypedNames(''' + var names = _computeSubtypedNames(''' import 'lib.dart'; class X extends A {} '''); @@ -526,7 +525,7 @@ class X extends A {} } Set _computeSubtypedNames(String code) { - var parseResult = parseStringWithErrors(code); + var parseResult = parseTestCodeWithDiagnostics(code); var unit = parseResult.unit; return computeSubtypedNames(unit); } diff --git a/pkg/analyzer/test/src/dart/analysis/unlinked_api_signature_test.dart b/pkg/analyzer/test/src/dart/analysis/unlinked_api_signature_test.dart index 400f8870d2b..ae81012ee42 100644 --- a/pkg/analyzer/test/src/dart/analysis/unlinked_api_signature_test.dart +++ b/pkg/analyzer/test/src/dart/analysis/unlinked_api_signature_test.dart @@ -169,11 +169,15 @@ class C { r''' class C { C.foo() : ; +// ^ +// [diag.missingInitializer] Expected an initializer. } ''', r''' class C { C.foo() : f; +// ^ +// [diag.missingAssignmentInInitializer] Expected an assignment after the field name. } ''', ); @@ -380,6 +384,8 @@ class A { r''' class A { factory A() =; +// ^ +// [diag.missingIdentifier] Expected an identifier. } ''', ); @@ -485,6 +491,8 @@ class A { class A { factory A() = static void foo() {} +//^^^^^^ +// [diag.expectedToken] Expected to find ';'. } ''', ); @@ -525,12 +533,16 @@ augment class A { r''' class A { static f = Object(); +// ^ +// [diag.missingConstFinalVarOrType] Variables must be declared using the keywords 'const', 'final', 'var' or a type name. } ''', r''' class A { const static f = Object(); +//^^^^^^ +// [diag.modifierOutOfOrder] The modifier 'static' should be before the modifier 'const'. } ''', ); @@ -729,6 +741,9 @@ static final f = Object(); r''' const static final f = Object(); +// [diag.modifierOutOfOrder][column 1][length 6] The modifier 'static' should be before the modifier 'const'. +// ^^^^^ +// [diag.constAndFinal] Members can't be declared to be both 'const' and 'final'. ''', ); } @@ -1866,10 +1881,20 @@ class A {} r''' foo Future> bar() {} +// [diag.missingFunctionParameters][column 1][length 6] Functions must have an explicit list of parameters. +// ^^^^ +// [diag.expectedToken] Expected to find '>'. +// ^^^ +// [diag.missingFunctionBody] A function body must be provided. ''', r''' foo Future> bar(int x) {} +// [diag.missingFunctionParameters][column 1][length 6] Functions must have an explicit list of parameters. +// ^^^^ +// [diag.expectedToken] Expected to find '>'. +// ^^^ +// [diag.missingFunctionBody] A function body must be provided. ''', ); } @@ -2223,10 +2248,10 @@ var a = 2; test_topLevelVariable_withoutType2() { _assertNotSameSignature( r''' -var a = 1, b = 2, c, d = 4;; +var a = 1, b = 2, c, d = 4; ''', r''' -var a = 1, b, c = 3, d = 4;; +var a = 1, b, c = 3, d = 4; ''', ); } @@ -2322,11 +2347,11 @@ typedef F = void Function(double); } void _assertSignature(String oldCode, String newCode, {required bool same}) { - var oldResult = parseStringWithErrors(oldCode); + var oldResult = parseTestCodeWithDiagnostics(oldCode); var oldUnit = oldResult.unit; var oldSignature = computeUnlinkedApiSignature(oldUnit); - var newResult = parseStringWithErrors(newCode); + var newResult = parseTestCodeWithDiagnostics(newCode); var newUnit = newResult.unit; var newSignature = computeUnlinkedApiSignature(newUnit); diff --git a/pkg/analyzer/test/src/dart/ast/utilities_test.dart b/pkg/analyzer/test/src/dart/ast/utilities_test.dart index 0b844c74162..0e6ed8ddf74 100644 --- a/pkg/analyzer/test/src/dart/ast/utilities_test.dart +++ b/pkg/analyzer/test/src/dart/ast/utilities_test.dart @@ -25,9 +25,9 @@ class NodeLocator2Test extends ParserDiagnosticsTest { } void test_onlyStartOffset() { - String code = ' f() {} '; + var code = ' f() {} '; // 01234567 - CompilationUnit unit = parseStringWithErrors(code).unit; + var unit = parseTestCodeWithDiagnostics(code).unit; var function = unit.declarations.single as FunctionDeclaration; var expression = function.functionExpression; var body = expression.body as BlockFunctionBody; @@ -46,7 +46,7 @@ class NodeLocator2Test extends ParserDiagnosticsTest { var source = r''' class A {} '''; - var unit = parseStringWithErrors(source).unit; + var unit = parseTestCodeWithDiagnostics(source).unit; var node = _assertLocate(unit, source.indexOf(' {}')); expect(node, isClassDeclaration); } @@ -57,7 +57,7 @@ class A { A() {} } '''; - var unit = parseStringWithErrors(source).unit; + var unit = parseTestCodeWithDiagnostics(source).unit; // TODO(dantup): Update these tests to use markers. var node = _assertLocate(unit, source.indexOf('() {}')); expect(node, isConstructorDeclaration); @@ -67,7 +67,7 @@ class A { var source = r''' void f() {} '''; - var unit = parseStringWithErrors(source).unit; + var unit = parseTestCodeWithDiagnostics(source).unit; var node = _assertLocate(unit, source.indexOf('() {}')); expect(node, isFunctionDeclaration); } @@ -76,7 +76,7 @@ void f() {} var source = r''' void f() {} '''; - var unit = parseStringWithErrors(source).unit; + var unit = parseTestCodeWithDiagnostics(source).unit; var node = _assertLocate(unit, source.indexOf('() {}')); expect(node, isFunctionDeclaration); } @@ -87,7 +87,7 @@ class A { void m() {} } '''; - var unit = parseStringWithErrors(source).unit; + var unit = parseTestCodeWithDiagnostics(source).unit; var node = _assertLocate(unit, source.indexOf('() {}')); expect(node, isMethodDeclaration); } @@ -98,7 +98,7 @@ class A { void m() {} } '''; - var unit = parseStringWithErrors(source).unit; + var unit = parseTestCodeWithDiagnostics(source).unit; var node = _assertLocate(unit, source.indexOf('() {}')); expect(node, isMethodDeclaration); } @@ -109,7 +109,7 @@ class A { A.c() {} } '''; - var unit = parseStringWithErrors(source).unit; + var unit = parseTestCodeWithDiagnostics(source).unit; var node = _assertLocate(unit, source.indexOf('() {}')); expect(node, isConstructorDeclaration); } @@ -118,15 +118,15 @@ class A { var source = r''' set s(int i) {} '''; - var unit = parseStringWithErrors(source).unit; + var unit = parseTestCodeWithDiagnostics(source).unit; var node = _assertLocate(unit, source.indexOf('(int i)')); expect(node, isFunctionDeclaration); } void test_startEndOffset() { - String code = ' f() {} '; + var code = ' f() {} '; // 01234567 - CompilationUnit unit = parseStringWithErrors(code).unit; + var unit = parseTestCodeWithDiagnostics(code).unit; var function = unit.declarations.single as FunctionDeclaration; expect(NodeLocator2(-1, 2).searchWithin(unit), isNull); expect(NodeLocator2(0, 2).searchWithin(unit), same(unit)); diff --git a/pkg/analyzer/test/src/dart/parser/record_literal_test.dart b/pkg/analyzer/test/src/dart/parser/record_literal_test.dart index 674608fc884..6fd883e86d5 100644 --- a/pkg/analyzer/test/src/dart/parser/record_literal_test.dart +++ b/pkg/analyzer/test/src/dart/parser/record_literal_test.dart @@ -60,8 +60,10 @@ RecordLiteral } void test_namedFieldRecovery_language219() { - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' final x = (a: 0); +// ^ +// [diag.experimentNotEnabled] This requires the 'records' language feature to be enabled. ''', featureSet: FeatureSets.language_2_19); var node = parseResult.findNode.singleParenthesizedExpression; diff --git a/pkg/analyzer/test/src/dart/parser/top_level_function_test.dart b/pkg/analyzer/test/src/dart/parser/top_level_function_test.dart index 1e45d6379f3..7198fca9f41 100644 --- a/pkg/analyzer/test/src/dart/parser/top_level_function_test.dart +++ b/pkg/analyzer/test/src/dart/parser/top_level_function_test.dart @@ -322,9 +322,11 @@ FunctionDeclaration test_recovery_body_issue56355() { // https://github.com/dart-lang/sdk/issues/56355 - var parseResult = parseStringWithErrors(r''' + var parseResult = parseTestCodeWithDiagnostics(r''' void get() { http.Response response = http2 +// ^^^^^ +// [diag.expectedToken] Expected to find ';'. } '''); diff --git a/pkg/analyzer/test/src/diagnostics/parser_diagnostics.dart b/pkg/analyzer/test/src/diagnostics/parser_diagnostics.dart index 6e22e86f8cc..6da7dbf62f3 100644 --- a/pkg/analyzer/test/src/diagnostics/parser_diagnostics.dart +++ b/pkg/analyzer/test/src/diagnostics/parser_diagnostics.dart @@ -52,6 +52,22 @@ class ParserDiagnosticsTest { ); } + /// Parses [content] without checking diagnostics. + /// + /// Use this only for parser smoke tests where diagnostics are intentionally + /// irrelevant, for example when verifying that a broad set of inputs does not + /// crash or loop. + ParseStringResult parseTestCodeIgnoringDiagnostics( + String content, { + FeatureSet? featureSet, + }) { + return parseString( + content: content, + featureSet: featureSet ?? testFeatureSet, + throwIfDiagnostics: false, + ); + } + /// Parses [content] and checks that its inline diagnostic markers match the /// diagnostics. Marker lines are test metadata and are removed before /// parsing, so they cannot influence parser recovery.