Skip to content

Commit d0de238

Browse files
committed
Merge pull request #5765 from weswigham/emit-module-in-strict-mode
Emit modules in strict mode
2 parents 2a21558 + 27149f3 commit d0de238

File tree

1,454 files changed

+17438
-15720
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,454 files changed

+17438
-15720
lines changed

src/compiler/emitter.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7383,7 +7383,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
73837383
write(`], function(${exportFunctionForFile}) {`);
73847384
writeLine();
73857385
increaseIndent();
7386-
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true);
7386+
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true, /*ensureUseStrict*/ true);
73877387
emitEmitHelpers(node);
73887388
emitCaptureThisForNodeIfNecessary(node);
73897389
emitSystemModuleBody(node, dependencyGroups, startIndex);
@@ -7493,7 +7493,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
74937493
writeModuleName(node, emitRelativePathAsModuleName);
74947494
emitAMDDependencies(node, /*includeNonAmdDependencies*/ true, emitRelativePathAsModuleName);
74957495
increaseIndent();
7496-
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true);
7496+
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true, /*ensureUseStrict*/ true);
74977497
emitExportStarHelper();
74987498
emitCaptureThisForNodeIfNecessary(node);
74997499
emitLinesStartingAt(node.statements, startIndex);
@@ -7505,7 +7505,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
75057505
}
75067506

