mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-19 01:33:15 -05:00
Hoisted declarations should occur after prologue directives (#24386)
This commit is contained in:
@@ -1006,21 +1006,6 @@ namespace ts {
|
||||
return to;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
export function prependRange<T>(to: T[], from: ReadonlyArray<T> | undefined): T[] | undefined {
|
||||
if (from === undefined || from.length === 0) return to;
|
||||
if (to === undefined) return from.slice();
|
||||
to.unshift(...from);
|
||||
return to;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Whether the value was added.
|
||||
*/
|
||||
|
||||
@@ -529,7 +529,7 @@ namespace ts {
|
||||
createVariableStatement(/*modifiers*/ undefined,
|
||||
createVariableDeclarationList(taggedTemplateStringDeclarations)));
|
||||
}
|
||||
prependRange(statements, endLexicalEnvironment());
|
||||
prependStatements(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);
|
||||
|
||||
prependRange(statements, endLexicalEnvironment());
|
||||
prependStatements(statements, endLexicalEnvironment());
|
||||
|
||||
const block = createBlock(setTextRange(createNodeArray(statements), /*location*/ node.members), /*multiLine*/ true);
|
||||
setEmitFlags(block, EmitFlags.NoComments);
|
||||
@@ -980,7 +980,7 @@ namespace ts {
|
||||
);
|
||||
}
|
||||
|
||||
prependRange(statements, endLexicalEnvironment());
|
||||
prependStatements(statements, endLexicalEnvironment());
|
||||
|
||||
if (constructor) {
|
||||
prependCaptureNewTargetIfNeeded(statements, constructor, /*copyOnWrite*/ false);
|
||||
@@ -1896,7 +1896,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
const lexicalEnvironment = context.endLexicalEnvironment();
|
||||
prependRange(statements, lexicalEnvironment);
|
||||
prependStatements(statements, lexicalEnvironment);
|
||||
|
||||
prependCaptureNewTargetIfNeeded(statements, node, /*copyOnWrite*/ false);
|
||||
|
||||
@@ -2712,7 +2712,7 @@ namespace ts {
|
||||
if (loopOutParameters.length) {
|
||||
copyOutParameters(loopOutParameters, CopyDirection.ToOutParameter, statements);
|
||||
}
|
||||
prependRange(statements, lexicalEnvironment);
|
||||
prependStatements(statements, lexicalEnvironment);
|
||||
loopBody = createBlock(statements, /*multiline*/ true);
|
||||
}
|
||||
|
||||
|
||||
@@ -413,7 +413,7 @@ namespace ts {
|
||||
)
|
||||
);
|
||||
|
||||
prependRange(statements, endLexicalEnvironment());
|
||||
prependStatements(statements, endLexicalEnvironment());
|
||||
|
||||
const block = createBlock(statements, /*multiLine*/ true);
|
||||
setTextRange(block, node.body);
|
||||
|
||||
@@ -663,7 +663,7 @@ namespace ts {
|
||||
)
|
||||
);
|
||||
|
||||
prependRange(statements, endLexicalEnvironment());
|
||||
prependStatements(statements, endLexicalEnvironment());
|
||||
const block = updateBlock(node.body!, statements);
|
||||
|
||||
// Minor optimization, emit `_super` helper to capture `super` access in an arrow.
|
||||
@@ -695,7 +695,7 @@ namespace ts {
|
||||
const leadingStatements = endLexicalEnvironment();
|
||||
if (statementOffset > 0 || some(statements) || some(leadingStatements)) {
|
||||
const block = convertToFunctionBody(body, /*multiLine*/ true);
|
||||
prependRange(statements, leadingStatements);
|
||||
prependStatements(statements, leadingStatements);
|
||||
addRange(statements, block.statements.slice(statementOffset));
|
||||
return updateBlock(block, setTextRange(createNodeArray(statements), block.statements));
|
||||
}
|
||||
|
||||
@@ -587,7 +587,7 @@ namespace ts {
|
||||
transformAndEmitStatements(body.statements, statementOffset);
|
||||
|
||||
const buildResult = build();
|
||||
prependRange(statements, endLexicalEnvironment());
|
||||
prependStatements(statements, endLexicalEnvironment());
|
||||
statements.push(createReturn(buildResult));
|
||||
|
||||
// Restore previous generator state
|
||||
|
||||
@@ -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);
|
||||
prependRange(statements, endLexicalEnvironment());
|
||||
prependStatements(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.
|
||||
prependRange(statements, endLexicalEnvironment());
|
||||
prependStatements(statements, endLexicalEnvironment());
|
||||
|
||||
const body = createBlock(statements, /*multiLine*/ true);
|
||||
if (currentModuleInfo.hasExportStarsToExportValues && !compilerOptions.importHelpers) {
|
||||
|
||||
@@ -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
|
||||
prependRange(statements, endLexicalEnvironment());
|
||||
prependStatements(statements, endLexicalEnvironment());
|
||||
|
||||
const exportStarFunction = addExportStarIfNeeded(statements)!; // TODO: GH#18217
|
||||
const moduleObject = createObjectLiteral([
|
||||
|
||||
@@ -669,7 +669,7 @@ namespace ts {
|
||||
setEmitFlags(statement, EmitFlags.NoComments | EmitFlags.NoTokenSourceMaps);
|
||||
statements.push(statement);
|
||||
|
||||
prependRange(statements, context.endLexicalEnvironment());
|
||||
prependStatements(statements, context.endLexicalEnvironment());
|
||||
|
||||
const iife = createImmediatelyInvokedArrowFunction(statements);
|
||||
setEmitFlags(iife, EmitFlags.TypeScriptClassWrapper);
|
||||
@@ -2693,7 +2693,7 @@ namespace ts {
|
||||
const statements: Statement[] = [];
|
||||
startLexicalEnvironment();
|
||||
const members = map(node.members, transformEnumMember);
|
||||
prependRange(statements, endLexicalEnvironment());
|
||||
prependStatements(statements, endLexicalEnvironment());
|
||||
addRange(statements, members);
|
||||
|
||||
currentNamespaceContainerName = savedCurrentNamespaceLocalName;
|
||||
@@ -3008,7 +3008,7 @@ namespace ts {
|
||||
statementsLocation = moveRangePos(moduleBlock.statements, -1);
|
||||
}
|
||||
|
||||
prependRange(statements, endLexicalEnvironment());
|
||||
prependStatements(statements, endLexicalEnvironment());
|
||||
currentNamespaceContainerName = savedCurrentNamespaceContainerName;
|
||||
currentNamespace = savedCurrentNamespace;
|
||||
currentScopeFirstDeclarationsOfName = savedCurrentScopeFirstDeclarationsOfName;
|
||||
|
||||
@@ -254,6 +254,25 @@ namespace ts {
|
||||
return !nodeIsMissing(node);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
export function prependStatements<T extends Statement>(to: T[], from: ReadonlyArray<T> | undefined): T[] | undefined {
|
||||
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);
|
||||
}
|
||||
return to;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the given comment is a triple-slash
|
||||
*
|
||||
|
||||
@@ -1469,7 +1469,7 @@ namespace ts {
|
||||
|
||||
return isNodeArray(statements)
|
||||
? setTextRange(createNodeArray(concatenate(declarations, statements)), statements)
|
||||
: prependRange(statements, declarations);
|
||||
: prependStatements(statements, declarations);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user