mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
only emit use strict if a use strict prologue isnt found
This commit is contained in:
+24
-14
@@ -7383,8 +7383,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
|
||||
write(`], function(${exportFunctionForFile}) {`);
|
||||
writeLine();
|
||||
increaseIndent();
|
||||
emitUseStrictPrologueDirective();
|
||||
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true);
|
||||
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true, /*ensureUseStrict*/ true);
|
||||
emitEmitHelpers(node);
|
||||
emitCaptureThisForNodeIfNecessary(node);
|
||||
emitSystemModuleBody(node, dependencyGroups, startIndex);
|
||||
@@ -7494,8 +7493,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
|
||||
writeModuleName(node, emitRelativePathAsModuleName);
|
||||
emitAMDDependencies(node, /*includeNonAmdDependencies*/ true, emitRelativePathAsModuleName);
|
||||
increaseIndent();
|
||||
emitUseStrictPrologueDirective();
|
||||
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true);
|
||||
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true, /*ensureUseStrict*/ true);
|
||||
emitExportStarHelper();
|
||||
emitCaptureThisForNodeIfNecessary(node);
|
||||
emitLinesStartingAt(node.statements, startIndex);
|
||||
@@ -7507,8 +7505,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
|
||||
}
|
||||
|
||||
function emitCommonJSModule(node: SourceFile) {
|
||||
emitUseStrictPrologueDirective();
|
||||
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false);
|
||||
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false, /*ensureUseStrict*/ true);
|
||||
emitEmitHelpers(node);
|
||||
collectExternalModuleInfo(node);
|
||||
emitExportStarHelper();
|
||||
@@ -7518,11 +7515,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
|
||||
emitExportEquals(/*emitAsReturn*/ false);
|
||||
}
|
||||
|
||||
function emitUseStrictPrologueDirective() {
|
||||
writeLine();
|
||||
write("\"use strict\";");
|
||||
}
|
||||
|
||||
function emitUMDModule(node: SourceFile) {
|
||||
emitEmitHelpers(node);
|
||||
collectExternalModuleInfo(node);
|
||||
@@ -7542,8 +7534,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
|
||||
})(`);
|
||||
emitAMDFactoryHeader(dependencyNames);
|
||||
increaseIndent();
|
||||
emitUseStrictPrologueDirective();
|
||||
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true);
|
||||
const startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true, /*ensureUseStrict*/ true);
|
||||
emitExportStarHelper();
|
||||
emitCaptureThisForNodeIfNecessary(node);
|
||||
emitLinesStartingAt(node.statements, startIndex);
|
||||
@@ -7685,19 +7676,38 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
|
||||
}
|
||||
}
|
||||
|
||||
function emitDirectivePrologues(statements: Node[], startWithNewLine: boolean): number {
|
||||
function isUseStrictPrologue(node: ExpressionStatement): boolean {
|
||||
return !!(node.expression as StringLiteral).text.match(/use strict/);
|
||||
}
|
||||
|
||||
function ensureUseStrictPrologue(startWithNewLine: boolean, writeUseStrict: boolean) {
|
||||
if (writeUseStrict) {
|
||||
if (startWithNewLine) {
|
||||
writeLine();
|
||||
}
|
||||
write("\"use strict\";");
|
||||
}
|
||||
}
|
||||
|
||||
function emitDirectivePrologues(statements: Node[], startWithNewLine: boolean, ensureUseStrict?: boolean): number {
|
||||
let foundUseStrict = false;
|
||||
for (let i = 0; i < statements.length; ++i) {
|
||||
if (isPrologueDirective(statements[i])) {
|
||||
if (isUseStrictPrologue(statements[i] as ExpressionStatement)) {
|
||||
foundUseStrict = true;
|
||||
}
|
||||
if (startWithNewLine || i > 0) {
|
||||
writeLine();
|
||||
}
|
||||
emit(statements[i]);
|
||||
}
|
||||
else {
|
||||
ensureUseStrictPrologue(startWithNewLine || i > 0, !foundUseStrict && ensureUseStrict);
|
||||
// return index of the first non prologue directive
|
||||
return i;
|
||||
}
|
||||
}
|
||||
ensureUseStrictPrologue(startWithNewLine, !foundUseStrict && ensureUseStrict);
|
||||
return statements.length;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user