Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/tools/sass-variable-explainer/clean-ast.ts
6433 views
1
import { withTypeAndArray, filterDeep, mapDeep } from "./ast-utils.ts";
2
import { isRealContent, isNotPunctuation } from "./remove-nonsemantic-entries.ts";
3
import { simplifyLineInfo } from "./line-info.ts";
4
import { explicitlyTagDefaultValues } from "./default-declarations.ts";
5
import { fixImmediateTypes, tagNamedColors, tagColorConstructors, findDimensionValues } from "./value-types.ts";
6
import { groupArguments } from "./group-arguments.ts";
7
import { forwardAnnotations } from "./forward-annotations.ts";
8
9
const valueArrayToObjectKeys = (outer: any) =>
10
withTypeAndArray(outer, (node: any) => {
11
const keys = node.children.map((v: any) => v.type);
12
if (keys.length !== new Set(keys).size) {
13
return node;
14
}
15
return {
16
...node,
17
...Object.fromEntries(node.children
18
.map((v: any) => {
19
const key = v.type;
20
let children = v.children;
21
return [key, {...v, children}];
22
})),
23
};
24
});
25
26
export const cleanSassAst = (ast: any) => {
27
// we now attempt to turn this glorified lexer into a real AST
28
29
// before everything else, we associate declarations with the
30
// annotations that tell us which part of the theming system
31
// they belong to
32
ast = forwardAnnotations(ast);
33
34
// before clearing out the punctuation, group arguments
35
ast = filterDeep(ast, isRealContent);
36
ast = mapDeep(ast, groupArguments);
37
38
// clear out all the gunk from the AST
39
ast = filterDeep(ast, isNotPunctuation);
40
41
ast = mapDeep(ast, simplifyLineInfo);
42
ast = mapDeep(ast, explicitlyTagDefaultValues);
43
ast = mapDeep(ast, findDimensionValues);
44
ast = mapDeep(ast, fixImmediateTypes);
45
ast = mapDeep(ast, tagColorConstructors);
46
ast = mapDeep(ast, tagNamedColors);
47
48
// if the value array looks like an array of keys and values,
49
// insert those values into the parent object
50
// additionally, in these properties, if the value is an array
51
// with a single element, remove the array and just use the
52
// element.
53
ast = mapDeep(ast, valueArrayToObjectKeys);
54
55
return ast;
56
}
57
58