Skip to content

Commit 6ba4b87

Browse files
authored
Merge pull request #12134 from Microsoft/release-2.1_fix11806_omitUseStrict
[Release 2.1] fix11806 omit use strict
2 parents 34d41de + d629bd6 commit 6ba4b87

17 files changed

+111
-1
lines changed

src/compiler/transformers/ts.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,11 @@ namespace ts {
457457
currentSourceFile = node;
458458

459459
// ensure "use strict" is emitted in all scenarios in alwaysStrict mode
460-
if (compilerOptions.alwaysStrict) {
460+
// There is no need to emit "use strict" in the following cases:
461+
// 1. The file is an external module and target is es2015 or higher
462+
// or 2. The file is an external module and module-kind is es6 or es2015 as such value is not allowed when targeting es5 or lower
463+
if (compilerOptions.alwaysStrict &&
464+
!(isExternalModule(node) && (compilerOptions.target >= ScriptTarget.ES2015 || compilerOptions.module === ModuleKind.ES2015))) {
461465
node = ensureUseStrict(node);
462466
}
463467

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [alwaysStrictModule3.ts]
2+
3+
// module ES2015
4+
export const a = 1;
5+
6+
//// [alwaysStrictModule3.js]
7+
// module ES2015
8+
export var a = 1;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/alwaysStrictModule3.ts ===
2+
3+
// module ES2015
4+
export const a = 1;
5+
>a : Symbol(a, Decl(alwaysStrictModule3.ts, 2, 12))
6+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/alwaysStrictModule3.ts ===
2+
3+
// module ES2015
4+
export const a = 1;
5+
>a : 1
6+
>1 : 1
7+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [alwaysStrictModule4.ts]
2+
3+
// Module commonjs
4+
export const a = 1
5+
6+
//// [alwaysStrictModule4.js]
7+
"use strict";
8+
// Module commonjs
9+
exports.a = 1;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/alwaysStrictModule4.ts ===
2+
3+
// Module commonjs
4+
export const a = 1
5+
>a : Symbol(a, Decl(alwaysStrictModule4.ts, 2, 12))
6+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/alwaysStrictModule4.ts ===
2+
3+
// Module commonjs
4+
export const a = 1
5+
>a : 1
6+
>1 : 1
7+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [alwaysStrictModule5.ts]
2+
3+
// Targeting ES6
4+
export const a = 1;
5+
6+
//// [alwaysStrictModule5.js]
7+
// Targeting ES6
8+
export const a = 1;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/alwaysStrictModule5.ts ===
2+
3+
// Targeting ES6
4+
export const a = 1;
5+
>a : Symbol(a, Decl(alwaysStrictModule5.ts, 2, 12))
6+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/alwaysStrictModule5.ts ===
2+
3+
// Targeting ES6
4+
export const a = 1;
5+
>a : 1
6+
>1 : 1
7+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [alwaysStrictModule6.ts]
2+
3+
// Targeting ES5
4+
export const a = 1;
5+
6+
//// [alwaysStrictModule6.js]
7+
"use strict";
8+
// Targeting ES5
9+
exports.a = 1;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/alwaysStrictModule6.ts ===
2+
3+
// Targeting ES5
4+
export const a = 1;
5+
>a : Symbol(a, Decl(alwaysStrictModule6.ts, 2, 12))
6+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/alwaysStrictModule6.ts ===
2+
3+
// Targeting ES5
4+
export const a = 1;
5+
>a : 1
6+
>1 : 1
7+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @alwaysStrict: true
2+
// @module: es2015
3+
4+
// module ES2015
5+
export const a = 1;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @alwaysStrict: true
2+
// @module: commonjs
3+
4+
// Module commonjs
5+
export const a = 1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @alwaysStrict: true
2+
// @target: es6
3+
4+
// Targeting ES6
5+
export const a = 1;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @alwaysStrict: true
2+
// @target: es5
3+
4+
// Targeting ES5
5+
export const a = 1;

0 commit comments

Comments
 (0)