Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ea808f5

Browse filesBrowse files
committed
Fix microsoft#10758 Add compiler option to parse in strict mode
* add unit test to ensure "use strict" is not added twice * fix code
1 parent 29a85e0 commit ea808f5
Copy full SHA for ea808f5

6 files changed

+66-8Lines changed: 66 additions & 8 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/factory.ts‎

Copy file name to clipboardExpand all lines: src/compiler/factory.ts
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,6 +2235,36 @@ namespace ts {
22352235
return statementOffset;
22362236
}
22372237

2238+
/**
2239+
* Ensures "use strict" directive is added
2240+
*
2241+
* @param node source file
2242+
*/
2243+
export function ensureUseStrict(node: SourceFile): SourceFile {
2244+
let foundUseStrict = false;
2245+
let statementOffset = 0;
2246+
const numStatements = node.statements.length;
2247+
while (statementOffset < numStatements) {
2248+
const statement = node.statements[statementOffset];
2249+
if (isPrologueDirective(statement)) {
2250+
if (isUseStrictPrologue(statement as ExpressionStatement)) {
2251+
foundUseStrict = true;
2252+
}
2253+
}
2254+
else {
2255+
break;
2256+
}
2257+
statementOffset++;
2258+
}
2259+
if (!foundUseStrict) {
2260+
const statements: Statement[] = [];
2261+
statements.push(startOnNewLine(createStatement(createLiteral("use strict"))));
2262+
// add "use strict" as the first statement
2263+
return updateSourceFileNode(node, statements.concat(node.statements));
2264+
}
2265+
return node;
2266+
}
2267+
22382268
/**
22392269
* Wraps the operand to a BinaryExpression in parentheses if they are needed to preserve the intended
22402270
* order of operations.
Collapse file

‎src/compiler/transformers/ts.ts‎

Copy file name to clipboardExpand all lines: src/compiler/transformers/ts.ts
+1-8Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ namespace ts {
438438

439439
// ensure "use strict"" is emitted in all scenarios in alwaysStrict mode
440440
if (compilerOptions.alwaysStrict) {
441-
node = emitUseStrict(node);
441+
node = ensureUseStrict(node);
442442
}
443443

444444
// If the source file requires any helpers and is an external module, and
@@ -477,13 +477,6 @@ namespace ts {
477477
return node;
478478
}
479479

480-
function emitUseStrict(node: SourceFile): SourceFile {
481-
const statements: Statement[] = [];
482-
statements.push(startOnNewLine(createStatement(createLiteral("use strict"))));
483-
// add "use strict" as the first statement
484-
return updateSourceFileNode(node, statements.concat(node.statements));
485-
}
486-
487480
/**
488481
* Tests whether we should emit a __decorate call for a class declaration.
489482
*/
Collapse file
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [alwaysStrictAlreadyUseStrict.ts]
2+
"use strict"
3+
function f() {
4+
var a = [];
5+
}
6+
7+
//// [alwaysStrictAlreadyUseStrict.js]
8+
"use strict";
9+
function f() {
10+
var a = [];
11+
}
Collapse file
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/alwaysStrictAlreadyUseStrict.ts ===
2+
"use strict"
3+
function f() {
4+
>f : Symbol(f, Decl(alwaysStrictAlreadyUseStrict.ts, 0, 12))
5+
6+
var a = [];
7+
>a : Symbol(a, Decl(alwaysStrictAlreadyUseStrict.ts, 2, 7))
8+
}
Collapse file
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/alwaysStrictAlreadyUseStrict.ts ===
2+
"use strict"
3+
>"use strict" : "use strict"
4+
5+
function f() {
6+
>f : () => void
7+
8+
var a = [];
9+
>a : any[]
10+
>[] : undefined[]
11+
}
Collapse file
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @alwaysStrict: true
2+
"use strict"
3+
function f() {
4+
var a = [];
5+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.