From 1c2ff006c1870c827fd32cefd34212856d3f3a18 Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Wed, 8 Apr 2020 15:24:45 -0700 Subject: [PATCH] Fix parenthesization rules for yield --- src/compiler/factoryPublic.ts | 6 +- .../reference/importCallExpressionInUMD5.js | 59 +++++++++++++++++++ .../importCallExpressionInUMD5.symbols | 17 ++++++ .../importCallExpressionInUMD5.types | 23 ++++++++ .../importCallExpressionInUMD5.ts | 12 ++++ 5 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 tests/baselines/reference/importCallExpressionInUMD5.js create mode 100644 tests/baselines/reference/importCallExpressionInUMD5.symbols create mode 100644 tests/baselines/reference/importCallExpressionInUMD5.types create mode 100644 tests/cases/conformance/dynamicImport/importCallExpressionInUMD5.ts diff --git a/src/compiler/factoryPublic.ts b/src/compiler/factoryPublic.ts index f0aae0daf938b..e17cf3c505459 100644 --- a/src/compiler/factoryPublic.ts +++ b/src/compiler/factoryPublic.ts @@ -1555,9 +1555,11 @@ namespace ts { export function createYield(expression?: Expression): YieldExpression; export function createYield(asteriskToken: AsteriskToken | undefined, expression: Expression): YieldExpression; export function createYield(asteriskTokenOrExpression?: AsteriskToken | undefined | Expression, expression?: Expression) { + const asteriskToken = asteriskTokenOrExpression && asteriskTokenOrExpression.kind === SyntaxKind.AsteriskToken ? asteriskTokenOrExpression : undefined; + expression = asteriskTokenOrExpression && asteriskTokenOrExpression.kind !== SyntaxKind.AsteriskToken ? asteriskTokenOrExpression : expression; const node = createSynthesizedNode(SyntaxKind.YieldExpression); - node.asteriskToken = asteriskTokenOrExpression && asteriskTokenOrExpression.kind === SyntaxKind.AsteriskToken ? asteriskTokenOrExpression : undefined; - node.expression = asteriskTokenOrExpression && asteriskTokenOrExpression.kind !== SyntaxKind.AsteriskToken ? asteriskTokenOrExpression : expression; + node.asteriskToken = asteriskToken; + node.expression = expression && parenthesizeExpressionForList(expression); return node; } diff --git a/tests/baselines/reference/importCallExpressionInUMD5.js b/tests/baselines/reference/importCallExpressionInUMD5.js new file mode 100644 index 0000000000000..15140f553bd79 --- /dev/null +++ b/tests/baselines/reference/importCallExpressionInUMD5.js @@ -0,0 +1,59 @@ +//// [tests/cases/conformance/dynamicImport/importCallExpressionInUMD5.ts] //// + +//// [0.ts] +export function foo() { return "foo"; } + +//// [1.ts] +// https://github.com/microsoft/TypeScript/issues/36780 +async function func() { + const packageName = '.'; + const packageJson = await import(packageName + '/package.json'); +} + + +//// [0.js] +(function (factory) { + if (typeof module === "object" && typeof module.exports === "object") { + var v = factory(require, exports); + if (v !== undefined) module.exports = v; + } + else if (typeof define === "function" && define.amd) { + define(["require", "exports"], factory); + } +})(function (require, exports) { + "use strict"; + Object.defineProperty(exports, "__esModule", { value: true }); + exports.foo = void 0; + function foo() { return "foo"; } + exports.foo = foo; +}); +//// [1.js] +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +(function (factory) { + if (typeof module === "object" && typeof module.exports === "object") { + var v = factory(require, exports); + if (v !== undefined) module.exports = v; + } + else if (typeof define === "function" && define.amd) { + define(["require", "exports"], factory); + } +})(function (require, exports) { + "use strict"; + var __syncRequire = typeof module === "object" && typeof module.exports === "object"; + // https://github.com/microsoft/TypeScript/issues/36780 + function func() { + return __awaiter(this, void 0, void 0, function* () { + var _a; + const packageName = '.'; + const packageJson = yield (_a = packageName + '/package.json', __syncRequire ? Promise.resolve().then(() => require(_a)) : new Promise((resolve_1, reject_1) => { require([_a], resolve_1, reject_1); })); + }); + } +}); diff --git a/tests/baselines/reference/importCallExpressionInUMD5.symbols b/tests/baselines/reference/importCallExpressionInUMD5.symbols new file mode 100644 index 0000000000000..6a99620fad5c1 --- /dev/null +++ b/tests/baselines/reference/importCallExpressionInUMD5.symbols @@ -0,0 +1,17 @@ +=== tests/cases/conformance/dynamicImport/0.ts === +export function foo() { return "foo"; } +>foo : Symbol(foo, Decl(0.ts, 0, 0)) + +=== tests/cases/conformance/dynamicImport/1.ts === +// https://github.com/microsoft/TypeScript/issues/36780 +async function func() { +>func : Symbol(func, Decl(1.ts, 0, 0)) + + const packageName = '.'; +>packageName : Symbol(packageName, Decl(1.ts, 2, 9)) + + const packageJson = await import(packageName + '/package.json'); +>packageJson : Symbol(packageJson, Decl(1.ts, 3, 9)) +>packageName : Symbol(packageName, Decl(1.ts, 2, 9)) +} + diff --git a/tests/baselines/reference/importCallExpressionInUMD5.types b/tests/baselines/reference/importCallExpressionInUMD5.types new file mode 100644 index 0000000000000..dfcc5ad7e6df4 --- /dev/null +++ b/tests/baselines/reference/importCallExpressionInUMD5.types @@ -0,0 +1,23 @@ +=== tests/cases/conformance/dynamicImport/0.ts === +export function foo() { return "foo"; } +>foo : () => string +>"foo" : "foo" + +=== tests/cases/conformance/dynamicImport/1.ts === +// https://github.com/microsoft/TypeScript/issues/36780 +async function func() { +>func : () => Promise + + const packageName = '.'; +>packageName : "." +>'.' : "." + + const packageJson = await import(packageName + '/package.json'); +>packageJson : any +>await import(packageName + '/package.json') : any +>import(packageName + '/package.json') : Promise +>packageName + '/package.json' : string +>packageName : "." +>'/package.json' : "/package.json" +} + diff --git a/tests/cases/conformance/dynamicImport/importCallExpressionInUMD5.ts b/tests/cases/conformance/dynamicImport/importCallExpressionInUMD5.ts new file mode 100644 index 0000000000000..15605bd4bfa80 --- /dev/null +++ b/tests/cases/conformance/dynamicImport/importCallExpressionInUMD5.ts @@ -0,0 +1,12 @@ +// @module: umd +// @target: es2015 +// @filename: 0.ts +export function foo() { return "foo"; } + +// @filename: 1.ts + +// https://github.com/microsoft/TypeScript/issues/36780 +async function func() { + const packageName = '.'; + const packageJson = await import(packageName + '/package.json'); +}