@oxy.so/bloom-language-codemod 0.0.0-bootstrap.0 → 0.1.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/lib/table.js ADDED
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LANGUAGE_PACKAGE = void 0;
4
+ exports.chainOf = chainOf;
5
+ exports.loadTable = loadTable;
6
+ /**
7
+ * The className table (`packages/bloom-language/codemod/classnames.generated.json`)
8
+ * as modifier calls: each utility the table maps to a plain chain of literal
9
+ * modifiers is read into `Mod`s; everything else keeps its status and the
10
+ * table's words, which the resolver reports.
11
+ */
12
+ const node_fs_1 = require("node:fs");
13
+ const node_module_1 = require("node:module");
14
+ const node_path_1 = require("node:path");
15
+ const parser_1 = require("@babel/parser");
16
+ /**
17
+ * The Bloom language package the codemod writes for: the one installed beside
18
+ * it (its peer), whose spec, exports and className table it reads.
19
+ */
20
+ exports.LANGUAGE_PACKAGE = (0, node_path_1.dirname)((0, node_module_1.createRequire)(__filename).resolve('@oxy.so/bloom-language/package.json'));
21
+ function literal(node) {
22
+ switch (node.type) {
23
+ case 'StringLiteral':
24
+ case 'NumericLiteral':
25
+ case 'BooleanLiteral':
26
+ return node.value;
27
+ case 'ObjectExpression': {
28
+ const out = {};
29
+ for (const property of node.properties) {
30
+ if (property.type !== 'ObjectProperty' || property.computed)
31
+ return undefined;
32
+ const key = property.key.type === 'Identifier'
33
+ ? property.key.name
34
+ : property.key.type === 'StringLiteral'
35
+ ? property.key.value
36
+ : undefined;
37
+ if (key === undefined || property.value.type !== 'StringLiteral')
38
+ return undefined;
39
+ out[key] = property.value.value;
40
+ }
41
+ return out;
42
+ }
43
+ default:
44
+ return undefined;
45
+ }
46
+ }
47
+ /** `.a('x').b({ k: 'v' })` as mods, or null when it is anything but literal calls. */
48
+ function chainOf(language) {
49
+ let expression;
50
+ try {
51
+ expression = (0, parser_1.parseExpression)(`$${language}`);
52
+ }
53
+ catch {
54
+ return null;
55
+ }
56
+ const mods = [];
57
+ let at = expression;
58
+ while (at.type === 'CallExpression') {
59
+ const callee = at.callee;
60
+ if (callee.type !== 'MemberExpression' || callee.property.type !== 'Identifier')
61
+ return null;
62
+ const args = [];
63
+ for (const argument of at.arguments) {
64
+ if (argument.type === 'SpreadElement' || argument.type === 'ArgumentPlaceholder')
65
+ return null;
66
+ const value = literal(argument);
67
+ if (value === undefined)
68
+ return null;
69
+ args.push(value);
70
+ }
71
+ mods.unshift({ name: callee.property.name, args });
72
+ at = callee.object;
73
+ }
74
+ return at.type === 'Identifier' && at.name === '$' && mods.length > 0 ? mods : null;
75
+ }
76
+ function loadTable(path = (0, node_path_1.join)(exports.LANGUAGE_PACKAGE, 'codemod/classnames.generated.json')) {
77
+ const raw = JSON.parse((0, node_fs_1.readFileSync)(path, 'utf8'));
78
+ const utilities = new Map();
79
+ for (const [name, entry] of Object.entries(raw.utilities)) {
80
+ utilities.set(name, {
81
+ status: entry.status,
82
+ language: entry.language,
83
+ mods: entry.status === 'mapped' ? chainOf(entry.language) : null,
84
+ scale: entry.scale,
85
+ value: entry.value,
86
+ });
87
+ }
88
+ return { utilities, bareIcon: chainOf(raw.icons.bare) ?? [] };
89
+ }
@@ -0,0 +1,24 @@
1
+ /** Bloom's own entry: Tailwind, NativeWind's theme and Bloom's tokens, what the className table was built against. */
2
+ export declare const BLOOM_BASELINE_CSS: string;
3
+ /** Package roots given by hand (a package the app installs that Bloom does not, read from its own repository). */
4
+ export type PackageRoots = Readonly<Record<string, string>>;
5
+ /** The directory of package `name` in the first `node_modules` above any of `from`, in order. */
6
+ export declare function packageRoot(name: string, from: readonly string[]): string | undefined;
7
+ export interface Stylesheet {
8
+ /** The CSS Tailwind writes for these classes, as the app's build writes it. */
9
+ build(classes: readonly string[]): string;
10
+ }
11
+ /**
12
+ * Compiles `css` (an app's entry, read from `from`) as Tailwind does in the
13
+ * app's build; its packages are read from `roots`, then above `from`, then
14
+ * above `packages` (default: this package).
15
+ */
16
+ export declare function tailwind(css: string, from: string, roots?: PackageRoots, packages?: string): Promise<Stylesheet>;
17
+ /**
18
+ * The class candidates in a package's built JavaScript, as an app's `@source`
19
+ * glob over it (`node_modules/@oxy.so/bloom/lib/**\/*.{js,jsx}`) hands them to
20
+ * Tailwind: every word of each file, split at whitespace, quotes and template
21
+ * holes. Tailwind writes CSS only for the words that are utilities, so a word
22
+ * that is not one adds nothing.
23
+ */
24
+ export declare function packageCandidates(dir: string): string[];
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BLOOM_BASELINE_CSS = void 0;
4
+ exports.packageRoot = packageRoot;
5
+ exports.tailwind = tailwind;
6
+ exports.packageCandidates = packageCandidates;
7
+ /**
8
+ * An app's NativeWind stylesheet, compiled as its build compiles it: the app's
9
+ * CSS entry through Tailwind 4 (`@tailwindcss/node`), for the candidates given.
10
+ *
11
+ * A package the entry imports (`tailwindcss`, `nativewind/theme`,
12
+ * `@oxy.so/bloom/design-tokens/theme.css`) is read from the first of: a root
13
+ * given by hand (`--package`), the `node_modules` above the entry (the app's
14
+ * own install), then the `node_modules` above this package (an app read from
15
+ * a copy with nothing installed takes this install's).
16
+ */
17
+ const node_fs_1 = require("node:fs");
18
+ const node_module_1 = require("node:module");
19
+ const node_path_1 = require("node:path");
20
+ const node_1 = require("@tailwindcss/node");
21
+ /** Bloom's own entry: Tailwind, NativeWind's theme and Bloom's tokens, what the className table was built against. */
22
+ exports.BLOOM_BASELINE_CSS = [
23
+ '@import "tailwindcss/theme.css" layer(theme);',
24
+ '@import "tailwindcss/preflight.css" layer(base);',
25
+ '@import "tailwindcss/utilities.css";',
26
+ '@import "nativewind/theme";',
27
+ '@import "@oxy.so/bloom/design-tokens/theme.css";',
28
+ ].join('\n');
29
+ /** The directory of package `name` in the first `node_modules` above any of `from`, in order. */
30
+ function packageRoot(name, from) {
31
+ for (const start of from)
32
+ for (let dir = (0, node_path_1.resolve)(start);; dir = (0, node_path_1.dirname)(dir)) {
33
+ const root = (0, node_path_1.join)(dir, 'node_modules', name);
34
+ if ((0, node_fs_1.existsSync)((0, node_path_1.join)(root, 'package.json')))
35
+ return root;
36
+ if ((0, node_path_1.dirname)(dir) === dir)
37
+ break;
38
+ }
39
+ return undefined;
40
+ }
41
+ function packageFile(id, roots, from) {
42
+ const [scope, name, ...rest] = id.split('/');
43
+ const pkgName = scope?.startsWith('@') ? `${scope}/${name}` : scope;
44
+ const subpath = scope?.startsWith('@') ? rest : [name, ...rest].filter(Boolean);
45
+ const root = roots[pkgName] ?? packageRoot(pkgName, from);
46
+ if (root === undefined)
47
+ return undefined;
48
+ const pkg = JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)(root, 'package.json'), 'utf8'));
49
+ const key = subpath.length ? `./${subpath.join('/')}` : '.';
50
+ const entry = pkg.exports?.[key];
51
+ const style = typeof entry === 'string' ? entry : (entry?.style ?? entry?.default ?? entry?.import);
52
+ if (typeof style === 'string')
53
+ return (0, node_path_1.join)(root, style);
54
+ if (!subpath.length && typeof pkg.style === 'string')
55
+ return (0, node_path_1.join)(root, pkg.style);
56
+ const direct = subpath.length ? (0, node_path_1.join)(root, ...subpath) : (0, node_path_1.join)(root, 'index.css');
57
+ for (const file of [direct, `${direct}.css`])
58
+ if ((0, node_fs_1.existsSync)(file) && (0, node_fs_1.statSync)(file).isFile())
59
+ return file;
60
+ return undefined;
61
+ }
62
+ function resolveJs(id, base, from) {
63
+ if (id.startsWith('.') || (0, node_path_1.isAbsolute)(id))
64
+ return (0, node_path_1.resolve)(base, id);
65
+ for (const dir of from)
66
+ try {
67
+ return (0, node_module_1.createRequire)((0, node_path_1.join)(dir, 'noop.js')).resolve(id);
68
+ }
69
+ catch { }
70
+ return undefined;
71
+ }
72
+ /**
73
+ * Compiles `css` (an app's entry, read from `from`) as Tailwind does in the
74
+ * app's build; its packages are read from `roots`, then above `from`, then
75
+ * above `packages` (default: this package).
76
+ */
77
+ async function tailwind(css, from, roots = {}, packages = __dirname) {
78
+ const search = [(0, node_path_1.dirname)(from), packages];
79
+ // `@source` globs scan the app's files; the candidates are given, so they are not read.
80
+ const entry = css.replace(/^@source\s[^;]*;/gm, '');
81
+ const compiler = await (0, node_1.compile)(entry, {
82
+ base: (0, node_path_1.dirname)(from),
83
+ from,
84
+ onDependency: () => { },
85
+ customCssResolver: async (id) => id.startsWith('.') || (0, node_path_1.isAbsolute)(id) ? undefined : packageFile(id, roots, search),
86
+ customJsResolver: async (id, base) => resolveJs(id, base, search),
87
+ });
88
+ return { build: (classes) => compiler.build([...classes]) };
89
+ }
90
+ /**
91
+ * The class candidates in a package's built JavaScript, as an app's `@source`
92
+ * glob over it (`node_modules/@oxy.so/bloom/lib/**\/*.{js,jsx}`) hands them to
93
+ * Tailwind: every word of each file, split at whitespace, quotes and template
94
+ * holes. Tailwind writes CSS only for the words that are utilities, so a word
95
+ * that is not one adds nothing.
96
+ */
97
+ function packageCandidates(dir) {
98
+ const words = new Set();
99
+ const walk = (at) => {
100
+ for (const name of (0, node_fs_1.readdirSync)(at)) {
101
+ const path = (0, node_path_1.join)(at, name);
102
+ if ((0, node_fs_1.statSync)(path).isDirectory())
103
+ walk(path);
104
+ else if (/\.jsx?$/.test(name))
105
+ for (const word of (0, node_fs_1.readFileSync)(path, 'utf8').split(/[\s"'`]+|\$\{|\}/))
106
+ if (word)
107
+ words.add(word);
108
+ }
109
+ };
110
+ walk(dir);
111
+ return [...words].sort();
112
+ }
@@ -0,0 +1,59 @@
1
+ import { type Reason } from './resolve';
2
+ import { type Table } from './table';
3
+ /** Where an element's component comes from: a module and the name it exports. */
4
+ export interface Source {
5
+ readonly module: string;
6
+ readonly imported: string;
7
+ }
8
+ export interface Unconverted {
9
+ readonly line: number;
10
+ readonly element: string;
11
+ readonly reasons: readonly Reason[];
12
+ }
13
+ export interface Fixture {
14
+ readonly name: string;
15
+ /** The region as written, every carried value a hole. */
16
+ readonly before: string;
17
+ /** The chain the codemod wrote, with the same holes. */
18
+ readonly after: string;
19
+ /** Local names the two sides use, and where each comes from. */
20
+ readonly bindings: Readonly<Record<string, Source>>;
21
+ readonly language: readonly string[];
22
+ readonly classes: readonly string[];
23
+ /** Bloom components (`module#export`) both sides are drawn inside, as the region's nodes are only drawn in them. */
24
+ readonly within: readonly string[];
25
+ /** The before side reads Bloom's theme (`parityTheme`, its `useTheme()`): a fill the chain names by role. */
26
+ readonly themed: boolean;
27
+ /** The after side is a route's body root, drawn by the language's `ScreenHost` as `screen()` draws it. */
28
+ readonly screen: boolean;
29
+ /** The app colours the chain paints with: the table's local name and each token's colour. */
30
+ readonly appColors: {
31
+ readonly table: string;
32
+ readonly color: Readonly<Record<string, string>>;
33
+ } | null;
34
+ }
35
+ export interface FileResult {
36
+ readonly code: string;
37
+ readonly changed: boolean;
38
+ /** Elements the codemod considered: View, Pressable, Bloom Text, Bloom icons. */
39
+ readonly candidates: number;
40
+ readonly converted: number;
41
+ readonly regions: number;
42
+ readonly unconverted: readonly Unconverted[];
43
+ readonly fixtures: readonly Fixture[];
44
+ /** Every class written on a considered element. */
45
+ readonly classes: readonly string[];
46
+ /** Bloom components left as JSX (`module#export`), with how often: the next nodes to map. */
47
+ readonly bloomComponents: Readonly<Record<string, number>>;
48
+ }
49
+ export interface TransformOptions {
50
+ readonly table: Table;
51
+ readonly overridden?: ReadonlySet<string>;
52
+ /** Write a fixture per region, for the parity proof. */
53
+ readonly fixtures?: boolean;
54
+ readonly name?: string;
55
+ }
56
+ /** React's JSX text rule: lines trimmed and joined, an empty result is no child. */
57
+ export declare function cleanJsxText(value: string): string;
58
+ /** Transforms one file's source. */
59
+ export declare function transformSource(source: string, options: TransformOptions): FileResult;