Skip to content

Do not emit this for helpers at the top level of a strict-mode source file #32823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions src/compiler/transformers/es2017.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ namespace ts {
/** A set of node IDs for generated super accessors (variable statements). */
const substitutedSuperAccessors: boolean[] = [];

let topLevel: boolean;

// Save the previous transformation hooks.
const previousOnEmitNode = context.onEmitNode;
const previousOnSubstituteNode = context.onSubstituteNode;
Expand All @@ -56,11 +58,26 @@ namespace ts {
return node;
}

topLevel = isEffectiveStrictModeSourceFile(node, compilerOptions);
const visited = visitEachChild(node, visitor, context);
addEmitHelpers(visited, context.readEmitHelpers());
return visited;
}

function doOutsideOfTopLevel<T, U>(cb: (value: T) => U, value: T) {
if (topLevel) {
topLevel = false;
const result = cb(value);
topLevel = true;
return result;
}
return cb(value);
}

function visitDefault(node: Node): VisitResult<Node> {
return visitEachChild(node, visitor, context);
}

function visitor(node: Node): VisitResult<Node> {
if ((node.transformFlags & TransformFlags.ContainsES2017) === 0) {
return node;
Expand All @@ -74,13 +91,13 @@ namespace ts {
return visitAwaitExpression(<AwaitExpression>node);

case SyntaxKind.MethodDeclaration:
return visitMethodDeclaration(<MethodDeclaration>node);
return doOutsideOfTopLevel(visitMethodDeclaration, <MethodDeclaration>node);

case SyntaxKind.FunctionDeclaration:
return visitFunctionDeclaration(<FunctionDeclaration>node);
return doOutsideOfTopLevel(visitFunctionDeclaration, <FunctionDeclaration>node);

case SyntaxKind.FunctionExpression:
return visitFunctionExpression(<FunctionExpression>node);
return doOutsideOfTopLevel(visitFunctionExpression, <FunctionExpression>node);

case SyntaxKind.ArrowFunction:
return visitArrowFunction(<ArrowFunction>node);
Expand All @@ -97,6 +114,13 @@ namespace ts {
}
return visitEachChild(node, visitor, context);

case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
case SyntaxKind.Constructor:
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
return doOutsideOfTopLevel(visitDefault, node);

default:
return visitEachChild(node, visitor, context);
}
Expand Down Expand Up @@ -433,6 +457,7 @@ namespace ts {
createReturn(
createAwaiterHelper(
context,
!topLevel,
hasLexicalArguments,
promiseConstructor,
transformAsyncFunctionBodyWorker(<Block>node.body, statementOffset)
Expand Down Expand Up @@ -473,6 +498,7 @@ namespace ts {
else {
const expression = createAwaiterHelper(
context,
!topLevel,
hasLexicalArguments,
promiseConstructor,
transformAsyncFunctionBodyWorker(node.body!)
Expand Down Expand Up @@ -786,7 +812,7 @@ namespace ts {
};`
};

function createAwaiterHelper(context: TransformationContext, hasLexicalArguments: boolean, promiseConstructor: EntityName | Expression | undefined, body: Block) {
function createAwaiterHelper(context: TransformationContext, hasLexicalThis: boolean, hasLexicalArguments: boolean, promiseConstructor: EntityName | Expression | undefined, body: Block) {
context.requestEmitHelper(awaiterHelper);

const generatorFunc = createFunctionExpression(
Expand All @@ -806,7 +832,7 @@ namespace ts {
getUnscopedHelperName("__awaiter"),
/*typeArguments*/ undefined,
[
createThis(),
hasLexicalThis ? createThis() : createVoidZero(),
hasLexicalArguments ? createIdentifier("arguments") : createVoidZero(),
promiseConstructor ? createExpressionFromEntityName(promiseConstructor) : createVoidZero(),
generatorFunc
Expand Down
38 changes: 29 additions & 9 deletions src/compiler/transformers/es2018.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace ts {
let enabledSubstitutions: ESNextSubstitutionFlags;
let enclosingFunctionFlags: FunctionFlags;
let enclosingSuperContainerFlags: NodeCheckFlags = 0;
let topLevel: boolean;

/** Keeps track of property names accessed on super (`super.x`) within async functions. */
let capturedSuperProperties: UnderscoreEscapedMap<true>;
Expand All @@ -42,6 +43,7 @@ namespace ts {
}

exportedVariableStatement = false;
topLevel = isEffectiveStrictModeSourceFile(node, compilerOptions);
const visited = visitEachChild(node, visitor, context);
addEmitHelpers(visited, context.readEmitHelpers());
return visited;
Expand All @@ -62,6 +64,20 @@ namespace ts {
return node;
}

function doOutsideOfTopLevel<T, U>(cb: (value: T) => U, value: T) {
if (topLevel) {
topLevel = false;
const result = cb(value);
topLevel = true;
return result;
}
return cb(value);
}

function visitDefault(node: Node): VisitResult<Node> {
return visitEachChild(node, visitor, context);
}

function visitorWorker(node: Node, noDestructuringValue: boolean): VisitResult<Node> {
if ((node.transformFlags & TransformFlags.ContainsES2018) === 0) {
return node;
Expand Down Expand Up @@ -92,17 +108,17 @@ namespace ts {
case SyntaxKind.VoidExpression:
return visitVoidExpression(node as VoidExpression);
case SyntaxKind.Constructor:
return visitConstructorDeclaration(node as ConstructorDeclaration);
return doOutsideOfTopLevel(visitConstructorDeclaration, node as ConstructorDeclaration);
case SyntaxKind.MethodDeclaration:
return visitMethodDeclaration(node as MethodDeclaration);
return doOutsideOfTopLevel(visitMethodDeclaration, node as MethodDeclaration);
case SyntaxKind.GetAccessor:
return visitGetAccessorDeclaration(node as GetAccessorDeclaration);
return doOutsideOfTopLevel(visitGetAccessorDeclaration, node as GetAccessorDeclaration);
case SyntaxKind.SetAccessor:
return visitSetAccessorDeclaration(node as SetAccessorDeclaration);
return doOutsideOfTopLevel(visitSetAccessorDeclaration, node as SetAccessorDeclaration);
case SyntaxKind.FunctionDeclaration:
return visitFunctionDeclaration(node as FunctionDeclaration);
return doOutsideOfTopLevel(visitFunctionDeclaration, node as FunctionDeclaration);
case SyntaxKind.FunctionExpression:
return visitFunctionExpression(node as FunctionExpression);
return doOutsideOfTopLevel(visitFunctionExpression, node as FunctionExpression);
case SyntaxKind.ArrowFunction:
return visitArrowFunction(node as ArrowFunction);
case SyntaxKind.Parameter:
Expand All @@ -121,6 +137,9 @@ namespace ts {
hasSuperElementAccess = true;
}
return visitEachChild(node, visitor, context);
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ClassExpression:
return doOutsideOfTopLevel(visitDefault, node);
default:
return visitEachChild(node, visitor, context);
}
Expand Down Expand Up @@ -754,7 +773,8 @@ namespace ts {
node.body!,
visitLexicalEnvironment(node.body!.statements, visitor, context, statementOffset)
)
)
),
!topLevel
)
);

Expand Down Expand Up @@ -1057,7 +1077,7 @@ namespace ts {
};`
};

function createAsyncGeneratorHelper(context: TransformationContext, generatorFunc: FunctionExpression) {
function createAsyncGeneratorHelper(context: TransformationContext, generatorFunc: FunctionExpression, hasLexicalThis: boolean) {
context.requestEmitHelper(awaitHelper);
context.requestEmitHelper(asyncGeneratorHelper);

Expand All @@ -1068,7 +1088,7 @@ namespace ts {
getUnscopedHelperName("__asyncGenerator"),
/*typeArguments*/ undefined,
[
createThis(),
hasLexicalThis ? createThis() : createVoidZero(),
createIdentifier("arguments"),
generatorFunc
]
Expand Down
37 changes: 37 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,43 @@ namespace ts {
return isExternalModule(node) || compilerOptions.isolatedModules || ((getEmitModuleKind(compilerOptions) === ModuleKind.CommonJS) && !!node.commonJsModuleIndicator);
}

/**
* Returns whether the source file will be treated as if it were in strict mode at runtime.
*/
export function isEffectiveStrictModeSourceFile(node: SourceFile, compilerOptions: CompilerOptions) {
// We can only verify strict mode for JS/TS files
switch (node.scriptKind) {
case ScriptKind.JS:
case ScriptKind.TS:
case ScriptKind.JSX:
case ScriptKind.TSX:
break;
default:
return false;
}
// Strict mode does not matter for declaration files.
if (node.isDeclarationFile) {
return false;
}
// If `alwaysStrict` is set, then treat the file as strict.
if (getStrictOptionValue(compilerOptions, "alwaysStrict")) {
return true;
}
// Starting with a "use strict" directive indicates the file is strict.
if (startsWithUseStrict(node.statements)) {
return true;
}
if (isExternalModule(node) || compilerOptions.isolatedModules) {
// ECMAScript Modules are always strict.
if (getEmitModuleKind(compilerOptions) >= ModuleKind.ES2015) {
return true;
}
// Other modules are strict unless otherwise specified.
return !compilerOptions.noImplicitUseStrict;
}
return false;
}

export function isBlockScope(node: Node, parentNode: Node): boolean {
switch (node.kind) {
case SyntaxKind.SourceFile:
Expand Down
15 changes: 7 additions & 8 deletions tests/baselines/reference/asyncAwaitIsolatedModules_es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var _this = this;
Object.defineProperty(exports, "__esModule", { value: true });
var missing_1 = require("missing");
function f0() {
Expand Down Expand Up @@ -110,25 +109,25 @@ var f6 = function () {
return [2 /*return*/];
}); });
};
var f7 = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
var f7 = function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/];
}); }); };
var f8 = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
var f8 = function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/];
}); }); };
var f9 = function () { return __awaiter(_this, void 0, missing_1.MyPromise, function () { return __generator(this, function (_a) {
var f9 = function () { return __awaiter(void 0, void 0, missing_1.MyPromise, function () { return __generator(this, function (_a) {
return [2 /*return*/];
}); }); };
var f10 = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
var f10 = function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/, p];
}); }); };
var f11 = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
var f11 = function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/, mp];
}); }); };
var f12 = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
var f12 = function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
return [2 /*return*/, mp];
}); }); };
var f13 = function () { return __awaiter(_this, void 0, missing_1.MyPromise, function () { return __generator(this, function (_a) {
var f13 = function () { return __awaiter(void 0, void 0, missing_1.MyPromise, function () { return __generator(this, function (_a) {
return [2 /*return*/, p];
}); }); };
var o = {
Expand Down
14 changes: 7 additions & 7 deletions tests/baselines/reference/asyncAwaitIsolatedModules_es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ let f5 = function () {
let f6 = function () {
return __awaiter(this, void 0, void 0, function* () { });
};
let f7 = () => __awaiter(this, void 0, void 0, function* () { });
let f8 = () => __awaiter(this, void 0, void 0, function* () { });
let f9 = () => __awaiter(this, void 0, void 0, function* () { });
let f10 = () => __awaiter(this, void 0, void 0, function* () { return p; });
let f11 = () => __awaiter(this, void 0, void 0, function* () { return mp; });
let f12 = () => __awaiter(this, void 0, void 0, function* () { return mp; });
let f13 = () => __awaiter(this, void 0, void 0, function* () { return p; });
let f7 = () => __awaiter(void 0, void 0, void 0, function* () { });
let f8 = () => __awaiter(void 0, void 0, void 0, function* () { });
let f9 = () => __awaiter(void 0, void 0, void 0, function* () { });
let f10 = () => __awaiter(void 0, void 0, void 0, function* () { return p; });
let f11 = () => __awaiter(void 0, void 0, void 0, function* () { return mp; });
let f12 = () => __awaiter(void 0, void 0, void 0, function* () { return mp; });
let f13 = () => __awaiter(void 0, void 0, void 0, function* () { return p; });
let o = {
m1() {
return __awaiter(this, void 0, void 0, function* () { });
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/asyncFunctionsAcrossFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
import { a } from './a';
export const b = {
f: () => __awaiter(this, void 0, void 0, function* () {
f: () => __awaiter(void 0, void 0, void 0, function* () {
yield a.f();
})
};
Expand All @@ -43,7 +43,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
import { b } from './b';
export const a = {
f: () => __awaiter(this, void 0, void 0, function* () {
f: () => __awaiter(void 0, void 0, void 0, function* () {
yield b.f();
})
};
2 changes: 1 addition & 1 deletion tests/baselines/reference/exportDefaultAsyncFunction2.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
export default () => __awaiter(this, void 0, void 0, function* () { return 0; });
export default () => __awaiter(void 0, void 0, void 0, function* () { return 0; });
//// [c.js]
import { async } from 'asyncawait';
export default async();
Expand Down
5 changes: 2 additions & 3 deletions tests/baselines/reference/importCallExpressionAsyncES3AMD.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
};
define(["require", "exports"], function (require, exports) {
"use strict";
var _this = this;
exports.__esModule = true;
function fn() {
return __awaiter(this, void 0, void 0, function () {
Expand Down Expand Up @@ -105,7 +104,7 @@ define(["require", "exports"], function (require, exports) {
}());
exports.cl1 = cl1;
exports.obj = {
m: function () { return __awaiter(_this, void 0, void 0, function () {
m: function () { return __awaiter(void 0, void 0, void 0, function () {
var req;
return __generator(this, function (_a) {
switch (_a.label) {
Expand Down Expand Up @@ -139,7 +138,7 @@ define(["require", "exports"], function (require, exports) {
return cl2;
}());
exports.cl2 = cl2;
exports.l = function () { return __awaiter(_this, void 0, void 0, function () {
exports.l = function () { return __awaiter(void 0, void 0, void 0, function () {
var req;
return __generator(this, function (_a) {
switch (_a.label) {
Expand Down
5 changes: 2 additions & 3 deletions tests/baselines/reference/importCallExpressionAsyncES3CJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var _this = this;
exports.__esModule = true;
function fn() {
return __awaiter(this, void 0, void 0, function () {
Expand Down Expand Up @@ -104,7 +103,7 @@ var cl1 = /** @class */ (function () {
}());
exports.cl1 = cl1;
exports.obj = {
m: function () { return __awaiter(_this, void 0, void 0, function () {
m: function () { return __awaiter(void 0, void 0, void 0, function () {
var req;
return __generator(this, function (_a) {
switch (_a.label) {
Expand Down Expand Up @@ -138,7 +137,7 @@ var cl2 = /** @class */ (function () {
return cl2;
}());
exports.cl2 = cl2;
exports.l = function () { return __awaiter(_this, void 0, void 0, function () {
exports.l = function () { return __awaiter(void 0, void 0, void 0, function () {
var req;
return __generator(this, function (_a) {
switch (_a.label) {
Expand Down
Loading