Skip to content

Commit

Permalink
Fix single-line comment disrupting return w/optional chain (#42026)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Dec 18, 2020
1 parent 052d730 commit e789cb1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/compiler/transformers/es2020.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ namespace ts {
if (!isSimpleCopiableExpression(expression)) {
thisArg = factory.createTempVariable(hoistVariableDeclaration);
expression = factory.createAssignment(thisArg, expression);
// if (inParameterInitializer) tempVariableInParameter = true;
}
else {
thisArg = expression;
Expand Down Expand Up @@ -117,7 +116,6 @@ namespace ts {
if (!isSimpleCopiableExpression(leftExpression)) {
capturedLeft = factory.createTempVariable(hoistVariableDeclaration);
leftExpression = factory.createAssignment(capturedLeft, leftExpression);
// if (inParameterInitializer) tempVariableInParameter = true;
}
let rightExpression = capturedLeft;
let thisArg: Expression | undefined;
Expand All @@ -130,7 +128,6 @@ namespace ts {
if (!isSimpleCopiableExpression(rightExpression)) {
thisArg = factory.createTempVariable(hoistVariableDeclaration);
rightExpression = factory.createAssignment(thisArg, rightExpression);
// if (inParameterInitializer) tempVariableInParameter = true;
}
else {
thisArg = rightExpression;
Expand Down Expand Up @@ -163,6 +160,7 @@ namespace ts {
const target = isDelete
? factory.createConditionalExpression(createNotNullCondition(leftExpression, capturedLeft, /*invert*/ true), /*questionToken*/ undefined, factory.createTrue(), /*colonToken*/ undefined, factory.createDeleteExpression(rightExpression))
: factory.createConditionalExpression(createNotNullCondition(leftExpression, capturedLeft, /*invert*/ true), /*questionToken*/ undefined, factory.createVoidZero(), /*colonToken*/ undefined, rightExpression);
setTextRange(target, node);
return thisArg ? factory.createSyntheticReferenceExpression(target, thisArg) : target;
}

Expand All @@ -188,15 +186,14 @@ namespace ts {
if (!isSimpleCopiableExpression(left)) {
right = factory.createTempVariable(hoistVariableDeclaration);
left = factory.createAssignment(right, left);
// if (inParameterInitializer) tempVariableInParameter = true;
}
return factory.createConditionalExpression(
return setTextRange(factory.createConditionalExpression(
createNotNullCondition(left, right),
/*questionToken*/ undefined,
right,
/*colonToken*/ undefined,
visitNode(node.right, visitor, isExpression),
);
), node);
}

function visitDeleteExpression(node: DeleteExpression) {
Expand Down
13 changes: 13 additions & 0 deletions tests/baselines/reference/optionalChainingInArrow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//// [optionalChainingInArrow.ts]
// https://github.com/microsoft/TypeScript/issues/41814
const test = (names: string[]) =>
// single-line comment
names?.filter(x => x);


//// [optionalChainingInArrow.js]
// https://github.com/microsoft/TypeScript/issues/41814
var test = function (names) {
// single-line comment
return names === null || names === void 0 ? void 0 : names.filter(function (x) { return x; });
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @target: es5
// @noTypesAndSymbols: true
// https://github.com/microsoft/TypeScript/issues/41814
const test = (names: string[]) =>
// single-line comment
names?.filter(x => x);

0 comments on commit e789cb1

Please sign in to comment.