From c191725deeeb0a952df57aa29e8cd7a873364cae Mon Sep 17 00:00:00 2001 From: Brian Wilkerson Date: Tue, 6 Sep 2022 14:26:26 +0000 Subject: [PATCH] Test that record types work correctly in outlines I don't believe there's any way for record literals to show up in the outline, and I think this is a reasonable representative sample, but if you think more tests would be better, let me know. Change-Id: Ia62ad30d860d70a483249879f2ae8bea8e65b6a6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/257442 Reviewed-by: Samuel Rawlins Commit-Queue: Brian Wilkerson Reviewed-by: Konstantin Shcheglov --- .../src/computer/outline_computer_test.dart | 67 +++++++++++++++++++ .../lib/src/dart/ast/to_source_visitor.dart | 3 + .../src/dart/ast/to_source_visitor_test.dart | 11 +++ 3 files changed, 81 insertions(+) diff --git a/pkg/analysis_server/test/src/computer/outline_computer_test.dart b/pkg/analysis_server/test/src/computer/outline_computer_test.dart index dcf19c685c6..0048367fe63 100644 --- a/pkg/analysis_server/test/src/computer/outline_computer_test.dart +++ b/pkg/analysis_server/test/src/computer/outline_computer_test.dart @@ -1572,6 +1572,73 @@ set propB(int v) {} } } + Future test_topLevelFunction_recordTypes() async { + var unitOutline = await _computeOutline(''' +(int, int) f((String, String) r) => throw ''; +'''); + + var topOutlines = unitOutline.children!; + expect(topOutlines, hasLength(1)); + + assertJsonText(topOutlines[0], ''' +{ + "element": { + "kind": "FUNCTION", + "name": "f", + "location": { + "file": "/home/test/lib/test.dart", + "offset": 11, + "length": 1, + "startLine": 1, + "startColumn": 12, + "endLine": 1, + "endColumn": 13 + }, + "flags": 8, + "parameters": "((String, String) r)", + "returnType": "(int, int)" + }, + "offset": 0, + "length": 45, + "codeOffset": 0, + "codeLength": 45 +} +'''); + } + + Future test_topLevelVariable_recordTypes() async { + var unitOutline = await _computeOutline(''' +(int, int)? r = null; +'''); + + var topOutlines = unitOutline.children!; + expect(topOutlines, hasLength(1)); + + assertJsonText(topOutlines[0], ''' +{ + "element": { + "kind": "TOP_LEVEL_VARIABLE", + "name": "r", + "location": { + "file": "/home/test/lib/test.dart", + "offset": 12, + "length": 1, + "startLine": 1, + "startColumn": 13, + "endLine": 1, + "endColumn": 14 + }, + "flags": 0, + "returnType": "(int, int)?" + }, + "offset": 0, + "length": 21, + "codeOffset": 12, + "codeLength": 8 +} +'''); + } + void _expect(Outline outline, {ElementKind? kind, bool leaf = false, diff --git a/pkg/analyzer/lib/src/dart/ast/to_source_visitor.dart b/pkg/analyzer/lib/src/dart/ast/to_source_visitor.dart index f09dfe3162d..dc54ce8d7a8 100644 --- a/pkg/analyzer/lib/src/dart/ast/to_source_visitor.dart +++ b/pkg/analyzer/lib/src/dart/ast/to_source_visitor.dart @@ -915,6 +915,9 @@ class ToSourceVisitor implements AstVisitor { } _visitNode(namedFields); sink.write(')'); + if (node.question != null) { + sink.write('?'); + } } @override diff --git a/pkg/analyzer/test/src/dart/ast/to_source_visitor_test.dart b/pkg/analyzer/test/src/dart/ast/to_source_visitor_test.dart index ec97b241f18..3db0a62e1b6 100644 --- a/pkg/analyzer/test/src/dart/ast/to_source_visitor_test.dart +++ b/pkg/analyzer/test/src/dart/ast/to_source_visitor_test.dart @@ -2852,6 +2852,17 @@ $code f() {} ); } + void test_visitRecordTypeAnnotation_positional_nullable() { + final code = '(int, bool)?'; + var findNode = _parseStringToFindNode(''' +$code f() {} +'''); + _assertSource( + code, + findNode.recordTypeAnnotation(code), + ); + } + void test_visitRedirectingConstructorInvocation_named() { _assertSource( "this.c()", AstTestFactory.redirectingConstructorInvocation2("c"));