Skip to content

Commit 10522f9

Browse files
committed
Add flag to emit modules in loose mode
1 parent 1154ab8 commit 10522f9

24 files changed

Lines changed: 158 additions & 20 deletions

src/compiler/commandLineParser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ namespace ts {
320320
name: "allowSyntheticDefaultImports",
321321
type: "boolean",
322322
description: Diagnostics.Allow_default_imports_from_modules_with_no_default_export_This_does_not_affect_code_emit_just_typechecking
323+
},
324+
{
325+
name: "emitModulesInLooseMode",
326+
type: "boolean",
327+
description: Diagnostics.Do_not_emit_use_strict_directives_in_module_output
323328
}
324329
];
325330

src/compiler/diagnosticMessages.json

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"Unterminated string literal.": {
33
"category": "Error",
44
"code": 1002
@@ -2187,6 +2187,7 @@
21872187
"category": "Error",
21882188
"code": 5062
21892189
},
2190+
21902191
"Concatenate and emit output to single file.": {
21912192
"category": "Message",
21922193
"code": 6001
@@ -2231,10 +2232,10 @@
22312232
"category": "Message",
22322233
"code": 6011
22332234
},
2234-
"Specify ECMAScript target version: 'ES3' (default), 'ES5', or 'ES2015' (experimental)": {
2235-
"category": "Message",
2236-
"code": 6015
2237-
},
2235+
"Specify ECMAScript target version: 'ES3' (default), 'ES5', or 'ES2015' (experimental)": {
2236+
"category": "Message",
2237+
"code": 6015
2238+
},
22382239
"Specify module code generation: 'commonjs', 'amd', 'system', 'umd' or 'es2015'": {
22392240
"category": "Message",
22402241
"code": 6016
@@ -2571,6 +2572,11 @@
25712572
"category": "Message",
25722573
"code": 6111
25732574
},
2575+
"Do not emit 'use strict' directives in module output.": {
2576+
"category": "Message",
2577+
"code": 6112
2578+
},
2579+
25742580
"Variable '{0}' implicitly has an '{1}' type.": {
25752581
"category": "Error",
25762582
"code": 7005
@@ -2757,23 +2763,23 @@
27572763
"code": 17004
27582764
},
27592765
"A constructor cannot contain a 'super' call when its class extends 'null'": {
2760-
"category": "Error",
2761-
"code": 17005
2766+
"category": "Error",
2767+
"code": 17005
27622768
},
27632769
"An unary expression with the '{0}' operator is not allowed in the left-hand side of an exponentiation expression. Consider enclosing the expression in parentheses.": {
2764-
"category": "Error",
2765-
"code": 17006
2770+
"category": "Error",
2771+
"code": 17006
27662772
},
27672773
"A type assertion expression is not allowed in the left-hand side of an exponentiation expression. Consider enclosing the expression in parentheses.": {
2768-
"category": "Error",
2769-
"code": 17007
2774+
"category": "Error",
2775+
"code": 17007
27702776
},
27712777
"JSX element '{0}' has no corresponding closing tag.": {
2772-
"category": "Error",
2773-
"code": 17008
2778+
"category": "Error",
2779+
"code": 17008
27742780
},
27752781
"'super' must be called before accessing 'this' in the constructor of a derived class.": {
2776-
"category": "Error",
2777-
"code": 17009
2782+
"category": "Error",
2783+
"code": 17009
27782784
}
2779-
}
2785+
}

src/compiler/emitter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7179,7 +7179,7 @@ const _super = (function (geti, seti) {
71797179
write(`], function(${exportFunctionForFile}, ${contextObjectForFile}) {`);
71807180
writeLine();
71817181
increaseIndent();
7182-
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true, /*ensureUseStrict*/ true);
7182+
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true, /*ensureUseStrict*/ !compilerOptions.emitModulesInLooseMode);
71837183
writeLine();
71847184
write(`var __moduleName = ${contextObjectForFile} && ${contextObjectForFile}.id;`);
71857185
writeLine();
@@ -7285,7 +7285,7 @@ const _super = (function (geti, seti) {
72857285
writeModuleName(node, emitRelativePathAsModuleName);
72867286
emitAMDDependencies(node, /*includeNonAmdDependencies*/ true, emitRelativePathAsModuleName);
72877287
increaseIndent();
7288-
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true, /*ensureUseStrict*/ true);
7288+
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true, /*ensureUseStrict*/!compilerOptions.emitModulesInLooseMode);
72897289
emitExportStarHelper();
72907290
emitCaptureThisForNodeIfNecessary(node);
72917291
emitLinesStartingAt(node.statements, startIndex);
@@ -7297,7 +7297,7 @@ const _super = (function (geti, seti) {
72977297
}
72987298

72997299
function emitCommonJSModule(node: SourceFile) {
7300-
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false, /*ensureUseStrict*/ true);
7300+
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false, /*ensureUseStrict*/ !compilerOptions.emitModulesInLooseMode);
73017301
emitEmitHelpers(node);
73027302
collectExternalModuleInfo(node);
73037303
emitExportStarHelper();
@@ -7326,7 +7326,7 @@ const _super = (function (geti, seti) {
73267326
})(`);
73277327
emitAMDFactoryHeader(dependencyNames);
73287328
increaseIndent();
7329-
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true, /*ensureUseStrict*/ true);
7329+
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true, /*ensureUseStrict*/ !compilerOptions.emitModulesInLooseMode);
73307330
emitExportStarHelper();
73317331
emitCaptureThisForNodeIfNecessary(node);
73327332
emitLinesStartingAt(node.statements, startIndex);

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,6 +2422,7 @@ namespace ts {
24222422
traceModuleResolution?: boolean;
24232423
allowSyntheticDefaultImports?: boolean;
24242424
allowJs?: boolean;
2425+
emitModulesInLooseMode?: boolean;
24252426
/* @internal */ stripInternal?: boolean;
24262427

24272428
// Skip checking lib.d.ts to help speed up tests.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [emitModulesInLooseMode_amd.ts]
2+
3+
export var x = 0;
4+
5+
//// [emitModulesInLooseMode_amd.js]
6+
define(["require", "exports"], function (require, exports) {
7+
exports.x = 0;
8+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/compiler/emitModulesInLooseMode_amd.ts ===
2+
3+
export var x = 0;
4+
>x : Symbol(x, Decl(emitModulesInLooseMode_amd.ts, 1, 10))
5+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/emitModulesInLooseMode_amd.ts ===
2+
3+
export var x = 0;
4+
>x : number
5+
>0 : number
6+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [emitModulesInLooseMode_commonjs.ts]
2+
3+
export var x = 0;
4+
5+
//// [emitModulesInLooseMode_commonjs.js]
6+
exports.x = 0;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/compiler/emitModulesInLooseMode_commonjs.ts ===
2+
3+
export var x = 0;
4+
>x : Symbol(x, Decl(emitModulesInLooseMode_commonjs.ts, 1, 10))
5+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/emitModulesInLooseMode_commonjs.ts ===
2+
3+
export var x = 0;
4+
>x : number
5+
>0 : number
6+

0 commit comments

Comments
 (0)