From e789cb13561a2f12b44bd19e54aa82fe88249cba Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Thu, 17 Dec 2020 18:55:09 -0800 Subject: [PATCH] Fix single-line comment disrupting return w/optional chain (#42026) --- src/compiler/transformers/es2020.ts | 9 +++------ .../baselines/reference/optionalChainingInArrow.js | 13 +++++++++++++ .../optionalChaining/optionalChainingInArrow.ts | 6 ++++++ 3 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 tests/baselines/reference/optionalChainingInArrow.js create mode 100644 tests/cases/conformance/expressions/optionalChaining/optionalChainingInArrow.ts diff --git a/src/compiler/transformers/es2020.ts b/src/compiler/transformers/es2020.ts index a3a87df91ed..bd0d12cf8d7 100644 --- a/src/compiler/transformers/es2020.ts +++ b/src/compiler/transformers/es2020.ts @@ -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; @@ -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; @@ -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; @@ -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; } @@ -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) { diff --git a/tests/baselines/reference/optionalChainingInArrow.js b/tests/baselines/reference/optionalChainingInArrow.js new file mode 100644 index 00000000000..d30b8b35e55 --- /dev/null +++ b/tests/baselines/reference/optionalChainingInArrow.js @@ -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; }); +}; diff --git a/tests/cases/conformance/expressions/optionalChaining/optionalChainingInArrow.ts b/tests/cases/conformance/expressions/optionalChaining/optionalChainingInArrow.ts new file mode 100644 index 00000000000..1c885d1fd4c --- /dev/null +++ b/tests/cases/conformance/expressions/optionalChaining/optionalChainingInArrow.ts @@ -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);