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