@md-code/component-uniqueness 0.1.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/LICENSE +202 -0
- package/README.md +21 -3
- package/dist/config.d.ts +50 -0
- package/dist/config.js +402 -0
- package/dist/constants.d.ts +55 -0
- package/dist/constants.js +86 -0
- package/dist/decision-maker.d.ts +39 -0
- package/dist/decision-maker.js +119 -0
- package/dist/filter/child-element.d.ts +3 -0
- package/dist/filter/child-element.js +17 -0
- package/dist/filter/missing-component.d.ts +10 -0
- package/dist/filter/missing-component.js +96 -0
- package/dist/filter/no-structure.d.ts +3 -0
- package/dist/filter/no-structure.js +16 -0
- package/dist/filter/usages.d.ts +3 -0
- package/dist/filter/usages.js +14 -0
- package/dist/index.d.ts +105 -0
- package/dist/index.js +244 -0
- package/dist/lib-registry.d.ts +4 -0
- package/dist/lib-registry.js +376 -0
- package/dist/logger.d.ts +7 -0
- package/dist/logger.js +70 -0
- package/dist/main.d.ts +25 -0
- package/dist/main.js +666 -0
- package/dist/matcher/a11y.d.ts +19 -0
- package/dist/matcher/a11y.js +311 -0
- package/dist/matcher/behavior.d.ts +10 -0
- package/dist/matcher/behavior.js +32 -0
- package/dist/matcher/css-matching.d.ts +18 -0
- package/dist/matcher/css-matching.js +159 -0
- package/dist/matcher/framework/index.d.ts +2 -0
- package/dist/matcher/framework/index.js +37 -0
- package/dist/matcher/framework/react.d.ts +12 -0
- package/dist/matcher/framework/react.js +70 -0
- package/dist/matcher/index.d.ts +64 -0
- package/dist/matcher/index.js +38 -0
- package/dist/matcher/name.d.ts +15 -0
- package/dist/matcher/name.js +154 -0
- package/dist/matcher/structure.d.ts +22 -0
- package/dist/matcher/structure.js +329 -0
- package/dist/matcher/styles.d.ts +20 -0
- package/dist/matcher/styles.js +49 -0
- package/dist/normalize/candidate.d.ts +3 -0
- package/dist/normalize/candidate.js +21 -0
- package/dist/normalize/dom.d.ts +22 -0
- package/dist/normalize/dom.js +180 -0
- package/dist/normalize/external-html.d.ts +5 -0
- package/dist/normalize/external-html.js +40 -0
- package/dist/normalize/framework.d.ts +20 -0
- package/dist/normalize/framework.js +130 -0
- package/dist/normalize/resolver.d.ts +7 -0
- package/dist/normalize/resolver.js +28 -0
- package/dist/normalize/style-resolver.d.ts +7 -0
- package/dist/normalize/style-resolver.js +28 -0
- package/dist/normalize/style.d.ts +15 -0
- package/dist/normalize/style.js +174 -0
- package/dist/normalize/words.d.ts +3 -0
- package/dist/normalize/words.js +40 -0
- package/dist/package.json +88 -0
- package/dist/playgrounds/angular.d.ts +7 -0
- package/dist/playgrounds/angular.js +9 -0
- package/dist/playgrounds/framework-entrypoint.d.ts +10 -0
- package/dist/playgrounds/framework-entrypoint.js +102 -0
- package/dist/playgrounds/framework.d.ts +10 -0
- package/dist/playgrounds/framework.js +102 -0
- package/dist/playgrounds/react.d.ts +14 -0
- package/dist/playgrounds/react.js +1288 -0
- package/dist/playgrounds/scss.d.ts +6 -0
- package/dist/playgrounds/scss.js +9 -0
- package/dist/playgrounds/style-entrypoint.d.ts +13 -0
- package/dist/playgrounds/style-entrypoint.js +82 -0
- package/dist/playgrounds/style.d.ts +13 -0
- package/dist/playgrounds/style.js +82 -0
- package/dist/playgrounds/styled-components.d.ts +7 -0
- package/dist/playgrounds/styled-components.js +190 -0
- package/dist/playgrounds/tailwind.d.ts +15 -0
- package/dist/playgrounds/tailwind.js +256 -0
- package/dist/playgrounds/vue.d.ts +7 -0
- package/dist/playgrounds/vue.js +9 -0
- package/dist/report/asks.d.ts +27 -0
- package/dist/report/asks.js +28 -0
- package/dist/report/cli-report.d.ts +7 -0
- package/dist/report/cli-report.js +187 -0
- package/dist/report/codes/index.d.ts +37 -0
- package/dist/report/codes/index.js +31 -0
- package/dist/report/errorTypes/index.d.ts +92 -0
- package/dist/report/errorTypes/index.js +186 -0
- package/dist/report/evidence.d.ts +2 -0
- package/dist/report/evidence.js +8 -0
- package/dist/report/explain.d.ts +27 -0
- package/dist/report/explain.js +76 -0
- package/dist/report/index.d.ts +2 -0
- package/dist/report/index.js +24 -0
- package/dist/report/matchers/a11y/model.d.ts +20 -0
- package/dist/report/matchers/a11y/model.js +14 -0
- package/dist/report/matchers/a11y/text.d.ts +2 -0
- package/dist/report/matchers/a11y/text.js +22 -0
- package/dist/report/matchers/behavior/model.d.ts +20 -0
- package/dist/report/matchers/behavior/model.js +12 -0
- package/dist/report/matchers/behavior/text.d.ts +2 -0
- package/dist/report/matchers/behavior/text.js +22 -0
- package/dist/report/matchers/framework/model.d.ts +10 -0
- package/dist/report/matchers/framework/model.js +39 -0
- package/dist/report/matchers/framework/text.d.ts +2 -0
- package/dist/report/matchers/framework/text.js +26 -0
- package/dist/report/matchers/index.d.ts +5 -0
- package/dist/report/matchers/index.js +97 -0
- package/dist/report/matchers/name/model.d.ts +20 -0
- package/dist/report/matchers/name/model.js +30 -0
- package/dist/report/matchers/name/text.d.ts +2 -0
- package/dist/report/matchers/name/text.js +17 -0
- package/dist/report/matchers/set-model.d.ts +31 -0
- package/dist/report/matchers/set-model.js +42 -0
- package/dist/report/matchers/structure/model.d.ts +10 -0
- package/dist/report/matchers/structure/model.js +58 -0
- package/dist/report/matchers/structure/text.d.ts +2 -0
- package/dist/report/matchers/structure/text.js +40 -0
- package/dist/report/matchers/styles/model.d.ts +11 -0
- package/dist/report/matchers/styles/model.js +36 -0
- package/dist/report/matchers/styles/text.d.ts +2 -0
- package/dist/report/matchers/styles/text.js +40 -0
- package/dist/report/user-report/build/bundle.d.ts +3 -0
- package/dist/report/user-report/build/bundle.js +185 -0
- package/dist/report/user-report/build/bundle.prebuilt.js +955 -0
- package/dist/report/user-report/data/collect.d.ts +68 -0
- package/dist/report/user-report/data/collect.js +150 -0
- package/dist/report/user-report/data/folders.d.ts +3 -0
- package/dist/report/user-report/data/folders.js +73 -0
- package/dist/report/user-report/data/groups.d.ts +13 -0
- package/dist/report/user-report/data/groups.js +76 -0
- package/dist/report/user-report/data/links.d.ts +39 -0
- package/dist/report/user-report/data/links.js +222 -0
- package/dist/report/user-report/data/snapshot.d.ts +9 -0
- package/dist/report/user-report/data/snapshot.js +180 -0
- package/dist/report/user-report/data/source.d.ts +13 -0
- package/dist/report/user-report/data/source.js +58 -0
- package/dist/report/user-report/data/word.d.ts +3 -0
- package/dist/report/user-report/data/word.js +32 -0
- package/dist/report/user-report/html/shell.d.ts +2 -0
- package/dist/report/user-report/html/shell.js +27 -0
- package/dist/report/user-report/index.d.ts +7 -0
- package/dist/report/user-report/index.js +47 -0
- package/dist/report/vocabulary/evidence.d.ts +48 -0
- package/dist/report/vocabulary/evidence.js +66 -0
- package/dist/report/vocabulary/format.d.ts +4 -0
- package/dist/report/vocabulary/format.js +20 -0
- package/dist/report/vocabulary/labels.d.ts +134 -0
- package/dist/report/vocabulary/labels.js +87 -0
- package/dist/report/vocabulary/phrases.d.ts +20 -0
- package/dist/report/vocabulary/phrases.js +34 -0
- package/dist/self-catalog.d.ts +4 -0
- package/dist/self-catalog.js +179 -0
- package/dist/utils.d.ts +6 -0
- package/dist/utils.js +125 -0
- package/package.json +86 -60
- package/config.js +0 -362
- package/decision-maker.js +0 -109
- package/filter/drop-child-element.js +0 -13
- package/filter/drop-usages.js +0 -13
- package/filter/missing-component.js +0 -68
- package/index.js +0 -184
- package/logger.js +0 -44
- package/main.js +0 -539
- package/matcher/a11y.js +0 -65
- package/matcher/behavior.js +0 -31
- package/matcher/framework/index.js +0 -15
- package/matcher/framework/react.js +0 -25
- package/matcher/index.js +0 -36
- package/matcher/name.js +0 -132
- package/matcher/structure.js +0 -208
- package/normalize/candidate.js +0 -22
- package/normalize/dom.js +0 -167
- package/normalize/framework.js +0 -129
- package/normalize/style-resolver.js +0 -24
- package/normalize/style.js +0 -175
- package/normalize/words.js +0 -40
- package/playgrounds/angular.js +0 -11
- package/playgrounds/framework-entrypoint.js +0 -90
- package/playgrounds/react.js +0 -997
- package/playgrounds/scss.js +0 -7
- package/playgrounds/style-entrypoint.js +0 -78
- package/playgrounds/styled-components.js +0 -138
- package/playgrounds/tailwind.js +0 -186
- package/playgrounds/vue.js +0 -11
- package/plugin.js +0 -3
- package/report.js +0 -159
- package/utils.js +0 -92
package/playgrounds/react.js
DELETED
|
@@ -1,997 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const fs = require("node:fs");
|
|
4
|
-
const path = require("node:path");
|
|
5
|
-
const ts = require("typescript");
|
|
6
|
-
const { entries } = require("remeda");
|
|
7
|
-
const style = require("../normalize/style.js");
|
|
8
|
-
const { styleObjectToCssText, isPascalCase, isHtmlTag, INTERACTIVE_TAGS } = require("../normalize/dom.js");
|
|
9
|
-
|
|
10
|
-
const MAX_UNFOLD_DEPTH = 3;
|
|
11
|
-
const MAX_TREE_NODES = 400;
|
|
12
|
-
|
|
13
|
-
let skipNodes = null;
|
|
14
|
-
|
|
15
|
-
const stringLiterals = (node, out, depth = 0) => {
|
|
16
|
-
if (!node || depth > 4 || out.length >= 24) return;
|
|
17
|
-
if (skipNodes && skipNodes.has(node)) return;
|
|
18
|
-
if (ts.isJsxExpression(node)) {
|
|
19
|
-
stringLiterals(node.expression, out, depth);
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
|
|
23
|
-
out.push(node.text);
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
if (ts.isTemplateExpression(node)) {
|
|
27
|
-
if (node.head.text) out.push(node.head.text);
|
|
28
|
-
for (const span of node.templateSpans) {
|
|
29
|
-
if (span.literal.text) out.push(span.literal.text);
|
|
30
|
-
stringLiterals(span.expression, out, depth + 1);
|
|
31
|
-
}
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
if (ts.isObjectLiteralExpression(node)) {
|
|
35
|
-
for (const p of node.properties) stringLiterals(p.initializer, out, depth + 1);
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
if (ts.isArrayLiteralExpression(node)) {
|
|
39
|
-
for (const el of node.elements) stringLiterals(el, out, depth + 1);
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
if (ts.isCallExpression(node)) {
|
|
43
|
-
for (const arg of node.arguments) stringLiterals(arg, out, depth + 1);
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
if (ts.isConditionalExpression(node)) {
|
|
47
|
-
stringLiterals(node.whenTrue, out, depth + 1);
|
|
48
|
-
stringLiterals(node.whenFalse, out, depth + 1);
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
if (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.Plus) {
|
|
52
|
-
stringLiterals(node.left, out, depth + 1);
|
|
53
|
-
stringLiterals(node.right, out, depth + 1);
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const resolveLocalIdentifier = (name, fileSf, kind) => {
|
|
59
|
-
const found = { str: [], obj: null };
|
|
60
|
-
const walk = (n) => {
|
|
61
|
-
if (ts.isVariableDeclaration(n) && ts.isIdentifier(n.name) && n.name.text === name && n.initializer) {
|
|
62
|
-
if (kind === "str") {
|
|
63
|
-
if (variantConfigOf(n.initializer)) return;
|
|
64
|
-
const strs = [];
|
|
65
|
-
stringLiterals(n.initializer, strs);
|
|
66
|
-
found.str.push(...strs);
|
|
67
|
-
} else if (kind === "obj" && ts.isObjectLiteralExpression(n.initializer)) {
|
|
68
|
-
found.obj = n.initializer;
|
|
69
|
-
}
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
ts.forEachChild(n, walk);
|
|
73
|
-
};
|
|
74
|
-
walk(fileSf);
|
|
75
|
-
return found;
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
const styleObjectStyles = (expr, sf) => {
|
|
79
|
-
if (expr && ts.isJsxExpression(expr)) expr = expr.expression;
|
|
80
|
-
const styles = {};
|
|
81
|
-
const add = (prop, value) => {
|
|
82
|
-
if (!Object.prototype.hasOwnProperty.call(styles, prop)) styles[prop] = [];
|
|
83
|
-
if (!styles[prop].includes(value)) styles[prop].push(value);
|
|
84
|
-
};
|
|
85
|
-
const visitObj = (n) => {
|
|
86
|
-
if (!n) return;
|
|
87
|
-
if (ts.isObjectLiteralExpression(n)) {
|
|
88
|
-
for (const p of n.properties) {
|
|
89
|
-
if (!ts.isPropertyAssignment(p) || !p.name) continue;
|
|
90
|
-
const key = ts.isIdentifier(p.name) ? p.name.text : ts.isStringLiteral(p.name) ? p.name.text : null;
|
|
91
|
-
if (!key) continue;
|
|
92
|
-
const prop = style.camelToKebab(key);
|
|
93
|
-
if (ts.isStringLiteral(p.initializer)) {
|
|
94
|
-
add(prop, p.initializer.text);
|
|
95
|
-
} else if (ts.isNumericLiteral(p.initializer)) {
|
|
96
|
-
add(prop, p.initializer.text);
|
|
97
|
-
} else if (ts.isPrefixUnaryExpression(p.initializer) && ts.isNumericLiteral(p.initializer.operand)) {
|
|
98
|
-
add(prop, `${p.operator === ts.SyntaxKind.MinusToken ? "-" : ""}${p.initializer.operand.text}`);
|
|
99
|
-
} else {
|
|
100
|
-
const strs = [];
|
|
101
|
-
stringLiterals(p.initializer, strs);
|
|
102
|
-
for (const s of strs) add(prop, s);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
if (ts.isConditionalExpression(n)) {
|
|
108
|
-
visitObj(n.whenTrue);
|
|
109
|
-
visitObj(n.whenFalse);
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
if (ts.isIdentifier(n)) {
|
|
113
|
-
const r = resolveLocalIdentifier(n.text, sf, "obj");
|
|
114
|
-
if (r.obj) visitObj(r.obj);
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
visitObj(expr);
|
|
118
|
-
return styles;
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
const dottedName = (name) => {
|
|
122
|
-
if (ts.isIdentifier(name)) return name.text;
|
|
123
|
-
if (name && name.kind === ts.SyntaxKind.QualifiedName) return `${dottedName(name.left)}.${name.right.text}`;
|
|
124
|
-
if (name && name.kind === ts.SyntaxKind.PropertyAccessExpression) return `${dottedName(name.expression)}.${name.name.text}`;
|
|
125
|
-
return null;
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
const tagOf = (name) => dottedName(name);
|
|
129
|
-
const stringBranchOf = (expr, sf) => {
|
|
130
|
-
if (!expr) return null;
|
|
131
|
-
if (ts.isStringLiteral(expr) || ts.isNoSubstitutionTemplateLiteral(expr)) return expr.text;
|
|
132
|
-
if (ts.isIdentifier(expr)) {
|
|
133
|
-
const decl = rawDeclarations(sf).find((item) => item.name === expr.text);
|
|
134
|
-
if (decl) return stringBranchOf(decl.initializer, sf);
|
|
135
|
-
}
|
|
136
|
-
if (ts.isConditionalExpression(expr)) return stringBranchOf(expr.whenTrue, sf) || stringBranchOf(expr.whenFalse, sf);
|
|
137
|
-
if (ts.isParenthesizedExpression(expr)) return stringBranchOf(expr.expression, sf);
|
|
138
|
-
return null;
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
const openingOf = (node) => (ts.isJsxElement(node) ? node.openingElement : node);
|
|
142
|
-
|
|
143
|
-
const isJsxNode = (n) => ts.isJsxElement(n) || ts.isJsxSelfClosingElement(n);
|
|
144
|
-
|
|
145
|
-
const propName = (name) => (name && (ts.isIdentifier(name) || ts.isStringLiteral(name)) ? name.text : null);
|
|
146
|
-
|
|
147
|
-
const variantConfigOf = (call) => {
|
|
148
|
-
if (!call || !ts.isCallExpression(call)) return null;
|
|
149
|
-
const last = call.arguments[call.arguments.length - 1];
|
|
150
|
-
if (!last || !ts.isObjectLiteralExpression(last)) return null;
|
|
151
|
-
const member = (name) => last.properties.find((p) => ts.isPropertyAssignment(p) && propName(p.name) === name);
|
|
152
|
-
const variantsProp = member("variants");
|
|
153
|
-
if (!variantsProp || !ts.isObjectLiteralExpression(variantsProp.initializer)) return null;
|
|
154
|
-
const variants = {};
|
|
155
|
-
for (const group of variantsProp.initializer.properties) {
|
|
156
|
-
if (!ts.isPropertyAssignment(group) || !ts.isObjectLiteralExpression(group.initializer)) continue;
|
|
157
|
-
const options = {};
|
|
158
|
-
for (const option of group.initializer.properties) {
|
|
159
|
-
if (!ts.isPropertyAssignment(option)) continue;
|
|
160
|
-
const strs = [];
|
|
161
|
-
stringLiterals(option.initializer, strs);
|
|
162
|
-
options[propName(option.name)] = strs;
|
|
163
|
-
}
|
|
164
|
-
variants[propName(group.name)] = options;
|
|
165
|
-
}
|
|
166
|
-
const defaults = {};
|
|
167
|
-
const defaultsProp = member("defaultVariants");
|
|
168
|
-
if (defaultsProp && ts.isObjectLiteralExpression(defaultsProp.initializer)) {
|
|
169
|
-
for (const d of defaultsProp.initializer.properties) {
|
|
170
|
-
if (ts.isPropertyAssignment(d) && ts.isStringLiteral(d.initializer)) defaults[propName(d.name)] = d.initializer.text;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
const base = [];
|
|
174
|
-
for (const arg of call.arguments.slice(0, -1)) stringLiterals(arg, base);
|
|
175
|
-
return { base, variants, defaults };
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
const selectedVariantStrings = (call, sf, props) => {
|
|
179
|
-
if (!ts.isIdentifier(call.expression)) return null;
|
|
180
|
-
const decl = rawDeclarations(sf).find((item) => item.name === call.expression.text);
|
|
181
|
-
const config = decl ? variantConfigOf(decl.initializer) : null;
|
|
182
|
-
if (!config) return null;
|
|
183
|
-
const given = {};
|
|
184
|
-
const arg = call.arguments[0];
|
|
185
|
-
if (arg && ts.isObjectLiteralExpression(arg)) {
|
|
186
|
-
for (const p of arg.properties) {
|
|
187
|
-
if (ts.isShorthandPropertyAssignment(p)) given[p.name.text] = props ? props[p.name.text] : undefined;
|
|
188
|
-
else if (ts.isPropertyAssignment(p) && ts.isStringLiteral(p.initializer)) given[propName(p.name)] = p.initializer.text;
|
|
189
|
-
else if (ts.isPropertyAssignment(p) && ts.isIdentifier(p.initializer)) given[propName(p.name)] = props ? props[p.initializer.text] : undefined;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
const out = [...config.base];
|
|
193
|
-
for (const [group, options] of Object.entries(config.variants)) {
|
|
194
|
-
const picked = typeof given[group] === "string" ? given[group] : config.defaults[group];
|
|
195
|
-
out.push(...(options[picked] || []));
|
|
196
|
-
}
|
|
197
|
-
return out;
|
|
198
|
-
};
|
|
199
|
-
|
|
200
|
-
const collectTokens = (expr, sf, out, props) => {
|
|
201
|
-
if (!expr) return;
|
|
202
|
-
if (ts.isJsxExpression(expr)) expr = expr.expression;
|
|
203
|
-
const strs = [];
|
|
204
|
-
const skip = new Set();
|
|
205
|
-
const findVariantCalls = (n) => {
|
|
206
|
-
if (ts.isCallExpression(n)) {
|
|
207
|
-
const picked = selectedVariantStrings(n, sf, props);
|
|
208
|
-
if (picked) {
|
|
209
|
-
strs.push(...picked);
|
|
210
|
-
skip.add(n);
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
ts.forEachChild(n, findVariantCalls);
|
|
215
|
-
};
|
|
216
|
-
findVariantCalls(expr);
|
|
217
|
-
const previousSkip = skipNodes;
|
|
218
|
-
skipNodes = skip;
|
|
219
|
-
try {
|
|
220
|
-
stringLiterals(expr, strs);
|
|
221
|
-
} finally {
|
|
222
|
-
skipNodes = previousSkip;
|
|
223
|
-
}
|
|
224
|
-
const idents = new Set();
|
|
225
|
-
const visitIds = (n) => {
|
|
226
|
-
if (skip.has(n)) return;
|
|
227
|
-
if (ts.isIdentifier(n) && n.text.length > 0) idents.add(n.text);
|
|
228
|
-
ts.forEachChild(n, visitIds);
|
|
229
|
-
};
|
|
230
|
-
visitIds(expr);
|
|
231
|
-
for (const id of idents) {
|
|
232
|
-
const r = resolveLocalIdentifier(id, sf, "str");
|
|
233
|
-
for (const s of r.str) strs.push(s);
|
|
234
|
-
}
|
|
235
|
-
for (const s of strs) {
|
|
236
|
-
for (const tok of s.split(/\s+/)) {
|
|
237
|
-
if (tok && !out.has(tok)) out.add(tok);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
const moduleBindings = (sf) => {
|
|
243
|
-
const map = new Map();
|
|
244
|
-
for (const stmt of sf.statements) {
|
|
245
|
-
if (!ts.isImportDeclaration(stmt) || !stmt.importClause || !ts.isStringLiteral(stmt.moduleSpecifier)) continue;
|
|
246
|
-
const spec = stmt.moduleSpecifier.text;
|
|
247
|
-
const clause = stmt.importClause;
|
|
248
|
-
if (clause.name) map.set(clause.name.text, { spec, exported: "default" });
|
|
249
|
-
const named = clause.namedBindings;
|
|
250
|
-
if (named && ts.isNamedImports(named)) {
|
|
251
|
-
for (const el of named.elements) map.set(el.name.text, { spec, exported: (el.propertyName || el.name).text });
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
return map;
|
|
255
|
-
};
|
|
256
|
-
|
|
257
|
-
const isSourceFile = (file) => !!file && !/\.d\.ts$/.test(file) && !/[\\/]node_modules[\\/]/.test(file) && !/\.(stories|test|spec)\.[a-z]+$/.test(file);
|
|
258
|
-
|
|
259
|
-
const optionsCache = new Map();
|
|
260
|
-
let fallbackConfigPath = null;
|
|
261
|
-
|
|
262
|
-
const configPathOf = (target) => {
|
|
263
|
-
if (!target) return null;
|
|
264
|
-
const abs = path.resolve(target);
|
|
265
|
-
if (abs.endsWith(".json")) return abs;
|
|
266
|
-
return ts.findConfigFile(path.dirname(abs), ts.sys.fileExists) || null;
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
const readCompilerOptions = (configPath) => {
|
|
270
|
-
if (optionsCache.has(configPath)) return optionsCache.get(configPath);
|
|
271
|
-
let options = { allowJs: true };
|
|
272
|
-
try {
|
|
273
|
-
const parsed = ts.getParsedCommandLineOfConfigFile(configPath, {}, { ...ts.sys, onUnRecoverableConfigFileDiagnostic: () => {} });
|
|
274
|
-
if (parsed) options = { ...parsed.options, allowJs: true };
|
|
275
|
-
} catch {
|
|
276
|
-
options = { allowJs: true };
|
|
277
|
-
}
|
|
278
|
-
optionsCache.set(configPath, options);
|
|
279
|
-
return options;
|
|
280
|
-
};
|
|
281
|
-
|
|
282
|
-
const resolveModuleFile = (fromFile, spec) => {
|
|
283
|
-
const nearest = ts.findConfigFile(path.dirname(fromFile), ts.sys.fileExists) || null;
|
|
284
|
-
const configs = [nearest, fallbackConfigPath].filter((item, index, all) => item && all.indexOf(item) === index);
|
|
285
|
-
const candidates = configs.length > 0 ? configs.map(readCompilerOptions) : [{ allowJs: true }];
|
|
286
|
-
for (const options of candidates) {
|
|
287
|
-
const hit = ts.resolveModuleName(spec, fromFile, options, ts.sys).resolvedModule;
|
|
288
|
-
if (hit && isSourceFile(hit.resolvedFileName)) return hit.resolvedFileName;
|
|
289
|
-
}
|
|
290
|
-
return null;
|
|
291
|
-
};
|
|
292
|
-
|
|
293
|
-
const sourceFiles = new Map();
|
|
294
|
-
|
|
295
|
-
const sourceFileOf = (fileAbs) => {
|
|
296
|
-
if (sourceFiles.has(fileAbs)) return sourceFiles.get(fileAbs);
|
|
297
|
-
const source = fs.readFileSync(fileAbs, "utf8");
|
|
298
|
-
const kind = fileAbs.endsWith(".tsx") || fileAbs.endsWith(".jsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
299
|
-
const sf = ts.createSourceFile(fileAbs, source, ts.ScriptTarget.ES2020, true, kind);
|
|
300
|
-
sourceFiles.set(fileAbs, sf);
|
|
301
|
-
return sf;
|
|
302
|
-
};
|
|
303
|
-
|
|
304
|
-
const resolvedTags = new Map();
|
|
305
|
-
|
|
306
|
-
const exportSpecs = (sf) => {
|
|
307
|
-
const specs = [];
|
|
308
|
-
for (const stmt of sf.statements) {
|
|
309
|
-
if (!ts.isExportDeclaration(stmt) || !stmt.moduleSpecifier || !ts.isStringLiteral(stmt.moduleSpecifier)) continue;
|
|
310
|
-
const spec = stmt.moduleSpecifier.text;
|
|
311
|
-
if (!stmt.exportClause) {
|
|
312
|
-
specs.push({ spec, all: true });
|
|
313
|
-
continue;
|
|
314
|
-
}
|
|
315
|
-
if (!ts.isNamedExports(stmt.exportClause)) continue;
|
|
316
|
-
for (const el of stmt.exportClause.elements) specs.push({ spec, exported: el.name.text, local: (el.propertyName || el.name).text });
|
|
317
|
-
}
|
|
318
|
-
return specs;
|
|
319
|
-
};
|
|
320
|
-
|
|
321
|
-
const localExportNames = (sf) => {
|
|
322
|
-
const names = new Set();
|
|
323
|
-
for (const stmt of sf.statements) {
|
|
324
|
-
if (ts.isExportAssignment(stmt) && ts.isIdentifier(stmt.expression)) names.add(stmt.expression.text);
|
|
325
|
-
if (!ts.isExportDeclaration(stmt) || stmt.moduleSpecifier || !stmt.exportClause || !ts.isNamedExports(stmt.exportClause)) continue;
|
|
326
|
-
for (const el of stmt.exportClause.elements) names.add((el.propertyName || el.name).text);
|
|
327
|
-
}
|
|
328
|
-
return names;
|
|
329
|
-
};
|
|
330
|
-
|
|
331
|
-
const findExportFile = (fileAbs, name, seen) => {
|
|
332
|
-
if (seen.has(fileAbs)) return null;
|
|
333
|
-
seen.add(fileAbs);
|
|
334
|
-
const sf = sourceFileOf(fileAbs);
|
|
335
|
-
const listed = localExportNames(sf);
|
|
336
|
-
if (iterateDeclarations(sf).some((item) => item.name === name && (isExported(item.node, sf) || listed.has(name)))) return { file: fileAbs, name };
|
|
337
|
-
const specs = exportSpecs(sf);
|
|
338
|
-
const named = specs.find((exp) => !exp.all && exp.exported === name);
|
|
339
|
-
if (named) {
|
|
340
|
-
const target = resolveModuleFile(fileAbs, named.spec);
|
|
341
|
-
return target ? findExportFile(target, named.local, seen) : null;
|
|
342
|
-
}
|
|
343
|
-
for (const exp of specs.filter((item) => item.all)) {
|
|
344
|
-
const target = resolveModuleFile(fileAbs, exp.spec);
|
|
345
|
-
if (!target) continue;
|
|
346
|
-
const found = findExportFile(target, name, seen);
|
|
347
|
-
if (found) return found;
|
|
348
|
-
}
|
|
349
|
-
return null;
|
|
350
|
-
};
|
|
351
|
-
|
|
352
|
-
const memberTarget = (sf, head, prop) => {
|
|
353
|
-
let found = null;
|
|
354
|
-
const fromObject = (obj) => {
|
|
355
|
-
for (const p of obj.properties) {
|
|
356
|
-
if (ts.isShorthandPropertyAssignment(p) && p.name.text === prop) return prop;
|
|
357
|
-
if (ts.isPropertyAssignment(p) && propName(p.name) === prop && ts.isIdentifier(p.initializer)) return p.initializer.text;
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
return null;
|
|
361
|
-
};
|
|
362
|
-
const visit = (n) => {
|
|
363
|
-
if (found) return;
|
|
364
|
-
if (ts.isVariableDeclaration(n) && ts.isIdentifier(n.name) && n.name.text === head && n.initializer && ts.isCallExpression(n.initializer)) {
|
|
365
|
-
for (const arg of n.initializer.arguments) if (!found && ts.isObjectLiteralExpression(arg)) found = fromObject(arg);
|
|
366
|
-
}
|
|
367
|
-
if (ts.isBinaryExpression(n) && n.operatorToken.kind === ts.SyntaxKind.EqualsToken && ts.isPropertyAccessExpression(n.left) && ts.isIdentifier(n.left.expression) && n.left.expression.text === head && n.left.name.text === prop && ts.isIdentifier(n.right)) found = n.right.text;
|
|
368
|
-
ts.forEachChild(n, visit);
|
|
369
|
-
};
|
|
370
|
-
visit(sf);
|
|
371
|
-
|
|
372
|
-
return found;
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
const resolveTag = (name, fileAbs, depth, props, styledResolver) => {
|
|
376
|
-
const staticProps = Object.entries(props || {}).filter(([, v]) => typeof v === "string").sort();
|
|
377
|
-
const key = `${fileAbs}::${name}::${JSON.stringify(staticProps)}`;
|
|
378
|
-
if (resolvedTags.has(key)) return resolvedTags.get(key);
|
|
379
|
-
if (depth > MAX_UNFOLD_DEPTH) return null;
|
|
380
|
-
const sf = sourceFileOf(fileAbs);
|
|
381
|
-
const dot = name.indexOf(".");
|
|
382
|
-
const head = dot > 0 ? name.slice(0, dot) : name;
|
|
383
|
-
const member = dot > 0 ? name.slice(dot + 1) : null;
|
|
384
|
-
const binding = moduleBindings(sf).get(head);
|
|
385
|
-
if (!binding) return null;
|
|
386
|
-
const imported = resolveModuleFile(fileAbs, binding.spec);
|
|
387
|
-
if (!imported) return null;
|
|
388
|
-
const located = findExportFile(imported, binding.exported, new Set());
|
|
389
|
-
if (!located) return null;
|
|
390
|
-
const targetSf = sourceFileOf(located.file);
|
|
391
|
-
const declName = member ? memberTarget(targetSf, located.name, member) : located.name;
|
|
392
|
-
if (!declName) return null;
|
|
393
|
-
const decl = iterateDeclarations(targetSf).find((item) => item.name === declName);
|
|
394
|
-
if (!decl) return null;
|
|
395
|
-
const inner = firstComponentNodeIn(decl.body);
|
|
396
|
-
if (!inner) return null;
|
|
397
|
-
resolvedTags.set(key, null);
|
|
398
|
-
const built = buildTree(inner, targetSf, { bodies: localComponentBodies(targetSf), depth: depth + 1, nodes: 0, props: Object.fromEntries(staticProps), styledResolver, resolveTag: (tag, nested) => resolveTag(tag, located.file, depth + 1, nested, styledResolver) });
|
|
399
|
-
const tree = built && { ...built, via: [declName, ...(built.via || [])] };
|
|
400
|
-
resolvedTags.set(key, tree);
|
|
401
|
-
return tree;
|
|
402
|
-
};
|
|
403
|
-
|
|
404
|
-
const localComponentBodies = (sf) => {
|
|
405
|
-
const map = new Map();
|
|
406
|
-
for (const item of rawDeclarations(sf)) {
|
|
407
|
-
if (!isPascalCase(item.name)) continue;
|
|
408
|
-
const body = componentBody(item.initializer, sf);
|
|
409
|
-
if (body) map.set(item.name, body);
|
|
410
|
-
}
|
|
411
|
-
return map;
|
|
412
|
-
};
|
|
413
|
-
|
|
414
|
-
const jsxNodesIn = (expr) => {
|
|
415
|
-
if (!expr) return [];
|
|
416
|
-
if (isJsxNode(expr) || ts.isJsxFragment(expr)) return [expr];
|
|
417
|
-
if (ts.isJsxExpression(expr) || ts.isParenthesizedExpression(expr) || ts.isAsExpression(expr) || ts.isTypeAssertionExpression(expr) || ts.isSatisfiesExpression(expr) || ts.isNonNullExpression(expr)) return jsxNodesIn(expr.expression);
|
|
418
|
-
if (ts.isConditionalExpression(expr)) return [...jsxNodesIn(expr.whenTrue), ...jsxNodesIn(expr.whenFalse)];
|
|
419
|
-
if (ts.isBinaryExpression(expr)) {
|
|
420
|
-
const op = expr.operatorToken.kind;
|
|
421
|
-
if (op === ts.SyntaxKind.AmpersandAmpersandToken) return jsxNodesIn(expr.right);
|
|
422
|
-
if (op === ts.SyntaxKind.BarBarToken || op === ts.SyntaxKind.QuestionQuestionToken) return [...jsxNodesIn(expr.left), ...jsxNodesIn(expr.right)];
|
|
423
|
-
|
|
424
|
-
return [];
|
|
425
|
-
}
|
|
426
|
-
if (ts.isArrayLiteralExpression(expr)) return expr.elements.flatMap((el) => jsxNodesIn(el));
|
|
427
|
-
if (ts.isCallExpression(expr)) return expr.arguments.flatMap((arg) => jsxNodesIn(arg));
|
|
428
|
-
if (ts.isArrowFunction(expr) || ts.isFunctionExpression(expr) || ts.isFunctionDeclaration(expr)) return jsxNodesIn(expr.body);
|
|
429
|
-
if (ts.isBlock(expr)) return expr.statements.filter((stmt) => ts.isReturnStatement(stmt)).flatMap((stmt) => jsxNodesIn(stmt.expression));
|
|
430
|
-
|
|
431
|
-
return [];
|
|
432
|
-
};
|
|
433
|
-
|
|
434
|
-
const firstComponentNodeIn = (body) => jsxNodesIn(body)[0] || null;
|
|
435
|
-
|
|
436
|
-
const buildTree = (node, sf, ctx) => {
|
|
437
|
-
if (ts.isJsxFragment(node)) return fragmentTree(node, sf, ctx);
|
|
438
|
-
if (ctx.nodes >= MAX_TREE_NODES) return null;
|
|
439
|
-
ctx.nodes += 1;
|
|
440
|
-
const opening = openingOf(node);
|
|
441
|
-
const rawTag = tagOf(opening.tagName);
|
|
442
|
-
const tag = rawTag && !isHtmlTag(rawTag) ? stringBranchOf(ts.isIdentifier(opening.tagName) ? opening.tagName : null, sf) || rawTag : rawTag;
|
|
443
|
-
if (!tag) return null;
|
|
444
|
-
const attrs = {};
|
|
445
|
-
let className = null;
|
|
446
|
-
const styleObj = {};
|
|
447
|
-
let dynamic = false;
|
|
448
|
-
const dynamicRefs = [];
|
|
449
|
-
for (const attr of opening.attributes.properties) {
|
|
450
|
-
if (!ts.isJsxAttribute(attr) || !attr.name) continue;
|
|
451
|
-
const aname = attr.name.getText(sf);
|
|
452
|
-
const value = attr.initializer;
|
|
453
|
-
const strs = [];
|
|
454
|
-
if (value) stringLiterals(value, strs);
|
|
455
|
-
if (aname === "className" || aname === "class") {
|
|
456
|
-
const tokens = new Set();
|
|
457
|
-
collectTokens(value, sf, tokens, ctx.props);
|
|
458
|
-
if (tokens.size > 0) className = [...tokens].sort().join(" ");
|
|
459
|
-
else if (value) dynamic = true;
|
|
460
|
-
} else if (aname === "style") {
|
|
461
|
-
const so = styleObjectStyles(value, sf);
|
|
462
|
-
for (const [k, v] of Object.entries(so)) styleObj[k] = v;
|
|
463
|
-
} else {
|
|
464
|
-
attrs[aname] = strs.length > 0 ? strs[0] : value ? null : true;
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
const children = childTrees(node, sf, ctx);
|
|
468
|
-
const spread = opening.attributes.properties.some((attr) => ts.isJsxSpreadAttribute(attr));
|
|
469
|
-
if (spread) {
|
|
470
|
-
children.push({ tag: "children", attrs: {}, className: null, style: null, cssText: "", children: [] });
|
|
471
|
-
}
|
|
472
|
-
const tree = {
|
|
473
|
-
tag,
|
|
474
|
-
spread,
|
|
475
|
-
attrs,
|
|
476
|
-
className,
|
|
477
|
-
style: Object.keys(styleObj).length > 0 ? styleObj : null,
|
|
478
|
-
cssText: styleObjectToCssText(styleObj),
|
|
479
|
-
children,
|
|
480
|
-
};
|
|
481
|
-
if (!isHtmlTag(tag) && !ctx.noUnfold) {
|
|
482
|
-
const body = ctx.bodies.get(tag);
|
|
483
|
-
if (body && ctx.depth < MAX_UNFOLD_DEPTH) {
|
|
484
|
-
const inner = firstComponentNodeIn(body);
|
|
485
|
-
if (inner) {
|
|
486
|
-
const unfolded = buildTree(inner, sf, { ...ctx, depth: ctx.depth + 1, props: attrs });
|
|
487
|
-
if (unfolded) return graftChildren({ ...unfolded, via: [tag, ...(unfolded.via || [])] }, children, tree);
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
const resolved = ctx.resolveTag ? ctx.resolveTag(tag, attrs) : null;
|
|
491
|
-
if (resolved) return graftChildren(resolved, children, tree);
|
|
492
|
-
const styledInfo = ctx.styledResolver ? ctx.styledResolver(tag, sf) : null;
|
|
493
|
-
if (styledInfo) return styledNode(tag, styledInfo, tree, children, sf, ctx);
|
|
494
|
-
dynamic = true;
|
|
495
|
-
dynamicRefs.push(tag);
|
|
496
|
-
}
|
|
497
|
-
if (dynamic) {
|
|
498
|
-
tree.dynamic = true;
|
|
499
|
-
if (dynamicRefs.length > 0) tree.dynamicRefs = dynamicRefs;
|
|
500
|
-
}
|
|
501
|
-
return tree;
|
|
502
|
-
};
|
|
503
|
-
|
|
504
|
-
const fragmentTree = (node, sf, ctx) => ({
|
|
505
|
-
tag: "fragment",
|
|
506
|
-
attrs: {},
|
|
507
|
-
className: null,
|
|
508
|
-
style: null,
|
|
509
|
-
cssText: "",
|
|
510
|
-
children: childTrees(node, sf, ctx),
|
|
511
|
-
});
|
|
512
|
-
|
|
513
|
-
const childTrees = (node, sf, ctx) => {
|
|
514
|
-
const children = [];
|
|
515
|
-
for (const child of node.children || []) {
|
|
516
|
-
if (isJsxNode(child) || ts.isJsxFragment(child)) {
|
|
517
|
-
const t = buildTree(child, sf, ctx);
|
|
518
|
-
if (t) children.push(t);
|
|
519
|
-
} else if (ts.isJsxExpression(child)) {
|
|
520
|
-
const found = jsxNodesIn(child.expression);
|
|
521
|
-
for (const inner of found) {
|
|
522
|
-
const t = buildTree(inner, sf, ctx);
|
|
523
|
-
if (t) children.push(t);
|
|
524
|
-
}
|
|
525
|
-
if (found.length === 0 && child.expression && ts.isIdentifier(child.expression) && child.expression.text === "children") {
|
|
526
|
-
children.push({ tag: "children", attrs: {}, className: null, style: null, cssText: "", children: [] });
|
|
527
|
-
}
|
|
528
|
-
} else if (ts.isJsxText(child)) {
|
|
529
|
-
const text = child.text.replace(/\s+/g, " ").trim();
|
|
530
|
-
if (text) children.push({ tag: "text", text, attrs: {}, className: null, style: null, cssText: "", children: [] });
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
return children;
|
|
534
|
-
};
|
|
535
|
-
|
|
536
|
-
const mergeClass = (a, b) => [...new Set([...(a ? a.split(/\s+/) : []), ...(b ? b.split(/\s+/) : [])])].sort().join(" ") || null;
|
|
537
|
-
|
|
538
|
-
const fillSlot = (node, children) => {
|
|
539
|
-
const kids = node.children || [];
|
|
540
|
-
const index = kids.findIndex((child) => child.tag === "children");
|
|
541
|
-
if (index >= 0) return { node: { ...node, children: [...kids.slice(0, index), ...children, ...kids.slice(index + 1)] }, done: true };
|
|
542
|
-
for (let i = 0; i < kids.length; i += 1) {
|
|
543
|
-
const filled = fillSlot(kids[i], children);
|
|
544
|
-
if (filled.done) return { node: { ...node, children: [...kids.slice(0, i), filled.node, ...kids.slice(i + 1)] }, done: true };
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
return { node, done: false };
|
|
548
|
-
};
|
|
549
|
-
|
|
550
|
-
const graftChildren = (resolved, children, usage) => {
|
|
551
|
-
if (resolved.tag === "fragment") return children.length > 0 ? fillSlot(resolved, children).node : resolved;
|
|
552
|
-
const clone = { ...resolved, spread: usage.spread === true, attrs: { ...(resolved.attrs || {}), ...(usage.attrs || {}) }, className: mergeClass(resolved.className, usage.className) };
|
|
553
|
-
|
|
554
|
-
return fillSlot(clone, children).node;
|
|
555
|
-
};
|
|
556
|
-
|
|
557
|
-
const styledNode = (tag, info, usage, children, sf, ctx) => {
|
|
558
|
-
let base = null;
|
|
559
|
-
if (info.componentRef) {
|
|
560
|
-
const body = ctx.bodies.get(info.componentRef);
|
|
561
|
-
if (body && ctx.depth < MAX_UNFOLD_DEPTH) {
|
|
562
|
-
const inner = firstComponentNodeIn(body);
|
|
563
|
-
if (inner) base = buildTree(inner, sf, { ...ctx, depth: ctx.depth + 1, props: {} });
|
|
564
|
-
}
|
|
565
|
-
if (!base) base = ctx.resolveTag ? ctx.resolveTag(info.componentRef, {}) : null;
|
|
566
|
-
}
|
|
567
|
-
if (base) {
|
|
568
|
-
const merged = { ...(base.style || {}) };
|
|
569
|
-
for (const [k, v] of Object.entries(info.styleObj || {})) merged[k] = v;
|
|
570
|
-
const clone = { ...base, style: Object.keys(merged).length > 0 ? merged : null, cssText: styleObjectToCssText(merged), via: [tag, ...(base.via || [])] };
|
|
571
|
-
return graftChildren(clone, children, usage);
|
|
572
|
-
}
|
|
573
|
-
return {
|
|
574
|
-
tag: info.baseTag || tag,
|
|
575
|
-
spread: usage.spread,
|
|
576
|
-
attrs: usage.attrs,
|
|
577
|
-
className: usage.className,
|
|
578
|
-
style: Object.keys(info.styleObj || {}).length > 0 ? info.styleObj : null,
|
|
579
|
-
cssText: styleObjectToCssText(info.styleObj || {}),
|
|
580
|
-
children,
|
|
581
|
-
via: [tag],
|
|
582
|
-
};
|
|
583
|
-
};
|
|
584
|
-
|
|
585
|
-
const a11yOfOpening = (opening, sf, out) => {
|
|
586
|
-
for (const attr of opening.attributes.properties) {
|
|
587
|
-
if (!ts.isJsxAttribute(attr) || !attr.name) continue;
|
|
588
|
-
const aname = attr.name.getText(sf);
|
|
589
|
-
if (aname === "role" || aname.startsWith("aria-")) {
|
|
590
|
-
const strs = [];
|
|
591
|
-
if (attr.initializer) stringLiterals(attr.initializer, strs);
|
|
592
|
-
out.add(`${aname}:${strs[0] ?? "true"}`);
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
};
|
|
596
|
-
|
|
597
|
-
const gatherA11y = (node, sf, out) => {
|
|
598
|
-
const visit = (n) => {
|
|
599
|
-
if (isJsxNode(n)) a11yOfOpening(openingOf(n), sf, out);
|
|
600
|
-
ts.forEachChild(n, visit);
|
|
601
|
-
};
|
|
602
|
-
visit(node);
|
|
603
|
-
};
|
|
604
|
-
|
|
605
|
-
const rootTagsOf = (node, sf) => {
|
|
606
|
-
const out = new Set();
|
|
607
|
-
const visit = (n, parent) => {
|
|
608
|
-
if (n !== node && isJsxNode(n)) {
|
|
609
|
-
const isRoot = !parent || !(isJsxNode(parent) || ts.isJsxFragment(parent));
|
|
610
|
-
if (isRoot) {
|
|
611
|
-
const tag = tagOf(openingOf(n).tagName);
|
|
612
|
-
if (tag) out.add(tag);
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
ts.forEachChild(n, (c) => visit(c, n));
|
|
616
|
-
};
|
|
617
|
-
visit(node, null);
|
|
618
|
-
return out;
|
|
619
|
-
};
|
|
620
|
-
|
|
621
|
-
const viaNames = (tree, names, out = new Set()) => {
|
|
622
|
-
if (!tree) return out;
|
|
623
|
-
for (const name of tree.via || []) if (names.has(name)) out.add(name);
|
|
624
|
-
for (const child of tree.children || []) viaNames(child, names, out);
|
|
625
|
-
|
|
626
|
-
return out;
|
|
627
|
-
};
|
|
628
|
-
|
|
629
|
-
const canonicalTagsUsed = (node, names) => {
|
|
630
|
-
const found = new Set();
|
|
631
|
-
const visit = (n) => {
|
|
632
|
-
if (isJsxNode(n)) {
|
|
633
|
-
const opening = openingOf(n);
|
|
634
|
-
if (ts.isIdentifier(opening.tagName) && names.has(opening.tagName.text)) found.add(opening.tagName.text);
|
|
635
|
-
}
|
|
636
|
-
ts.forEachChild(n, visit);
|
|
637
|
-
};
|
|
638
|
-
visit(node);
|
|
639
|
-
return found;
|
|
640
|
-
};
|
|
641
|
-
|
|
642
|
-
const hasComponentMarkup = (node) => {
|
|
643
|
-
let found = false;
|
|
644
|
-
const visit = (n) => {
|
|
645
|
-
if (found) return;
|
|
646
|
-
if (isJsxNode(n)) {
|
|
647
|
-
found = true;
|
|
648
|
-
return;
|
|
649
|
-
}
|
|
650
|
-
ts.forEachChild(n, visit);
|
|
651
|
-
};
|
|
652
|
-
visit(node);
|
|
653
|
-
return found;
|
|
654
|
-
};
|
|
655
|
-
|
|
656
|
-
const isExported = (node, sf) => {
|
|
657
|
-
const mods = ts.getModifiers(node) || [];
|
|
658
|
-
if (mods.some((m) => m.kind === ts.SyntaxKind.ExportKeyword)) return true;
|
|
659
|
-
const parent = node.parent;
|
|
660
|
-
if (parent && (ts.isExportDeclaration(parent) || ts.isExportAssignment(parent))) return true;
|
|
661
|
-
return false;
|
|
662
|
-
};
|
|
663
|
-
|
|
664
|
-
const unwrap = (expr) => {
|
|
665
|
-
let current = expr;
|
|
666
|
-
while (current && (ts.isParenthesizedExpression(current) || ts.isAsExpression(current) || ts.isTypeAssertionExpression(current) || ts.isSatisfiesExpression(current))) current = current.expression;
|
|
667
|
-
return current;
|
|
668
|
-
};
|
|
669
|
-
|
|
670
|
-
const componentBody = (expr, sf) => {
|
|
671
|
-
if (!expr) return null;
|
|
672
|
-
if (ts.isArrowFunction(expr) || ts.isFunctionExpression(expr)) return unwrap(expr.body);
|
|
673
|
-
if (ts.isCallExpression(expr)) {
|
|
674
|
-
for (const arg of expr.arguments) {
|
|
675
|
-
const body = componentBody(arg, sf);
|
|
676
|
-
if (body) return body;
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
if (ts.isParenthesizedExpression(expr)) return componentBody(expr.expression, sf);
|
|
680
|
-
if (ts.isObjectLiteralExpression(expr)) {
|
|
681
|
-
const rendering = expr.properties.find((prop) => ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name) && prop.name.text === "body");
|
|
682
|
-
if (rendering) return componentBody(rendering.initializer, sf);
|
|
683
|
-
if (expr.properties.some((prop) => ts.isMethodDeclaration(prop) && ts.isIdentifier(prop.name) && prop.name.text === "body")) {
|
|
684
|
-
const method = expr.properties.find((prop) => ts.isMethodDeclaration(prop) && prop.name.text === "body");
|
|
685
|
-
|
|
686
|
-
return unwrap(method.body);
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
if (ts.isIdentifier(expr) && sf) {
|
|
690
|
-
const decl = rawDeclarations(sf).find((item) => item.name === expr.text);
|
|
691
|
-
if (decl) return componentBody(decl.initializer, null);
|
|
692
|
-
}
|
|
693
|
-
return null;
|
|
694
|
-
};
|
|
695
|
-
|
|
696
|
-
const rawDeclarations = (sf) => {
|
|
697
|
-
const out = [];
|
|
698
|
-
const visit = (node) => {
|
|
699
|
-
if (ts.isVariableStatement(node)) {
|
|
700
|
-
for (const d of node.declarationList.declarations) {
|
|
701
|
-
if (ts.isIdentifier(d.name)) out.push({ name: d.name.text, initializer: d.initializer });
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
if (ts.isFunctionDeclaration(node) && node.name) out.push({ name: node.name.text, initializer: node });
|
|
705
|
-
ts.forEachChild(node, visit);
|
|
706
|
-
};
|
|
707
|
-
visit(sf);
|
|
708
|
-
return out;
|
|
709
|
-
};
|
|
710
|
-
|
|
711
|
-
const iterateDeclarations = (sf) => {
|
|
712
|
-
const found = [];
|
|
713
|
-
const visit = (n) => {
|
|
714
|
-
if (ts.isFunctionDeclaration(n) && n.name && isPascalCase(n.name.text) && n.body) {
|
|
715
|
-
found.push({ name: n.name.text, body: n.body, node: n });
|
|
716
|
-
}
|
|
717
|
-
if (ts.isVariableStatement(n)) {
|
|
718
|
-
for (const d of n.declarationList.declarations) {
|
|
719
|
-
if (!ts.isIdentifier(d.name) || !isPascalCase(d.name.text)) continue;
|
|
720
|
-
const body = componentBody(d.initializer, sf);
|
|
721
|
-
if (body) found.push({ name: d.name.text, body, node: n });
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
ts.forEachChild(n, visit);
|
|
725
|
-
};
|
|
726
|
-
visit(sf);
|
|
727
|
-
return found;
|
|
728
|
-
};
|
|
729
|
-
|
|
730
|
-
const useConfig = (opts) => {
|
|
731
|
-
fallbackConfigPath = configPathOf(opts && opts.tsconfig && path.resolve(opts.root || process.cwd(), opts.tsconfig));
|
|
732
|
-
};
|
|
733
|
-
|
|
734
|
-
const parseElements = (fileAbs, canonicalNames, opts) => {
|
|
735
|
-
useConfig(opts);
|
|
736
|
-
const sf = sourceFileOf(fileAbs);
|
|
737
|
-
const bodies = localComponentBodies(sf);
|
|
738
|
-
const out = [];
|
|
739
|
-
const visit = (n) => {
|
|
740
|
-
if (isJsxNode(n)) {
|
|
741
|
-
const opening = openingOf(n);
|
|
742
|
-
const tag = tagOf(opening.tagName);
|
|
743
|
-
if (tag && isHtmlTag(tag)) {
|
|
744
|
-
const tree = buildTree(n, sf, { bodies, depth: 0, nodes: 0, props: {}, styledResolver: opts.styledResolver, resolveTag: (name, props) => resolveTag(name, fileAbs, 0, props, opts.styledResolver) });
|
|
745
|
-
if (tree) {
|
|
746
|
-
const a11y = new Set();
|
|
747
|
-
a11yOfOpening(opening, sf, a11y);
|
|
748
|
-
const pos = sf.getLineAndCharacterOfPosition(opening.getStart(sf));
|
|
749
|
-
out.push({ name: tag, line: pos.line + 1, column: pos.character, tree: { ...tree, children: [] }, rootTags: [tag], a11y: [...a11y].sort() });
|
|
750
|
-
}
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
ts.forEachChild(n, visit);
|
|
754
|
-
};
|
|
755
|
-
visit(sf);
|
|
756
|
-
return out;
|
|
757
|
-
};
|
|
758
|
-
|
|
759
|
-
const MIN_PART_NODES = 3;
|
|
760
|
-
const MAX_PART_NODES = 60;
|
|
761
|
-
|
|
762
|
-
const jsxCount = (node) => {
|
|
763
|
-
let count = 0;
|
|
764
|
-
const visit = (n) => {
|
|
765
|
-
if (isJsxNode(n)) count += 1;
|
|
766
|
-
ts.forEachChild(n, visit);
|
|
767
|
-
};
|
|
768
|
-
visit(node);
|
|
769
|
-
|
|
770
|
-
return count;
|
|
771
|
-
};
|
|
772
|
-
|
|
773
|
-
const treeSize = (tree) => (tree ? (tree.tag === "fragment" || tree.tag === "children" ? 0 : 1) + (tree.children || []).reduce((sum, child) => sum + treeSize(child), 0) : 0);
|
|
774
|
-
|
|
775
|
-
const parseParts = (fileAbs, canonicalNames, opts) => {
|
|
776
|
-
useConfig(opts);
|
|
777
|
-
const sf = sourceFileOf(fileAbs);
|
|
778
|
-
const bodies = localComponentBodies(sf);
|
|
779
|
-
const names = canonicalNames || new Set();
|
|
780
|
-
const out = [];
|
|
781
|
-
for (const decl of iterateDeclarations(sf)) {
|
|
782
|
-
if (!hasComponentMarkup(decl.body)) continue;
|
|
783
|
-
const root = firstComponentNodeIn(decl.body);
|
|
784
|
-
const visit = (n) => {
|
|
785
|
-
if (n !== root && isJsxNode(n)) {
|
|
786
|
-
const size = jsxCount(n);
|
|
787
|
-
if (size >= MIN_PART_NODES && size <= MAX_PART_NODES) {
|
|
788
|
-
const tree = buildTree(n, sf, { bodies, depth: 0, nodes: 0, props: {}, styledResolver: opts.styledResolver, resolveTag: (name, props) => resolveTag(name, fileAbs, 0, props, opts.styledResolver) });
|
|
789
|
-
if (tree && treeSize(tree) >= MIN_PART_NODES) {
|
|
790
|
-
const rootTag = tagOf(openingOf(n).tagName);
|
|
791
|
-
const a11y = new Set();
|
|
792
|
-
gatherA11y(n, sf, a11y);
|
|
793
|
-
const start = sf.getLineAndCharacterOfPosition(n.getStart(sf));
|
|
794
|
-
const end = sf.getLineAndCharacterOfPosition(n.getEnd());
|
|
795
|
-
out.push({
|
|
796
|
-
name: `part@${start.line + 1}`,
|
|
797
|
-
owner: decl.name,
|
|
798
|
-
rootTag,
|
|
799
|
-
line: start.line + 1,
|
|
800
|
-
column: start.character,
|
|
801
|
-
endLine: end.line + 1,
|
|
802
|
-
tree,
|
|
803
|
-
rootTags: [rootTag],
|
|
804
|
-
a11y: [...a11y].sort(),
|
|
805
|
-
usesCanonical: names.size > 0 ? [...new Set([...canonicalTagsUsed(n, names), ...viaNames(tree, names)])].sort() : [],
|
|
806
|
-
});
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
}
|
|
810
|
-
ts.forEachChild(n, visit);
|
|
811
|
-
};
|
|
812
|
-
visit(decl.body);
|
|
813
|
-
}
|
|
814
|
-
|
|
815
|
-
return out;
|
|
816
|
-
};
|
|
817
|
-
|
|
818
|
-
const declaredPropsOf = (node, sf) => {
|
|
819
|
-
let fn = null;
|
|
820
|
-
if (ts.isFunctionDeclaration(node)) fn = node;
|
|
821
|
-
else if (ts.isVariableStatement(node)) {
|
|
822
|
-
for (const d of node.declarationList.declarations) {
|
|
823
|
-
const init = d.initializer;
|
|
824
|
-
if (init && (ts.isArrowFunction(init) || ts.isFunctionExpression(init))) fn = init;
|
|
825
|
-
break;
|
|
826
|
-
}
|
|
827
|
-
}
|
|
828
|
-
if (!fn || !fn.parameters || fn.parameters.length === 0) return [];
|
|
829
|
-
const param = fn.parameters[0];
|
|
830
|
-
const out = [];
|
|
831
|
-
const visit = (n) => {
|
|
832
|
-
if (!n) return;
|
|
833
|
-
if (ts.isObjectBindingPattern(n)) {
|
|
834
|
-
for (const el of n.elements) {
|
|
835
|
-
if (ts.isIdentifier(el.name)) out.push(el.name.text);
|
|
836
|
-
else visit(el.name);
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
};
|
|
840
|
-
if (ts.isObjectBindingPattern(param.name)) visit(param.name);
|
|
841
|
-
return [...new Set(out)].sort();
|
|
842
|
-
};
|
|
843
|
-
|
|
844
|
-
const parseComponents = (fileAbs, canonicalNames, opts) => {
|
|
845
|
-
useConfig(opts);
|
|
846
|
-
const source = fs.readFileSync(fileAbs, "utf8");
|
|
847
|
-
const kind = fileAbs.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
848
|
-
const sf = ts.createSourceFile(fileAbs, source, ts.ScriptTarget.ES2020, true, kind);
|
|
849
|
-
const canonNameSet = canonicalNames || new Set();
|
|
850
|
-
const bodies = localComponentBodies(sf);
|
|
851
|
-
const out = [];
|
|
852
|
-
let decls = iterateDeclarations(sf);
|
|
853
|
-
if (decls.length === 0 && hasComponentMarkup(sf)) {
|
|
854
|
-
const base = path.basename(fileAbs, path.extname(fileAbs));
|
|
855
|
-
const name = base
|
|
856
|
-
.split(/[-_ ]+/)
|
|
857
|
-
.filter(Boolean)
|
|
858
|
-
.map((w) => w[0].toUpperCase() + w.slice(1))
|
|
859
|
-
.join("");
|
|
860
|
-
decls = [{ name, body: sf, node: sf }];
|
|
861
|
-
}
|
|
862
|
-
for (const d of decls) {
|
|
863
|
-
if (!hasComponentMarkup(d.body)) continue;
|
|
864
|
-
const root = firstComponentNodeIn(d.body);
|
|
865
|
-
const tree = root ? buildTree(root, sf, { bodies, depth: 0, nodes: 0, styledResolver: opts.styledResolver, resolveTag: (tag, props) => resolveTag(tag, fileAbs, 0, props, opts.styledResolver) }) : null;
|
|
866
|
-
const rawTree = root ? buildTree(root, sf, { bodies, depth: 0, nodes: 0, noUnfold: true }) : null;
|
|
867
|
-
const a11y = new Set();
|
|
868
|
-
gatherA11y(d.body, sf, a11y);
|
|
869
|
-
out.push({
|
|
870
|
-
name: d.name,
|
|
871
|
-
line: sf.getLineAndCharacterOfPosition(d.node.getStart(sf)).line + 1,
|
|
872
|
-
isExported: isExported(d.node, sf) || localExportNames(sf).has(d.name),
|
|
873
|
-
declProps: declaredPropsOf(d.node, sf),
|
|
874
|
-
tree,
|
|
875
|
-
rawTree,
|
|
876
|
-
rootTags: [...rootTagsOf(d.body, sf)].sort(),
|
|
877
|
-
actions: [],
|
|
878
|
-
a11y: [...a11y].sort(),
|
|
879
|
-
usesCanonical: canonNameSet.size > 0 ? [...new Set([...canonicalTagsUsed(d.body, canonNameSet), ...viaNames(tree, canonNameSet)])].sort() : [],
|
|
880
|
-
});
|
|
881
|
-
}
|
|
882
|
-
return out;
|
|
883
|
-
};
|
|
884
|
-
|
|
885
|
-
let JSDOM = null;
|
|
886
|
-
|
|
887
|
-
const loadJSDOM = () => {
|
|
888
|
-
if (JSDOM) return JSDOM;
|
|
889
|
-
try {
|
|
890
|
-
JSDOM = require("jsdom").JSDOM;
|
|
891
|
-
} catch {
|
|
892
|
-
JSDOM = null;
|
|
893
|
-
}
|
|
894
|
-
return JSDOM;
|
|
895
|
-
};
|
|
896
|
-
|
|
897
|
-
const buildDom = (tree, doc) => {
|
|
898
|
-
if (!tree) return null;
|
|
899
|
-
if (tree.tag === "fragment" || tree.tag === "children") {
|
|
900
|
-
const holder = doc.createElement("cu-root");
|
|
901
|
-
for (const child of tree.children || []) {
|
|
902
|
-
const node = buildDom(child, doc);
|
|
903
|
-
if (node) holder.appendChild(node);
|
|
904
|
-
}
|
|
905
|
-
return holder;
|
|
906
|
-
}
|
|
907
|
-
const tag = String(tree.tag || "").toLowerCase();
|
|
908
|
-
const el = isHtmlTag(tag) ? doc.createElement(tag) : doc.createElement("cu-root");
|
|
909
|
-
for (const [key, value] of entries(tree.attrs || {})) {
|
|
910
|
-
if (value === true) el.setAttribute(key, "");
|
|
911
|
-
else if (value !== null && value !== undefined) el.setAttribute(key, String(value));
|
|
912
|
-
}
|
|
913
|
-
if (tree.className) el.className = tree.className;
|
|
914
|
-
if (tree.style) {
|
|
915
|
-
for (const [prop, values] of entries(tree.style)) el.style.setProperty(prop, Array.isArray(values) ? values.join(" ") : String(values));
|
|
916
|
-
}
|
|
917
|
-
for (const child of tree.children || []) {
|
|
918
|
-
if (child.tag === "text") {
|
|
919
|
-
el.appendChild(doc.createTextNode(child.text || ""));
|
|
920
|
-
continue;
|
|
921
|
-
}
|
|
922
|
-
const node = buildDom(child, doc);
|
|
923
|
-
if (node) el.appendChild(node);
|
|
924
|
-
}
|
|
925
|
-
return el;
|
|
926
|
-
};;
|
|
927
|
-
|
|
928
|
-
const nodesOf = (el, win, baselineOf) => {
|
|
929
|
-
const kids = [...el.children].flatMap((child) => nodesOf(child, win, baselineOf));
|
|
930
|
-
if (el.tagName === "CU-ROOT") return kids;
|
|
931
|
-
const computed = win.getComputedStyle(el);
|
|
932
|
-
const base = baselineOf(el);
|
|
933
|
-
const css = {};
|
|
934
|
-
for (const prop of style.LAYOUT_PROPS) {
|
|
935
|
-
const value = computed.getPropertyValue(prop).trim();
|
|
936
|
-
if (value && value !== base.getPropertyValue(prop).trim()) css[prop] = value;
|
|
937
|
-
}
|
|
938
|
-
const tag = el.tagName.toLowerCase();
|
|
939
|
-
const interactive = INTERACTIVE_TAGS.has(tag) || el.getAttribute("role") === "button" || el.getAttribute("role") === "link" || [...el.attributes].some((attr) => /^(on|@)/.test(attr.name));
|
|
940
|
-
const text = [...el.childNodes].filter((n) => n.nodeType === 3).map((n) => n.nodeValue).join("").replace(/\s+/g, " ").trim();
|
|
941
|
-
|
|
942
|
-
return [{ tag, css, interactive, text: text || undefined, children: kids }];
|
|
943
|
-
};
|
|
944
|
-
|
|
945
|
-
let sharedDom = null;
|
|
946
|
-
|
|
947
|
-
const sharedDocument = (Cls) => {
|
|
948
|
-
if (!sharedDom) {
|
|
949
|
-
const dom = new Cls("<!DOCTYPE html><html><head><style></style></head><body></body></html>");
|
|
950
|
-
sharedDom = { window: dom.window, doc: dom.window.document, style: dom.window.document.querySelector("style") };
|
|
951
|
-
}
|
|
952
|
-
|
|
953
|
-
return sharedDom;
|
|
954
|
-
};
|
|
955
|
-
|
|
956
|
-
const render = (tree, extra) => {
|
|
957
|
-
const Cls = loadJSDOM();
|
|
958
|
-
if (!Cls || !tree) return { tree: null, available: false };
|
|
959
|
-
const stylesheet = (extra && extra.stylesheet) || "";
|
|
960
|
-
const { window, doc, style: styleEl } = sharedDocument(Cls);
|
|
961
|
-
styleEl.textContent = stylesheet;
|
|
962
|
-
doc.body.replaceChildren();
|
|
963
|
-
const built = buildDom(tree, doc);
|
|
964
|
-
if (!built) return { tree: null, available: false };
|
|
965
|
-
doc.body.appendChild(built);
|
|
966
|
-
const baselines = new Map();
|
|
967
|
-
const baselineOf = (el) => {
|
|
968
|
-
const tag = el.tagName.toLowerCase();
|
|
969
|
-
if (!baselines.has(tag)) {
|
|
970
|
-
const bare = doc.createElement(tag);
|
|
971
|
-
doc.body.appendChild(bare);
|
|
972
|
-
baselines.set(tag, window.getComputedStyle(bare));
|
|
973
|
-
}
|
|
974
|
-
return baselines.get(tag);
|
|
975
|
-
};
|
|
976
|
-
let nodes;
|
|
977
|
-
try {
|
|
978
|
-
nodes = nodesOf(built, window, baselineOf);
|
|
979
|
-
} finally {
|
|
980
|
-
doc.body.replaceChildren();
|
|
981
|
-
}
|
|
982
|
-
|
|
983
|
-
return { tree: nodes.length === 1 ? nodes[0] : { tag: "fragment", css: {}, children: nodes }, available: true };
|
|
984
|
-
};
|
|
985
|
-
|
|
986
|
-
const resolveCss = (css, unresolved, ctx) => {
|
|
987
|
-
return { css, unresolved };
|
|
988
|
-
};
|
|
989
|
-
|
|
990
|
-
module.exports = {
|
|
991
|
-
parseComponents,
|
|
992
|
-
parseElements,
|
|
993
|
-
parseParts,
|
|
994
|
-
render,
|
|
995
|
-
resolveCss,
|
|
996
|
-
available: () => !!loadJSDOM(),
|
|
997
|
-
};
|