Skip to content

Commit

Permalink
fix(47296): add outlining spans for parenthesized expressions (#47307)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk committed Feb 22, 2022
1 parent 2d85e1e commit aa920c0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 12 deletions.
10 changes: 9 additions & 1 deletion src/services/outliningElementsCollector.ts
Expand Up @@ -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 {
Expand All @@ -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());
Expand Down Expand Up @@ -307,6 +309,12 @@ namespace ts.OutliningElementsCollector {
function spanForNodeArray(nodeArray: NodeArray<Node>): 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 {
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/fourslash/getJSXOutliningSpans.tsx
Expand Up @@ -2,7 +2,7 @@
////
////export class Home extends Component[| {
//// render()[| {
//// return (
//// return [|(
//// [|<div>
//// [|<h1>Hello, world!</h1>|]
//// [|<ul>
Expand All @@ -29,7 +29,7 @@
//// text
//// </>|]
//// </div>|]
//// );
//// )|];
//// }|]
////}|]

Expand Down
8 changes: 4 additions & 4 deletions tests/cases/fourslash/getOutliningSpans.ts
Expand Up @@ -26,11 +26,11 @@
//// }|]
////}|]
////// class expressions
//// (new class[| {
//// [|(new class[| {
//// bla()[| {
////
//// }|]
//// }|])
//// }|])|]
////switch(1)[| {
//// case 1:[| break;|]
////}|]
Expand Down Expand Up @@ -64,9 +64,9 @@
////}|]
////
////// function expressions
////(function f()[| {
////[|(function f()[| {
////
////}|])
////}|])|]
////
////// trivia handeling
////class ClassFooWithTrivia[| /* some comments */
Expand Down
Expand Up @@ -5,7 +5,7 @@
//// () =>[| {
//// 42
//// }|];
//// () =>[| (
//// () => [|(
//// 42
//// )|];
//// () =>[| "foo" +
Expand Down
33 changes: 33 additions & 0 deletions tests/cases/fourslash/outliningSpansForParenthesizedExpression.ts
@@ -0,0 +1,33 @@
/// <reference path="fourslash.ts"/>

////const a = [|(
//// true
//// ? true
//// : false
//// ? true
//// : false
////)|];
////
////const b = ( 1 );
////
////const c = [|(
//// 1
////)|];
////
////( 1 );
////
////[|(
//// [|(
//// [|(
//// 1
//// )|]
//// )|]
////)|];
////
////[|(
//// [|(
//// ( 1 )
//// )|]
////)|];

verify.outliningSpansInCurrentFile(test.ranges());
4 changes: 2 additions & 2 deletions tests/cases/fourslash/shims-pp/getOutliningSpans.ts
Expand Up @@ -58,9 +58,9 @@
////}|]
////
////// function expressions
////(function f()[| {
////[|(function f()[| {
////
////}|])
////}|])|]
////
////// trivia handeling
////class ClassFooWithTrivia[| /* some comments */
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/fourslash/shims/getOutliningSpans.ts
Expand Up @@ -58,9 +58,9 @@
////}|]
////
////// function expressions
////(function f()[| {
////[|(function f()[| {
////
////}|])
////}|])|]
////
////// trivia handeling
////class ClassFooWithTrivia[| /* some comments */
Expand Down

0 comments on commit aa920c0

Please sign in to comment.