Path: blob/main/tools/sass-variable-explainer/clean-ast.ts
6433 views
import { withTypeAndArray, filterDeep, mapDeep } from "./ast-utils.ts";1import { isRealContent, isNotPunctuation } from "./remove-nonsemantic-entries.ts";2import { simplifyLineInfo } from "./line-info.ts";3import { explicitlyTagDefaultValues } from "./default-declarations.ts";4import { fixImmediateTypes, tagNamedColors, tagColorConstructors, findDimensionValues } from "./value-types.ts";5import { groupArguments } from "./group-arguments.ts";6import { forwardAnnotations } from "./forward-annotations.ts";78const valueArrayToObjectKeys = (outer: any) =>9withTypeAndArray(outer, (node: any) => {10const keys = node.children.map((v: any) => v.type);11if (keys.length !== new Set(keys).size) {12return node;13}14return {15...node,16...Object.fromEntries(node.children17.map((v: any) => {18const key = v.type;19let children = v.children;20return [key, {...v, children}];21})),22};23});2425export const cleanSassAst = (ast: any) => {26// we now attempt to turn this glorified lexer into a real AST2728// before everything else, we associate declarations with the29// annotations that tell us which part of the theming system30// they belong to31ast = forwardAnnotations(ast);3233// before clearing out the punctuation, group arguments34ast = filterDeep(ast, isRealContent);35ast = mapDeep(ast, groupArguments);3637// clear out all the gunk from the AST38ast = filterDeep(ast, isNotPunctuation);3940ast = mapDeep(ast, simplifyLineInfo);41ast = mapDeep(ast, explicitlyTagDefaultValues);42ast = mapDeep(ast, findDimensionValues);43ast = mapDeep(ast, fixImmediateTypes);44ast = mapDeep(ast, tagColorConstructors);45ast = mapDeep(ast, tagNamedColors);4647// if the value array looks like an array of keys and values,48// insert those values into the parent object49// additionally, in these properties, if the value is an array50// with a single element, remove the array and just use the51// element.52ast = mapDeep(ast, valueArrayToObjectKeys);5354return ast;55}565758