Skip to content

Commit 8e5e765

Browse files
committed
Fix parser TODO
1 parent ad6d086 commit 8e5e765

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

src/compiler/parser.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,7 +2449,7 @@ namespace ts {
24492449
return parseElement();
24502450
}
24512451

2452-
function currentNode(parsingContext: ParsingContext): Node | undefined {
2452+
function currentNode(parsingContext: ParsingContext, pos?: number): Node | undefined {
24532453
// If we don't have a cursor or the parsing context isn't reusable, there's nothing to reuse.
24542454
//
24552455
// If there is an outstanding parse error that we've encountered, but not attached to
@@ -2463,7 +2463,7 @@ namespace ts {
24632463
return undefined;
24642464
}
24652465

2466-
const node = syntaxCursor.currentNode(scanner.getStartPos());
2466+
const node = syntaxCursor.currentNode(pos ?? scanner.getStartPos());
24672467

24682468
// Can't reuse a missing node.
24692469
// Can't reuse a node that intersected the change range.
@@ -6610,25 +6610,20 @@ namespace ts {
66106610
}
66116611

66126612
function parseDeclaration(): Statement {
6613-
// TODO: Can we hold onto the parsed decorators/modifiers and advance the scanner
6614-
// if we can't reuse the declaration, so that we don't do this work twice?
6615-
//
66166613
// `parseListElement` attempted to get the reused node at this position,
66176614
// but the ambient context flag was not yet set, so the node appeared
66186615
// not reusable in that context.
6619-
const isAmbient = some(lookAhead(() => (parseDecorators(), parseModifiers())), isDeclareModifier);
6620-
if (isAmbient) {
6621-
const node = tryReuseAmbientDeclaration();
6622-
if (node) {
6623-
return node;
6624-
}
6625-
}
6626-
66276616
const pos = getNodePos();
66286617
const hasJSDoc = hasPrecedingJSDocComment();
66296618
const decorators = parseDecorators();
66306619
const modifiers = parseModifiers();
6620+
const isAmbient = some(modifiers, isDeclareModifier);
66316621
if (isAmbient) {
6622+
const node = tryReuseAmbientDeclaration(pos);
6623+
if (node) {
6624+
return node;
6625+
}
6626+
66326627
for (const m of modifiers!) {
66336628
(m as Mutable<Node>).flags |= NodeFlags.Ambient;
66346629
}
@@ -6639,9 +6634,9 @@ namespace ts {
66396634
}
66406635
}
66416636

6642-
function tryReuseAmbientDeclaration(): Statement | undefined {
6637+
function tryReuseAmbientDeclaration(pos: number): Statement | undefined {
66436638
return doInsideOfContext(NodeFlags.Ambient, () => {
6644-
const node = currentNode(parsingContext);
6639+
const node = currentNode(parsingContext, pos);
66456640
if (node) {
66466641
return consumeNode(node) as Statement;
66476642
}

0 commit comments

Comments
 (0)