From aa920c00f21e8a378e0b282401786b00d6310803 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Tue, 22 Feb 2022 22:12:12 +0200 Subject: [PATCH] fix(47296): add outlining spans for parenthesized expressions (#47307) --- src/services/outliningElementsCollector.ts | 10 +++++- .../cases/fourslash/getJSXOutliningSpans.tsx | 4 +-- tests/cases/fourslash/getOutliningSpans.ts | 8 ++--- .../outliningSpansForArrowFunctionBody.ts | 2 +- ...utliningSpansForParenthesizedExpression.ts | 33 +++++++++++++++++++ .../fourslash/shims-pp/getOutliningSpans.ts | 4 +-- .../fourslash/shims/getOutliningSpans.ts | 4 +-- 7 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 tests/cases/fourslash/outliningSpansForParenthesizedExpression.ts diff --git a/src/services/outliningElementsCollector.ts b/src/services/outliningElementsCollector.ts index c071fb145cc18..ee06b0c3a6833 100644 --- a/src/services/outliningElementsCollector.ts +++ b/src/services/outliningElementsCollector.ts @@ -240,6 +240,8 @@ namespace ts.OutliningElementsCollector { return spanForArrowFunction(n as ArrowFunction); case SyntaxKind.CallExpression: return spanForCallExpression(n as CallExpression); + case SyntaxKind.ParenthesizedExpression: + return spanForParenthesizedExpression(n as ParenthesizedExpression); } function spanForCallExpression(node: CallExpression): OutliningSpan | undefined { @@ -256,7 +258,7 @@ namespace ts.OutliningElementsCollector { } function spanForArrowFunction(node: ArrowFunction): OutliningSpan | undefined { - if (isBlock(node.body) || positionsAreOnSameLine(node.body.getFullStart(), node.body.getEnd(), sourceFile)) { + if (isBlock(node.body) || isParenthesizedExpression(node.body) || positionsAreOnSameLine(node.body.getFullStart(), node.body.getEnd(), sourceFile)) { return undefined; } const textSpan = createTextSpanFromBounds(node.body.getFullStart(), node.body.getEnd()); @@ -307,6 +309,12 @@ namespace ts.OutliningElementsCollector { function spanForNodeArray(nodeArray: NodeArray): OutliningSpan | undefined { return nodeArray.length ? createOutliningSpan(createTextSpanFromRange(nodeArray), OutliningSpanKind.Code) : undefined; } + + function spanForParenthesizedExpression(node: ParenthesizedExpression): OutliningSpan | undefined { + if (positionsAreOnSameLine(node.getStart(), node.getEnd(), sourceFile)) return undefined; + const textSpan = createTextSpanFromBounds(node.getStart(), node.getEnd()); + return createOutliningSpan(textSpan, OutliningSpanKind.Code, createTextSpanFromNode(node)); + } } function functionSpan(node: SignatureDeclaration, body: Block, sourceFile: SourceFile): OutliningSpan | undefined { diff --git a/tests/cases/fourslash/getJSXOutliningSpans.tsx b/tests/cases/fourslash/getJSXOutliningSpans.tsx index f00eac19d5cd0..1ddc0cafa3e2a 100644 --- a/tests/cases/fourslash/getJSXOutliningSpans.tsx +++ b/tests/cases/fourslash/getJSXOutliningSpans.tsx @@ -2,7 +2,7 @@ //// ////export class Home extends Component[| { //// render()[| { -//// return ( +//// return [|( //// [|
//// [|

Hello, world!

|] //// [|
    @@ -29,7 +29,7 @@ //// text //// |] ////
|] -//// ); +//// )|]; //// }|] ////}|] diff --git a/tests/cases/fourslash/getOutliningSpans.ts b/tests/cases/fourslash/getOutliningSpans.ts index f8bc10ad95544..a522d47ce8bb8 100644 --- a/tests/cases/fourslash/getOutliningSpans.ts +++ b/tests/cases/fourslash/getOutliningSpans.ts @@ -26,11 +26,11 @@ //// }|] ////}|] ////// class expressions -//// (new class[| { +//// [|(new class[| { //// bla()[| { //// //// }|] -//// }|]) +//// }|])|] ////switch(1)[| { //// case 1:[| break;|] ////}|] @@ -64,9 +64,9 @@ ////}|] //// ////// function expressions -////(function f()[| { +////[|(function f()[| { //// -////}|]) +////}|])|] //// ////// trivia handeling ////class ClassFooWithTrivia[| /* some comments */ diff --git a/tests/cases/fourslash/outliningSpansForArrowFunctionBody.ts b/tests/cases/fourslash/outliningSpansForArrowFunctionBody.ts index 7dfc6e8ec2735..2a970c1342ea4 100644 --- a/tests/cases/fourslash/outliningSpansForArrowFunctionBody.ts +++ b/tests/cases/fourslash/outliningSpansForArrowFunctionBody.ts @@ -5,7 +5,7 @@ //// () =>[| { //// 42 //// }|]; -//// () =>[| ( +//// () => [|( //// 42 //// )|]; //// () =>[| "foo" + diff --git a/tests/cases/fourslash/outliningSpansForParenthesizedExpression.ts b/tests/cases/fourslash/outliningSpansForParenthesizedExpression.ts new file mode 100644 index 0000000000000..54ec56bdd0156 --- /dev/null +++ b/tests/cases/fourslash/outliningSpansForParenthesizedExpression.ts @@ -0,0 +1,33 @@ +/// + +////const a = [|( +//// true +//// ? true +//// : false +//// ? true +//// : false +////)|]; +//// +////const b = ( 1 ); +//// +////const c = [|( +//// 1 +////)|]; +//// +////( 1 ); +//// +////[|( +//// [|( +//// [|( +//// 1 +//// )|] +//// )|] +////)|]; +//// +////[|( +//// [|( +//// ( 1 ) +//// )|] +////)|]; + +verify.outliningSpansInCurrentFile(test.ranges()); diff --git a/tests/cases/fourslash/shims-pp/getOutliningSpans.ts b/tests/cases/fourslash/shims-pp/getOutliningSpans.ts index 3050912101ee2..849d7bcf8b922 100644 --- a/tests/cases/fourslash/shims-pp/getOutliningSpans.ts +++ b/tests/cases/fourslash/shims-pp/getOutliningSpans.ts @@ -58,9 +58,9 @@ ////}|] //// ////// function expressions -////(function f()[| { +////[|(function f()[| { //// -////}|]) +////}|])|] //// ////// trivia handeling ////class ClassFooWithTrivia[| /* some comments */ diff --git a/tests/cases/fourslash/shims/getOutliningSpans.ts b/tests/cases/fourslash/shims/getOutliningSpans.ts index d6c354c334844..c5b41ff79977a 100644 --- a/tests/cases/fourslash/shims/getOutliningSpans.ts +++ b/tests/cases/fourslash/shims/getOutliningSpans.ts @@ -58,9 +58,9 @@ ////}|] //// ////// function expressions -////(function f()[| { +////[|(function f()[| { //// -////}|]) +////}|])|] //// ////// trivia handeling ////class ClassFooWithTrivia[| /* some comments */