Skip to content

Reuse unchanged ambient declarations in incremental parsing #32849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5482,10 +5482,22 @@ namespace ts {
}

function parseDeclaration(): Statement {
const modifiers = lookAhead(() => (parseDecorators(), parseModifiers()));
// `parseListElement` attempted to get the reused node at this position,
// but the ambient context flag was not yet set, so the node appeared
// not reusable in that context.
const isAmbient = some(modifiers, isDeclareModifier);
if (isAmbient) {
const node = tryReuseAmbientDeclaration();
if (node) {
return node;
}
}

const node = <Statement>createNodeWithJSDoc(SyntaxKind.Unknown);
node.decorators = parseDecorators();
node.modifiers = parseModifiers();
if (some(node.modifiers, isDeclareModifier)) {
if (isAmbient) {
for (const m of node.modifiers!) {
m.flags |= NodeFlags.Ambient;
}
Expand All @@ -5496,6 +5508,15 @@ namespace ts {
}
}

function tryReuseAmbientDeclaration(): Statement | undefined {
return doInsideOfContext(NodeFlags.Ambient, () => {
const node = currentNode(parsingContext);
if (node) {
return consumeNode(node) as Statement;
}
});
}

function parseDeclarationWorker(node: Statement): Statement {
switch (token()) {
case SyntaxKind.VarKeyword:
Expand Down
4 changes: 2 additions & 2 deletions src/testRunner/unittests/incrementalParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ module m3 { }\
const oldText = ScriptSnapshot.fromString(source);
const newTextAndChange = withInsert(oldText, 0, "{");

compareTrees(oldText, newTextAndChange.text, newTextAndChange.textChangeRange, 4);
compareTrees(oldText, newTextAndChange.text, newTextAndChange.textChangeRange, 9);
});

it("Removing block around function declarations", () => {
Expand All @@ -680,7 +680,7 @@ module m3 { }\
const oldText = ScriptSnapshot.fromString(source);
const newTextAndChange = withDelete(oldText, 0, "{".length);

compareTrees(oldText, newTextAndChange.text, newTextAndChange.textChangeRange, 4);
compareTrees(oldText, newTextAndChange.text, newTextAndChange.textChangeRange, 9);
});

it("Moving methods from class to object literal", () => {
Expand Down