Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/tools/sass-variable-explainer/forward-annotations.ts
6438 views
1
import { annotateNode } from './ast-utils.ts';
2
3
export const forwardAnnotations = (ast: any) => {
4
const pragmaAnnotation = "quarto-scss-analysis-annotation";
5
const annotationStack: Record<string, string>[] = [{}];
6
let currentAnnotation: Record<string, unknown> = annotationStack[0];
7
let hasAnnotations: boolean = false;
8
for (const node of ast.children) {
9
if (node.type === "comment_singleline") {
10
const value = node?.value?.trim();
11
if (value.startsWith(pragmaAnnotation)) {
12
let payload: Record<string, any> = {};
13
try {
14
payload = JSON.parse(value.slice(pragmaAnnotation.length).trim());
15
} catch (e) {
16
console.error("Could not parse annotation payload", e);
17
continue;
18
}
19
if (payload.action === "push") {
20
annotationStack.push(JSON.parse(JSON.stringify(currentAnnotation)));
21
currentAnnotation = annotationStack[annotationStack.length - 1];
22
} else if (payload.action === "pop" && annotationStack.length) {
23
annotationStack.pop();
24
currentAnnotation = annotationStack[annotationStack.length - 1];
25
}
26
for (const [key, value] of Object.entries(payload)) {
27
if (key === "action") {
28
continue;
29
}
30
if (value === null) {
31
delete currentAnnotation[key];
32
} else {
33
currentAnnotation[key] = value;
34
}
35
}
36
hasAnnotations = Object.keys(currentAnnotation).length > 0;
37
}
38
}
39
if (node.type === "declaration" && hasAnnotations) {
40
annotateNode(node, currentAnnotation);
41
}
42
}
43
return ast;
44
}
45
46