@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
|
@@ -0,0 +1,1288 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.available = exports.setLiveSource = exports.resolveCss = exports.render = exports.parseParts = exports.parseElements = exports.parseComponents = void 0;
|
|
27
|
+
const fs = __importStar(require("node:fs"));
|
|
28
|
+
const path = __importStar(require("node:path"));
|
|
29
|
+
const node_module_1 = require("node:module");
|
|
30
|
+
const ts = __importStar(require("typescript"));
|
|
31
|
+
const remeda_1 = require("remeda");
|
|
32
|
+
const style = __importStar(require("../normalize/style.js"));
|
|
33
|
+
const dom_js_1 = require("../normalize/dom.js");
|
|
34
|
+
const external_html_js_1 = require("../normalize/external-html.js");
|
|
35
|
+
const MAX_UNFOLD_DEPTH = 12;
|
|
36
|
+
const MAX_TREE_NODES = 1500;
|
|
37
|
+
let skipNodes = null;
|
|
38
|
+
const stringLiterals = (node, out, depth = 0) => {
|
|
39
|
+
if (!node || depth > 4 || out.length >= 24)
|
|
40
|
+
return;
|
|
41
|
+
if (skipNodes && skipNodes.has(node))
|
|
42
|
+
return;
|
|
43
|
+
if (ts.isJsxExpression(node)) {
|
|
44
|
+
stringLiterals(node.expression, out, depth);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
|
|
48
|
+
out.push(node.text);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (ts.isTemplateExpression(node)) {
|
|
52
|
+
if (node.head.text)
|
|
53
|
+
out.push(node.head.text);
|
|
54
|
+
for (const span of node.templateSpans) {
|
|
55
|
+
if (span.literal.text)
|
|
56
|
+
out.push(span.literal.text);
|
|
57
|
+
stringLiterals(span.expression, out, depth + 1);
|
|
58
|
+
}
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
62
|
+
for (const p of node.properties)
|
|
63
|
+
stringLiterals(p.initializer, out, depth + 1);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (ts.isArrayLiteralExpression(node)) {
|
|
67
|
+
for (const el of node.elements)
|
|
68
|
+
stringLiterals(el, out, depth + 1);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (ts.isCallExpression(node)) {
|
|
72
|
+
for (const arg of node.arguments)
|
|
73
|
+
stringLiterals(arg, out, depth + 1);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (ts.isConditionalExpression(node)) {
|
|
77
|
+
stringLiterals(node.whenTrue, out, depth + 1);
|
|
78
|
+
stringLiterals(node.whenFalse, out, depth + 1);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.PlusToken) {
|
|
82
|
+
stringLiterals(node.left, out, depth + 1);
|
|
83
|
+
stringLiterals(node.right, out, depth + 1);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const resolveLocalIdentifier = (name, fileSf, kind) => {
|
|
88
|
+
const found = { str: [], obj: null };
|
|
89
|
+
const walk = (n) => {
|
|
90
|
+
if (ts.isVariableDeclaration(n) && ts.isIdentifier(n.name) && n.name.text === name && n.initializer) {
|
|
91
|
+
if (kind === "str") {
|
|
92
|
+
if (variantConfigOf(n.initializer))
|
|
93
|
+
return;
|
|
94
|
+
const strs = [];
|
|
95
|
+
stringLiterals(n.initializer, strs);
|
|
96
|
+
found.str.push(...strs);
|
|
97
|
+
}
|
|
98
|
+
else if (kind === "obj" && ts.isObjectLiteralExpression(n.initializer)) {
|
|
99
|
+
found.obj = n.initializer;
|
|
100
|
+
}
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
ts.forEachChild(n, walk);
|
|
104
|
+
};
|
|
105
|
+
walk(fileSf);
|
|
106
|
+
return found;
|
|
107
|
+
};
|
|
108
|
+
const styleObjectStyles = (expr, sf) => {
|
|
109
|
+
if (expr && ts.isJsxExpression(expr))
|
|
110
|
+
expr = expr.expression;
|
|
111
|
+
const styles = {};
|
|
112
|
+
const add = (prop, value) => {
|
|
113
|
+
if (!Object.prototype.hasOwnProperty.call(styles, prop))
|
|
114
|
+
styles[prop] = [];
|
|
115
|
+
if (!styles[prop].includes(value))
|
|
116
|
+
styles[prop].push(value);
|
|
117
|
+
};
|
|
118
|
+
const visitObj = (n) => {
|
|
119
|
+
if (!n)
|
|
120
|
+
return;
|
|
121
|
+
if (ts.isObjectLiteralExpression(n)) {
|
|
122
|
+
for (const p of n.properties) {
|
|
123
|
+
if (!ts.isPropertyAssignment(p) || !p.name)
|
|
124
|
+
continue;
|
|
125
|
+
const key = ts.isIdentifier(p.name) ? p.name.text : ts.isStringLiteral(p.name) ? p.name.text : null;
|
|
126
|
+
if (!key)
|
|
127
|
+
continue;
|
|
128
|
+
const prop = style.camelToKebab(key);
|
|
129
|
+
if (ts.isStringLiteral(p.initializer)) {
|
|
130
|
+
add(prop, p.initializer.text);
|
|
131
|
+
}
|
|
132
|
+
else if (ts.isNumericLiteral(p.initializer)) {
|
|
133
|
+
add(prop, p.initializer.text);
|
|
134
|
+
}
|
|
135
|
+
else if (ts.isPrefixUnaryExpression(p.initializer) && ts.isNumericLiteral(p.initializer.operand)) {
|
|
136
|
+
add(prop, `${p.operator === ts.SyntaxKind.MinusToken ? "-" : ""}${p.initializer.operand.text}`);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
const strs = [];
|
|
140
|
+
stringLiterals(p.initializer, strs);
|
|
141
|
+
for (const s of strs)
|
|
142
|
+
add(prop, s);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (ts.isConditionalExpression(n)) {
|
|
148
|
+
visitObj(n.whenTrue);
|
|
149
|
+
visitObj(n.whenFalse);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (ts.isIdentifier(n)) {
|
|
153
|
+
const r = resolveLocalIdentifier(n.text, sf, "obj");
|
|
154
|
+
if (r.obj)
|
|
155
|
+
visitObj(r.obj);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
visitObj(expr);
|
|
159
|
+
return styles;
|
|
160
|
+
};
|
|
161
|
+
const dottedName = (name) => {
|
|
162
|
+
if (ts.isIdentifier(name))
|
|
163
|
+
return name.text;
|
|
164
|
+
if (name && name.kind === ts.SyntaxKind.QualifiedName)
|
|
165
|
+
return `${dottedName(name.left)}.${name.right.text}`;
|
|
166
|
+
if (name && name.kind === ts.SyntaxKind.PropertyAccessExpression)
|
|
167
|
+
return `${dottedName(name.expression)}.${name.name.text}`;
|
|
168
|
+
return null;
|
|
169
|
+
};
|
|
170
|
+
const tagOf = (name) => dottedName(name);
|
|
171
|
+
const stringBranchOf = (expr, sf) => {
|
|
172
|
+
if (!expr)
|
|
173
|
+
return null;
|
|
174
|
+
if (ts.isStringLiteral(expr) || ts.isNoSubstitutionTemplateLiteral(expr))
|
|
175
|
+
return expr.text;
|
|
176
|
+
if (ts.isIdentifier(expr)) {
|
|
177
|
+
const decl = rawDeclarations(sf).find((item) => item.name === expr.text);
|
|
178
|
+
if (decl)
|
|
179
|
+
return stringBranchOf(decl.initializer, sf);
|
|
180
|
+
}
|
|
181
|
+
if (ts.isConditionalExpression(expr))
|
|
182
|
+
return stringBranchOf(expr.whenTrue, sf) || stringBranchOf(expr.whenFalse, sf);
|
|
183
|
+
if (ts.isParenthesizedExpression(expr))
|
|
184
|
+
return stringBranchOf(expr.expression, sf);
|
|
185
|
+
return null;
|
|
186
|
+
};
|
|
187
|
+
const openingOf = (node) => (ts.isJsxElement(node) ? node.openingElement : node);
|
|
188
|
+
const isJsxNode = (n) => ts.isJsxElement(n) || ts.isJsxSelfClosingElement(n);
|
|
189
|
+
const propName = (name) => (name && (ts.isIdentifier(name) || ts.isStringLiteral(name)) ? name.text : null);
|
|
190
|
+
const variantConfigOf = (call) => {
|
|
191
|
+
if (!call || !ts.isCallExpression(call))
|
|
192
|
+
return null;
|
|
193
|
+
const last = call.arguments[call.arguments.length - 1];
|
|
194
|
+
if (!last || !ts.isObjectLiteralExpression(last))
|
|
195
|
+
return null;
|
|
196
|
+
const member = (name) => last.properties.find((p) => ts.isPropertyAssignment(p) && propName(p.name) === name);
|
|
197
|
+
const variantsProp = member("variants");
|
|
198
|
+
if (!variantsProp || !ts.isObjectLiteralExpression(variantsProp.initializer))
|
|
199
|
+
return null;
|
|
200
|
+
const variants = {};
|
|
201
|
+
for (const group of variantsProp.initializer.properties) {
|
|
202
|
+
if (!ts.isPropertyAssignment(group) || !ts.isObjectLiteralExpression(group.initializer))
|
|
203
|
+
continue;
|
|
204
|
+
const options = {};
|
|
205
|
+
for (const option of group.initializer.properties) {
|
|
206
|
+
if (!ts.isPropertyAssignment(option))
|
|
207
|
+
continue;
|
|
208
|
+
const strs = [];
|
|
209
|
+
stringLiterals(option.initializer, strs);
|
|
210
|
+
options[propName(option.name)] = strs;
|
|
211
|
+
}
|
|
212
|
+
variants[propName(group.name)] = options;
|
|
213
|
+
}
|
|
214
|
+
const defaults = {};
|
|
215
|
+
const defaultsProp = member("defaultVariants");
|
|
216
|
+
if (defaultsProp && ts.isObjectLiteralExpression(defaultsProp.initializer)) {
|
|
217
|
+
for (const d of defaultsProp.initializer.properties) {
|
|
218
|
+
if (ts.isPropertyAssignment(d) && ts.isStringLiteral(d.initializer))
|
|
219
|
+
defaults[propName(d.name)] = d.initializer.text;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const base = [];
|
|
223
|
+
for (const arg of call.arguments.slice(0, -1))
|
|
224
|
+
stringLiterals(arg, base);
|
|
225
|
+
return { base, variants, defaults };
|
|
226
|
+
};
|
|
227
|
+
const selectedVariantStrings = (call, sf, props) => {
|
|
228
|
+
if (!ts.isIdentifier(call.expression))
|
|
229
|
+
return null;
|
|
230
|
+
const decl = rawDeclarations(sf).find((item) => item.name === call.expression.text);
|
|
231
|
+
const config = decl ? variantConfigOf(decl.initializer) : null;
|
|
232
|
+
if (!config)
|
|
233
|
+
return null;
|
|
234
|
+
const given = {};
|
|
235
|
+
const arg = call.arguments[0];
|
|
236
|
+
if (arg && ts.isObjectLiteralExpression(arg)) {
|
|
237
|
+
for (const p of arg.properties) {
|
|
238
|
+
if (ts.isShorthandPropertyAssignment(p))
|
|
239
|
+
given[p.name.text] = props ? props[p.name.text] : undefined;
|
|
240
|
+
else if (ts.isPropertyAssignment(p) && ts.isStringLiteral(p.initializer))
|
|
241
|
+
given[propName(p.name)] = p.initializer.text;
|
|
242
|
+
else if (ts.isPropertyAssignment(p) && ts.isIdentifier(p.initializer))
|
|
243
|
+
given[propName(p.name)] = props ? props[p.initializer.text] : undefined;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const out = [...config.base];
|
|
247
|
+
for (const [group, options] of Object.entries(config.variants)) {
|
|
248
|
+
const picked = typeof given[group] === "string" ? given[group] : config.defaults[group];
|
|
249
|
+
out.push(...(options[picked] || []));
|
|
250
|
+
}
|
|
251
|
+
return out;
|
|
252
|
+
};
|
|
253
|
+
const collectTokens = (expr, sf, out, props) => {
|
|
254
|
+
if (!expr)
|
|
255
|
+
return;
|
|
256
|
+
if (ts.isJsxExpression(expr))
|
|
257
|
+
expr = expr.expression;
|
|
258
|
+
const strs = [];
|
|
259
|
+
const skip = new Set();
|
|
260
|
+
const findVariantCalls = (n) => {
|
|
261
|
+
if (ts.isCallExpression(n)) {
|
|
262
|
+
const picked = selectedVariantStrings(n, sf, props);
|
|
263
|
+
if (picked) {
|
|
264
|
+
strs.push(...picked);
|
|
265
|
+
skip.add(n);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
ts.forEachChild(n, findVariantCalls);
|
|
270
|
+
};
|
|
271
|
+
findVariantCalls(expr);
|
|
272
|
+
const previousSkip = skipNodes;
|
|
273
|
+
skipNodes = skip;
|
|
274
|
+
try {
|
|
275
|
+
stringLiterals(expr, strs);
|
|
276
|
+
}
|
|
277
|
+
finally {
|
|
278
|
+
skipNodes = previousSkip;
|
|
279
|
+
}
|
|
280
|
+
const idents = new Set();
|
|
281
|
+
const visitIds = (n) => {
|
|
282
|
+
if (skip.has(n))
|
|
283
|
+
return;
|
|
284
|
+
if (ts.isIdentifier(n) && n.text.length > 0)
|
|
285
|
+
idents.add(n.text);
|
|
286
|
+
ts.forEachChild(n, visitIds);
|
|
287
|
+
};
|
|
288
|
+
visitIds(expr);
|
|
289
|
+
for (const id of idents) {
|
|
290
|
+
const r = resolveLocalIdentifier(id, sf, "str");
|
|
291
|
+
for (const s of r.str)
|
|
292
|
+
strs.push(s);
|
|
293
|
+
}
|
|
294
|
+
for (const s of strs) {
|
|
295
|
+
for (const tok of s.split(/\s+/)) {
|
|
296
|
+
if (tok && !out.has(tok))
|
|
297
|
+
out.add(tok);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
const moduleBindings = (sf) => {
|
|
302
|
+
const map = new Map();
|
|
303
|
+
for (const stmt of sf.statements) {
|
|
304
|
+
if (!ts.isImportDeclaration(stmt) || !stmt.importClause || !ts.isStringLiteral(stmt.moduleSpecifier))
|
|
305
|
+
continue;
|
|
306
|
+
const spec = stmt.moduleSpecifier.text;
|
|
307
|
+
const clause = stmt.importClause;
|
|
308
|
+
if (clause.name)
|
|
309
|
+
map.set(clause.name.text, { spec, exported: "default" });
|
|
310
|
+
const named = clause.namedBindings;
|
|
311
|
+
if (named && ts.isNamedImports(named)) {
|
|
312
|
+
for (const el of named.elements)
|
|
313
|
+
map.set(el.name.text, { spec, exported: (el.propertyName || el.name).text });
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return map;
|
|
317
|
+
};
|
|
318
|
+
const isSourceFile = (file) => !!file && !/\.d\.ts$/.test(file) && !/[\\/]node_modules[\\/]/.test(file) && !/\.(stories|test|spec)\.[a-z]+$/.test(file);
|
|
319
|
+
const optionsCache = new Map();
|
|
320
|
+
let fallbackConfigPath = null;
|
|
321
|
+
const configPathOf = (target) => {
|
|
322
|
+
if (!target)
|
|
323
|
+
return null;
|
|
324
|
+
const abs = path.resolve(target);
|
|
325
|
+
if (abs.endsWith(".json"))
|
|
326
|
+
return abs;
|
|
327
|
+
return ts.findConfigFile(path.dirname(abs), ts.sys.fileExists) || null;
|
|
328
|
+
};
|
|
329
|
+
const readCompilerOptions = (configPath) => {
|
|
330
|
+
if (optionsCache.has(configPath))
|
|
331
|
+
return optionsCache.get(configPath);
|
|
332
|
+
let options = { allowJs: true };
|
|
333
|
+
try {
|
|
334
|
+
const parsed = ts.getParsedCommandLineOfConfigFile(configPath, {}, { ...ts.sys, onUnRecoverableConfigFileDiagnostic: () => undefined });
|
|
335
|
+
if (parsed)
|
|
336
|
+
options = { ...parsed.options, allowJs: true };
|
|
337
|
+
}
|
|
338
|
+
catch {
|
|
339
|
+
options = { allowJs: true };
|
|
340
|
+
}
|
|
341
|
+
optionsCache.set(configPath, options);
|
|
342
|
+
return options;
|
|
343
|
+
};
|
|
344
|
+
const resolveModuleFile = (fromFile, spec) => {
|
|
345
|
+
const nearest = ts.findConfigFile(path.dirname(fromFile), ts.sys.fileExists) || null;
|
|
346
|
+
const configs = [nearest, fallbackConfigPath].filter((item, index, all) => item && all.indexOf(item) === index);
|
|
347
|
+
const candidates = configs.length > 0 ? configs.map(readCompilerOptions) : [{ allowJs: true }];
|
|
348
|
+
for (const options of candidates) {
|
|
349
|
+
const hit = ts.resolveModuleName(spec, fromFile, options, ts.sys).resolvedModule;
|
|
350
|
+
if (hit && isSourceFile(hit.resolvedFileName))
|
|
351
|
+
return hit.resolvedFileName;
|
|
352
|
+
}
|
|
353
|
+
return null;
|
|
354
|
+
};
|
|
355
|
+
const sourceFiles = new Map();
|
|
356
|
+
// The editor lints the in-memory buffer (ESLint `lintText`), which can differ
|
|
357
|
+
// from the on-disk file. The rule sets the live source here before parsing.
|
|
358
|
+
let liveSource = null;
|
|
359
|
+
const setLiveSource = (fileAbs, text) => {
|
|
360
|
+
liveSource = { fileAbs, text };
|
|
361
|
+
sourceFiles.delete(fileAbs);
|
|
362
|
+
};
|
|
363
|
+
exports.setLiveSource = setLiveSource;
|
|
364
|
+
const readSource = (fileAbs) => {
|
|
365
|
+
if (liveSource && liveSource.fileAbs === fileAbs)
|
|
366
|
+
return liveSource.text;
|
|
367
|
+
return fs.readFileSync(fileAbs, "utf8");
|
|
368
|
+
};
|
|
369
|
+
const sourceFileOf = (fileAbs) => {
|
|
370
|
+
if (sourceFiles.has(fileAbs))
|
|
371
|
+
return sourceFiles.get(fileAbs);
|
|
372
|
+
const source = readSource(fileAbs);
|
|
373
|
+
const kind = fileAbs.endsWith(".tsx") || fileAbs.endsWith(".jsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
374
|
+
const sf = ts.createSourceFile(fileAbs, source, ts.ScriptTarget.ES2020, true, kind);
|
|
375
|
+
sourceFiles.set(fileAbs, sf);
|
|
376
|
+
return sf;
|
|
377
|
+
};
|
|
378
|
+
const resolvedTags = new Map();
|
|
379
|
+
const exportSpecs = (sf) => {
|
|
380
|
+
const specs = [];
|
|
381
|
+
for (const stmt of sf.statements) {
|
|
382
|
+
if (!ts.isExportDeclaration(stmt) || !stmt.moduleSpecifier || !ts.isStringLiteral(stmt.moduleSpecifier))
|
|
383
|
+
continue;
|
|
384
|
+
const spec = stmt.moduleSpecifier.text;
|
|
385
|
+
if (!stmt.exportClause) {
|
|
386
|
+
specs.push({ spec, all: true });
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
if (!ts.isNamedExports(stmt.exportClause))
|
|
390
|
+
continue;
|
|
391
|
+
for (const el of stmt.exportClause.elements)
|
|
392
|
+
specs.push({ spec, exported: el.name.text, local: (el.propertyName || el.name).text });
|
|
393
|
+
}
|
|
394
|
+
return specs;
|
|
395
|
+
};
|
|
396
|
+
const localExportNames = (sf) => {
|
|
397
|
+
const names = new Set();
|
|
398
|
+
for (const stmt of sf.statements) {
|
|
399
|
+
if (ts.isExportAssignment(stmt) && ts.isIdentifier(stmt.expression))
|
|
400
|
+
names.add(stmt.expression.text);
|
|
401
|
+
if (!ts.isExportDeclaration(stmt) || stmt.moduleSpecifier || !stmt.exportClause || !ts.isNamedExports(stmt.exportClause))
|
|
402
|
+
continue;
|
|
403
|
+
for (const el of stmt.exportClause.elements)
|
|
404
|
+
names.add((el.propertyName || el.name).text);
|
|
405
|
+
}
|
|
406
|
+
return names;
|
|
407
|
+
};
|
|
408
|
+
const findExportFile = (fileAbs, name, seen) => {
|
|
409
|
+
if (seen.has(fileAbs))
|
|
410
|
+
return null;
|
|
411
|
+
seen.add(fileAbs);
|
|
412
|
+
const sf = sourceFileOf(fileAbs);
|
|
413
|
+
const listed = localExportNames(sf);
|
|
414
|
+
if (iterateDeclarations(sf).some((item) => item.name === name && (isExported(item.node, sf) || listed.has(name))))
|
|
415
|
+
return { file: fileAbs, name };
|
|
416
|
+
// A STyled declaration (`export const X = styled.div\`…\``) is an export too,
|
|
417
|
+
// even though it is not a function component — resolve it where it is declared.
|
|
418
|
+
for (const stmt of sf.statements) {
|
|
419
|
+
if (!ts.isVariableStatement(stmt))
|
|
420
|
+
continue;
|
|
421
|
+
for (const d of stmt.declarationList.declarations) {
|
|
422
|
+
if (ts.isIdentifier(d.name) && d.name.text === name && (isExported(stmt, sf) || listed.has(name)))
|
|
423
|
+
return { file: fileAbs, name };
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
// A re-exported IMPORT alias (`import { StyledInput as Input } from './styles';
|
|
427
|
+
// export { Input }`) is not a local declaration: follow the import binding to
|
|
428
|
+
// the file that actually declares it, so styled components resolve cross-file.
|
|
429
|
+
const binding = moduleBindings(sf).get(name);
|
|
430
|
+
if (binding && listed.has(name)) {
|
|
431
|
+
const target = resolveModuleFile(fileAbs, binding.spec);
|
|
432
|
+
if (target) {
|
|
433
|
+
const found = findExportFile(target, binding.exported, seen);
|
|
434
|
+
if (found)
|
|
435
|
+
return found;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
const specs = exportSpecs(sf);
|
|
439
|
+
const named = specs.find((exp) => !exp.all && exp.exported === name);
|
|
440
|
+
if (named) {
|
|
441
|
+
const target = resolveModuleFile(fileAbs, named.spec);
|
|
442
|
+
return target ? findExportFile(target, named.local, seen) : null;
|
|
443
|
+
}
|
|
444
|
+
for (const exp of specs.filter((item) => item.all)) {
|
|
445
|
+
const target = resolveModuleFile(fileAbs, exp.spec);
|
|
446
|
+
if (!target)
|
|
447
|
+
continue;
|
|
448
|
+
const found = findExportFile(target, name, seen);
|
|
449
|
+
if (found)
|
|
450
|
+
return found;
|
|
451
|
+
}
|
|
452
|
+
return null;
|
|
453
|
+
};
|
|
454
|
+
const memberTarget = (sf, head, prop) => {
|
|
455
|
+
let found = null;
|
|
456
|
+
const fromObject = (obj) => {
|
|
457
|
+
for (const p of obj.properties) {
|
|
458
|
+
if (ts.isShorthandPropertyAssignment(p) && p.name.text === prop)
|
|
459
|
+
return prop;
|
|
460
|
+
if (ts.isPropertyAssignment(p) && propName(p.name) === prop && ts.isIdentifier(p.initializer))
|
|
461
|
+
return p.initializer.text;
|
|
462
|
+
}
|
|
463
|
+
return null;
|
|
464
|
+
};
|
|
465
|
+
const visit = (n) => {
|
|
466
|
+
if (found)
|
|
467
|
+
return;
|
|
468
|
+
if (ts.isVariableDeclaration(n) && ts.isIdentifier(n.name) && n.name.text === head && n.initializer && ts.isCallExpression(n.initializer)) {
|
|
469
|
+
for (const arg of n.initializer.arguments)
|
|
470
|
+
if (!found && ts.isObjectLiteralExpression(arg))
|
|
471
|
+
found = fromObject(arg);
|
|
472
|
+
}
|
|
473
|
+
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))
|
|
474
|
+
found = n.right.text;
|
|
475
|
+
ts.forEachChild(n, visit);
|
|
476
|
+
};
|
|
477
|
+
visit(sf);
|
|
478
|
+
return found;
|
|
479
|
+
};
|
|
480
|
+
const resolveTag = (name, fileAbs, depth, props, styledResolver) => {
|
|
481
|
+
const staticProps = Object.entries(props || {}).filter(([, v]) => typeof v === "string").sort();
|
|
482
|
+
const key = `${fileAbs}::${name}::${JSON.stringify(staticProps)}`;
|
|
483
|
+
if (resolvedTags.has(key))
|
|
484
|
+
return resolvedTags.get(key);
|
|
485
|
+
if (depth > MAX_UNFOLD_DEPTH)
|
|
486
|
+
return null;
|
|
487
|
+
const sf = sourceFileOf(fileAbs);
|
|
488
|
+
const dot = name.indexOf(".");
|
|
489
|
+
const head = dot > 0 ? name.slice(0, dot) : name;
|
|
490
|
+
const member = dot > 0 ? name.slice(dot + 1) : null;
|
|
491
|
+
const binding = moduleBindings(sf).get(head);
|
|
492
|
+
if (!binding)
|
|
493
|
+
return null;
|
|
494
|
+
const imported = resolveModuleFile(fileAbs, binding.spec);
|
|
495
|
+
if (!imported)
|
|
496
|
+
return null;
|
|
497
|
+
const located = findExportFile(imported, binding.exported, new Set());
|
|
498
|
+
if (!located)
|
|
499
|
+
return null;
|
|
500
|
+
const targetSf = sourceFileOf(located.file);
|
|
501
|
+
const declName = member ? memberTarget(targetSf, located.name, member) : located.name;
|
|
502
|
+
if (!declName)
|
|
503
|
+
return null;
|
|
504
|
+
const decl = iterateDeclarations(targetSf).find((item) => item.name === declName);
|
|
505
|
+
if (!decl) {
|
|
506
|
+
// Not a function component: it may be a STyled component declared in another
|
|
507
|
+
// file (`export const FieldContainer = styled.fieldset\`…\``). Resolve its
|
|
508
|
+
// base tag + CSS there, so the snapshot is real HTML, not the wrapper name.
|
|
509
|
+
const styledInfo = styledResolver ? styledResolver(declName, targetSf) : null;
|
|
510
|
+
if (!styledInfo)
|
|
511
|
+
return null;
|
|
512
|
+
resolvedTags.set(key, null);
|
|
513
|
+
const usage = { tag: name, spread: false, attrs: Object.fromEntries(staticProps), className: null, children: [] };
|
|
514
|
+
const built = styledNode(declName, styledInfo, usage, [], targetSf, { bodies: localComponentBodies(targetSf), depth: depth + 1, nodes: 0, props: {}, local: false, styledResolver, resolveTag: (tag, nested) => resolveTag(tag, located.file, depth + 1, nested, styledResolver) });
|
|
515
|
+
// The caller grafts the USAGE children onto this tree; keep the declaration's
|
|
516
|
+
// own children (a styled wrapper may render its own markup around the slot).
|
|
517
|
+
const tree = built && { ...built, children: built.children && built.children.length > 0 ? built.children : [] };
|
|
518
|
+
resolvedTags.set(key, tree);
|
|
519
|
+
return tree;
|
|
520
|
+
}
|
|
521
|
+
const inner = firstComponentNodeIn(decl.body);
|
|
522
|
+
if (!inner)
|
|
523
|
+
return null;
|
|
524
|
+
resolvedTags.set(key, null);
|
|
525
|
+
const built = buildTree(inner, targetSf, { bodies: localComponentBodies(targetSf), depth: depth + 1, nodes: 0, props: Object.fromEntries(staticProps), local: false, styledResolver, resolveTag: (tag, nested) => resolveTag(tag, located.file, depth + 1, nested, styledResolver) });
|
|
526
|
+
const rawVia = [declName, ...(built?.via || [])];
|
|
527
|
+
const tree = built && { ...built, via: rawVia.filter((n, i) => i === 0 || n !== rawVia[i - 1]) };
|
|
528
|
+
resolvedTags.set(key, tree);
|
|
529
|
+
return tree;
|
|
530
|
+
};
|
|
531
|
+
const localComponentBodies = (sf) => {
|
|
532
|
+
const map = new Map();
|
|
533
|
+
for (const item of rawDeclarations(sf)) {
|
|
534
|
+
if (!(0, dom_js_1.isPascalCase)(item.name))
|
|
535
|
+
continue;
|
|
536
|
+
const body = componentBody(item.initializer, sf);
|
|
537
|
+
if (body)
|
|
538
|
+
map.set(item.name, body);
|
|
539
|
+
}
|
|
540
|
+
return map;
|
|
541
|
+
};
|
|
542
|
+
const jsxNodesIn = (expr) => {
|
|
543
|
+
if (!expr)
|
|
544
|
+
return [];
|
|
545
|
+
if (isJsxNode(expr) || ts.isJsxFragment(expr))
|
|
546
|
+
return [expr];
|
|
547
|
+
if (ts.isJsxExpression(expr) || ts.isParenthesizedExpression(expr) || ts.isAsExpression(expr) || ts.isTypeAssertionExpression(expr) || ts.isSatisfiesExpression(expr) || ts.isNonNullExpression(expr))
|
|
548
|
+
return jsxNodesIn(expr.expression);
|
|
549
|
+
if (ts.isConditionalExpression(expr))
|
|
550
|
+
return [...jsxNodesIn(expr.whenTrue), ...jsxNodesIn(expr.whenFalse)];
|
|
551
|
+
if (ts.isBinaryExpression(expr)) {
|
|
552
|
+
const op = expr.operatorToken.kind;
|
|
553
|
+
if (op === ts.SyntaxKind.AmpersandAmpersandToken)
|
|
554
|
+
return jsxNodesIn(expr.right);
|
|
555
|
+
if (op === ts.SyntaxKind.BarBarToken || op === ts.SyntaxKind.QuestionQuestionToken)
|
|
556
|
+
return [...jsxNodesIn(expr.left), ...jsxNodesIn(expr.right)];
|
|
557
|
+
return [];
|
|
558
|
+
}
|
|
559
|
+
if (ts.isArrayLiteralExpression(expr))
|
|
560
|
+
return expr.elements.flatMap((el) => jsxNodesIn(el));
|
|
561
|
+
if (ts.isCallExpression(expr))
|
|
562
|
+
return expr.arguments.flatMap((arg) => jsxNodesIn(arg));
|
|
563
|
+
if (ts.isArrowFunction(expr) || ts.isFunctionExpression(expr) || ts.isFunctionDeclaration(expr))
|
|
564
|
+
return jsxNodesIn(expr.body);
|
|
565
|
+
if (ts.isBlock(expr))
|
|
566
|
+
return expr.statements.filter((stmt) => ts.isReturnStatement(stmt)).flatMap((stmt) => jsxNodesIn(stmt.expression));
|
|
567
|
+
return [];
|
|
568
|
+
};
|
|
569
|
+
const firstComponentNodeIn = (body) => jsxNodesIn(body)[0] || null;
|
|
570
|
+
const buildTree = (node, sf, ctx) => {
|
|
571
|
+
if (ts.isJsxFragment(node))
|
|
572
|
+
return fragmentTree(node, sf, ctx);
|
|
573
|
+
if (ctx.nodes >= MAX_TREE_NODES)
|
|
574
|
+
return null;
|
|
575
|
+
ctx.nodes += 1;
|
|
576
|
+
const opening = openingOf(node);
|
|
577
|
+
const rawTag = tagOf(opening.tagName);
|
|
578
|
+
const tag = rawTag && !(0, dom_js_1.isHtmlTag)(rawTag) ? stringBranchOf(ts.isIdentifier(opening.tagName) ? opening.tagName : null, sf) || rawTag : rawTag;
|
|
579
|
+
if (!tag)
|
|
580
|
+
return null;
|
|
581
|
+
const attrs = {};
|
|
582
|
+
let className = null;
|
|
583
|
+
const styleObj = {};
|
|
584
|
+
let dynamic = false;
|
|
585
|
+
const dynamicRefs = [];
|
|
586
|
+
for (const attr of opening.attributes.properties) {
|
|
587
|
+
if (!ts.isJsxAttribute(attr) || !attr.name)
|
|
588
|
+
continue;
|
|
589
|
+
const aname = attr.name.getText(sf);
|
|
590
|
+
const value = attr.initializer;
|
|
591
|
+
const strs = [];
|
|
592
|
+
if (value)
|
|
593
|
+
stringLiterals(value, strs);
|
|
594
|
+
if (aname === "className" || aname === "class") {
|
|
595
|
+
const tokens = new Set();
|
|
596
|
+
collectTokens(value, sf, tokens, ctx.props);
|
|
597
|
+
if (tokens.size > 0)
|
|
598
|
+
className = [...tokens].sort().join(" ");
|
|
599
|
+
else if (value)
|
|
600
|
+
dynamic = true;
|
|
601
|
+
}
|
|
602
|
+
else if (aname === "style") {
|
|
603
|
+
const so = styleObjectStyles(value, sf);
|
|
604
|
+
for (const [k, v] of Object.entries(so))
|
|
605
|
+
styleObj[k] = v;
|
|
606
|
+
}
|
|
607
|
+
else {
|
|
608
|
+
attrs[aname] = strs.length > 0 ? strs[0] : value ? null : true;
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
const children = childTrees(node, sf, ctx);
|
|
612
|
+
const spread = opening.attributes.properties.some((attr) => ts.isJsxSpreadAttribute(attr));
|
|
613
|
+
if (spread) {
|
|
614
|
+
children.push({ tag: "children", attrs: {}, className: null, style: null, cssText: "", children: [] });
|
|
615
|
+
}
|
|
616
|
+
const tree = {
|
|
617
|
+
tag,
|
|
618
|
+
spread,
|
|
619
|
+
attrs,
|
|
620
|
+
className,
|
|
621
|
+
style: Object.keys(styleObj).length > 0 ? styleObj : null,
|
|
622
|
+
cssText: (0, dom_js_1.styleObjectToCssText)(styleObj),
|
|
623
|
+
children,
|
|
624
|
+
// WHERE this element is written: the opening tag's line, so a report row can
|
|
625
|
+
// anchor on the exact source line instead of searching text (comments included).
|
|
626
|
+
line: sf.getLineAndCharacterOfPosition(opening.getStart(sf)).line + 1,
|
|
627
|
+
// The props PASSED to this element (non-event JSX attributes) and the component
|
|
628
|
+
// it was written as. These are what the framework signal compares — never the
|
|
629
|
+
// props a component ACCEPTS in its parameter list.
|
|
630
|
+
passedProps: Object.keys(attrs).filter((name) => !/^(on[A-Z]|ng|v-on:|@)/.test(name)),
|
|
631
|
+
component: !(0, dom_js_1.isHtmlTag)(tag) ? tag : null,
|
|
632
|
+
// A node written in the ROOT file keeps its own line + name; library-expanded
|
|
633
|
+
// nodes inherit the root usage (see stampLine). `__local` marks root-file JSX.
|
|
634
|
+
__local: ctx.local !== false,
|
|
635
|
+
codeName: tag,
|
|
636
|
+
};
|
|
637
|
+
if (!(0, dom_js_1.isHtmlTag)(tag) && !ctx.noUnfold) {
|
|
638
|
+
const body = ctx.bodies.get(tag);
|
|
639
|
+
if (body && ctx.depth < MAX_UNFOLD_DEPTH) {
|
|
640
|
+
const inner = firstComponentNodeIn(body);
|
|
641
|
+
if (inner) {
|
|
642
|
+
const unfolded = buildTree(inner, sf, { ...ctx, depth: ctx.depth + 1, props: attrs });
|
|
643
|
+
if (unfolded)
|
|
644
|
+
return graftChildren({ ...unfolded, via: [tag, ...(unfolded.via || [])] }, children, tree);
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
const resolved = ctx.resolveTag ? ctx.resolveTag(tag, attrs) : null;
|
|
648
|
+
if (resolved) {
|
|
649
|
+
// Remember the ROOT usage line + name ONLY when this usage is written in the
|
|
650
|
+
// root file (ctx.local). A library-internal usage (Loader inside Button) is
|
|
651
|
+
// NOT a root anchor — its nodes must inherit the closest root usage (Button).
|
|
652
|
+
const marked = ctx.local !== false ? stampUsage(resolved, tree.line, tag) : resolved;
|
|
653
|
+
return graftChildren(marked, children, tree);
|
|
654
|
+
}
|
|
655
|
+
const styledInfo = ctx.styledResolver ? ctx.styledResolver(tag, sf) : null;
|
|
656
|
+
if (styledInfo)
|
|
657
|
+
return styledNode(tag, styledInfo, tree, children, sf, ctx);
|
|
658
|
+
// A known external import (next/image → img, next/link → a, lucide-react
|
|
659
|
+
// icons → svg) renders real HTML even though its source is unavailable.
|
|
660
|
+
// The import binding names the package; external-html knows the tag.
|
|
661
|
+
const binding = moduleBindings(sf).get(tag);
|
|
662
|
+
const externalTag = binding ? (0, external_html_js_1.externalTagOf)(binding.spec, binding.exported) : null;
|
|
663
|
+
if (externalTag)
|
|
664
|
+
return graftChildren({ ...tree, tag: externalTag, via: [tag] }, children, tree);
|
|
665
|
+
// A known dynamic component (LucideIcon picks an icon at runtime) still
|
|
666
|
+
// renders real HTML: show that tag, not the wrapper name.
|
|
667
|
+
const knownTag = (0, external_html_js_1.knownRenderTag)(tag);
|
|
668
|
+
if (knownTag)
|
|
669
|
+
return graftChildren({ ...tree, tag: knownTag, via: [tag] }, children, tree);
|
|
670
|
+
dynamic = true;
|
|
671
|
+
dynamicRefs.push(tag);
|
|
672
|
+
}
|
|
673
|
+
if (dynamic) {
|
|
674
|
+
tree.dynamic = true;
|
|
675
|
+
if (dynamicRefs.length > 0)
|
|
676
|
+
tree.dynamicRefs = dynamicRefs;
|
|
677
|
+
}
|
|
678
|
+
return tree;
|
|
679
|
+
};
|
|
680
|
+
const fragmentTree = (node, sf, ctx) => ({
|
|
681
|
+
tag: "fragment",
|
|
682
|
+
attrs: {},
|
|
683
|
+
className: null,
|
|
684
|
+
style: null,
|
|
685
|
+
cssText: "",
|
|
686
|
+
children: childTrees(node, sf, ctx),
|
|
687
|
+
});
|
|
688
|
+
const childTrees = (node, sf, ctx) => {
|
|
689
|
+
const children = [];
|
|
690
|
+
for (const child of node.children || []) {
|
|
691
|
+
if (isJsxNode(child) || ts.isJsxFragment(child)) {
|
|
692
|
+
const t = buildTree(child, sf, ctx);
|
|
693
|
+
if (t)
|
|
694
|
+
children.push(t);
|
|
695
|
+
}
|
|
696
|
+
else if (ts.isJsxExpression(child)) {
|
|
697
|
+
const found = jsxNodesIn(child.expression);
|
|
698
|
+
for (const inner of found) {
|
|
699
|
+
const t = buildTree(inner, sf, ctx);
|
|
700
|
+
if (t)
|
|
701
|
+
children.push(t);
|
|
702
|
+
}
|
|
703
|
+
if (found.length === 0 && child.expression && ts.isIdentifier(child.expression) && child.expression.text === "children") {
|
|
704
|
+
children.push({ tag: "children", attrs: {}, className: null, style: null, cssText: "", children: [] });
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
else if (ts.isJsxText(child)) {
|
|
708
|
+
const text = child.text.replace(/\s+/g, " ").trim();
|
|
709
|
+
if (text)
|
|
710
|
+
children.push({ tag: "text", text, attrs: {}, className: null, style: null, cssText: "", children: [] });
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
return children;
|
|
714
|
+
};
|
|
715
|
+
const mergeClass = (a, b) => [...new Set([...(a ? a.split(/\s+/) : []), ...(b ? b.split(/\s+/) : [])])].sort().join(" ") || null;
|
|
716
|
+
const fillSlot = (node, children) => {
|
|
717
|
+
const kids = node.children || [];
|
|
718
|
+
const index = kids.findIndex((child) => child.tag === "children");
|
|
719
|
+
if (index >= 0)
|
|
720
|
+
return { node: { ...node, children: [...kids.slice(0, index), ...children, ...kids.slice(index + 1)] }, done: true };
|
|
721
|
+
for (let i = 0; i < kids.length; i += 1) {
|
|
722
|
+
const filled = fillSlot(kids[i], children);
|
|
723
|
+
if (filled.done)
|
|
724
|
+
return { node: { ...node, children: [...kids.slice(0, i), filled.node, ...kids.slice(i + 1)] }, done: true };
|
|
725
|
+
}
|
|
726
|
+
return { node, done: false };
|
|
727
|
+
};
|
|
728
|
+
// Every node of an expanded subtree stands where the COMPONENT was used in THIS
|
|
729
|
+
// file, not where its body is declared — BUT a nested usage written in the library
|
|
730
|
+
// keeps ITS OWN line + name (a `Loader` inside `Button` is anchored on `<Loader />`
|
|
731
|
+
// in Button's file, not on `<Button>` in the caller). Only nodes that have no local
|
|
732
|
+
// line inherit the usage line. Never mutate a shared/cached tree: clone.
|
|
733
|
+
const stampLine = (node, line, codeName) => {
|
|
734
|
+
if (!node)
|
|
735
|
+
return node;
|
|
736
|
+
// A node written in the ROOT file keeps its own line + name. A library-expanded
|
|
737
|
+
// node is anchored to the ROOT usage (line + the component the reader wrote), so
|
|
738
|
+
// every ribbon reaches a word visible in the code column — never a library line.
|
|
739
|
+
// A node that carries a ROOT usage marker (it was expanded from a component the
|
|
740
|
+
// reader wrote in this file) ALWAYS keeps that anchor — no outer graft may
|
|
741
|
+
// overwrite it. A root-file JSX node keeps its own line + name. Everything else
|
|
742
|
+
// (library skeleton) inherits the usage it is being grafted into.
|
|
743
|
+
if (node.__usageLine != null && node.__usageName) {
|
|
744
|
+
const clone = { ...node, line: node.__usageLine, codeName: node.__usageName };
|
|
745
|
+
// Descendants inherit this root usage UNLESS they carry their own root marker
|
|
746
|
+
// (a nested root usage) — stampLine handles that recursively.
|
|
747
|
+
clone.children = (node.children || []).map((child) => stampLine(child, clone.line, clone.codeName));
|
|
748
|
+
return clone;
|
|
749
|
+
}
|
|
750
|
+
const local = node.__local === true && node.line != null;
|
|
751
|
+
const clone = local
|
|
752
|
+
? { ...node, codeName: node.codeName ?? node.tag }
|
|
753
|
+
: { ...node, line: line ?? node.line, codeName: codeName ?? node.tag };
|
|
754
|
+
clone.children = (node.children || []).map((child) => stampLine(child, clone.line, clone.codeName));
|
|
755
|
+
return clone;
|
|
756
|
+
};
|
|
757
|
+
// Tag every node of an expanded subtree with the ROOT usage it came from (line +
|
|
758
|
+
// name), so a later outer graft can restore the closest root anchor instead of
|
|
759
|
+
// overwriting it with the wrapper's. Only the top node gets the usage; children
|
|
760
|
+
// that are themselves root usages keep their own (set when they were expanded).
|
|
761
|
+
const stampUsage = (node, line, name) => {
|
|
762
|
+
if (!node)
|
|
763
|
+
return node;
|
|
764
|
+
return { ...node, __usageLine: line, __usageName: name };
|
|
765
|
+
};
|
|
766
|
+
const graftChildren = (resolved, children, usage) => {
|
|
767
|
+
// The expanded node stands where the COMPONENT was used, not where its body is
|
|
768
|
+
// declared: carry the usage line so the snapshot anchors on `<Button …>` here.
|
|
769
|
+
// via is a CHAIN from the root usage down to the producer; the first name is the
|
|
770
|
+
// root-file component the reader wrote, the rest is the internal expansion path.
|
|
771
|
+
const rawVia = usage.tag ? [usage.tag, ...(resolved.via || [])] : (resolved.via || []);
|
|
772
|
+
const via = rawVia.filter((name, i) => i === 0 || name !== rawVia[i - 1]);
|
|
773
|
+
if (resolved.tag === "fragment") {
|
|
774
|
+
const node = children.length > 0 ? fillSlot(resolved, children).node : resolved;
|
|
775
|
+
return stampLine({ ...node, via }, usage.line, usage.tag);
|
|
776
|
+
}
|
|
777
|
+
const clone = { ...resolved, spread: usage.spread === true, attrs: { ...(resolved.attrs || {}), ...(usage.attrs || {}) }, className: mergeClass(resolved.className, usage.className), via };
|
|
778
|
+
const filled = fillSlot(clone, children);
|
|
779
|
+
// No `{children}` slot in the wrapper: the usage children ARE its content
|
|
780
|
+
// (a styled.fieldset wraps whatever is written inside it). Never drop them.
|
|
781
|
+
const node = !filled.done && children.length > 0 ? { ...clone, children } : filled.node;
|
|
782
|
+
// The wrapper's own skeleton is stamped to the usage line; the usage children
|
|
783
|
+
// (written in the root file) are stamped to THEIR OWN usage line + name, so
|
|
784
|
+
// Input on line 41 anchors on `Input`, not on `FieldContainer`. A child that was
|
|
785
|
+
// already expanded (its via head is the root usage name) keeps that head as its
|
|
786
|
+
// codeName even if an outer graft overwrote it.
|
|
787
|
+
const stamped = stampLine({ ...node, children: [] }, usage.line, usage.tag);
|
|
788
|
+
stamped.children = (node.children || []).map((child) => {
|
|
789
|
+
// A child that came from a root-file usage carries its own line + the usage
|
|
790
|
+
// name as the via head. Restore both so the ribbon anchors on `Input`/`Button`
|
|
791
|
+
// at the line they were written, not on the wrapper that contains them.
|
|
792
|
+
if (child.__local === true && child.line != null)
|
|
793
|
+
return stampLine(child, child.line, child.codeName ?? child.tag);
|
|
794
|
+
if (child.__usageLine != null && child.__usageName)
|
|
795
|
+
return stampLine({ ...child, line: child.__usageLine }, child.__usageLine, child.__usageName);
|
|
796
|
+
return stampLine(child, stamped.line, stamped.codeName);
|
|
797
|
+
});
|
|
798
|
+
return stamped;
|
|
799
|
+
};
|
|
800
|
+
const styledNode = (tag, info, usage, children, sf, ctx) => {
|
|
801
|
+
let base = null;
|
|
802
|
+
if (info.componentRef) {
|
|
803
|
+
const body = ctx.bodies.get(info.componentRef);
|
|
804
|
+
if (body && ctx.depth < MAX_UNFOLD_DEPTH) {
|
|
805
|
+
const inner = firstComponentNodeIn(body);
|
|
806
|
+
if (inner)
|
|
807
|
+
base = buildTree(inner, sf, { ...ctx, depth: ctx.depth + 1, props: {} });
|
|
808
|
+
}
|
|
809
|
+
if (!base)
|
|
810
|
+
base = ctx.resolveTag ? ctx.resolveTag(info.componentRef, {}) : null;
|
|
811
|
+
}
|
|
812
|
+
if (base) {
|
|
813
|
+
const merged = { ...(base.style || {}) };
|
|
814
|
+
for (const [k, v] of Object.entries(info.styleObj || {}))
|
|
815
|
+
merged[k] = v;
|
|
816
|
+
const clone = { ...base, style: Object.keys(merged).length > 0 ? merged : null, cssText: (0, dom_js_1.styleObjectToCssText)(merged), via: [tag, ...(base.via || [])] };
|
|
817
|
+
return graftChildren(clone, children, usage);
|
|
818
|
+
}
|
|
819
|
+
return {
|
|
820
|
+
tag: info.baseTag || tag,
|
|
821
|
+
spread: usage.spread,
|
|
822
|
+
attrs: usage.attrs,
|
|
823
|
+
className: usage.className,
|
|
824
|
+
style: Object.keys(info.styleObj || {}).length > 0 ? info.styleObj : null,
|
|
825
|
+
cssText: (0, dom_js_1.styleObjectToCssText)(info.styleObj || {}),
|
|
826
|
+
children,
|
|
827
|
+
via: [tag],
|
|
828
|
+
line: usage.line,
|
|
829
|
+
};
|
|
830
|
+
};
|
|
831
|
+
const a11yOfOpening = (opening, sf, out) => {
|
|
832
|
+
for (const attr of opening.attributes.properties) {
|
|
833
|
+
if (!ts.isJsxAttribute(attr) || !attr.name)
|
|
834
|
+
continue;
|
|
835
|
+
const aname = attr.name.getText(sf);
|
|
836
|
+
if (aname === "role" || aname.startsWith("aria-")) {
|
|
837
|
+
const strs = [];
|
|
838
|
+
if (attr.initializer)
|
|
839
|
+
stringLiterals(attr.initializer, strs);
|
|
840
|
+
out.add(`${aname}:${strs[0] ?? "true"}`);
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
};
|
|
844
|
+
const gatherA11y = (node, sf, out) => {
|
|
845
|
+
const visit = (n) => {
|
|
846
|
+
if (isJsxNode(n))
|
|
847
|
+
a11yOfOpening(openingOf(n), sf, out);
|
|
848
|
+
ts.forEachChild(n, visit);
|
|
849
|
+
};
|
|
850
|
+
visit(node);
|
|
851
|
+
};
|
|
852
|
+
const rootTagsOf = (node, sf) => {
|
|
853
|
+
const out = new Set();
|
|
854
|
+
const visit = (n, parent) => {
|
|
855
|
+
if (n !== node && isJsxNode(n)) {
|
|
856
|
+
const isRoot = !parent || !(isJsxNode(parent) || ts.isJsxFragment(parent));
|
|
857
|
+
if (isRoot) {
|
|
858
|
+
const tag = tagOf(openingOf(n).tagName);
|
|
859
|
+
if (tag)
|
|
860
|
+
out.add(tag);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
ts.forEachChild(n, (c) => visit(c, n));
|
|
864
|
+
};
|
|
865
|
+
visit(node, null);
|
|
866
|
+
return out;
|
|
867
|
+
};
|
|
868
|
+
const viaNames = (tree, names, out = new Set()) => {
|
|
869
|
+
if (!tree)
|
|
870
|
+
return out;
|
|
871
|
+
for (const name of tree.via || [])
|
|
872
|
+
if (names.has(name))
|
|
873
|
+
out.add(name);
|
|
874
|
+
for (const child of tree.children || [])
|
|
875
|
+
viaNames(child, names, out);
|
|
876
|
+
return out;
|
|
877
|
+
};
|
|
878
|
+
const canonicalTagsUsed = (node, names) => {
|
|
879
|
+
const found = new Set();
|
|
880
|
+
const visit = (n) => {
|
|
881
|
+
if (isJsxNode(n)) {
|
|
882
|
+
const opening = openingOf(n);
|
|
883
|
+
if (ts.isIdentifier(opening.tagName) && names.has(opening.tagName.text))
|
|
884
|
+
found.add(opening.tagName.text);
|
|
885
|
+
}
|
|
886
|
+
ts.forEachChild(n, visit);
|
|
887
|
+
};
|
|
888
|
+
visit(node);
|
|
889
|
+
return found;
|
|
890
|
+
};
|
|
891
|
+
const hasComponentMarkup = (node) => {
|
|
892
|
+
let found = false;
|
|
893
|
+
const visit = (n) => {
|
|
894
|
+
if (found)
|
|
895
|
+
return;
|
|
896
|
+
if (isJsxNode(n)) {
|
|
897
|
+
found = true;
|
|
898
|
+
return;
|
|
899
|
+
}
|
|
900
|
+
ts.forEachChild(n, visit);
|
|
901
|
+
};
|
|
902
|
+
visit(node);
|
|
903
|
+
return found;
|
|
904
|
+
};
|
|
905
|
+
const isExported = (node, sf) => {
|
|
906
|
+
const mods = ts.getModifiers(node) || [];
|
|
907
|
+
if (mods.some((m) => m.kind === ts.SyntaxKind.ExportKeyword))
|
|
908
|
+
return true;
|
|
909
|
+
const parent = node.parent;
|
|
910
|
+
if (parent && (ts.isExportDeclaration(parent) || ts.isExportAssignment(parent)))
|
|
911
|
+
return true;
|
|
912
|
+
return false;
|
|
913
|
+
};
|
|
914
|
+
const unwrap = (expr) => {
|
|
915
|
+
let current = expr;
|
|
916
|
+
while (current && (ts.isParenthesizedExpression(current) || ts.isAsExpression(current) || ts.isTypeAssertionExpression(current) || ts.isSatisfiesExpression(current)))
|
|
917
|
+
current = current.expression;
|
|
918
|
+
return current;
|
|
919
|
+
};
|
|
920
|
+
const componentBody = (expr, sf) => {
|
|
921
|
+
if (!expr)
|
|
922
|
+
return null;
|
|
923
|
+
if (ts.isArrowFunction(expr) || ts.isFunctionExpression(expr))
|
|
924
|
+
return unwrap(expr.body);
|
|
925
|
+
// A `function Row(...) {...}` declaration is its own initializer in
|
|
926
|
+
// rawDeclarations — its body is the component body.
|
|
927
|
+
if (ts.isFunctionDeclaration(expr))
|
|
928
|
+
return expr.body ? unwrap(expr.body) : null;
|
|
929
|
+
if (ts.isCallExpression(expr)) {
|
|
930
|
+
for (const arg of expr.arguments) {
|
|
931
|
+
const body = componentBody(arg, sf);
|
|
932
|
+
if (body)
|
|
933
|
+
return body;
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
if (ts.isParenthesizedExpression(expr))
|
|
937
|
+
return componentBody(expr.expression, sf);
|
|
938
|
+
if (ts.isObjectLiteralExpression(expr)) {
|
|
939
|
+
const rendering = expr.properties.find((prop) => ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name) && prop.name.text === "body");
|
|
940
|
+
if (rendering)
|
|
941
|
+
return componentBody(rendering.initializer, sf);
|
|
942
|
+
if (expr.properties.some((prop) => ts.isMethodDeclaration(prop) && ts.isIdentifier(prop.name) && prop.name.text === "body")) {
|
|
943
|
+
const method = expr.properties.find((prop) => ts.isMethodDeclaration(prop) && prop.name.text === "body");
|
|
944
|
+
return unwrap(method.body);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
if (ts.isIdentifier(expr) && sf) {
|
|
948
|
+
const decl = rawDeclarations(sf).find((item) => item.name === expr.text);
|
|
949
|
+
if (decl)
|
|
950
|
+
return componentBody(decl.initializer, null);
|
|
951
|
+
}
|
|
952
|
+
return null;
|
|
953
|
+
};
|
|
954
|
+
const rawDeclarations = (sf) => {
|
|
955
|
+
const out = [];
|
|
956
|
+
const visit = (node) => {
|
|
957
|
+
if (ts.isVariableStatement(node)) {
|
|
958
|
+
for (const d of node.declarationList.declarations) {
|
|
959
|
+
if (ts.isIdentifier(d.name))
|
|
960
|
+
out.push({ name: d.name.text, initializer: d.initializer });
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
if (ts.isFunctionDeclaration(node) && node.name)
|
|
964
|
+
out.push({ name: node.name.text, initializer: node });
|
|
965
|
+
ts.forEachChild(node, visit);
|
|
966
|
+
};
|
|
967
|
+
visit(sf);
|
|
968
|
+
return out;
|
|
969
|
+
};
|
|
970
|
+
const iterateDeclarations = (sf) => {
|
|
971
|
+
const found = [];
|
|
972
|
+
const visit = (n) => {
|
|
973
|
+
if (ts.isFunctionDeclaration(n) && n.name && (0, dom_js_1.isPascalCase)(n.name.text) && n.body) {
|
|
974
|
+
found.push({ name: n.name.text, body: n.body, node: n });
|
|
975
|
+
}
|
|
976
|
+
if (ts.isVariableStatement(n)) {
|
|
977
|
+
for (const d of n.declarationList.declarations) {
|
|
978
|
+
if (!ts.isIdentifier(d.name) || !(0, dom_js_1.isPascalCase)(d.name.text))
|
|
979
|
+
continue;
|
|
980
|
+
const body = componentBody(d.initializer, sf);
|
|
981
|
+
if (body)
|
|
982
|
+
found.push({ name: d.name.text, body, node: n });
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
ts.forEachChild(n, visit);
|
|
986
|
+
};
|
|
987
|
+
visit(sf);
|
|
988
|
+
return found;
|
|
989
|
+
};
|
|
990
|
+
const useConfig = (opts) => {
|
|
991
|
+
fallbackConfigPath = configPathOf(opts && opts.tsconfig && path.resolve(opts.root || process.cwd(), opts.tsconfig));
|
|
992
|
+
};
|
|
993
|
+
const parseElements = (fileAbs, canonicalNames, opts) => {
|
|
994
|
+
useConfig(opts);
|
|
995
|
+
const sf = sourceFileOf(fileAbs);
|
|
996
|
+
const bodies = localComponentBodies(sf);
|
|
997
|
+
const out = [];
|
|
998
|
+
const visit = (n) => {
|
|
999
|
+
if (isJsxNode(n)) {
|
|
1000
|
+
const opening = openingOf(n);
|
|
1001
|
+
const tag = tagOf(opening.tagName);
|
|
1002
|
+
if (tag && (0, dom_js_1.isHtmlTag)(tag)) {
|
|
1003
|
+
const tree = buildTree(n, sf, { bodies, depth: 0, nodes: 0, props: {}, styledResolver: opts.styledResolver, resolveTag: (name, props) => resolveTag(name, fileAbs, 0, props, opts.styledResolver) });
|
|
1004
|
+
if (tree) {
|
|
1005
|
+
const a11y = new Set();
|
|
1006
|
+
a11yOfOpening(opening, sf, a11y);
|
|
1007
|
+
const pos = sf.getLineAndCharacterOfPosition(opening.getStart(sf));
|
|
1008
|
+
out.push({ name: tag, line: pos.line + 1, column: pos.character, tree: { ...tree, children: [] }, rootTags: [tag], a11y: [...a11y].sort() });
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
ts.forEachChild(n, visit);
|
|
1013
|
+
};
|
|
1014
|
+
visit(sf);
|
|
1015
|
+
return out;
|
|
1016
|
+
};
|
|
1017
|
+
exports.parseElements = parseElements;
|
|
1018
|
+
const MIN_PART_NODES = 3;
|
|
1019
|
+
const MAX_PART_NODES = 60;
|
|
1020
|
+
const jsxCount = (node) => {
|
|
1021
|
+
let count = 0;
|
|
1022
|
+
const visit = (n) => {
|
|
1023
|
+
if (isJsxNode(n))
|
|
1024
|
+
count += 1;
|
|
1025
|
+
ts.forEachChild(n, visit);
|
|
1026
|
+
};
|
|
1027
|
+
visit(node);
|
|
1028
|
+
return count;
|
|
1029
|
+
};
|
|
1030
|
+
const treeSize = (tree) => (tree ? (tree.tag === "fragment" || tree.tag === "children" ? 0 : 1) + (tree.children || []).reduce((sum, child) => sum + treeSize(child), 0) : 0);
|
|
1031
|
+
const parseParts = (fileAbs, canonicalNames, opts) => {
|
|
1032
|
+
useConfig(opts);
|
|
1033
|
+
const sf = sourceFileOf(fileAbs);
|
|
1034
|
+
const bodies = localComponentBodies(sf);
|
|
1035
|
+
const names = canonicalNames || new Set();
|
|
1036
|
+
const out = [];
|
|
1037
|
+
for (const decl of iterateDeclarations(sf)) {
|
|
1038
|
+
if (!hasComponentMarkup(decl.body))
|
|
1039
|
+
continue;
|
|
1040
|
+
const root = firstComponentNodeIn(decl.body);
|
|
1041
|
+
const visit = (n) => {
|
|
1042
|
+
if (n !== root && isJsxNode(n)) {
|
|
1043
|
+
const size = jsxCount(n);
|
|
1044
|
+
if (size >= MIN_PART_NODES && size <= MAX_PART_NODES) {
|
|
1045
|
+
const tree = buildTree(n, sf, { bodies, depth: 0, nodes: 0, props: {}, styledResolver: opts.styledResolver, resolveTag: (name, props) => resolveTag(name, fileAbs, 0, props, opts.styledResolver) });
|
|
1046
|
+
if (tree && treeSize(tree) >= MIN_PART_NODES) {
|
|
1047
|
+
const rootTag = tagOf(openingOf(n).tagName);
|
|
1048
|
+
const a11y = new Set();
|
|
1049
|
+
gatherA11y(n, sf, a11y);
|
|
1050
|
+
const start = sf.getLineAndCharacterOfPosition(n.getStart(sf));
|
|
1051
|
+
const end = sf.getLineAndCharacterOfPosition(n.getEnd());
|
|
1052
|
+
out.push({
|
|
1053
|
+
name: `part@${start.line + 1}`,
|
|
1054
|
+
owner: decl.name,
|
|
1055
|
+
rootTag,
|
|
1056
|
+
line: start.line + 1,
|
|
1057
|
+
column: start.character,
|
|
1058
|
+
endLine: end.line + 1,
|
|
1059
|
+
tree,
|
|
1060
|
+
rootTags: [rootTag],
|
|
1061
|
+
a11y: [...a11y].sort(), partial: true, usesCanonical: names.size > 0 ? [...new Set([...canonicalTagsUsed(n, names), ...viaNames(tree, names)])].sort() : [],
|
|
1062
|
+
});
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
ts.forEachChild(n, visit);
|
|
1067
|
+
};
|
|
1068
|
+
visit(decl.body);
|
|
1069
|
+
}
|
|
1070
|
+
return out;
|
|
1071
|
+
};
|
|
1072
|
+
exports.parseParts = parseParts;
|
|
1073
|
+
const declaredPropsOf = (node, sf) => {
|
|
1074
|
+
let fn = null;
|
|
1075
|
+
if (ts.isFunctionDeclaration(node))
|
|
1076
|
+
fn = node;
|
|
1077
|
+
else if (ts.isVariableStatement(node)) {
|
|
1078
|
+
for (const d of node.declarationList.declarations) {
|
|
1079
|
+
const init = d.initializer;
|
|
1080
|
+
if (init && (ts.isArrowFunction(init) || ts.isFunctionExpression(init)))
|
|
1081
|
+
fn = init;
|
|
1082
|
+
break;
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
if (!fn || !fn.parameters || fn.parameters.length === 0)
|
|
1086
|
+
return [];
|
|
1087
|
+
const param = fn.parameters[0];
|
|
1088
|
+
const out = [];
|
|
1089
|
+
const visit = (n) => {
|
|
1090
|
+
if (!n)
|
|
1091
|
+
return;
|
|
1092
|
+
if (ts.isObjectBindingPattern(n)) {
|
|
1093
|
+
for (const el of n.elements) {
|
|
1094
|
+
if (ts.isIdentifier(el.name))
|
|
1095
|
+
out.push(el.name.text);
|
|
1096
|
+
else
|
|
1097
|
+
visit(el.name);
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
};
|
|
1101
|
+
if (ts.isObjectBindingPattern(param.name))
|
|
1102
|
+
visit(param.name);
|
|
1103
|
+
return [...new Set(out)].sort();
|
|
1104
|
+
};
|
|
1105
|
+
const parseComponents = (fileAbs, canonicalNames, opts) => {
|
|
1106
|
+
useConfig(opts);
|
|
1107
|
+
const source = readSource(fileAbs);
|
|
1108
|
+
const kind = fileAbs.endsWith(".tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
1109
|
+
const sf = ts.createSourceFile(fileAbs, source, ts.ScriptTarget.ES2020, true, kind);
|
|
1110
|
+
const canonNameSet = canonicalNames || new Set();
|
|
1111
|
+
const bodies = localComponentBodies(sf);
|
|
1112
|
+
const out = [];
|
|
1113
|
+
let decls = iterateDeclarations(sf);
|
|
1114
|
+
if (decls.length === 0 && hasComponentMarkup(sf)) {
|
|
1115
|
+
const base = path.basename(fileAbs, path.extname(fileAbs));
|
|
1116
|
+
const name = base
|
|
1117
|
+
.split(/[-_ ]+/)
|
|
1118
|
+
.filter(Boolean)
|
|
1119
|
+
.map((w) => w[0].toUpperCase() + w.slice(1))
|
|
1120
|
+
.join("");
|
|
1121
|
+
decls = [{ name, body: sf, node: sf }];
|
|
1122
|
+
}
|
|
1123
|
+
for (const d of decls) {
|
|
1124
|
+
if (!hasComponentMarkup(d.body))
|
|
1125
|
+
continue;
|
|
1126
|
+
const root = firstComponentNodeIn(d.body);
|
|
1127
|
+
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;
|
|
1128
|
+
const rawTree = root ? buildTree(root, sf, { bodies, depth: 0, nodes: 0, noUnfold: true }) : null;
|
|
1129
|
+
const a11y = new Set();
|
|
1130
|
+
gatherA11y(d.body, sf, a11y);
|
|
1131
|
+
out.push({
|
|
1132
|
+
name: d.name,
|
|
1133
|
+
line: sf.getLineAndCharacterOfPosition(d.node.getStart(sf)).line + 1,
|
|
1134
|
+
endLine: sf.getLineAndCharacterOfPosition(d.node.getEnd()).line + 1,
|
|
1135
|
+
isExported: isExported(d.node, sf) || localExportNames(sf).has(d.name),
|
|
1136
|
+
declProps: declaredPropsOf(d.node, sf),
|
|
1137
|
+
tree,
|
|
1138
|
+
rawTree,
|
|
1139
|
+
rootTags: [...rootTagsOf(d.body, sf)].sort(),
|
|
1140
|
+
actions: [],
|
|
1141
|
+
a11y: [...a11y].sort(),
|
|
1142
|
+
usesCanonical: canonNameSet.size > 0 ? [...new Set([...canonicalTagsUsed(d.body, canonNameSet), ...viaNames(tree, canonNameSet)])].sort() : [],
|
|
1143
|
+
});
|
|
1144
|
+
}
|
|
1145
|
+
return out;
|
|
1146
|
+
};
|
|
1147
|
+
exports.parseComponents = parseComponents;
|
|
1148
|
+
// jsdom is optional: a missing install degrades to HTML-string parsing.
|
|
1149
|
+
const lazyRequire = (0, node_module_1.createRequire)(__filename);
|
|
1150
|
+
let JSDOM = null;
|
|
1151
|
+
const loadJSDOM = () => {
|
|
1152
|
+
if (JSDOM)
|
|
1153
|
+
return JSDOM;
|
|
1154
|
+
try {
|
|
1155
|
+
JSDOM = lazyRequire("jsdom").JSDOM;
|
|
1156
|
+
}
|
|
1157
|
+
catch {
|
|
1158
|
+
JSDOM = null;
|
|
1159
|
+
}
|
|
1160
|
+
return JSDOM;
|
|
1161
|
+
};
|
|
1162
|
+
const buildDom = (tree, doc) => {
|
|
1163
|
+
if (!tree)
|
|
1164
|
+
return null;
|
|
1165
|
+
if (tree.tag === "fragment" || tree.tag === "children") {
|
|
1166
|
+
const holder = doc.createElement("cu-root");
|
|
1167
|
+
for (const child of tree.children || []) {
|
|
1168
|
+
const node = buildDom(child, doc);
|
|
1169
|
+
if (node)
|
|
1170
|
+
holder.appendChild(node);
|
|
1171
|
+
}
|
|
1172
|
+
return holder;
|
|
1173
|
+
}
|
|
1174
|
+
const tag = String(tree.tag || "").toLowerCase();
|
|
1175
|
+
const el = (0, dom_js_1.isHtmlTag)(tag) ? doc.createElement(tag) : doc.createElement("cu-root");
|
|
1176
|
+
if (!(0, dom_js_1.isHtmlTag)(tag))
|
|
1177
|
+
el.__cuTag = String(tree.tag);
|
|
1178
|
+
for (const [key, value] of (0, remeda_1.entries)(tree.attrs || {})) {
|
|
1179
|
+
// JSDOM throws InvalidCharacterError for names that are not XML Names
|
|
1180
|
+
// (e.g. a `$preset` prop leaked from a template literal); skip those.
|
|
1181
|
+
if (!/^[a-zA-Z_:][-a-zA-Z0-9_:.]*$/.test(String(key)))
|
|
1182
|
+
continue;
|
|
1183
|
+
if (value === true)
|
|
1184
|
+
el.setAttribute(key, "");
|
|
1185
|
+
else if (value !== null && value !== undefined)
|
|
1186
|
+
el.setAttribute(key, String(value));
|
|
1187
|
+
}
|
|
1188
|
+
// JSDOM throws InvalidCharacterError when assigning className tokens that
|
|
1189
|
+
// are not valid CSS identifiers (e.g. `$preset` leaked from a template
|
|
1190
|
+
// literal). Such tokens carry no styling signal — drop them. Tailwind
|
|
1191
|
+
// arbitrary-value tokens (`bg-[#0f172a]`, `text-[18px]`) are valid class
|
|
1192
|
+
// attribute values AS WRITTEN (the generated stylesheet escapes them in its
|
|
1193
|
+
// selectors, which is where escaping belongs) — keep them raw so the
|
|
1194
|
+
// browser matches them and their styles resolve.
|
|
1195
|
+
if (tree.className) {
|
|
1196
|
+
const safe = tree.className
|
|
1197
|
+
.split(/\s+/)
|
|
1198
|
+
.filter((t) => /^[a-zA-Z0-9_-]+$/.test(t) || (/[\[\]#%.]/.test(t) && !/\$/.test(t)))
|
|
1199
|
+
.join(" ");
|
|
1200
|
+
if (safe)
|
|
1201
|
+
el.className = safe;
|
|
1202
|
+
}
|
|
1203
|
+
if (tree.style) {
|
|
1204
|
+
for (const [prop, values] of (0, remeda_1.entries)(tree.style)) {
|
|
1205
|
+
try {
|
|
1206
|
+
el.style.setProperty(prop, Array.isArray(values) ? values.join(" ") : String(values));
|
|
1207
|
+
}
|
|
1208
|
+
catch {
|
|
1209
|
+
// invalid property name/value — skip, no styling signal
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
for (const child of tree.children || []) {
|
|
1214
|
+
if (child.tag === "text") {
|
|
1215
|
+
el.appendChild(doc.createTextNode(child.text || ""));
|
|
1216
|
+
continue;
|
|
1217
|
+
}
|
|
1218
|
+
const node = buildDom(child, doc);
|
|
1219
|
+
if (node)
|
|
1220
|
+
el.appendChild(node);
|
|
1221
|
+
}
|
|
1222
|
+
return el;
|
|
1223
|
+
};
|
|
1224
|
+
;
|
|
1225
|
+
const nodesOf = (el, win, baselineOf) => {
|
|
1226
|
+
const kids = [...el.children].flatMap((child) => nodesOf(child, win, baselineOf));
|
|
1227
|
+
const compTag = el.tagName === "CU-ROOT" ? el.__cuTag || null : null;
|
|
1228
|
+
if (el.tagName === "CU-ROOT" && !compTag)
|
|
1229
|
+
return kids;
|
|
1230
|
+
const computed = win.getComputedStyle(el);
|
|
1231
|
+
const base = baselineOf(el);
|
|
1232
|
+
const css = {};
|
|
1233
|
+
for (const prop of style.LAYOUT_PROPS) {
|
|
1234
|
+
const value = computed.getPropertyValue(prop).trim();
|
|
1235
|
+
if (value && value !== base.getPropertyValue(prop).trim())
|
|
1236
|
+
css[prop] = value;
|
|
1237
|
+
}
|
|
1238
|
+
const tag = compTag || el.tagName.toLowerCase();
|
|
1239
|
+
const interactive = dom_js_1.INTERACTIVE_TAGS.has(tag) || el.getAttribute("role") === "button" || el.getAttribute("role") === "link" || [...el.attributes].some((attr) => /^(on|@)/.test(attr.name));
|
|
1240
|
+
const text = [...el.childNodes].filter((n) => n.nodeType === 3).map((n) => n.nodeValue).join("").replace(/\s+/g, " ").trim();
|
|
1241
|
+
return [{ tag, css, interactive, text: text || undefined, children: kids }];
|
|
1242
|
+
};
|
|
1243
|
+
let sharedDom = null;
|
|
1244
|
+
const sharedDocument = (Cls) => {
|
|
1245
|
+
if (!sharedDom) {
|
|
1246
|
+
const dom = new Cls("<!DOCTYPE html><html><head><style></style></head><body></body></html>");
|
|
1247
|
+
sharedDom = { window: dom.window, doc: dom.window.document, style: dom.window.document.querySelector("style") };
|
|
1248
|
+
}
|
|
1249
|
+
return sharedDom;
|
|
1250
|
+
};
|
|
1251
|
+
const render = (tree, extra) => {
|
|
1252
|
+
const Cls = loadJSDOM();
|
|
1253
|
+
if (!Cls || !tree)
|
|
1254
|
+
return { tree: null, available: false };
|
|
1255
|
+
const stylesheet = (extra && extra.stylesheet) || "";
|
|
1256
|
+
const { window, doc, style: styleEl } = sharedDocument(Cls);
|
|
1257
|
+
styleEl.textContent = stylesheet;
|
|
1258
|
+
doc.body.replaceChildren();
|
|
1259
|
+
const built = buildDom(tree, doc);
|
|
1260
|
+
if (!built)
|
|
1261
|
+
return { tree: null, available: false };
|
|
1262
|
+
doc.body.appendChild(built);
|
|
1263
|
+
const baselines = new Map();
|
|
1264
|
+
const baselineOf = (el) => {
|
|
1265
|
+
const tag = el.tagName.toLowerCase();
|
|
1266
|
+
if (!baselines.has(tag)) {
|
|
1267
|
+
const bare = doc.createElement(tag);
|
|
1268
|
+
doc.body.appendChild(bare);
|
|
1269
|
+
baselines.set(tag, window.getComputedStyle(bare));
|
|
1270
|
+
}
|
|
1271
|
+
return baselines.get(tag);
|
|
1272
|
+
};
|
|
1273
|
+
let nodes;
|
|
1274
|
+
try {
|
|
1275
|
+
nodes = nodesOf(built, window, baselineOf);
|
|
1276
|
+
}
|
|
1277
|
+
finally {
|
|
1278
|
+
doc.body.replaceChildren();
|
|
1279
|
+
}
|
|
1280
|
+
return { tree: nodes.length === 1 ? nodes[0] : { tag: "fragment", css: {}, children: nodes }, available: true };
|
|
1281
|
+
};
|
|
1282
|
+
exports.render = render;
|
|
1283
|
+
const resolveCss = (css, unresolved, ctx) => {
|
|
1284
|
+
return { css, unresolved };
|
|
1285
|
+
};
|
|
1286
|
+
exports.resolveCss = resolveCss;
|
|
1287
|
+
const available = () => !!loadJSDOM();
|
|
1288
|
+
exports.available = available;
|