75077507
function emitCommonJSModule(node: SourceFile) {
7508-
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false);
7508+
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false, /*ensureUseStrict*/ true);
75097509
emitEmitHelpers(node);
75107510
collectExternalModuleInfo(node);
75117511
emitExportStarHelper();
@@ -7534,7 +7534,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
75347534
})(`);
75357535
emitAMDFactoryHeader(dependencyNames);
75367536
increaseIndent();
7537-
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true);
7537+
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true, /*ensureUseStrict*/ true);
75387538
emitExportStarHelper();
75397539
emitCaptureThisForNodeIfNecessary(node);
75407540
emitLinesStartingAt(node.statements, startIndex);
@@ -7676,19 +7676,38 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
76767676
}
76777677
}
76787678

7679-
function emitDirectivePrologues(statements: Node[], startWithNewLine: boolean): number {
7679+
function isUseStrictPrologue(node: ExpressionStatement): boolean {
7680+
return !!(node.expression as StringLiteral).text.match(/use strict/);
7681+
}
7682+
7683+
function ensureUseStrictPrologue(startWithNewLine: boolean, writeUseStrict: boolean) {
7684+
if (writeUseStrict) {
7685+
if (startWithNewLine) {
7686+
writeLine();
7687+
}
7688+
write("\"use strict\";");
7689+
}
7690+
}
7691+
7692+
function emitDirectivePrologues(statements: Node[], startWithNewLine: boolean, ensureUseStrict?: boolean): number {
7693+
let foundUseStrict = false;
76807694
for (let i = 0; i < statements.length; ++i) {
76817695
if (isPrologueDirective(statements[i])) {
7696+
if (isUseStrictPrologue(statements[i] as ExpressionStatement)) {
7697+
foundUseStrict = true;
7698+
}
76827699
if (startWithNewLine || i > 0) {
76837700
writeLine();
76847701
}
76857702
emit(statements[i]);
76867703
}
76877704
else {
7705+
ensureUseStrictPrologue(startWithNewLine || i > 0, !foundUseStrict && ensureUseStrict);
76887706
// return index of the first non prologue directive
76897707
return i;
76907708
}
76917709
}
7710+
ensureUseStrictPrologue(startWithNewLine, !foundUseStrict && ensureUseStrict);
76927711
return statements.length;
76937712
}
76947713

tests/baselines/reference/APISample_compile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ compile(process.argv.slice(2), {
4040
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler
4141
* Please log a "breaking change" issue for any API breaking change affecting this issue
4242
*/
43+
"use strict";
4344
var ts = require("typescript");
4445
function compile(fileNames, options) {
4546
var program = ts.createProgram(fileNames, options);

tests/baselines/reference/APISample_linter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ fileNames.forEach(fileName => {
7070
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#traversing-the-ast-with-a-little-linter
7171
* Please log a "breaking change" issue for any API breaking change affecting this issue
7272
*/
73+
"use strict";
7374
var ts = require("typescript");
7475
function delint(sourceFile) {
7576
delintNode(sourceFile);

tests/baselines/reference/APISample_transform.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ console.log(JSON.stringify(result));
2222
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-simple-transform-function
2323
* Please log a "breaking change" issue for any API breaking change affecting this issue
2424
*/
25+
"use strict";
2526
var ts = require("typescript");
2627
var source = "let x: string = 'string'";
2728
var result = ts.transpile(source, { module: ts.ModuleKind.CommonJS });

tests/baselines/reference/APISample_watcher.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ watch(currentDirectoryFiles, { module: ts.ModuleKind.CommonJS });
109109
at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#incremental-build-support-using-the-language-services
110110
* Please log a "breaking change" issue for any API breaking change affecting this issue
111111
*/
112+
"use strict";
112113
var ts = require("typescript");
113114
function watch(rootFileNames, options) {
114115
var files = {};

tests/baselines/reference/ExportAssignment7.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export class C {
55
export = B;
66

77
//// [ExportAssignment7.js]
8+
"use strict";
89
var C = (function () {
910
function C() {
1011
}

tests/baselines/reference/ExportAssignment8.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export class C {
55
}
66

77
//// [ExportAssignment8.js]
8+
"use strict";
89
var C = (function () {
910
function C() {
1011
}

tests/baselines/reference/TwoInternalModulesThatMergeEachWithExportedLocalVarsOfTheSameName.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export module A {
3030

3131

3232
//// [part1.js]
33+
"use strict";
3334
var A;
3435
(function (A) {
3536
var Utils;
@@ -42,6 +43,7 @@ var A;
4243
A.Origin = { x: 0, y: 0 };
4344
})(A = exports.A || (exports.A = {}));
4445
//// [part2.js]
46+
"use strict";
4547
var A;
4648
(function (A) {
4749
// collision with 'Origin' var in other part of merged module

tests/baselines/reference/aliasAssignments.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ y = moduleA; // should be error
1414

1515

1616
//// [aliasAssignments_moduleA.js]
17+
"use strict";
1718
var someClass = (function () {
1819
function someClass() {
1920
}
2021
return someClass;
2122
})();
2223
exports.someClass = someClass;
2324
//// [aliasAssignments_1.js]
25+
"use strict";
2426
var moduleA = require("./aliasAssignments_moduleA");
2527
var x = moduleA;
2628
x = 1; // Should be error

tests/baselines/reference/aliasOnMergedModuleInterface.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var x: foo.A = foo.bar("hello"); // foo.A should be ok but foo.bar should be err
2323

2424
//// [aliasOnMergedModuleInterface_0.js]
2525
//// [aliasOnMergedModuleInterface_1.js]
26+
"use strict";
2627
var z;
2728
z.bar("hello"); // This should be ok
2829
var x = foo.bar("hello"); // foo.A should be ok but foo.bar should be error

tests/baselines/reference/aliasUsageInAccessorsOfClass.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ class C2 {
2828
}
2929

3030
//// [aliasUsage1_backbone.js]
31+
"use strict";
3132
var Model = (function () {
3233
function Model() {
3334
}
3435
return Model;
3536
})();
3637
exports.Model = Model;
3738
//// [aliasUsage1_moduleA.js]
39+
"use strict";
3840
var __extends = (this && this.__extends) || function (d, b) {
3941
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
4042
function __() { this.constructor = d; }
@@ -50,6 +52,7 @@ var VisualizationModel = (function (_super) {
5052
})(Backbone.Model);
5153
exports.VisualizationModel = VisualizationModel;
5254
//// [aliasUsage1_main.js]
55+
"use strict";
5356
var moduleA = require("./aliasUsage1_moduleA");
5457
var C2 = (function () {
5558
function C2() {

tests/baselines/reference/aliasUsageInArray.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ var xs: IHasVisualizationModel[] = [moduleA];
2222
var xs2: typeof moduleA[] = [moduleA];
2323

2424
//// [aliasUsageInArray_backbone.js]
25+
"use strict";
2526
var Model = (function () {
2627
function Model() {
2728
}
2829
return Model;
2930
})();
3031
exports.Model = Model;
3132
//// [aliasUsageInArray_moduleA.js]
33+
"use strict";
3234
var __extends = (this && this.__extends) || function (d, b) {
3335
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
3436
function __() { this.constructor = d; }
@@ -44,6 +46,7 @@ var VisualizationModel = (function (_super) {
4446
})(Backbone.Model);
4547
exports.VisualizationModel = VisualizationModel;
4648
//// [aliasUsageInArray_main.js]
49+
"use strict";
4750
var moduleA = require("./aliasUsageInArray_moduleA");
4851
var xs = [moduleA];
4952
var xs2 = [moduleA];

tests/baselines/reference/aliasUsageInFunctionExpression.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ var f = (x: IHasVisualizationModel) => x;
2121
f = (x) => moduleA;
2222

2323
//// [aliasUsageInFunctionExpression_backbone.js]
24+
"use strict";
2425
var Model = (function () {
2526
function Model() {
2627
}
2728
return Model;
2829
})();
2930
exports.Model = Model;
3031
//// [aliasUsageInFunctionExpression_moduleA.js]
32+
"use strict";
3133
var __extends = (this && this.__extends) || function (d, b) {
3234
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
3335
function __() { this.constructor = d; }
@@ -43,6 +45,7 @@ var VisualizationModel = (function (_super) {
4345
})(Backbone.Model);
4446
exports.VisualizationModel = VisualizationModel;
4547
//// [aliasUsageInFunctionExpression_main.js]
48+
"use strict";
4649
var moduleA = require("./aliasUsageInFunctionExpression_moduleA");
4750
var f = function (x) { return x; };
4851
f = function (x) { return moduleA; };

tests/baselines/reference/aliasUsageInGenericFunction.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ var r2 = foo({ a: <IHasVisualizationModel>null });
2525

2626

2727
//// [aliasUsageInGenericFunction_backbone.js]
28+
"use strict";
2829
var Model = (function () {
2930
function Model() {
3031
}
3132
return Model;
3233
})();
3334
exports.Model = Model;
3435
//// [aliasUsageInGenericFunction_moduleA.js]
36+
"use strict";
3537
var __extends = (this && this.__extends) || function (d, b) {
3638
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
3739
function __() { this.constructor = d; }
@@ -47,6 +49,7 @@ var VisualizationModel = (function (_super) {
4749
})(Backbone.Model);
4850
exports.VisualizationModel = VisualizationModel;
4951
//// [aliasUsageInGenericFunction_main.js]
52+
"use strict";
5053
var moduleA = require("./aliasUsageInGenericFunction_moduleA");
5154
function foo(x) {
5255
return x;

tests/baselines/reference/aliasUsageInIndexerOfClass.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ class N2 {
2727
}
2828

2929
//// [aliasUsageInIndexerOfClass_backbone.js]
30+
"use strict";
3031
var Model = (function () {
3132
function Model() {
3233
}
3334
return Model;
3435
})();
3536
exports.Model = Model;
3637
//// [aliasUsageInIndexerOfClass_moduleA.js]
38+
"use strict";
3739
var __extends = (this && this.__extends) || function (d, b) {
3840
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
3941
function __() { this.constructor = d; }
@@ -49,6 +51,7 @@ var VisualizationModel = (function (_super) {
4951
})(Backbone.Model);
5052
exports.VisualizationModel = VisualizationModel;
5153
//// [aliasUsageInIndexerOfClass_main.js]
54+
"use strict";
5255
var moduleA = require("./aliasUsageInIndexerOfClass_moduleA");
5356
var N = (function () {
5457
function N() {

tests/baselines/reference/aliasUsageInObjectLiteral.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ var b: { x: IHasVisualizationModel } = { x: moduleA };
2222
var c: { y: { z: IHasVisualizationModel } } = { y: { z: moduleA } };
2323

2424
//// [aliasUsageInObjectLiteral_backbone.js]
25+
"use strict";
2526
var Model = (function () {
2627
function Model() {
2728
}
2829
return Model;
2930
})();
3031
exports.Model = Model;
3132
//// [aliasUsageInObjectLiteral_moduleA.js]
33+
"use strict";
3234
var __extends = (this && this.__extends) || function (d, b) {
3335
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
3436
function __() { this.constructor = d; }
@@ -44,6 +46,7 @@ var VisualizationModel = (function (_super) {
4446
})(Backbone.Model);
4547
exports.VisualizationModel = VisualizationModel;
4648
//// [aliasUsageInObjectLiteral_main.js]
49+
"use strict";
4750
var moduleA = require("./aliasUsageInObjectLiteral_moduleA");
4851
var a = { x: moduleA };
4952
var b = { x: moduleA };

tests/baselines/reference/aliasUsageInOrExpression.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ var e: { x: IHasVisualizationModel } = <{ x: IHasVisualizationModel }>null || {
2525
var f: { x: IHasVisualizationModel } = <{ x: IHasVisualizationModel }>null ? { x: moduleA } : null;
2626

2727
//// [aliasUsageInOrExpression_backbone.js]
28+
"use strict";
2829
var Model = (function () {
2930
function Model() {
3031
}
3132
return Model;
3233
})();
3334
exports.Model = Model;
3435
//// [aliasUsageInOrExpression_moduleA.js]
36+
"use strict";
3537
var __extends = (this && this.__extends) || function (d, b) {
3638
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
3739
function __() { this.constructor = d; }
@@ -47,6 +49,7 @@ var VisualizationModel = (function (_super) {
4749
})(Backbone.Model);
4850
exports.VisualizationModel = VisualizationModel;
4951
//// [aliasUsageInOrExpression_main.js]
52+
"use strict";
5053
var moduleA = require("./aliasUsageInOrExpression_moduleA");
5154
var i;
5255
var d1 = i || moduleA;

tests/baselines/reference/aliasUsageInTypeArgumentOfExtendsClause.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ class D extends C<IHasVisualizationModel> {
2525
}
2626

2727
//// [aliasUsageInTypeArgumentOfExtendsClause_backbone.js]
28+
"use strict";
2829
var Model = (function () {
2930
function Model() {
3031
}
3132
return Model;
3233
})();
3334
exports.Model = Model;
3435
//// [aliasUsageInTypeArgumentOfExtendsClause_moduleA.js]
36+
"use strict";
3537
var __extends = (this && this.__extends) || function (d, b) {
3638
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
3739
function __() { this.constructor = d; }
@@ -47,6 +49,7 @@ var VisualizationModel = (function (_super) {
4749
})(Backbone.Model);
4850
exports.VisualizationModel = VisualizationModel;
4951
//// [aliasUsageInTypeArgumentOfExtendsClause_main.js]
52+
"use strict";
5053
var __extends = (this && this.__extends) || function (d, b) {
5154
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
5255
function __() { this.constructor = d; }

tests/baselines/reference/aliasUsageInVarAssignment.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ var i: IHasVisualizationModel;
2121
var m: typeof moduleA = i;
2222

2323
//// [aliasUsageInVarAssignment_backbone.js]
24+
"use strict";
2425
var Model = (function () {
2526
function Model() {
2627
}
2728
return Model;
2829
})();
2930
exports.Model = Model;
3031
//// [aliasUsageInVarAssignment_moduleA.js]
32+
"use strict";
3133
var __extends = (this && this.__extends) || function (d, b) {
3234
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
3335
function __() { this.constructor = d; }
@@ -43,5 +45,6 @@ var VisualizationModel = (function (_super) {
4345
})(Backbone.Model);
4446
exports.VisualizationModel = VisualizationModel;
4547
//// [aliasUsageInVarAssignment_main.js]
48+
"use strict";
4649
var i;
4750
var m = i;

tests/baselines/reference/aliasUsedAsNameValue.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ export var a = function () {
1919

2020

2121
//// [aliasUsedAsNameValue_0.js]
22+
"use strict";
2223
//// [aliasUsedAsNameValue_1.js]
24+
"use strict";
2325
function b(a) { return null; }
2426
exports.b = b;
2527
//// [aliasUsedAsNameValue_2.js]
28+
"use strict";
2629
///<reference path='aliasUsedAsNameValue_0.ts' />
2730
///<reference path='aliasUsedAsNameValue_1.ts' />
2831
var mod = require("./aliasUsedAsNameValue_0");

0 commit comments

Comments
 (0)