From f51a42c6de5b64acc49fc0f2e1ef43fb5faa6fe4 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 19 Nov 2014 17:04:34 -0800 Subject: [PATCH 1/2] Fixed issue where template expression in call expressions would emit with unnecessary parentheses. Fixes #1205. --- src/compiler/emitter.ts | 32 +++++++++++++------ .../reference/amdModuleName2.errors.txt | 30 ++++++++--------- .../templateStringInCallExpression.errors.txt | 7 ++++ .../templateStringInCallExpression.js | 5 +++ ...mplateStringInCallExpressionES6.errors.txt | 7 ++++ .../templateStringInCallExpressionES6.js | 5 +++ .../templateStringInNewExpression.errors.txt | 7 ++++ .../templateStringInNewExpression.js | 5 +++ ...emplateStringInNewExpressionES6.errors.txt | 7 ++++ .../templateStringInNewExpressionES6.js | 5 +++ .../templateStringInTaggedTemplate.errors.txt | 10 ++++++ ...mplateStringInTaggedTemplateES6.errors.txt | 7 ++++ .../templateStringInTaggedTemplateES6.js | 5 +++ .../templateStringInCallExpression.ts | 1 + .../templateStringInCallExpressionES6.ts | 2 ++ .../templateStringInNewExpression.ts | 1 + .../templateStringInNewExpressionES6.ts | 2 ++ .../templateStringInTaggedTemplate.ts | 1 + .../templateStringInTaggedTemplateES6.ts | 2 ++ 19 files changed, 117 insertions(+), 24 deletions(-) create mode 100644 tests/baselines/reference/templateStringInCallExpression.errors.txt create mode 100644 tests/baselines/reference/templateStringInCallExpression.js create mode 100644 tests/baselines/reference/templateStringInCallExpressionES6.errors.txt create mode 100644 tests/baselines/reference/templateStringInCallExpressionES6.js create mode 100644 tests/baselines/reference/templateStringInNewExpression.errors.txt create mode 100644 tests/baselines/reference/templateStringInNewExpression.js create mode 100644 tests/baselines/reference/templateStringInNewExpressionES6.errors.txt create mode 100644 tests/baselines/reference/templateStringInNewExpressionES6.js create mode 100644 tests/baselines/reference/templateStringInTaggedTemplate.errors.txt create mode 100644 tests/baselines/reference/templateStringInTaggedTemplateES6.errors.txt create mode 100644 tests/baselines/reference/templateStringInTaggedTemplateES6.js create mode 100644 tests/cases/conformance/es6/templates/templateStringInCallExpression.ts create mode 100644 tests/cases/conformance/es6/templates/templateStringInCallExpressionES6.ts create mode 100644 tests/cases/conformance/es6/templates/templateStringInNewExpression.ts create mode 100644 tests/cases/conformance/es6/templates/templateStringInNewExpressionES6.ts create mode 100644 tests/cases/conformance/es6/templates/templateStringInTaggedTemplate.ts create mode 100644 tests/cases/conformance/es6/templates/templateStringInTaggedTemplateES6.ts diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index bc74f8304d..06031c9b84 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -821,11 +821,10 @@ module ts { Debug.assert(node.parent.kind !== SyntaxKind.TaggedTemplateExpression); - var templateNeedsParens = isExpression(node.parent) - && node.parent.kind !== SyntaxKind.ParenExpression - && comparePrecedenceToBinaryPlus(node.parent) !== Comparison.LessThan; + var emitOuterParens = isExpression(node.parent) + && templateNeedsParens(node, node.parent); - if (templateNeedsParens) { + if (emitOuterParens) { write("("); } @@ -834,7 +833,7 @@ module ts { forEach(node.templateSpans, templateSpan => { // Check if the expression has operands and binds its operands less closely than binary '+'. // If it does, we need to wrap the expression in parentheses. Otherwise, something like - // `abc${ 1 << 2}` + // `abc${ 1 << 2 }` // becomes // "abc" + 1 << 2 + "" // which is really @@ -855,18 +854,34 @@ module ts { } // Only emit if the literal is non-empty. - // The binary '+' operator is left-associative, so the first string concatenation will force - // the result up to this point to be a string. Emitting a '+ ""' has no semantic effect. + // The binary '+' operator is left-associative, so the first string concatenation + // with the head will force the result up to this point to be a string. + // Emitting a '+ ""' has no semantic effect for middles and tails. if (templateSpan.literal.text.length !== 0) { write(" + ") emitLiteral(templateSpan.literal); } }); - if (templateNeedsParens) { + if (emitOuterParens) { write(")"); } + function templateNeedsParens(template: TemplateExpression, parent: Expression) { + switch (parent.kind) { + case SyntaxKind.TaggedTemplateExpression: + Debug.fail("Path should be unreachable; tagged templates not supported pre-ES6!"); + //return (parent).tag === template; + case SyntaxKind.CallExpression: + case SyntaxKind.NewExpression: + return (parent).func === template; + case SyntaxKind.ParenExpression: + return false; + default: + return comparePrecedenceToBinaryPlus(parent) !== Comparison.LessThan; + } + } + /** * Returns whether the expression has lesser, greater, * or equal precedence to the binary '+' operator @@ -899,7 +914,6 @@ module ts { return Comparison.GreaterThan; } } - } function emitTemplateSpan(span: TemplateSpan) { diff --git a/tests/baselines/reference/amdModuleName2.errors.txt b/tests/baselines/reference/amdModuleName2.errors.txt index 911f31a1f4..cf7fb82ae1 100644 --- a/tests/baselines/reference/amdModuleName2.errors.txt +++ b/tests/baselines/reference/amdModuleName2.errors.txt @@ -1,16 +1,16 @@ -tests/cases/compiler/amdModuleName2.ts(2,1): error TS2458: An AMD module cannot have multiple name assignments. - - -==== tests/cases/compiler/amdModuleName2.ts (1 errors) ==== - /// - /// - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2458: An AMD module cannot have multiple name assignments. - class Foo { - x: number; - constructor() { - this.x = 5; - } - } - export = Foo; +tests/cases/compiler/amdModuleName2.ts(2,1): error TS2458: An AMD module cannot have multiple name assignments. + + +==== tests/cases/compiler/amdModuleName2.ts (1 errors) ==== + /// + /// + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2458: An AMD module cannot have multiple name assignments. + class Foo { + x: number; + constructor() { + this.x = 5; + } + } + export = Foo; \ No newline at end of file diff --git a/tests/baselines/reference/templateStringInCallExpression.errors.txt b/tests/baselines/reference/templateStringInCallExpression.errors.txt new file mode 100644 index 0000000000..c483fd9d4c --- /dev/null +++ b/tests/baselines/reference/templateStringInCallExpression.errors.txt @@ -0,0 +1,7 @@ +tests/cases/conformance/es6/templates/templateStringInCallExpression.ts(1,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. + + +==== tests/cases/conformance/es6/templates/templateStringInCallExpression.ts (1 errors) ==== + `abc${0}abc`(`hello ${0} world`, ` `, `1${2}3`); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. \ No newline at end of file diff --git a/tests/baselines/reference/templateStringInCallExpression.js b/tests/baselines/reference/templateStringInCallExpression.js new file mode 100644 index 0000000000..aa0671a186 --- /dev/null +++ b/tests/baselines/reference/templateStringInCallExpression.js @@ -0,0 +1,5 @@ +//// [templateStringInCallExpression.ts] +`abc${0}abc`(`hello ${0} world`, ` `, `1${2}3`); + +//// [templateStringInCallExpression.js] +("abc" + 0 + "abc")("hello " + 0 + " world", " ", "1" + 2 + "3"); diff --git a/tests/baselines/reference/templateStringInCallExpressionES6.errors.txt b/tests/baselines/reference/templateStringInCallExpressionES6.errors.txt new file mode 100644 index 0000000000..7a391b6afe --- /dev/null +++ b/tests/baselines/reference/templateStringInCallExpressionES6.errors.txt @@ -0,0 +1,7 @@ +tests/cases/conformance/es6/templates/templateStringInCallExpressionES6.ts(1,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. + + +==== tests/cases/conformance/es6/templates/templateStringInCallExpressionES6.ts (1 errors) ==== + `abc${0}abc`(`hello ${0} world`, ` `, `1${2}3`); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. \ No newline at end of file diff --git a/tests/baselines/reference/templateStringInCallExpressionES6.js b/tests/baselines/reference/templateStringInCallExpressionES6.js new file mode 100644 index 0000000000..a9f5f9153c --- /dev/null +++ b/tests/baselines/reference/templateStringInCallExpressionES6.js @@ -0,0 +1,5 @@ +//// [templateStringInCallExpressionES6.ts] +`abc${0}abc`(`hello ${0} world`, ` `, `1${2}3`); + +//// [templateStringInCallExpressionES6.js] +`abc${0}abc`(`hello ${0} world`, ` `, `1${2}3`); diff --git a/tests/baselines/reference/templateStringInNewExpression.errors.txt b/tests/baselines/reference/templateStringInNewExpression.errors.txt new file mode 100644 index 0000000000..00ae3a6e4a --- /dev/null +++ b/tests/baselines/reference/templateStringInNewExpression.errors.txt @@ -0,0 +1,7 @@ +tests/cases/conformance/es6/templates/templateStringInNewExpression.ts(1,1): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + + +==== tests/cases/conformance/es6/templates/templateStringInNewExpression.ts (1 errors) ==== + new `abc${0}abc`(`hello ${0} world`, ` `, `1${2}3`); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. \ No newline at end of file diff --git a/tests/baselines/reference/templateStringInNewExpression.js b/tests/baselines/reference/templateStringInNewExpression.js new file mode 100644 index 0000000000..1b1770c619 --- /dev/null +++ b/tests/baselines/reference/templateStringInNewExpression.js @@ -0,0 +1,5 @@ +//// [templateStringInNewExpression.ts] +new `abc${0}abc`(`hello ${0} world`, ` `, `1${2}3`); + +//// [templateStringInNewExpression.js] +new ("abc" + 0 + "abc")("hello " + 0 + " world", " ", "1" + 2 + "3"); diff --git a/tests/baselines/reference/templateStringInNewExpressionES6.errors.txt b/tests/baselines/reference/templateStringInNewExpressionES6.errors.txt new file mode 100644 index 0000000000..3170afc98b --- /dev/null +++ b/tests/baselines/reference/templateStringInNewExpressionES6.errors.txt @@ -0,0 +1,7 @@ +tests/cases/conformance/es6/templates/templateStringInNewExpressionES6.ts(1,1): error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. + + +==== tests/cases/conformance/es6/templates/templateStringInNewExpressionES6.ts (1 errors) ==== + new `abc${0}abc`(`hello ${0} world`, ` `, `1${2}3`); + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2351: Cannot use 'new' with an expression whose type lacks a call or construct signature. \ No newline at end of file diff --git a/tests/baselines/reference/templateStringInNewExpressionES6.js b/tests/baselines/reference/templateStringInNewExpressionES6.js new file mode 100644 index 0000000000..f970c11167 --- /dev/null +++ b/tests/baselines/reference/templateStringInNewExpressionES6.js @@ -0,0 +1,5 @@ +//// [templateStringInNewExpressionES6.ts] +new `abc${0}abc`(`hello ${0} world`, ` `, `1${2}3`); + +//// [templateStringInNewExpressionES6.js] +new `abc${0}abc`(`hello ${0} world`, ` `, `1${2}3`); diff --git a/tests/baselines/reference/templateStringInTaggedTemplate.errors.txt b/tests/baselines/reference/templateStringInTaggedTemplate.errors.txt new file mode 100644 index 0000000000..1bad2f0bf5 --- /dev/null +++ b/tests/baselines/reference/templateStringInTaggedTemplate.errors.txt @@ -0,0 +1,10 @@ +tests/cases/conformance/es6/templates/templateStringInTaggedTemplate.ts(1,1): error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. +tests/cases/conformance/es6/templates/templateStringInTaggedTemplate.ts(1,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. + + +==== tests/cases/conformance/es6/templates/templateStringInTaggedTemplate.ts (2 errors) ==== + `I AM THE ${ `${ `TAG` } ` } PORTION` `I ${ "AM" } THE TEMPLATE PORTION` + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS1159: Tagged templates are only available when targeting ECMAScript 6 and higher. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. \ No newline at end of file diff --git a/tests/baselines/reference/templateStringInTaggedTemplateES6.errors.txt b/tests/baselines/reference/templateStringInTaggedTemplateES6.errors.txt new file mode 100644 index 0000000000..fd30d7f407 --- /dev/null +++ b/tests/baselines/reference/templateStringInTaggedTemplateES6.errors.txt @@ -0,0 +1,7 @@ +tests/cases/conformance/es6/templates/templateStringInTaggedTemplateES6.ts(1,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. + + +==== tests/cases/conformance/es6/templates/templateStringInTaggedTemplateES6.ts (1 errors) ==== + `I AM THE ${ `${ `TAG` } ` } PORTION` `I ${ "AM" } THE TEMPLATE PORTION` + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. \ No newline at end of file diff --git a/tests/baselines/reference/templateStringInTaggedTemplateES6.js b/tests/baselines/reference/templateStringInTaggedTemplateES6.js new file mode 100644 index 0000000000..419471a052 --- /dev/null +++ b/tests/baselines/reference/templateStringInTaggedTemplateES6.js @@ -0,0 +1,5 @@ +//// [templateStringInTaggedTemplateES6.ts] +`I AM THE ${ `${ `TAG` } ` } PORTION` `I ${ "AM" } THE TEMPLATE PORTION` + +//// [templateStringInTaggedTemplateES6.js] +`I AM THE ${`${`TAG`} `} PORTION` `I ${"AM"} THE TEMPLATE PORTION`; diff --git a/tests/cases/conformance/es6/templates/templateStringInCallExpression.ts b/tests/cases/conformance/es6/templates/templateStringInCallExpression.ts new file mode 100644 index 0000000000..04597a4c12 --- /dev/null +++ b/tests/cases/conformance/es6/templates/templateStringInCallExpression.ts @@ -0,0 +1 @@ +`abc${0}abc`(`hello ${0} world`, ` `, `1${2}3`); \ No newline at end of file diff --git a/tests/cases/conformance/es6/templates/templateStringInCallExpressionES6.ts b/tests/cases/conformance/es6/templates/templateStringInCallExpressionES6.ts new file mode 100644 index 0000000000..d1cf67787e --- /dev/null +++ b/tests/cases/conformance/es6/templates/templateStringInCallExpressionES6.ts @@ -0,0 +1,2 @@ +// @target: ES6 +`abc${0}abc`(`hello ${0} world`, ` `, `1${2}3`); \ No newline at end of file diff --git a/tests/cases/conformance/es6/templates/templateStringInNewExpression.ts b/tests/cases/conformance/es6/templates/templateStringInNewExpression.ts new file mode 100644 index 0000000000..cac8571553 --- /dev/null +++ b/tests/cases/conformance/es6/templates/templateStringInNewExpression.ts @@ -0,0 +1 @@ +new `abc${0}abc`(`hello ${0} world`, ` `, `1${2}3`); \ No newline at end of file diff --git a/tests/cases/conformance/es6/templates/templateStringInNewExpressionES6.ts b/tests/cases/conformance/es6/templates/templateStringInNewExpressionES6.ts new file mode 100644 index 0000000000..6d069f7004 --- /dev/null +++ b/tests/cases/conformance/es6/templates/templateStringInNewExpressionES6.ts @@ -0,0 +1,2 @@ +// @target: ES6 +new `abc${0}abc`(`hello ${0} world`, ` `, `1${2}3`); \ No newline at end of file diff --git a/tests/cases/conformance/es6/templates/templateStringInTaggedTemplate.ts b/tests/cases/conformance/es6/templates/templateStringInTaggedTemplate.ts new file mode 100644 index 0000000000..79a0d770be --- /dev/null +++ b/tests/cases/conformance/es6/templates/templateStringInTaggedTemplate.ts @@ -0,0 +1 @@ +`I AM THE ${ `${ `TAG` } ` } PORTION` `I ${ "AM" } THE TEMPLATE PORTION` \ No newline at end of file diff --git a/tests/cases/conformance/es6/templates/templateStringInTaggedTemplateES6.ts b/tests/cases/conformance/es6/templates/templateStringInTaggedTemplateES6.ts new file mode 100644 index 0000000000..dfb2de1f4b --- /dev/null +++ b/tests/cases/conformance/es6/templates/templateStringInTaggedTemplateES6.ts @@ -0,0 +1,2 @@ +// @target: ES6 +`I AM THE ${ `${ `TAG` } ` } PORTION` `I ${ "AM" } THE TEMPLATE PORTION` \ No newline at end of file From 8d4e9064d0d40883c825fa7180d076b1d8c4fabb Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Wed, 19 Nov 2014 22:11:17 -0800 Subject: [PATCH 2/2] Addressed CR feedback. --- src/compiler/emitter.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 06031c9b84..f6ccc4e6d5 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -869,14 +869,13 @@ module ts { function templateNeedsParens(template: TemplateExpression, parent: Expression) { switch (parent.kind) { - case SyntaxKind.TaggedTemplateExpression: - Debug.fail("Path should be unreachable; tagged templates not supported pre-ES6!"); - //return (parent).tag === template; case SyntaxKind.CallExpression: case SyntaxKind.NewExpression: return (parent).func === template; case SyntaxKind.ParenExpression: return false; + case SyntaxKind.TaggedTemplateExpression: + Debug.fail("Path should be unreachable; tagged templates not supported pre-ES6."); default: return comparePrecedenceToBinaryPlus(parent) !== Comparison.LessThan; }