Merge pull request #25561 from ajafff/multiple-prologue-directives

handle multiple prologue directives
This commit is contained in:
Mohamed Hegazy
2018-07-11 11:38:01 -07:00
committed by GitHub
13 changed files with 34 additions and 30 deletions
+5 -5
View File
@@ -529,7 +529,7 @@ namespace ts {
createVariableStatement(/*modifiers*/ undefined,
createVariableDeclarationList(taggedTemplateStringDeclarations)));
}
prependStatements(statements, endLexicalEnvironment());
addStatementsAfterPrologue(statements, endLexicalEnvironment());
exitSubtree(ancestorFacts, HierarchyFacts.None, HierarchyFacts.None);
return updateSourceFileNode(
node,
@@ -837,7 +837,7 @@ namespace ts {
setEmitFlags(statement, EmitFlags.NoComments | EmitFlags.NoTokenSourceMaps);
statements.push(statement);
prependStatements(statements, endLexicalEnvironment());
addStatementsAfterPrologue(statements, endLexicalEnvironment());
const block = createBlock(setTextRange(createNodeArray(statements), /*location*/ node.members), /*multiLine*/ true);
setEmitFlags(block, EmitFlags.NoComments);
@@ -980,7 +980,7 @@ namespace ts {
);
}
prependStatements(statements, endLexicalEnvironment());
addStatementsAfterPrologue(statements, endLexicalEnvironment());
if (constructor) {
prependCaptureNewTargetIfNeeded(statements, constructor, /*copyOnWrite*/ false);
@@ -1892,7 +1892,7 @@ namespace ts {
}
const lexicalEnvironment = context.endLexicalEnvironment();
prependStatements(statements, lexicalEnvironment);
addStatementsAfterPrologue(statements, lexicalEnvironment);
prependCaptureNewTargetIfNeeded(statements, node, /*copyOnWrite*/ false);
// If we added any final generated statements, this must be a multi-line block
@@ -2707,7 +2707,7 @@ namespace ts {
if (loopOutParameters.length) {
copyOutParameters(loopOutParameters, CopyDirection.ToOutParameter, statements);
}
prependStatements(statements, lexicalEnvironment);
addStatementsAfterPrologue(statements, lexicalEnvironment);
loopBody = createBlock(statements, /*multiline*/ true);
}
+1 -1
View File
@@ -413,7 +413,7 @@ namespace ts {
)
);
prependStatements(statements, endLexicalEnvironment());
addStatementsAfterPrologue(statements, endLexicalEnvironment());
const block = createBlock(statements, /*multiLine*/ true);
setTextRange(block, node.body);
+2 -2
View File
@@ -675,7 +675,7 @@ namespace ts {
)
);
prependStatements(statements, endLexicalEnvironment());
addStatementsAfterPrologue(statements, endLexicalEnvironment());
const block = updateBlock(node.body!, statements);
// Minor optimization, emit `_super` helper to capture `super` access in an arrow.
@@ -707,7 +707,7 @@ namespace ts {
const leadingStatements = endLexicalEnvironment();
if (statementOffset > 0 || some(statements) || some(leadingStatements)) {
const block = convertToFunctionBody(body, /*multiLine*/ true);
prependStatements(statements, leadingStatements);
addStatementsAfterPrologue(statements, leadingStatements);
addRange(statements, block.statements.slice(statementOffset));
return updateBlock(block, setTextRange(createNodeArray(statements), block.statements));
}
+1 -1
View File
@@ -587,7 +587,7 @@ namespace ts {
transformAndEmitStatements(body.statements, statementOffset);
const buildResult = build();
prependStatements(statements, endLexicalEnvironment());
addStatementsAfterPrologue(statements, endLexicalEnvironment());
statements.push(createReturn(buildResult));
// Restore previous generator state
+2 -2
View File
@@ -97,7 +97,7 @@ namespace ts {
append(statements, visitNode(currentModuleInfo.externalHelpersImportDeclaration, sourceElementVisitor, isStatement));
addRange(statements, visitNodes(node.statements, sourceElementVisitor, isStatement, statementOffset));
addExportEqualsIfNeeded(statements, /*emitAsReturn*/ false);
prependStatements(statements, endLexicalEnvironment());
addStatementsAfterPrologue(statements, endLexicalEnvironment());
const updated = updateSourceFileNode(node, setTextRange(createNodeArray(statements), node.statements));
if (currentModuleInfo.hasExportStarsToExportValues && !compilerOptions.importHelpers) {
@@ -426,7 +426,7 @@ namespace ts {
// End the lexical environment for the module body
// and merge any new lexical declarations.
prependStatements(statements, endLexicalEnvironment());
addStatementsAfterPrologue(statements, endLexicalEnvironment());
const body = createBlock(statements, /*multiLine*/ true);
if (currentModuleInfo.hasExportStarsToExportValues && !compilerOptions.importHelpers) {
+1 -1
View File
@@ -257,7 +257,7 @@ namespace ts {
// We emit hoisted variables early to align roughly with our previous emit output.
// Two key differences in this approach are:
// - Temporary variables will appear at the top rather than at the bottom of the file
prependStatements(statements, endLexicalEnvironment());
addStatementsAfterPrologue(statements, endLexicalEnvironment());
const exportStarFunction = addExportStarIfNeeded(statements)!; // TODO: GH#18217
const moduleObject = createObjectLiteral([
+3 -3
View File
@@ -682,7 +682,7 @@ namespace ts {
setEmitFlags(statement, EmitFlags.NoComments | EmitFlags.NoTokenSourceMaps);
statements.push(statement);
prependStatements(statements, context.endLexicalEnvironment());
addStatementsAfterPrologue(statements, context.endLexicalEnvironment());
const iife = createImmediatelyInvokedArrowFunction(statements);
setEmitFlags(iife, EmitFlags.TypeScriptClassWrapper);
@@ -2711,7 +2711,7 @@ namespace ts {
const statements: Statement[] = [];
startLexicalEnvironment();
const members = map(node.members, transformEnumMember);
prependStatements(statements, endLexicalEnvironment());
addStatementsAfterPrologue(statements, endLexicalEnvironment());
addRange(statements, members);
currentNamespaceContainerName = savedCurrentNamespaceLocalName;
@@ -3026,7 +3026,7 @@ namespace ts {
statementsLocation = moveRangePos(moduleBlock.statements, -1);
}
prependStatements(statements, endLexicalEnvironment());
addStatementsAfterPrologue(statements, endLexicalEnvironment());
currentNamespaceContainerName = savedCurrentNamespaceContainerName;
currentNamespace = savedCurrentNamespace;
currentScopeFirstDeclarationsOfName = savedCurrentScopeFirstDeclarationsOfName;
+9 -12
View File
@@ -401,21 +401,18 @@ namespace ts {
}
/**
* Appends a range of value to begin of an array, returning the array.
*
* @param to The array to which `value` is to be appended. If `to` is `undefined`, a new array
* is created if `value` was appended.
* @param from The values to append to the array. If `from` is `undefined`, nothing is
* appended. If an element of `from` is `undefined`, that element is not appended.
* Prepends statements to an array while taking care of prologue directives.
*/
export function prependStatements<T extends Statement>(to: T[], from: ReadonlyArray<T> | undefined): T[] | undefined {
export function addStatementsAfterPrologue<T extends Statement>(to: T[], from: ReadonlyArray<T> | undefined): T[] {
if (from === undefined || from.length === 0) return to;
if (to === undefined) return from.slice();
const prologue = to.length && isPrologueDirective(to[0]) && to.shift();
to.unshift(...from);
if (prologue) {
to.unshift(prologue);
let statementIndex = 0;
// skip all prologue directives to insert at the correct position
for (; statementIndex < to.length; ++statementIndex) {
if (!isPrologueDirective(to[statementIndex])) {
break;
}
}
to.splice(statementIndex, 0, ...from);
return to;
}
+2 -2
View File
@@ -1476,8 +1476,8 @@ namespace ts {
}
return isNodeArray(statements)
? setTextRange(createNodeArray(prependStatements(statements.slice(), declarations)), statements)
: prependStatements(statements, declarations);
? setTextRange(createNodeArray(addStatementsAfterPrologue(statements.slice(), declarations)), statements)
: addStatementsAfterPrologue(statements, declarations);
}
/**
@@ -1,12 +1,14 @@
//// [destructuringTempOccursAfterPrologue.ts]
function test(p: any) {
'use strict';
'use strong';
p = { prop: p } = p;
}
//// [destructuringTempOccursAfterPrologue.js]
function test(p) {
'use strict';
'use strong';
var _a;
p = (_a = p, p = _a.prop, _a);
}
@@ -4,9 +4,10 @@ function test(p: any) {
>p : Symbol(p, Decl(destructuringTempOccursAfterPrologue.ts, 0, 14))
'use strict';
'use strong';
p = { prop: p } = p;
>p : Symbol(p, Decl(destructuringTempOccursAfterPrologue.ts, 0, 14))
>prop : Symbol(prop, Decl(destructuringTempOccursAfterPrologue.ts, 2, 9))
>prop : Symbol(prop, Decl(destructuringTempOccursAfterPrologue.ts, 3, 9))
>p : Symbol(p, Decl(destructuringTempOccursAfterPrologue.ts, 0, 14))
>p : Symbol(p, Decl(destructuringTempOccursAfterPrologue.ts, 0, 14))
}
@@ -6,6 +6,9 @@ function test(p: any) {
'use strict';
>'use strict' : "use strict"
'use strong';
>'use strong' : "use strong"
p = { prop: p } = p;
>p = { prop: p } = p : any
>p : any
@@ -1,4 +1,5 @@
function test(p: any) {
'use strict';
'use strong';
p = { prop: p } = p;
}