@oxy.so/bloom-language-codemod 0.0.0-stage → 0.2.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 +32 -0
- package/LICENSE +673 -0
- package/README.md +42 -2
- package/lib/cli.d.ts +2 -0
- package/lib/cli.js +140 -0
- package/lib/components.d.ts +207 -0
- package/lib/components.js +824 -0
- package/lib/corpus.d.ts +10 -0
- package/lib/corpus.js +193 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.js +18 -0
- package/lib/overrides.d.ts +7 -0
- package/lib/overrides.js +98 -0
- package/lib/report.d.ts +41 -0
- package/lib/report.js +88 -0
- package/lib/resolve.d.ts +60 -0
- package/lib/resolve.js +483 -0
- package/lib/style.d.ts +68 -0
- package/lib/style.js +475 -0
- package/lib/table.d.ts +41 -0
- package/lib/table.js +96 -0
- package/lib/tailwind.d.ts +24 -0
- package/lib/tailwind.js +112 -0
- package/lib/transform.d.ts +61 -0
- package/lib/transform.js +4454 -0
- package/package.json +62 -4
package/lib/corpus.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Fixture } from './transform';
|
|
2
|
+
/**
|
|
3
|
+
* The Bloom that draws a class handed to a Bloom component: Bloom 22, the last
|
|
4
|
+
* that took one (Bloom 23 has no `className` prop), installed as `bloom-22`.
|
|
5
|
+
*/
|
|
6
|
+
export declare const BEFORE_BLOOM = "bloom-22";
|
|
7
|
+
/** Whether the before side hands a class to a Bloom component, which only Bloom 22 draws. */
|
|
8
|
+
export declare function handsBloomAClass(fixture: Fixture): boolean;
|
|
9
|
+
/** The corpus module for `fixtures`, to be written to the directory `dir`. */
|
|
10
|
+
export declare function corpusModule(fixtures: readonly Fixture[], dir: string): string;
|
package/lib/corpus.js
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BEFORE_BLOOM = void 0;
|
|
4
|
+
exports.handsBloomAClass = handsBloomAClass;
|
|
5
|
+
exports.corpusModule = corpusModule;
|
|
6
|
+
/**
|
|
7
|
+
* The parity corpus as a module: each fixture's two sides as functions. The
|
|
8
|
+
* before side is the app as it draws today: React Native's components as
|
|
9
|
+
* NativeWind's own (react-native-css, as the app's Babel preset rewrites React
|
|
10
|
+
* Native's imports, its Text among them, which names no face of its own) and
|
|
11
|
+
* Bloom's. The after side is the language over this checkout's
|
|
12
|
+
* Bloom. `parity/corpus.*.test.tsx` renders them.
|
|
13
|
+
*
|
|
14
|
+
* Bloom 23 has no `className`: a region whose before side hands a class to a
|
|
15
|
+
* Bloom component (`<Text className>`, `contentContainerClassName`) is drawn,
|
|
16
|
+
* before, with the last Bloom that drew it (22, `bloom22` on the case); every
|
|
17
|
+
* other region with this checkout's Bloom on both sides, so the two differ
|
|
18
|
+
* only by what the codemod wrote.
|
|
19
|
+
*/
|
|
20
|
+
const node_path_1 = require("node:path");
|
|
21
|
+
const parser_1 = require("@babel/parser");
|
|
22
|
+
const table_1 = require("./table");
|
|
23
|
+
/** What `screen()` draws a body's root with. */
|
|
24
|
+
const SCREEN_HOST = (0, node_path_1.join)(table_1.LANGUAGE_PACKAGE, 'src/router/screen-host');
|
|
25
|
+
/**
|
|
26
|
+
* The Bloom that draws a class handed to a Bloom component: Bloom 22, the last
|
|
27
|
+
* that took one (Bloom 23 has no `className` prop), installed as `bloom-22`.
|
|
28
|
+
*/
|
|
29
|
+
exports.BEFORE_BLOOM = 'bloom-22';
|
|
30
|
+
const NATIVEWIND = {
|
|
31
|
+
View: 'react-native-css/components/View',
|
|
32
|
+
Pressable: 'react-native-css/components/Pressable',
|
|
33
|
+
Text: 'react-native-css/components/Text',
|
|
34
|
+
Image: 'react-native-css/components/Image',
|
|
35
|
+
};
|
|
36
|
+
/** A module path relative to the corpus written to `dir`. */
|
|
37
|
+
function relativeTo(dir, module) {
|
|
38
|
+
const path = (0, node_path_1.relative)(dir, module);
|
|
39
|
+
return path.startsWith('.') ? path : `./${path}`;
|
|
40
|
+
}
|
|
41
|
+
const BLOOM_MODULE = /^@oxy\.so\/bloom(\/|$)/;
|
|
42
|
+
/** A class prop Bloom 22 took and Bloom 23 removed (`className`, `contentContainerClassName`…). */
|
|
43
|
+
const CLASS_PROP = /^(className|[a-z]\w*ClassName)$/;
|
|
44
|
+
/** A Bloom module on `side`: Bloom 22's for `before22`, else this checkout's. */
|
|
45
|
+
function bloomModule(module, side) {
|
|
46
|
+
if (side !== 'before22' || !BLOOM_MODULE.test(module))
|
|
47
|
+
return module;
|
|
48
|
+
return module.replace('@oxy.so/bloom', exports.BEFORE_BLOOM);
|
|
49
|
+
}
|
|
50
|
+
/** Whether the before side hands a class to a Bloom component, which only Bloom 22 draws. */
|
|
51
|
+
function handsBloomAClass(fixture) {
|
|
52
|
+
const bloom = (name) => {
|
|
53
|
+
const source = fixture.bindings[name.split('.')[0]];
|
|
54
|
+
return source !== undefined && BLOOM_MODULE.test(source.module);
|
|
55
|
+
};
|
|
56
|
+
let found = false;
|
|
57
|
+
const visit = (node) => {
|
|
58
|
+
if (found || node === null || typeof node !== 'object')
|
|
59
|
+
return;
|
|
60
|
+
if (Array.isArray(node)) {
|
|
61
|
+
for (const item of node)
|
|
62
|
+
visit(item);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const n = node;
|
|
66
|
+
if (n.type === 'JSXOpeningElement') {
|
|
67
|
+
const name = n.name.type === 'JSXIdentifier'
|
|
68
|
+
? n.name.name
|
|
69
|
+
: n.name.type === 'JSXMemberExpression' && n.name.object.type === 'JSXIdentifier'
|
|
70
|
+
? n.name.object.name
|
|
71
|
+
: undefined;
|
|
72
|
+
if (name !== undefined &&
|
|
73
|
+
bloom(name) &&
|
|
74
|
+
n.attributes.some((a) => a.type === 'JSXAttribute' &&
|
|
75
|
+
a.name.type === 'JSXIdentifier' &&
|
|
76
|
+
CLASS_PROP.test(a.name.name)))
|
|
77
|
+
found = true;
|
|
78
|
+
}
|
|
79
|
+
for (const [key, value] of Object.entries(n))
|
|
80
|
+
if (key !== 'loc' && key !== 'start' && key !== 'end')
|
|
81
|
+
visit(value);
|
|
82
|
+
};
|
|
83
|
+
visit((0, parser_1.parseExpression)(`<>${fixture.before}</>`, { plugins: ['jsx', 'typescript'] }));
|
|
84
|
+
return found;
|
|
85
|
+
}
|
|
86
|
+
/** The module and export a binding is drawn from on `side` of a corpus written to `dir`. */
|
|
87
|
+
function origin(source, dir, side) {
|
|
88
|
+
if (source.module === 'react-native') {
|
|
89
|
+
const module = NATIVEWIND[source.imported];
|
|
90
|
+
if (module)
|
|
91
|
+
return { module, name: source.imported };
|
|
92
|
+
}
|
|
93
|
+
const module = source.module === '@oxy.so/bloom/icons'
|
|
94
|
+
? `@oxy.so/bloom/icons/${source.imported}`
|
|
95
|
+
: source.module;
|
|
96
|
+
return { module: bloomModule(module, side), name: source.imported };
|
|
97
|
+
}
|
|
98
|
+
/** The corpus module for `fixtures`, to be written to the directory `dir`. */
|
|
99
|
+
function corpusModule(fixtures, dir) {
|
|
100
|
+
const imports = new Map();
|
|
101
|
+
const alias = (source, side) => {
|
|
102
|
+
const { module, name } = origin(source, dir, side);
|
|
103
|
+
const key = `${module}#${name}`;
|
|
104
|
+
const known = imports.get(key);
|
|
105
|
+
if (known)
|
|
106
|
+
return known;
|
|
107
|
+
const local = `I${imports.size}`;
|
|
108
|
+
imports.set(key, local);
|
|
109
|
+
return local;
|
|
110
|
+
};
|
|
111
|
+
/** Each region's app colour table, declared once at module scope as the app declares it. */
|
|
112
|
+
const tables = [];
|
|
113
|
+
const cases = fixtures.map((fixture) => {
|
|
114
|
+
const bloom22 = handsBloomAClass(fixture);
|
|
115
|
+
const beforeSide = bloom22 ? 'before22' : 'before';
|
|
116
|
+
const bindings = (side) => Object.entries(fixture.bindings)
|
|
117
|
+
.map(([local, source]) => `const ${local} = ${alias(source, side)};`)
|
|
118
|
+
.join(' ');
|
|
119
|
+
// A language name the file already imports (a file partly in the language) is bound once, as a binding.
|
|
120
|
+
const names = fixture.language.filter((entry) => !(entry.split(':').pop().trim() in fixture.bindings));
|
|
121
|
+
const language = names.length ? `const { ${names.join(', ')} } = L;` : '';
|
|
122
|
+
// A node drawn only inside a Bloom component (a screen's scroll view in a Screen) is, on both sides.
|
|
123
|
+
const within = (side) => fixture.within.map((key) => {
|
|
124
|
+
const [module, imported] = key.split('#');
|
|
125
|
+
return alias({ module, imported }, side);
|
|
126
|
+
});
|
|
127
|
+
// A region that pads by the device's insets is drawn under the corpus's, on both sides.
|
|
128
|
+
const open = (side) => [
|
|
129
|
+
...(fixture.insets ? ['<SafeAreaInsetsContext.Provider value={PARITY_INSETS}>{'] : []),
|
|
130
|
+
...within(side).map((local) => `<${local}>{`),
|
|
131
|
+
].join('');
|
|
132
|
+
const close = (side) => [
|
|
133
|
+
...within(side)
|
|
134
|
+
.map((local) => `}</${local}>`)
|
|
135
|
+
.reverse(),
|
|
136
|
+
...(fixture.insets ? ['}</SafeAreaInsetsContext.Provider>'] : []),
|
|
137
|
+
].join('');
|
|
138
|
+
// A before side that reads the theme is a component, so it reads it where it is drawn.
|
|
139
|
+
const theme = bloom22 ? 'useParityTheme22' : 'useParityTheme';
|
|
140
|
+
const before = fixture.themed
|
|
141
|
+
? `function Before() { const parityTheme = ${theme}(); const parityTokens = useParityTokens(); const paritySurface = useParitySurface(); ${bindings(beforeSide)} return (${fixture.before}); } return (${open(beforeSide)}<Before />${close(beforeSide)});`
|
|
142
|
+
: `${bindings(beforeSide)} return (${open(beforeSide)}${fixture.before}${close(beforeSide)});`;
|
|
143
|
+
let colors = '';
|
|
144
|
+
if (fixture.appColors) {
|
|
145
|
+
const local = `T${tables.length}`;
|
|
146
|
+
tables.push(`const ${local} = L.defineTokens({ color: ${JSON.stringify(fixture.appColors.color)} });`);
|
|
147
|
+
colors = `const ${fixture.appColors.table} = ${local};`;
|
|
148
|
+
}
|
|
149
|
+
// A route's Screen is drawn as `screen()` draws its body's root.
|
|
150
|
+
const after = fixture.screen ? `<ScreenHost root={${fixture.after}} />` : fixture.after;
|
|
151
|
+
return ` {
|
|
152
|
+
name: ${JSON.stringify(fixture.name)},
|
|
153
|
+
bloom22: ${bloom22},
|
|
154
|
+
before: () => { ${before} },
|
|
155
|
+
after: () => { ${bindings('after')} ${language} ${colors} return (${open('after')}${after}${close('after')}); },
|
|
156
|
+
},`;
|
|
157
|
+
});
|
|
158
|
+
const lines = [
|
|
159
|
+
'// @ts-nocheck GENERATED by bloom-language-codemod --fixtures: each region of the app, before and after.',
|
|
160
|
+
"import * as RN from 'react-native';",
|
|
161
|
+
"import * as L from '@oxy.so/bloom-language';",
|
|
162
|
+
"import { toElement } from '@oxy.so/bloom-language';",
|
|
163
|
+
"import { useColorTokens as useParityTokens, useTheme as useParityTheme } from '@oxy.so/bloom/theme';",
|
|
164
|
+
`import { useTheme as useParityTheme22 } from '${exports.BEFORE_BLOOM}/theme';`,
|
|
165
|
+
"import { useSurfaceFill as useParitySurface } from '@oxy.so/bloom/styles';",
|
|
166
|
+
"import { SafeAreaInsetsContext } from 'react-native-safe-area-context';",
|
|
167
|
+
`import { ScreenHost } from '${relativeTo(dir, SCREEN_HOST)}';`,
|
|
168
|
+
...[...imports].map(([key, local]) => {
|
|
169
|
+
const [module, name] = key.split('#');
|
|
170
|
+
return `import { ${name} as ${local} } from '${module}';`;
|
|
171
|
+
}),
|
|
172
|
+
'',
|
|
173
|
+
...(tables.length ? [...tables, ''] : []),
|
|
174
|
+
'/** The insets of a phone with a notch and a home bar. */',
|
|
175
|
+
'const PARITY_INSETS = { top: 47, bottom: 34, left: 0, right: 0 };',
|
|
176
|
+
'',
|
|
177
|
+
'function Hole({ n }: { n: string }) {',
|
|
178
|
+
" return <RN.View testID={'hole-' + n} />;",
|
|
179
|
+
'}',
|
|
180
|
+
'',
|
|
181
|
+
'/** The items an array of data stands for, on both sides: records with the fields Bloom reads of a list, a choice or a member. */',
|
|
182
|
+
'const ITEMS = [',
|
|
183
|
+
" { id: 'a', value: 'a', label: 'Ana', title: 'Ana', name: 'Ana' },",
|
|
184
|
+
" { id: 'b', value: 'b', label: 'Rui', title: 'Rui', name: 'Rui' },",
|
|
185
|
+
'];',
|
|
186
|
+
'',
|
|
187
|
+
'export const CASES = [',
|
|
188
|
+
...cases,
|
|
189
|
+
'];',
|
|
190
|
+
'',
|
|
191
|
+
];
|
|
192
|
+
return lines.join('\n');
|
|
193
|
+
}
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@oxy.so/bloom-language-codemod`: the className codemod as a library, for a
|
|
3
|
+
* tool that drives it itself. The CLI (`bloom-language-codemod`) is the usual
|
|
4
|
+
* way in (`docs/bloom-language-codemod.mdx` in the Bloom repository).
|
|
5
|
+
*/
|
|
6
|
+
export { overriddenClasses } from './overrides';
|
|
7
|
+
export { report } from './report';
|
|
8
|
+
export { loadTable, type Table } from './table';
|
|
9
|
+
export { type PackageRoots, tailwind } from './tailwind';
|
|
10
|
+
export { type FileResult, type Fixture, type TransformOptions, transformSource, type Unconverted, } from './transform';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.transformSource = exports.tailwind = exports.loadTable = exports.report = exports.overriddenClasses = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* `@oxy.so/bloom-language-codemod`: the className codemod as a library, for a
|
|
6
|
+
* tool that drives it itself. The CLI (`bloom-language-codemod`) is the usual
|
|
7
|
+
* way in (`docs/bloom-language-codemod.mdx` in the Bloom repository).
|
|
8
|
+
*/
|
|
9
|
+
var overrides_1 = require("./overrides");
|
|
10
|
+
Object.defineProperty(exports, "overriddenClasses", { enumerable: true, get: function () { return overrides_1.overriddenClasses; } });
|
|
11
|
+
var report_1 = require("./report");
|
|
12
|
+
Object.defineProperty(exports, "report", { enumerable: true, get: function () { return report_1.report; } });
|
|
13
|
+
var table_1 = require("./table");
|
|
14
|
+
Object.defineProperty(exports, "loadTable", { enumerable: true, get: function () { return table_1.loadTable; } });
|
|
15
|
+
var tailwind_1 = require("./tailwind");
|
|
16
|
+
Object.defineProperty(exports, "tailwind", { enumerable: true, get: function () { return tailwind_1.tailwind; } });
|
|
17
|
+
var transform_1 = require("./transform");
|
|
18
|
+
Object.defineProperty(exports, "transformSource", { enumerable: true, get: function () { return transform_1.transformSource; } });
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type PackageRoots } from './tailwind';
|
|
2
|
+
/** The CSS class selector of a Tailwind candidate. */
|
|
3
|
+
export declare function classSelector(candidate: string): string;
|
|
4
|
+
/** Each candidate's declarations, its variables resolved, from one built stylesheet. */
|
|
5
|
+
export declare function signatures(css: string, candidates: readonly string[]): Map<string, string>;
|
|
6
|
+
/** The candidates an app's stylesheet (`css`, read from `from`) draws differently from Bloom's. */
|
|
7
|
+
export declare function overriddenClasses(css: string, from: string, candidates: readonly string[], roots?: PackageRoots): Promise<Set<string>>;
|
package/lib/overrides.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.classSelector = classSelector;
|
|
7
|
+
exports.signatures = signatures;
|
|
8
|
+
exports.overriddenClasses = overriddenClasses;
|
|
9
|
+
/**
|
|
10
|
+
* Which classes an app's stylesheet draws differently from Bloom's.
|
|
11
|
+
*
|
|
12
|
+
* The className table was built against Bloom's stylesheet (Tailwind, NativeWind's
|
|
13
|
+
* theme and Bloom's tokens). An app's own `@theme` or `:root` can redefine a
|
|
14
|
+
* value under the same class (`--radius-lg: var(--radius)`), so a class lands on
|
|
15
|
+
* the table's token only when the app's CSS for it is the same as Bloom's:
|
|
16
|
+
* every declaration its rules write, under the same at-rules, with each
|
|
17
|
+
* `var()` resolved through the root custom properties of that stylesheet.
|
|
18
|
+
*/
|
|
19
|
+
const node_path_1 = require("node:path");
|
|
20
|
+
const postcss_1 = __importDefault(require("postcss"));
|
|
21
|
+
const tailwind_1 = require("./tailwind");
|
|
22
|
+
const ROOT = /^(:root|:host|html)(\s*,\s*(:root|:host|html))*$/;
|
|
23
|
+
/** The CSS class selector of a Tailwind candidate. */
|
|
24
|
+
function classSelector(candidate) {
|
|
25
|
+
return `.${candidate.replace(/[^a-zA-Z0-9_-]/g, (c) => `\\${c}`)}`;
|
|
26
|
+
}
|
|
27
|
+
function atRules(node) {
|
|
28
|
+
const out = [];
|
|
29
|
+
for (let at = node.parent; at && at.type !== 'root'; at = at.parent) {
|
|
30
|
+
if (at.type === 'atrule') {
|
|
31
|
+
const rule = at;
|
|
32
|
+
if (rule.name !== 'layer')
|
|
33
|
+
out.unshift(`@${rule.name} ${rule.params}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return out.join(' ');
|
|
37
|
+
}
|
|
38
|
+
function inLayer(node) {
|
|
39
|
+
for (let at = node.parent; at && at.type !== 'root'; at = at.parent)
|
|
40
|
+
if (at.type === 'atrule' && at.name === 'layer')
|
|
41
|
+
return true;
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
/** Each candidate's declarations, its variables resolved, from one built stylesheet. */
|
|
45
|
+
function signatures(css, candidates) {
|
|
46
|
+
const root = postcss_1.default.parse(css);
|
|
47
|
+
const layered = {};
|
|
48
|
+
const unlayered = {};
|
|
49
|
+
root.walkRules((rule) => {
|
|
50
|
+
if (!ROOT.test(rule.selector.trim()) || atRules(rule))
|
|
51
|
+
return;
|
|
52
|
+
const into = inLayer(rule) ? layered : unlayered;
|
|
53
|
+
rule.each((child) => {
|
|
54
|
+
if (child.type === 'decl' && child.prop.startsWith('--'))
|
|
55
|
+
into[child.prop] = child.value;
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
const vars = { ...layered, ...unlayered };
|
|
59
|
+
const resolve = (value, depth = 0) => depth > 16
|
|
60
|
+
? value
|
|
61
|
+
: value.replace(/var\(\s*(--[\w-]+)\s*(?:,\s*([^()]*(?:\([^()]*\)[^()]*)*))?\)/g, (all, name, fallback) => {
|
|
62
|
+
const found = vars[name];
|
|
63
|
+
if (found !== undefined)
|
|
64
|
+
return resolve(found, depth + 1);
|
|
65
|
+
return fallback !== undefined ? resolve(fallback, depth + 1) : all;
|
|
66
|
+
});
|
|
67
|
+
const selectors = new Map(candidates.map((c) => [c, classSelector(c)]));
|
|
68
|
+
const out = new Map();
|
|
69
|
+
root.walkRules((rule) => {
|
|
70
|
+
for (const [candidate, selector] of selectors) {
|
|
71
|
+
const at = rule.selector.indexOf(selector);
|
|
72
|
+
if (at < 0)
|
|
73
|
+
continue;
|
|
74
|
+
const next = rule.selector[at + selector.length];
|
|
75
|
+
if (next !== undefined && /[\w-\\]/.test(next))
|
|
76
|
+
continue;
|
|
77
|
+
const lines = out.get(candidate) ?? [];
|
|
78
|
+
const where = `${atRules(rule)} ${rule.selector.split(selector).join('&')}`;
|
|
79
|
+
rule.each((child) => {
|
|
80
|
+
if (child.type === 'decl')
|
|
81
|
+
lines.push(`${where} { ${child.prop}: ${resolve(child.value)}${child.important ? ' !important' : ''} }`);
|
|
82
|
+
});
|
|
83
|
+
out.set(candidate, lines);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
return new Map([...out].map(([candidate, lines]) => [candidate, lines.join('\n')]));
|
|
87
|
+
}
|
|
88
|
+
/** The candidates an app's stylesheet (`css`, read from `from`) draws differently from Bloom's. */
|
|
89
|
+
async function overriddenClasses(css, from, candidates, roots = {}) {
|
|
90
|
+
const app = signatures((await (0, tailwind_1.tailwind)(css, from, roots)).build(candidates), candidates);
|
|
91
|
+
// Bloom's own entry, its packages read as this package reads them: the Bloom installed beside it.
|
|
92
|
+
const bloom = signatures((await (0, tailwind_1.tailwind)(tailwind_1.BLOOM_BASELINE_CSS, (0, node_path_1.join)(__dirname, 'bloom-baseline.css'))).build(candidates), candidates);
|
|
93
|
+
const out = new Set();
|
|
94
|
+
for (const candidate of candidates)
|
|
95
|
+
if (app.get(candidate) !== bloom.get(candidate))
|
|
96
|
+
out.add(candidate);
|
|
97
|
+
return out;
|
|
98
|
+
}
|
package/lib/report.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { FileResult } from './transform';
|
|
2
|
+
export interface ReportOptions {
|
|
3
|
+
readonly app: string;
|
|
4
|
+
readonly overridden: ReadonlySet<string>;
|
|
5
|
+
}
|
|
6
|
+
export declare function report(results: readonly {
|
|
7
|
+
file: string;
|
|
8
|
+
result: FileResult;
|
|
9
|
+
}[], options: ReportOptions): {
|
|
10
|
+
json: {
|
|
11
|
+
app: string;
|
|
12
|
+
files: number;
|
|
13
|
+
changedFiles: number;
|
|
14
|
+
candidates: number;
|
|
15
|
+
converted: number;
|
|
16
|
+
regions: number;
|
|
17
|
+
rate: number;
|
|
18
|
+
overridden: string[];
|
|
19
|
+
bloomComponents: {
|
|
20
|
+
[k: string]: number;
|
|
21
|
+
};
|
|
22
|
+
patterns: {
|
|
23
|
+
code: string;
|
|
24
|
+
elements: number;
|
|
25
|
+
top: {
|
|
26
|
+
[k: string]: number;
|
|
27
|
+
};
|
|
28
|
+
}[];
|
|
29
|
+
unconverted: {
|
|
30
|
+
file: string;
|
|
31
|
+
line: number;
|
|
32
|
+
element: string;
|
|
33
|
+
reasons: {
|
|
34
|
+
subject: string;
|
|
35
|
+
code: string;
|
|
36
|
+
why: string;
|
|
37
|
+
}[];
|
|
38
|
+
}[];
|
|
39
|
+
};
|
|
40
|
+
markdown: string;
|
|
41
|
+
};
|
package/lib/report.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.report = report;
|
|
4
|
+
/** The codemod's report: what converted, and every element that did not, with why. */
|
|
5
|
+
const node_path_1 = require("node:path");
|
|
6
|
+
function report(results, options) {
|
|
7
|
+
let candidates = 0;
|
|
8
|
+
let converted = 0;
|
|
9
|
+
let regions = 0;
|
|
10
|
+
const components = new Map();
|
|
11
|
+
const byCode = new Map();
|
|
12
|
+
const elements = [];
|
|
13
|
+
for (const { file, result } of results) {
|
|
14
|
+
candidates += result.candidates;
|
|
15
|
+
converted += result.converted;
|
|
16
|
+
regions += result.regions;
|
|
17
|
+
for (const [key, count] of Object.entries(result.bloomComponents))
|
|
18
|
+
components.set(key, (components.get(key) ?? 0) + count);
|
|
19
|
+
for (const u of result.unconverted) {
|
|
20
|
+
elements.push({ file, line: u.line, element: u.element, reasons: [...u.reasons] });
|
|
21
|
+
for (const code of new Set(u.reasons.map((r) => r.code))) {
|
|
22
|
+
const entry = byCode.get(code) ?? { elements: 0, subjects: new Map() };
|
|
23
|
+
entry.elements++;
|
|
24
|
+
byCode.set(code, entry);
|
|
25
|
+
}
|
|
26
|
+
for (const r of u.reasons) {
|
|
27
|
+
const entry = byCode.get(r.code);
|
|
28
|
+
if (entry)
|
|
29
|
+
entry.subjects.set(r.subject, (entry.subjects.get(r.subject) ?? 0) + 1);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const rate = candidates ? (100 * converted) / candidates : 0;
|
|
34
|
+
const patterns = [...byCode]
|
|
35
|
+
.sort((a, b) => b[1].elements - a[1].elements)
|
|
36
|
+
.map(([code, { elements: count, subjects }]) => ({
|
|
37
|
+
code,
|
|
38
|
+
elements: count,
|
|
39
|
+
top: [...subjects].sort((a, b) => b[1] - a[1]).slice(0, 6),
|
|
40
|
+
}));
|
|
41
|
+
const json = {
|
|
42
|
+
app: (0, node_path_1.relative)(process.cwd(), options.app) || '.',
|
|
43
|
+
files: results.length,
|
|
44
|
+
changedFiles: results.filter((r) => r.result.changed).length,
|
|
45
|
+
candidates,
|
|
46
|
+
converted,
|
|
47
|
+
regions,
|
|
48
|
+
rate: Number(rate.toFixed(1)),
|
|
49
|
+
overridden: [...options.overridden].sort(),
|
|
50
|
+
bloomComponents: Object.fromEntries([...components].sort((a, b) => b[1] - a[1])),
|
|
51
|
+
patterns: patterns.map((p) => ({
|
|
52
|
+
code: p.code,
|
|
53
|
+
elements: p.elements,
|
|
54
|
+
top: Object.fromEntries(p.top),
|
|
55
|
+
})),
|
|
56
|
+
unconverted: elements,
|
|
57
|
+
};
|
|
58
|
+
const lines = [
|
|
59
|
+
`# Bloom language codemod: ${(0, node_path_1.relative)(process.cwd(), options.app) || '.'}`,
|
|
60
|
+
'',
|
|
61
|
+
`- Files: ${json.files} read, ${json.changedFiles} changed`,
|
|
62
|
+
`- Elements considered (React Native View, Pressable, Text, Image; Bloom Text, Button, icons and mapped components): ${candidates}`,
|
|
63
|
+
`- Converted: ${converted} (${rate.toFixed(1)}%), in ${regions} regions`,
|
|
64
|
+
`- Classes the app's theme draws differently from Bloom's: ${options.overridden.size}`,
|
|
65
|
+
'',
|
|
66
|
+
'## Why elements stayed JSX',
|
|
67
|
+
'',
|
|
68
|
+
'| Reason | Elements | Most frequent |',
|
|
69
|
+
'| --- | ---: | --- |',
|
|
70
|
+
...patterns.map((p) => `| ${p.code} | ${p.elements} | ${p.top.map(([s, c]) => `\`${s}\` ${c}`).join(', ')} |`),
|
|
71
|
+
'',
|
|
72
|
+
'## Bloom components left as JSX',
|
|
73
|
+
'',
|
|
74
|
+
'Not mapped by the codemod yet (each needs its prop rules and parity first):',
|
|
75
|
+
'',
|
|
76
|
+
'| Component | Elements |',
|
|
77
|
+
'| --- | ---: |',
|
|
78
|
+
...[...components]
|
|
79
|
+
.sort((a, b) => b[1] - a[1])
|
|
80
|
+
.map(([key, count]) => `| \`${key.replace('#', '` `')}\` | ${count} |`),
|
|
81
|
+
'',
|
|
82
|
+
'## Each element',
|
|
83
|
+
'',
|
|
84
|
+
...elements.map((e) => `- ${e.file}:${e.line} \`<${e.element}>\`: ${e.reasons.map((r) => `\`${r.subject}\` (${r.code}${r.why ? `: ${r.why}` : ''})`).join('; ')}`),
|
|
85
|
+
'',
|
|
86
|
+
];
|
|
87
|
+
return { json, markdown: lines.join('\n') };
|
|
88
|
+
}
|
package/lib/resolve.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { type Mod, type Table } from './table';
|
|
2
|
+
/** What draws the className: a React Native View, Pressable or a Bloom Text. */
|
|
3
|
+
export type HostKind = 'view' | 'pressable' | 'text';
|
|
4
|
+
export interface Reason {
|
|
5
|
+
/** The class, as written, or the element's prop. */
|
|
6
|
+
readonly subject: string;
|
|
7
|
+
/** A stable code for the report's tallies. */
|
|
8
|
+
readonly code: string;
|
|
9
|
+
readonly why: string;
|
|
10
|
+
}
|
|
11
|
+
export interface Variant {
|
|
12
|
+
/** The `when()` conditions, outermost first. */
|
|
13
|
+
readonly conditions: readonly string[];
|
|
14
|
+
readonly mods: readonly Mod[];
|
|
15
|
+
}
|
|
16
|
+
export interface Resolved {
|
|
17
|
+
readonly axis: 'row' | 'column';
|
|
18
|
+
readonly mods: readonly Mod[];
|
|
19
|
+
readonly variants: readonly Variant[];
|
|
20
|
+
}
|
|
21
|
+
export type Resolution = {
|
|
22
|
+
readonly ok: true;
|
|
23
|
+
readonly value: Resolved;
|
|
24
|
+
} | {
|
|
25
|
+
readonly ok: false;
|
|
26
|
+
readonly reasons: readonly Reason[];
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Whether a variant under a condition that holds as the view renders (a
|
|
30
|
+
* platform, the app's own boolean) changes `mod`: one a variant varies, or
|
|
31
|
+
* anything but a box layer or a placement, which is then the chain's own
|
|
32
|
+
* (render.tsx, `heldAsOwn`).
|
|
33
|
+
*/
|
|
34
|
+
export declare function heldVariantChanges(mod: Mod): boolean;
|
|
35
|
+
/** Whether two corner sides round one corner: a physical corner meets either reading-direction one of its row right to left. */
|
|
36
|
+
export declare function cornersMeet(a: string, b: string): boolean;
|
|
37
|
+
/** Whether two edges write one side: a physical side meets either reading-direction side right to left. */
|
|
38
|
+
export declare function edgesMeet(a: string, b: string): boolean;
|
|
39
|
+
/** Splits `md:hover:p-4` into its variants and utility; brackets keep their colons. */
|
|
40
|
+
export declare function splitVariants(token: string): {
|
|
41
|
+
variants: string[];
|
|
42
|
+
utility: string;
|
|
43
|
+
};
|
|
44
|
+
export interface ResolveOptions {
|
|
45
|
+
/** Classes the app's own theme draws differently from Bloom's (see `overrides.ts`). */
|
|
46
|
+
readonly overridden?: ReadonlySet<string>;
|
|
47
|
+
/**
|
|
48
|
+
* Modifiers a `style` writes that no utility does (React Native's `flex: 1`
|
|
49
|
+
* as `.grow()`, a theme colour as its role), by the style key: they join the
|
|
50
|
+
* classes' modifiers, so a class setting the same thing is a conflict.
|
|
51
|
+
*/
|
|
52
|
+
readonly styleMods?: readonly {
|
|
53
|
+
readonly subject: string;
|
|
54
|
+
readonly mod: Mod;
|
|
55
|
+
}[];
|
|
56
|
+
/** The classes a `style` was read as (`gap: 12` is `gap-3`): inline, so no variant overrides them. */
|
|
57
|
+
readonly styleClasses?: ReadonlySet<string>;
|
|
58
|
+
}
|
|
59
|
+
/** Resolves `className` on a host of `kind`. */
|
|
60
|
+
export declare function resolveClassName(className: string, kind: HostKind, table: Table, options?: ResolveOptions): Resolution;
|