Skip to content

Commit

Permalink
Fix parenthesization rules for yield
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Apr 8, 2020
1 parent 5a79169 commit 1c2ff00
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/compiler/factoryPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? <AsteriskToken>asteriskTokenOrExpression : undefined;
expression = asteriskTokenOrExpression && asteriskTokenOrExpression.kind !== SyntaxKind.AsteriskToken ? asteriskTokenOrExpression : expression;
const node = <YieldExpression>createSynthesizedNode(SyntaxKind.YieldExpression);
node.asteriskToken = asteriskTokenOrExpression && asteriskTokenOrExpression.kind === SyntaxKind.AsteriskToken ? <AsteriskToken>asteriskTokenOrExpression : undefined;
node.expression = asteriskTokenOrExpression && asteriskTokenOrExpression.kind !== SyntaxKind.AsteriskToken ? asteriskTokenOrExpression : expression;
node.asteriskToken = asteriskToken;
node.expression = expression && parenthesizeExpressionForList(expression);
return node;
}

Expand Down
59 changes: 59 additions & 0 deletions tests/baselines/reference/importCallExpressionInUMD5.js
Original file line number Diff line number Diff line change
@@ -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); }));
});
}
});
17 changes: 17 additions & 0 deletions tests/baselines/reference/importCallExpressionInUMD5.symbols
Original file line number Diff line number Diff line change
@@ -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))
}

23 changes: 23 additions & 0 deletions tests/baselines/reference/importCallExpressionInUMD5.types
Original file line number Diff line number Diff line change
@@ -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<void>

const packageName = '.';
>packageName : "."
>'.' : "."

const packageJson = await import(packageName + '/package.json');
>packageJson : any
>await import(packageName + '/package.json') : any
>import(packageName + '/package.json') : Promise<any>
>packageName + '/package.json' : string
>packageName : "."
>'/package.json' : "/package.json"
}

Original file line number Diff line number Diff line change
@@ -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');
}

0 comments on commit 1c2ff00

Please sign in to comment.