@@ -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.
@@ -6615,25 +6615,20 @@ namespace ts {
66156615 }
66166616
66176617 function parseDeclaration(): Statement {
6618- // TODO: Can we hold onto the parsed decorators/modifiers and advance the scanner
6619- // if we can't reuse the declaration, so that we don't do this work twice?
6620- //
66216618 // `parseListElement` attempted to get the reused node at this position,
66226619 // but the ambient context flag was not yet set, so the node appeared
66236620 // not reusable in that context.
6624- const isAmbient = some(lookAhead(() => (parseDecorators(), parseModifiers())), isDeclareModifier);
6625- if (isAmbient) {
6626- const node = tryReuseAmbientDeclaration();
6627- if (node) {
6628- return node;
6629- }
6630- }
6631-
66326621 const pos = getNodePos();
66336622 const hasJSDoc = hasPrecedingJSDocComment();
66346623 const decorators = parseDecorators();
66356624 const modifiers = parseModifiers();
6625+ const isAmbient = some(modifiers, isDeclareModifier);
66366626 if (isAmbient) {
6627+ const node = tryReuseAmbientDeclaration(pos);
6628+ if (node) {
6629+ return node;
6630+ }
6631+
66376632 for (const m of modifiers!) {
66386633 (m as Mutable<Node>).flags |= NodeFlags.Ambient;
66396634 }
@@ -6644,9 +6639,9 @@ namespace ts {
66446639 }
66456640 }
66466641
6647- function tryReuseAmbientDeclaration(): Statement | undefined {
6642+ function tryReuseAmbientDeclaration(pos: number ): Statement | undefined {
66486643 return doInsideOfContext(NodeFlags.Ambient, () => {
6649- const node = currentNode(parsingContext);
6644+ const node = currentNode(parsingContext, pos );
66506645 if (node) {
66516646 return consumeNode(node) as Statement;
66526647 }
0 commit comments