Skip to content

Emit modules in strict mode #5765

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 10 commits into from
Nov 24, 2015
Merged
  •  
  •  
  •  
29 changes: 24 additions & 5 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7383,7 +7383,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
write(`], function(${exportFunctionForFile}) {`);
writeLine();
increaseIndent();
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true);
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true, /*ensureUseStrict*/ true);
emitEmitHelpers(node);
emitCaptureThisForNodeIfNecessary(node);
emitSystemModuleBody(node, dependencyGroups, startIndex);
Expand Down Expand Up @@ -7493,7 +7493,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
writeModuleName(node, emitRelativePathAsModuleName);
emitAMDDependencies(node, /*includeNonAmdDependencies*/ true, emitRelativePathAsModuleName);
increaseIndent();
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true);
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true, /*ensureUseStrict*/ true);
emitExportStarHelper();
emitCaptureThisForNodeIfNecessary(node);
emitLinesStartingAt(node.statements, startIndex);
Expand All @@ -7505,7 +7505,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
}

function emitCommonJSModule(node: SourceFile) {
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false);
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false, /*ensureUseStrict*/ true);
emitEmitHelpers(node);
collectExternalModuleInfo(node);
emitExportStarHelper();
Expand Down Expand Up @@ -7534,7 +7534,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
})(`);
emitAMDFactoryHeader(dependencyNames);
increaseIndent();
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true);
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true, /*ensureUseStrict*/ true);
emitExportStarHelper();
emitCaptureThisForNodeIfNecessary(node);
emitLinesStartingAt(node.statements, startIndex);
Expand Down Expand Up @@ -7676,19 +7676,38 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
}
}

function emitDirectivePrologues(statements: Node[], startWithNewLine: boolean): number {
function isUseStrictPrologue(node: ExpressionStatement): boolean {
return !!(node.expression as StringLiteral).text.match(/use strict/);
}

function ensureUseStrictPrologue(startWithNewLine: boolean, writeUseStrict: boolean) {
if (writeUseStrict) {
if (startWithNewLine) {
writeLine();
}
write("\"use strict\";");
}
}

function emitDirectivePrologues(statements: Node[], startWithNewLine: boolean, ensureUseStrict?: boolean): number {
let foundUseStrict = false;
for (let i = 0; i < statements.length; ++i) {
if (isPrologueDirective(statements[i])) {
if (isUseStrictPrologue(statements[i] as ExpressionStatement)) {
foundUseStrict = true;
}
if (startWithNewLine || i > 0) {
writeLine();
}
emit(statements[i]);
}
else {
ensureUseStrictPrologue(startWithNewLine || i > 0, !foundUseStrict && ensureUseStrict);
// return index of the first non prologue directive
return i;
}
}
ensureUseStrictPrologue(startWithNewLine, !foundUseStrict && ensureUseStrict);
return statements.length;
}

Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/APISample_compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ compile(process.argv.slice(2), {
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
"use strict";
var ts = require("typescript");
function compile(fileNames, options) {
var program = ts.createProgram(fileNames, options);
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/APISample_linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ fileNames.forEach(fileName => {
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#traversing-the-ast-with-a-little-linter
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
"use strict";
var ts = require("typescript");
function delint(sourceFile) {
delintNode(sourceFile);
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/APISample_transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ console.log(JSON.stringify(result));
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-simple-transform-function
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
"use strict";
var ts = require("typescript");
var source = "let x: string = 'string'";
var result = ts.transpile(source, { module: ts.ModuleKind.CommonJS });
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/APISample_watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ watch(currentDirectoryFiles, { module: ts.ModuleKind.CommonJS });
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#incremental-build-support-using-the-language-services
* Please log a "breaking change" issue for any API breaking change affecting this issue
*/
"use strict";
var ts = require("typescript");
function watch(rootFileNames, options) {
var files = {};
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/ExportAssignment7.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export class C {
export = B;

//// [ExportAssignment7.js]
"use strict";
var C = (function () {
function C() {
}
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/ExportAssignment8.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export class C {
}

//// [ExportAssignment8.js]
"use strict";
var C = (function () {
function C() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export module A {


//// [part1.js]
"use strict";
var A;
(function (A) {
var Utils;
Expand All @@ -42,6 +43,7 @@ var A;
A.Origin = { x: 0, y: 0 };
})(A = exports.A || (exports.A = {}));
//// [part2.js]
"use strict";
var A;
(function (A) {
// collision with 'Origin' var in other part of merged module
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/aliasAssignments.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ y = moduleA; // should be error


//// [aliasAssignments_moduleA.js]
"use strict";
var someClass = (function () {
function someClass() {
}
return someClass;
})();
exports.someClass = someClass;
//// [aliasAssignments_1.js]
"use strict";
var moduleA = require("./aliasAssignments_moduleA");
var x = moduleA;
x = 1; // Should be error
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/aliasOnMergedModuleInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var x: foo.A = foo.bar("hello"); // foo.A should be ok but foo.bar should be err

//// [aliasOnMergedModuleInterface_0.js]
//// [aliasOnMergedModuleInterface_1.js]
"use strict";
var z;
z.bar("hello"); // This should be ok
var x = foo.bar("hello"); // foo.A should be ok but foo.bar should be error
3 changes: 3 additions & 0 deletions tests/baselines/reference/aliasUsageInAccessorsOfClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ class C2 {
}

//// [aliasUsage1_backbone.js]
"use strict";
var Model = (function () {
function Model() {
}
return Model;
})();
exports.Model = Model;
//// [aliasUsage1_moduleA.js]
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
Expand All @@ -50,6 +52,7 @@ var VisualizationModel = (function (_super) {
})(Backbone.Model);
exports.VisualizationModel = VisualizationModel;
//// [aliasUsage1_main.js]
"use strict";
var moduleA = require("./aliasUsage1_moduleA");
var C2 = (function () {
function C2() {
Expand Down
3 changes: 3 additions & 0 deletions tests/baselines/reference/aliasUsageInArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ var xs: IHasVisualizationModel[] = [moduleA];
var xs2: typeof moduleA[] = [moduleA];

//// [aliasUsageInArray_backbone.js]
"use strict";
var Model = (function () {
function Model() {
}
return Model;
})();
exports.Model = Model;
//// [aliasUsageInArray_moduleA.js]
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
Expand All @@ -44,6 +46,7 @@ var VisualizationModel = (function (_super) {
})(Backbone.Model);
exports.VisualizationModel = VisualizationModel;
//// [aliasUsageInArray_main.js]
"use strict";
var moduleA = require("./aliasUsageInArray_moduleA");
var xs = [moduleA];
var xs2 = [moduleA];
3 changes: 3 additions & 0 deletions tests/baselines/reference/aliasUsageInFunctionExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ var f = (x: IHasVisualizationModel) => x;
f = (x) => moduleA;

//// [aliasUsageInFunctionExpression_backbone.js]
"use strict";
var Model = (function () {
function Model() {
}
return Model;
})();
exports.Model = Model;
//// [aliasUsageInFunctionExpression_moduleA.js]
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
Expand All @@ -43,6 +45,7 @@ var VisualizationModel = (function (_super) {
})(Backbone.Model);
exports.VisualizationModel = VisualizationModel;
//// [aliasUsageInFunctionExpression_main.js]
"use strict";
var moduleA = require("./aliasUsageInFunctionExpression_moduleA");
var f = function (x) { return x; };
f = function (x) { return moduleA; };
3 changes: 3 additions & 0 deletions tests/baselines/reference/aliasUsageInGenericFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ var r2 = foo({ a: <IHasVisualizationModel>null });


//// [aliasUsageInGenericFunction_backbone.js]
"use strict";
var Model = (function () {
function Model() {
}
return Model;
})();
exports.Model = Model;
//// [aliasUsageInGenericFunction_moduleA.js]
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
Expand All @@ -47,6 +49,7 @@ var VisualizationModel = (function (_super) {
})(Backbone.Model);
exports.VisualizationModel = VisualizationModel;
//// [aliasUsageInGenericFunction_main.js]
"use strict";
var moduleA = require("./aliasUsageInGenericFunction_moduleA");
function foo(x) {
return x;
Expand Down
3 changes: 3 additions & 0 deletions tests/baselines/reference/aliasUsageInIndexerOfClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ class N2 {
}

//// [aliasUsageInIndexerOfClass_backbone.js]
"use strict";
var Model = (function () {
function Model() {
}
return Model;
})();
exports.Model = Model;
//// [aliasUsageInIndexerOfClass_moduleA.js]
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
Expand All @@ -49,6 +51,7 @@ var VisualizationModel = (function (_super) {
})(Backbone.Model);
exports.VisualizationModel = VisualizationModel;
//// [aliasUsageInIndexerOfClass_main.js]
"use strict";
var moduleA = require("./aliasUsageInIndexerOfClass_moduleA");
var N = (function () {
function N() {
Expand Down
3 changes: 3 additions & 0 deletions tests/baselines/reference/aliasUsageInObjectLiteral.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ var b: { x: IHasVisualizationModel } = { x: moduleA };
var c: { y: { z: IHasVisualizationModel } } = { y: { z: moduleA } };

//// [aliasUsageInObjectLiteral_backbone.js]
"use strict";
var Model = (function () {
function Model() {
}
return Model;
})();
exports.Model = Model;
//// [aliasUsageInObjectLiteral_moduleA.js]
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
Expand All @@ -44,6 +46,7 @@ var VisualizationModel = (function (_super) {
})(Backbone.Model);
exports.VisualizationModel = VisualizationModel;
//// [aliasUsageInObjectLiteral_main.js]
"use strict";
var moduleA = require("./aliasUsageInObjectLiteral_moduleA");
var a = { x: moduleA };
var b = { x: moduleA };
Expand Down
3 changes: 3 additions & 0 deletions tests/baselines/reference/aliasUsageInOrExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ var e: { x: IHasVisualizationModel } = <{ x: IHasVisualizationModel }>null || {
var f: { x: IHasVisualizationModel } = <{ x: IHasVisualizationModel }>null ? { x: moduleA } : null;

//// [aliasUsageInOrExpression_backbone.js]
"use strict";
var Model = (function () {
function Model() {
}
return Model;
})();
exports.Model = Model;
//// [aliasUsageInOrExpression_moduleA.js]
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
Expand All @@ -47,6 +49,7 @@ var VisualizationModel = (function (_super) {
})(Backbone.Model);
exports.VisualizationModel = VisualizationModel;
//// [aliasUsageInOrExpression_main.js]
"use strict";
var moduleA = require("./aliasUsageInOrExpression_moduleA");
var i;
var d1 = i || moduleA;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ class D extends C<IHasVisualizationModel> {
}

//// [aliasUsageInTypeArgumentOfExtendsClause_backbone.js]
"use strict";
var Model = (function () {
function Model() {
}
return Model;
})();
exports.Model = Model;
//// [aliasUsageInTypeArgumentOfExtendsClause_moduleA.js]
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
Expand All @@ -47,6 +49,7 @@ var VisualizationModel = (function (_super) {
})(Backbone.Model);
exports.VisualizationModel = VisualizationModel;
//// [aliasUsageInTypeArgumentOfExtendsClause_main.js]
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
Expand Down
3 changes: 3 additions & 0 deletions tests/baselines/reference/aliasUsageInVarAssignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ var i: IHasVisualizationModel;
var m: typeof moduleA = i;

//// [aliasUsageInVarAssignment_backbone.js]
"use strict";
var Model = (function () {
function Model() {
}
return Model;
})();
exports.Model = Model;
//// [aliasUsageInVarAssignment_moduleA.js]
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
Expand All @@ -43,5 +45,6 @@ var VisualizationModel = (function (_super) {
})(Backbone.Model);
exports.VisualizationModel = VisualizationModel;
//// [aliasUsageInVarAssignment_main.js]
"use strict";
var i;
var m = i;
3 changes: 3 additions & 0 deletions tests/baselines/reference/aliasUsedAsNameValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ export var a = function () {


//// [aliasUsedAsNameValue_0.js]
"use strict";
//// [aliasUsedAsNameValue_1.js]
"use strict";
function b(a) { return null; }
exports.b = b;
//// [aliasUsedAsNameValue_2.js]
"use strict";
///<reference path='aliasUsedAsNameValue_0.ts' />
///<reference path='aliasUsedAsNameValue_1.ts' />
var mod = require("./aliasUsedAsNameValue_0");
Expand Down
Loading