@@ -2449,7 +2449,7 @@ namespace ts {
2449
2449
return parseElement();
2450
2450
}
2451
2451
2452
- function currentNode(parsingContext: ParsingContext): Node | undefined {
2452
+ function currentNode(parsingContext: ParsingContext, pos?: number ): Node | undefined {
2453
2453
// If we don't have a cursor or the parsing context isn't reusable, there's nothing to reuse.
2454
2454
//
2455
2455
// If there is an outstanding parse error that we've encountered, but not attached to
@@ -2463,7 +2463,7 @@ namespace ts {
2463
2463
return undefined;
2464
2464
}
2465
2465
2466
- const node = syntaxCursor.currentNode(scanner.getStartPos());
2466
+ const node = syntaxCursor.currentNode(pos ?? scanner.getStartPos());
2467
2467
2468
2468
// Can't reuse a missing node.
2469
2469
// Can't reuse a node that intersected the change range.
@@ -6615,25 +6615,20 @@ namespace ts {
6615
6615
}
6616
6616
6617
6617
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
- //
6621
6618
// `parseListElement` attempted to get the reused node at this position,
6622
6619
// but the ambient context flag was not yet set, so the node appeared
6623
6620
// 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
-
6632
6621
const pos = getNodePos();
6633
6622
const hasJSDoc = hasPrecedingJSDocComment();
6634
6623
const decorators = parseDecorators();
6635
6624
const modifiers = parseModifiers();
6625
+ const isAmbient = some(modifiers, isDeclareModifier);
6636
6626
if (isAmbient) {
6627
+ const node = tryReuseAmbientDeclaration(pos);
6628
+ if (node) {
6629
+ return node;
6630
+ }
6631
+
6637
6632
for (const m of modifiers!) {
6638
6633
(m as Mutable<Node>).flags |= NodeFlags.Ambient;
6639
6634
}
@@ -6644,9 +6639,9 @@ namespace ts {
6644
6639
}
6645
6640
}
6646
6641
6647
- function tryReuseAmbientDeclaration(): Statement | undefined {
6642
+ function tryReuseAmbientDeclaration(pos: number ): Statement | undefined {
6648
6643
return doInsideOfContext(NodeFlags.Ambient, () => {
6649
- const node = currentNode(parsingContext);
6644
+ const node = currentNode(parsingContext, pos );
6650
6645
if (node) {
6651
6646
return consumeNode(node) as Statement;
6652
6647
}
0 commit comments