mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-17 01:49:57 -05:00
Fix #10758 Add compiler option to parse in strict mode
* add unit test to ensure "use strict" is not added twice * fix code
This commit is contained in:
@@ -2235,6 +2235,36 @@ namespace ts {
|
||||
return statementOffset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures "use strict" directive is added
|
||||
*
|
||||
* @param node source file
|
||||
*/
|
||||
export function ensureUseStrict(node: SourceFile): SourceFile {
|
||||
let foundUseStrict = false;
|
||||
let statementOffset = 0;
|
||||
const numStatements = node.statements.length;
|
||||
while (statementOffset < numStatements) {
|
||||
const statement = node.statements[statementOffset];
|
||||
if (isPrologueDirective(statement)) {
|
||||
if (isUseStrictPrologue(statement as ExpressionStatement)) {
|
||||
foundUseStrict = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
statementOffset++;
|
||||
}
|
||||
if (!foundUseStrict) {
|
||||
const statements: Statement[] = [];
|
||||
statements.push(startOnNewLine(createStatement(createLiteral("use strict"))));
|
||||
// add "use strict" as the first statement
|
||||
return updateSourceFileNode(node, statements.concat(node.statements));
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps the operand to a BinaryExpression in parentheses if they are needed to preserve the intended
|
||||
* order of operations.
|
||||
|
||||
@@ -438,7 +438,7 @@ namespace ts {
|
||||
|
||||
// ensure "use strict"" is emitted in all scenarios in alwaysStrict mode
|
||||
if (compilerOptions.alwaysStrict) {
|
||||
node = emitUseStrict(node);
|
||||
node = ensureUseStrict(node);
|
||||
}
|
||||
|
||||
// If the source file requires any helpers and is an external module, and
|
||||
@@ -477,13 +477,6 @@ namespace ts {
|
||||
return node;
|
||||
}
|
||||
|
||||
function emitUseStrict(node: SourceFile): SourceFile {
|
||||
const statements: Statement[] = [];
|
||||
statements.push(startOnNewLine(createStatement(createLiteral("use strict"))));
|
||||
// add "use strict" as the first statement
|
||||
return updateSourceFileNode(node, statements.concat(node.statements));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether we should emit a __decorate call for a class declaration.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user