Path: blob/main/tools/sass-variable-explainer/get-dependencies.ts
6433 views
import { walk } from "./ast-utils.ts";1// import { assert } from "jsr:@std/assert";23const assert = (condition: any) => {4if (!condition) {5throw new Error("Assertion failed");6}7}89export const getVariableDependencies = (declarations: Map<string, any>) => {10const dependencies = new Map<string, {11node: any,12dependencies: Set<string>13}>();14for (const [name, node] of declarations) {15assert(node?.type === "declaration");16const varName = node?.property?.variable?.value;17assert(varName === name);18if (!dependencies.has(varName)) {19dependencies.set(varName, {node: node, dependencies: new Set()});20}21const varValue = node?.value;22walk(varValue, (inner: any) => {23if (inner?.type === "variable") {24const innerName = inner?.value;25if (!innerName) {26console.log(inner);27throw new Error("stop")28}29dependencies.get(varName)!.dependencies.add(innerName);30}31return true;32});33}34return dependencies;35}3637