Skip to content

Commit f1f1f5c

Browse files
committed
use a cache to keep track of resolved aliases
1 parent d56e819 commit f1f1f5c

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/languageservice/parser/ast-converter.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ type NodeRange = [number, number, number];
3636
export const aliasDepth = {
3737
maxRefCount: 1000,
3838
currentRefDepth: 0,
39+
aliasResolutionCache: new Map<Alias, ASTNode>(),
3940
};
4041

41-
const seenAlias = new Set<Alias>();
42-
4342
export function convertAST(parent: ASTNode, node: YamlNode, doc: Document, lineCounter: LineCounter): ASTNode | undefined {
4443
if (!parent) {
4544
// first invocation
4645
aliasDepth.currentRefDepth = 0;
46+
aliasDepth.aliasResolutionCache = new Map();
4747
}
4848

4949
if (!node) {
@@ -61,11 +61,8 @@ export function convertAST(parent: ASTNode, node: YamlNode, doc: Document, lineC
6161
if (isScalar(node)) {
6262
return convertScalar(node, parent);
6363
}
64-
if (isAlias(node) && !seenAlias.has(node) && aliasDepth.currentRefDepth < aliasDepth.maxRefCount) {
65-
seenAlias.add(node);
66-
const converted = convertAlias(node, parent, doc, lineCounter);
67-
seenAlias.delete(node);
68-
return converted;
64+
if (isAlias(node) && aliasDepth.currentRefDepth < aliasDepth.maxRefCount) {
65+
return convertAlias(node, parent, doc, lineCounter);
6966
} else {
7067
return;
7168
}
@@ -158,6 +155,10 @@ function convertScalar(node: Scalar, parent: ASTNode): ASTNode {
158155
}
159156

160157
function convertAlias(node: Alias, parent: ASTNode, doc: Document, lineCounter: LineCounter): ASTNode {
158+
if (aliasDepth.aliasResolutionCache.has(node)) {
159+
return aliasDepth.aliasResolutionCache.get(node);
160+
}
161+
161162
aliasDepth.currentRefDepth++;
162163
const resolvedNode = node.resolve(doc);
163164
let ans: ASTNode;
@@ -169,6 +170,7 @@ function convertAlias(node: Alias, parent: ASTNode, doc: Document, lineCounter:
169170
ans = resultNode;
170171
}
171172
aliasDepth.currentRefDepth--;
173+
aliasDepth.aliasResolutionCache.set(node, ans);
172174
return ans;
173175
}
174176

0 commit comments

Comments
 (0)