@md-code/component-uniqueness 0.1.1 → 0.1.3
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/README.md +10 -0
- package/config.js +29 -4
- package/decision-maker.js +4 -8
- package/filter/drop-usages.js +4 -1
- package/filter/missing-component.js +68 -0
- package/logger.js +44 -0
- package/main.js +115 -9
- package/matcher/behavior.js +6 -2
- package/matcher/name.js +19 -2
- package/matcher/structure.js +55 -15
- package/normalize/candidate.js +4 -2
- package/normalize/dom.js +2 -0
- package/normalize/framework.js +37 -8
- package/normalize/style-resolver.js +24 -0
- package/normalize/style.js +1 -0
- package/normalize/words.js +40 -0
- package/package.json +2 -1
- package/playgrounds/framework-entrypoint.js +22 -3
- package/playgrounds/react.js +77 -8
- package/playgrounds/styled-components.js +132 -1
- package/report.js +159 -126
package/normalize/candidate.js
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
const { normalizeDom } = require("./dom.js");
|
|
4
4
|
const { normalizeFramework } = require("./framework.js");
|
|
5
|
+
const { rawClassTokens } = require("../playgrounds/framework-entrypoint.js");
|
|
5
6
|
|
|
6
7
|
const buildCandidate = (comp, config) => ({
|
|
7
8
|
...comp,
|
|
8
9
|
dom: normalizeDom(comp.tree, config, comp.name),
|
|
9
|
-
framework: normalizeFramework(comp.tree, comp.name),
|
|
10
|
+
framework: normalizeFramework(comp.rawTree || comp.tree, comp.name),
|
|
11
|
+
rawClasses: [...rawClassTokens(comp.rawTree || comp.tree)],
|
|
10
12
|
});
|
|
11
13
|
|
|
12
14
|
const buildElementCandidate = (element, config) => {
|
|
@@ -14,7 +16,7 @@ const buildElementCandidate = (element, config) => {
|
|
|
14
16
|
if (framework.events.length === 0 && element.a11y.length === 0) return null;
|
|
15
17
|
const dom = normalizeDom(element.tree, config, element.name);
|
|
16
18
|
|
|
17
|
-
return { ...element, partial: true, dom, framework: { ...framework, names: [] } };
|
|
19
|
+
return { ...element, partial: true, dom, framework: { ...framework, names: [] }, rawClasses: [...rawClassTokens(element.tree)] };
|
|
18
20
|
};
|
|
19
21
|
|
|
20
22
|
module.exports = { buildCandidate, buildElementCandidate };
|
package/normalize/dom.js
CHANGED
|
@@ -143,6 +143,8 @@ const styleObjectToCssText = (obj) => {
|
|
|
143
143
|
|
|
144
144
|
const layoutOnly = (node) => ({
|
|
145
145
|
tag: node.tag,
|
|
146
|
+
text: node.text || undefined,
|
|
147
|
+
interactive: !!node.interactive,
|
|
146
148
|
css: Object.fromEntries(Object.entries(node.css || {}).filter(([prop]) => isLayoutProp(prop))),
|
|
147
149
|
children: (node.children || []).map(layoutOnly),
|
|
148
150
|
});
|
package/normalize/framework.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const { INTERACTIVE_TAGS } = require("./dom.js");
|
|
3
|
+
const { INTERACTIVE_TAGS, isPascalCase } = require("./dom.js");
|
|
4
4
|
|
|
5
5
|
const EVENT_PREFIX = /^(on|ng|v-on:|@)/;
|
|
6
6
|
|
|
@@ -46,7 +46,7 @@ const eventsOf = (tree) => {
|
|
|
46
46
|
const namesOf = (tree) => {
|
|
47
47
|
const out = [];
|
|
48
48
|
walk(tree, (node) => {
|
|
49
|
-
if (node.tag) out.push(node.tag);
|
|
49
|
+
if (node.tag && isPascalCase(node.tag)) out.push(node.tag);
|
|
50
50
|
});
|
|
51
51
|
return out;
|
|
52
52
|
};
|
|
@@ -76,14 +76,43 @@ const shared = (a, b) => {
|
|
|
76
76
|
return [...new Set(a || [])].filter((item) => B.has(item)).sort();
|
|
77
77
|
};
|
|
78
78
|
|
|
79
|
+
const smallSetDiscount = (arrA, arrB) => {
|
|
80
|
+
const A = new Set(arrA || []);
|
|
81
|
+
const B = new Set(arrB || []);
|
|
82
|
+
const inter = [...A].filter((x) => B.has(x)).length;
|
|
83
|
+
const union = A.size + B.size - inter;
|
|
84
|
+
return union === 0 ? 1 : Math.min(1, union / 2);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const setEqual = (a, b) => {
|
|
88
|
+
const A = new Set(a || []);
|
|
89
|
+
const B = new Set(b || []);
|
|
90
|
+
if (A.size !== B.size) return false;
|
|
91
|
+
for (const item of A) if (!B.has(item)) return false;
|
|
92
|
+
|
|
93
|
+
return true;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const SAME_NAMES_PROP_FLOOR = 0.95;
|
|
97
|
+
const SAME_NAMES_PROP_SPAN = 0.04;
|
|
98
|
+
|
|
79
99
|
const frameworkSimilarity = (a, b, label) => {
|
|
80
|
-
const
|
|
81
|
-
const
|
|
82
|
-
const
|
|
100
|
+
const A = a || { props: [], events: [], names: [] };
|
|
101
|
+
const B = b || { props: [], events: [], names: [] };
|
|
102
|
+
const props = jaccard(A.props, B.props) * smallSetDiscount(A.props, B.props);
|
|
103
|
+
const events = jaccard(A.events, B.events) * smallSetDiscount(A.events, B.events);
|
|
104
|
+
const names = jaccard(A.names, B.names);
|
|
105
|
+
const passed = (p) => [...new Set([...p.props, ...p.events])];
|
|
106
|
+
const passedA = passed(A);
|
|
107
|
+
const passedB = passed(B);
|
|
108
|
+
if (A.names.length > 0 && names >= 1) {
|
|
109
|
+
if (setEqual(passedA, passedB)) return { score: 1, props, events, names };
|
|
110
|
+
return { score: SAME_NAMES_PROP_FLOOR + SAME_NAMES_PROP_SPAN * jaccard(passedA, passedB), props, events, names };
|
|
111
|
+
}
|
|
83
112
|
const parts = [];
|
|
84
|
-
if (
|
|
85
|
-
if (
|
|
86
|
-
if (
|
|
113
|
+
if (A.props.length || B.props.length) parts.push({ w: 0.5, v: props });
|
|
114
|
+
if (A.events.length || B.events.length) parts.push({ w: 0.2, v: events });
|
|
115
|
+
if (A.names.length || B.names.length) parts.push({ w: 0.3, v: names });
|
|
87
116
|
const totalW = parts.reduce((sum, part) => sum + part.w, 0);
|
|
88
117
|
const score = totalW === 0 ? 0 : parts.reduce((sum, part) => sum + part.w * part.v, 0) / totalW;
|
|
89
118
|
return { score, props, events, names };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { styledDeclarations, available: styledAvailable } = require("../playgrounds/styled-components.js");
|
|
4
|
+
const { cssTextToStyles } = require("./style.js");
|
|
5
|
+
|
|
6
|
+
const declarationsCache = new WeakMap();
|
|
7
|
+
|
|
8
|
+
const declarationsOf = (sf) => {
|
|
9
|
+
if (!declarationsCache.has(sf)) declarationsCache.set(sf, styledDeclarations(sf));
|
|
10
|
+
return declarationsCache.get(sf);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const buildStyledResolver = () => {
|
|
14
|
+
if (!styledAvailable()) return null;
|
|
15
|
+
return (tag, sf) => {
|
|
16
|
+
if (!sf || !tag) return null;
|
|
17
|
+
const info = declarationsOf(sf).get(tag);
|
|
18
|
+
if (!info) return null;
|
|
19
|
+
const { css, unresolved } = cssTextToStyles(info.cssText);
|
|
20
|
+
return { baseTag: info.baseTag, componentRef: info.componentRef, styleObj: css, unresolved: [...unresolved, ...info.unresolved] };
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
module.exports = { buildStyledResolver };
|
package/normalize/style.js
CHANGED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const pascalWords = (s) => String(s || "").replace(/([a-z0-9])([A-Z])/g, "$1 $2").split(/[^A-Za-z0-9]+/).filter(Boolean);
|
|
4
|
+
|
|
5
|
+
const GENERIC_WORDS = new Set([
|
|
6
|
+
"button",
|
|
7
|
+
"field",
|
|
8
|
+
"input",
|
|
9
|
+
"select",
|
|
10
|
+
"box",
|
|
11
|
+
"list",
|
|
12
|
+
"item",
|
|
13
|
+
"row",
|
|
14
|
+
"cell",
|
|
15
|
+
"icon",
|
|
16
|
+
"label",
|
|
17
|
+
"value",
|
|
18
|
+
"text",
|
|
19
|
+
"data",
|
|
20
|
+
"info",
|
|
21
|
+
"meta",
|
|
22
|
+
"main",
|
|
23
|
+
"base",
|
|
24
|
+
"option",
|
|
25
|
+
"panel",
|
|
26
|
+
"view",
|
|
27
|
+
"form",
|
|
28
|
+
"card",
|
|
29
|
+
"tag",
|
|
30
|
+
"chip",
|
|
31
|
+
"badge",
|
|
32
|
+
"menu",
|
|
33
|
+
"bar",
|
|
34
|
+
"grid",
|
|
35
|
+
"table",
|
|
36
|
+
"section",
|
|
37
|
+
"state",
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
module.exports = { pascalWords, GENERIC_WORDS };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@md-code/component-uniqueness",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "ESLint rule and CLI that find duplicate components, raw HTML that should be a canonical component, and copied parts of components, by comparing structure, CSS, behavior, accessibility and framework profile.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"plugin.js",
|
|
27
27
|
"config.js",
|
|
28
28
|
"main.js",
|
|
29
|
+
"logger.js",
|
|
29
30
|
"utils.js",
|
|
30
31
|
"decision-maker.js",
|
|
31
32
|
"report.js",
|
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
const { normalizeOptions } = require("../config.js");
|
|
4
4
|
const { buildStylesheet } = require("./style-entrypoint.js");
|
|
5
|
+
const { buildStyledResolver } = require("../normalize/style-resolver.js");
|
|
6
|
+
|
|
7
|
+
let styledResolver = null;
|
|
8
|
+
const withStyledResolver = (opts) => {
|
|
9
|
+
if (!styledResolver) styledResolver = buildStyledResolver();
|
|
10
|
+
return styledResolver ? { ...opts, styledResolver } : opts;
|
|
11
|
+
};
|
|
5
12
|
|
|
6
13
|
const PLAYGROUNDS = {
|
|
7
14
|
react: () => require("./react.js"),
|
|
@@ -25,19 +32,19 @@ const getFramework = (opts) => normalizeOptions(opts).framework;
|
|
|
25
32
|
const parseComponents = (fileAbs, canonicalNames, opts) => {
|
|
26
33
|
const pg = getPlayground(getFramework(opts));
|
|
27
34
|
if (!pg.available || !pg.available()) return [];
|
|
28
|
-
return pg.parseComponents(fileAbs, canonicalNames, opts);
|
|
35
|
+
return pg.parseComponents(fileAbs, canonicalNames, withStyledResolver(opts));
|
|
29
36
|
};
|
|
30
37
|
|
|
31
38
|
const parseElements = (fileAbs, canonicalNames, opts) => {
|
|
32
39
|
const pg = getPlayground(getFramework(opts));
|
|
33
40
|
if (!pg.available || !pg.available() || !pg.parseElements) return [];
|
|
34
|
-
return pg.parseElements(fileAbs, canonicalNames, opts);
|
|
41
|
+
return pg.parseElements(fileAbs, canonicalNames, withStyledResolver(opts));
|
|
35
42
|
};
|
|
36
43
|
|
|
37
44
|
const parseParts = (fileAbs, canonicalNames, opts) => {
|
|
38
45
|
const pg = getPlayground(getFramework(opts));
|
|
39
46
|
if (!pg.available || !pg.available() || !pg.parseParts) return [];
|
|
40
|
-
return pg.parseParts(fileAbs, canonicalNames, opts);
|
|
47
|
+
return pg.parseParts(fileAbs, canonicalNames, withStyledResolver(opts));
|
|
41
48
|
};
|
|
42
49
|
|
|
43
50
|
const resolveCss = (tree, opts) => {
|
|
@@ -60,6 +67,17 @@ const render = (tree, opts) => {
|
|
|
60
67
|
return pg.render(tree, { stylesheet });
|
|
61
68
|
};
|
|
62
69
|
|
|
70
|
+
const rawClassTokens = (tree) => {
|
|
71
|
+
const out = new Set();
|
|
72
|
+
const visit = (n) => {
|
|
73
|
+
if (!n) return;
|
|
74
|
+
if (typeof n.className === "string") for (const token of n.className.split(/\s+/)) if (token) out.add(token);
|
|
75
|
+
for (const child of n.children || []) visit(child);
|
|
76
|
+
};
|
|
77
|
+
visit(tree);
|
|
78
|
+
return out;
|
|
79
|
+
};
|
|
80
|
+
|
|
63
81
|
module.exports = {
|
|
64
82
|
getPlayground,
|
|
65
83
|
getFramework,
|
|
@@ -68,4 +86,5 @@ module.exports = {
|
|
|
68
86
|
parseParts,
|
|
69
87
|
resolveCss,
|
|
70
88
|
render,
|
|
89
|
+
rawClassTokens,
|
|
71
90
|
};
|
package/playgrounds/react.js
CHANGED
|
@@ -5,7 +5,7 @@ const path = require("node:path");
|
|
|
5
5
|
const ts = require("typescript");
|
|
6
6
|
const { entries } = require("remeda");
|
|
7
7
|
const style = require("../normalize/style.js");
|
|
8
|
-
const { styleObjectToCssText, isPascalCase, isHtmlTag } = require("../normalize/dom.js");
|
|
8
|
+
const { styleObjectToCssText, isPascalCase, isHtmlTag, INTERACTIVE_TAGS } = require("../normalize/dom.js");
|
|
9
9
|
|
|
10
10
|
const MAX_UNFOLD_DEPTH = 3;
|
|
11
11
|
const MAX_TREE_NODES = 400;
|
|
@@ -372,7 +372,7 @@ const memberTarget = (sf, head, prop) => {
|
|
|
372
372
|
return found;
|
|
373
373
|
};
|
|
374
374
|
|
|
375
|
-
const resolveTag = (name, fileAbs, depth, props) => {
|
|
375
|
+
const resolveTag = (name, fileAbs, depth, props, styledResolver) => {
|
|
376
376
|
const staticProps = Object.entries(props || {}).filter(([, v]) => typeof v === "string").sort();
|
|
377
377
|
const key = `${fileAbs}::${name}::${JSON.stringify(staticProps)}`;
|
|
378
378
|
if (resolvedTags.has(key)) return resolvedTags.get(key);
|
|
@@ -395,7 +395,7 @@ const resolveTag = (name, fileAbs, depth, props) => {
|
|
|
395
395
|
const inner = firstComponentNodeIn(decl.body);
|
|
396
396
|
if (!inner) return null;
|
|
397
397
|
resolvedTags.set(key, null);
|
|
398
|
-
const built = buildTree(inner, targetSf, { bodies: localComponentBodies(targetSf), depth: depth + 1, nodes: 0, props: Object.fromEntries(staticProps), resolveTag: (tag, nested) => resolveTag(tag, located.file, depth + 1, nested) });
|
|
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
399
|
const tree = built && { ...built, via: [declName, ...(built.via || [])] };
|
|
400
400
|
resolvedTags.set(key, tree);
|
|
401
401
|
return tree;
|
|
@@ -478,7 +478,7 @@ const buildTree = (node, sf, ctx) => {
|
|
|
478
478
|
cssText: styleObjectToCssText(styleObj),
|
|
479
479
|
children,
|
|
480
480
|
};
|
|
481
|
-
if (!isHtmlTag(tag)) {
|
|
481
|
+
if (!isHtmlTag(tag) && !ctx.noUnfold) {
|
|
482
482
|
const body = ctx.bodies.get(tag);
|
|
483
483
|
if (body && ctx.depth < MAX_UNFOLD_DEPTH) {
|
|
484
484
|
const inner = firstComponentNodeIn(body);
|
|
@@ -489,6 +489,8 @@ const buildTree = (node, sf, ctx) => {
|
|
|
489
489
|
}
|
|
490
490
|
const resolved = ctx.resolveTag ? ctx.resolveTag(tag, attrs) : null;
|
|
491
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);
|
|
492
494
|
dynamic = true;
|
|
493
495
|
dynamicRefs.push(tag);
|
|
494
496
|
}
|
|
@@ -523,6 +525,9 @@ const childTrees = (node, sf, ctx) => {
|
|
|
523
525
|
if (found.length === 0 && child.expression && ts.isIdentifier(child.expression) && child.expression.text === "children") {
|
|
524
526
|
children.push({ tag: "children", attrs: {}, className: null, style: null, cssText: "", children: [] });
|
|
525
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: [] });
|
|
526
531
|
}
|
|
527
532
|
}
|
|
528
533
|
return children;
|
|
@@ -549,6 +554,34 @@ const graftChildren = (resolved, children, usage) => {
|
|
|
549
554
|
return fillSlot(clone, children).node;
|
|
550
555
|
};
|
|
551
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
|
+
|
|
552
585
|
const a11yOfOpening = (opening, sf, out) => {
|
|
553
586
|
for (const attr of opening.attributes.properties) {
|
|
554
587
|
if (!ts.isJsxAttribute(attr) || !attr.name) continue;
|
|
@@ -708,7 +741,7 @@ const parseElements = (fileAbs, canonicalNames, opts) => {
|
|
|
708
741
|
const opening = openingOf(n);
|
|
709
742
|
const tag = tagOf(opening.tagName);
|
|
710
743
|
if (tag && isHtmlTag(tag)) {
|
|
711
|
-
const tree = buildTree(n, sf, { bodies, depth: 0, nodes: 0, props: {}, resolveTag: (name, props) => resolveTag(name, fileAbs, 0, props) });
|
|
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) });
|
|
712
745
|
if (tree) {
|
|
713
746
|
const a11y = new Set();
|
|
714
747
|
a11yOfOpening(opening, sf, a11y);
|
|
@@ -752,7 +785,7 @@ const parseParts = (fileAbs, canonicalNames, opts) => {
|
|
|
752
785
|
if (n !== root && isJsxNode(n)) {
|
|
753
786
|
const size = jsxCount(n);
|
|
754
787
|
if (size >= MIN_PART_NODES && size <= MAX_PART_NODES) {
|
|
755
|
-
const tree = buildTree(n, sf, { bodies, depth: 0, nodes: 0, props: {}, resolveTag: (name, props) => resolveTag(name, fileAbs, 0, props) });
|
|
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) });
|
|
756
789
|
if (tree && treeSize(tree) >= MIN_PART_NODES) {
|
|
757
790
|
const rootTag = tagOf(openingOf(n).tagName);
|
|
758
791
|
const a11y = new Set();
|
|
@@ -782,6 +815,32 @@ const parseParts = (fileAbs, canonicalNames, opts) => {
|
|
|
782
815
|
return out;
|
|
783
816
|
};
|
|
784
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
|
+
|
|
785
844
|
const parseComponents = (fileAbs, canonicalNames, opts) => {
|
|
786
845
|
useConfig(opts);
|
|
787
846
|
const source = fs.readFileSync(fileAbs, "utf8");
|
|
@@ -803,14 +862,17 @@ const parseComponents = (fileAbs, canonicalNames, opts) => {
|
|
|
803
862
|
for (const d of decls) {
|
|
804
863
|
if (!hasComponentMarkup(d.body)) continue;
|
|
805
864
|
const root = firstComponentNodeIn(d.body);
|
|
806
|
-
const tree = root ? buildTree(root, sf, { bodies, depth: 0, nodes: 0, resolveTag: (tag, props) => resolveTag(tag, fileAbs, 0, props) }) : null;
|
|
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;
|
|
807
867
|
const a11y = new Set();
|
|
808
868
|
gatherA11y(d.body, sf, a11y);
|
|
809
869
|
out.push({
|
|
810
870
|
name: d.name,
|
|
811
871
|
line: sf.getLineAndCharacterOfPosition(d.node.getStart(sf)).line + 1,
|
|
812
872
|
isExported: isExported(d.node, sf) || localExportNames(sf).has(d.name),
|
|
873
|
+
declProps: declaredPropsOf(d.node, sf),
|
|
813
874
|
tree,
|
|
875
|
+
rawTree,
|
|
814
876
|
rootTags: [...rootTagsOf(d.body, sf)].sort(),
|
|
815
877
|
actions: [],
|
|
816
878
|
a11y: [...a11y].sort(),
|
|
@@ -853,6 +915,10 @@ const buildDom = (tree, doc) => {
|
|
|
853
915
|
for (const [prop, values] of entries(tree.style)) el.style.setProperty(prop, Array.isArray(values) ? values.join(" ") : String(values));
|
|
854
916
|
}
|
|
855
917
|
for (const child of tree.children || []) {
|
|
918
|
+
if (child.tag === "text") {
|
|
919
|
+
el.appendChild(doc.createTextNode(child.text || ""));
|
|
920
|
+
continue;
|
|
921
|
+
}
|
|
856
922
|
const node = buildDom(child, doc);
|
|
857
923
|
if (node) el.appendChild(node);
|
|
858
924
|
}
|
|
@@ -869,8 +935,11 @@ const nodesOf = (el, win, baselineOf) => {
|
|
|
869
935
|
const value = computed.getPropertyValue(prop).trim();
|
|
870
936
|
if (value && value !== base.getPropertyValue(prop).trim()) css[prop] = value;
|
|
871
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();
|
|
872
941
|
|
|
873
|
-
return [{ tag
|
|
942
|
+
return [{ tag, css, interactive, text: text || undefined, children: kids }];
|
|
874
943
|
};
|
|
875
944
|
|
|
876
945
|
let sharedDom = null;
|
|
@@ -1,7 +1,138 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
+
const ts = require("typescript");
|
|
4
|
+
|
|
5
|
+
const MAX_LITERAL_DEPTH = 4;
|
|
6
|
+
const MAX_LITERAL_COUNT = 12;
|
|
7
|
+
|
|
8
|
+
const stringLiterals = (node, out, depth = 0) => {
|
|
9
|
+
if (!node || depth > MAX_LITERAL_DEPTH || out.length >= MAX_LITERAL_COUNT) return;
|
|
10
|
+
if (ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) {
|
|
11
|
+
out.push(node.text);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
if (ts.isNumericLiteral(node)) {
|
|
15
|
+
out.push(node.text);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (ts.isParenthesizedExpression(node)) {
|
|
19
|
+
stringLiterals(node.expression, out, depth + 1);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (ts.isTemplateExpression(node)) {
|
|
23
|
+
if (node.head.text) out.push(node.head.text);
|
|
24
|
+
for (const span of node.templateSpans) {
|
|
25
|
+
if (span.literal.text) out.push(span.literal.text);
|
|
26
|
+
stringLiterals(span.expression, out, depth + 1);
|
|
27
|
+
}
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (ts.isCallExpression(node)) {
|
|
31
|
+
for (const arg of node.arguments) stringLiterals(arg, out, depth + 1);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (ts.isConditionalExpression(node)) {
|
|
35
|
+
stringLiterals(node.whenTrue, out, depth + 1);
|
|
36
|
+
stringLiterals(node.whenFalse, out, depth + 1);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
if (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.Plus) {
|
|
40
|
+
stringLiterals(node.left, out, depth + 1);
|
|
41
|
+
stringLiterals(node.right, out, depth + 1);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const localDeclarations = (sf) => {
|
|
47
|
+
const map = new Map();
|
|
48
|
+
const visit = (node) => {
|
|
49
|
+
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.initializer && !map.has(node.name.text)) {
|
|
50
|
+
map.set(node.name.text, node.initializer);
|
|
51
|
+
}
|
|
52
|
+
ts.forEachChild(node, visit);
|
|
53
|
+
};
|
|
54
|
+
visit(sf);
|
|
55
|
+
return map;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const literalOf = (expr, decls, depth = 0) => {
|
|
59
|
+
if (!expr || depth > 3) return null;
|
|
60
|
+
if (ts.isStringLiteral(expr) || ts.isNoSubstitutionTemplateLiteral(expr)) return expr.text;
|
|
61
|
+
if (ts.isNumericLiteral(expr)) return expr.text;
|
|
62
|
+
if (ts.isParenthesizedExpression(expr)) return literalOf(expr.expression, decls, depth + 1);
|
|
63
|
+
if (ts.isArrowFunction(expr) || ts.isFunctionExpression(expr)) {
|
|
64
|
+
let body = expr.body;
|
|
65
|
+
if (ts.isBlock(body)) {
|
|
66
|
+
const ret = body.statements.find((s) => ts.isReturnStatement(s) && s.expression);
|
|
67
|
+
if (!ret) return null;
|
|
68
|
+
body = ret.expression;
|
|
69
|
+
}
|
|
70
|
+
return literalOf(body, decls, depth + 1);
|
|
71
|
+
}
|
|
72
|
+
if (ts.isIdentifier(expr)) {
|
|
73
|
+
const decl = decls.get(expr.text);
|
|
74
|
+
if (!decl) return null;
|
|
75
|
+
return literalOf(decl, decls, depth + 1);
|
|
76
|
+
}
|
|
77
|
+
if (ts.isCallExpression(expr) && ts.isIdentifier(expr.expression)) {
|
|
78
|
+
const decl = decls.get(expr.expression.text);
|
|
79
|
+
if (!decl) return null;
|
|
80
|
+
return literalOf(decl, decls, depth + 1);
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const templateToCss = (template, decls, sf) => {
|
|
86
|
+
const unresolved = [];
|
|
87
|
+
if (ts.isNoSubstitutionTemplateLiteral(template)) return { cssText: template.text, unresolved };
|
|
88
|
+
if (!ts.isTemplateExpression(template)) return { cssText: "", unresolved };
|
|
89
|
+
let text = template.head.text;
|
|
90
|
+
for (const span of template.templateSpans) {
|
|
91
|
+
const value = literalOf(span.expression, decls);
|
|
92
|
+
if (value !== null) {
|
|
93
|
+
text += value;
|
|
94
|
+
} else {
|
|
95
|
+
const strs = [];
|
|
96
|
+
stringLiterals(span.expression, strs);
|
|
97
|
+
if (strs.length > 0) text += strs.join(" ");
|
|
98
|
+
else unresolved.push(span.expression.getText(sf));
|
|
99
|
+
}
|
|
100
|
+
text += span.literal.text;
|
|
101
|
+
}
|
|
102
|
+
return { cssText: text, unresolved };
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const styledTagInfo = (tag) => {
|
|
106
|
+
if (ts.isPropertyAccessExpression(tag) && ts.isIdentifier(tag.expression) && tag.expression.text === "styled" && ts.isIdentifier(tag.name)) {
|
|
107
|
+
return { baseTag: tag.name.text, componentRef: null };
|
|
108
|
+
}
|
|
109
|
+
if (ts.isCallExpression(tag) && ts.isIdentifier(tag.expression) && tag.expression.text === "styled" && tag.arguments.length === 1 && ts.isIdentifier(tag.arguments[0])) {
|
|
110
|
+
return { baseTag: null, componentRef: tag.arguments[0].text };
|
|
111
|
+
}
|
|
112
|
+
return null;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const styledDeclarations = (sf) => {
|
|
116
|
+
const out = new Map();
|
|
117
|
+
const decls = localDeclarations(sf);
|
|
118
|
+
const visit = (node) => {
|
|
119
|
+
if (ts.isVariableStatement(node)) {
|
|
120
|
+
for (const d of node.declarationList.declarations) {
|
|
121
|
+
if (!ts.isIdentifier(d.name) || !d.initializer || !ts.isTaggedTemplateExpression(d.initializer)) continue;
|
|
122
|
+
const info = styledTagInfo(d.initializer.tag);
|
|
123
|
+
if (!info) continue;
|
|
124
|
+
const { cssText, unresolved } = templateToCss(d.initializer.template, decls, sf);
|
|
125
|
+
out.set(d.name.text, { baseTag: info.baseTag, componentRef: info.componentRef, cssText, unresolved });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
ts.forEachChild(node, visit);
|
|
129
|
+
};
|
|
130
|
+
visit(sf);
|
|
131
|
+
return out;
|
|
132
|
+
};
|
|
133
|
+
|
|
3
134
|
const resolveClasses = (classes) => {
|
|
4
135
|
return { css: {}, unresolved: [...(classes || [])] };
|
|
5
136
|
};
|
|
6
137
|
|
|
7
|
-
module.exports = { resolveClasses, available: () =>
|
|
138
|
+
module.exports = { styledDeclarations, resolveClasses, available: () => true };
|