@octanejs/codemod 0.1.3 → 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/README.md +305 -5
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +162 -0
- package/dist/compiler.d.ts +11 -0
- package/dist/compiler.js +48 -0
- package/dist/effect-receivers.d.ts +11 -0
- package/dist/effect-receivers.js +315 -0
- package/dist/expression-wrappers.d.ts +6 -0
- package/dist/expression-wrappers.js +16 -0
- package/dist/forwarded-props.d.ts +27 -0
- package/dist/forwarded-props.js +489 -0
- package/dist/index.d.ts +6 -0
- package/{src → dist}/index.js +1 -0
- package/dist/linked-state.d.ts +17 -0
- package/dist/linked-state.js +126 -0
- package/dist/local-calls.d.ts +20 -0
- package/dist/local-calls.js +143 -0
- package/dist/local-react-hook.d.ts +15 -0
- package/dist/local-react-hook.js +353 -0
- package/dist/local-uses.d.ts +13 -0
- package/dist/local-uses.js +987 -0
- package/dist/mappings.d.ts +57 -0
- package/dist/mappings.js +343 -0
- package/dist/path-svg.d.ts +60 -0
- package/dist/path-svg.js +207 -0
- package/dist/project.d.ts +9 -0
- package/dist/project.js +1568 -0
- package/dist/react-intl.d.ts +4 -0
- package/dist/react-intl.js +389 -0
- package/dist/react-renderer.d.ts +9 -0
- package/dist/react-renderer.js +106 -0
- package/dist/scoped-signal.d.ts +4 -0
- package/dist/scoped-signal.js +480 -0
- package/dist/scoped-store-contract.d.ts +13 -0
- package/dist/scoped-store-contract.js +741 -0
- package/dist/store-hook-uses.d.ts +18 -0
- package/dist/store-hook-uses.js +403 -0
- package/dist/store-inputs.d.ts +16 -0
- package/dist/store-inputs.js +391 -0
- package/dist/strong.d.ts +24 -0
- package/dist/strong.js +142 -0
- package/dist/transform.d.ts +27 -0
- package/dist/transform.js +4890 -0
- package/dist/types.d.ts +203 -0
- package/dist/types.js +1 -0
- package/package.json +19 -6
- package/src/path-svg.ts +218 -0
- package/templates/octane-path-svg.ts +56 -0
- package/templates/octane-store-context.tsrx +49 -0
- package/templates/octane-store-hooks.ts +61 -0
- package/templates/react-intl.tsrx +116 -0
- package/src/bin.js +0 -151
- package/src/compiler.js +0 -61
- package/src/index.d.ts +0 -95
- package/src/mappings.js +0 -429
- package/src/project.js +0 -670
- package/src/strong.js +0 -149
- package/src/transform.js +0 -3425
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import type ts from 'typescript';
|
|
2
|
+
export interface Note {
|
|
3
|
+
rule: string;
|
|
4
|
+
severity: 'info' | 'warning' | 'error';
|
|
5
|
+
line: number;
|
|
6
|
+
message: string;
|
|
7
|
+
}
|
|
8
|
+
export interface TransformOptions {
|
|
9
|
+
/** Try receiver captures for a Strong effect with an explicit object dependency. */
|
|
10
|
+
captureEffectReceivers?: boolean;
|
|
11
|
+
/** Retry empty effect dependency lists with their source-verified ref helpers. */
|
|
12
|
+
captureEffectHelpers?: boolean;
|
|
13
|
+
/** Also preserve eligible property triggers at the original effect statement. */
|
|
14
|
+
snapshotEffectProperties?: boolean;
|
|
15
|
+
captureEffectSnapshots?: boolean;
|
|
16
|
+
/** Retry a render-time state update with an equivalent linked boolean. */
|
|
17
|
+
linkRenderState?: boolean;
|
|
18
|
+
/** `tsrx` (default) rewrites component bodies as templates; `tsx` keeps JSX and only ports the React APIs. */
|
|
19
|
+
target?: 'tsx' | 'tsrx';
|
|
20
|
+
/** Emit Strong-mode source: add `"use strong"` and leave omitted dependency lists to the compiler. */
|
|
21
|
+
strong?: boolean;
|
|
22
|
+
/** The project checks with `strictNullChecks`, so TypeScript types can prove text holes and boolean guards. */
|
|
23
|
+
strictNullChecks?: boolean;
|
|
24
|
+
/** This module in a program over the whole project, whose resolved imports type what the module's own program cannot. */
|
|
25
|
+
types?: {
|
|
26
|
+
sourceFile: ts.SourceFile;
|
|
27
|
+
checker: ts.TypeChecker;
|
|
28
|
+
};
|
|
29
|
+
/** The new spelling of an import specifier (a renamed module, a binding package), or null to keep it. */
|
|
30
|
+
resolveSpecifier?(specifier: string): string | null;
|
|
31
|
+
/** The issuer resolves the original installed React runtime and types. */
|
|
32
|
+
originalReact?: boolean;
|
|
33
|
+
/** A dispatcher hook directly called by an unchanged local TypeScript export. */
|
|
34
|
+
localReactHook?(module: string, name: string, nativeOnly: boolean): string | {
|
|
35
|
+
from: string;
|
|
36
|
+
} | undefined;
|
|
37
|
+
/** Resolve a source-backed workspace hook after verifying its implementation and its call sites. */
|
|
38
|
+
resolveStoreHook?(module: string, name: string, safeUses: (sharedInput: (module: string, name: string) => boolean | 'opaque', unchangedHelper?: (module: string, name: string) => boolean) => boolean): {
|
|
39
|
+
from: string;
|
|
40
|
+
name: string;
|
|
41
|
+
} | null;
|
|
42
|
+
/** Relative path to the project-local React Intl adapter, when its installed peers support it. */
|
|
43
|
+
reactIntlAdapter?: string;
|
|
44
|
+
/** Whether a directly imported, selected component forwards this prop only to text attributes. */
|
|
45
|
+
forwardedText?(module: string, exported: string, prop: string, ordinaryCaller?: boolean, through?: boolean): boolean;
|
|
46
|
+
/** Whether the selected component at this local declaration offset, or this imported name, accepts one explicit element. */
|
|
47
|
+
elementChild?(binding: number | {
|
|
48
|
+
module: string;
|
|
49
|
+
imported: string;
|
|
50
|
+
}): boolean;
|
|
51
|
+
/** Whether a selected, directly imported component inspects its child descriptors. */
|
|
52
|
+
descriptorChild?(binding: {
|
|
53
|
+
module: string;
|
|
54
|
+
imported: string;
|
|
55
|
+
}): boolean;
|
|
56
|
+
/** Whether an imported name is a context, or null when it cannot be known. */
|
|
57
|
+
isContext?(specifier: string, name: string): boolean | null;
|
|
58
|
+
/** The React package an import specifier names when that package has no Octane port, or null. */
|
|
59
|
+
unported?(specifier: string): string | null;
|
|
60
|
+
/** The project has React and React DOM 19.2 or newer in the React 19 series, so `ReactCompat` can host an unported package's components. */
|
|
61
|
+
reactCompat?: boolean;
|
|
62
|
+
/** Both installed Octane entry points support inline React islands. */
|
|
63
|
+
reactCompatInline?: boolean;
|
|
64
|
+
/** The selected imported component renders every use of this prop directly in native output. */
|
|
65
|
+
forwardedRender?(module: string, exported: string, prop: string, depth: number): boolean;
|
|
66
|
+
/** The file imports the installed clsx package through its original entry point. */
|
|
67
|
+
installedClsx?: boolean;
|
|
68
|
+
/** The physical CSS Modules stylesheet for a plain relative request, if present. */
|
|
69
|
+
cssModuleFile?(specifier: string): string | undefined;
|
|
70
|
+
/** The generated native SVG helper, if the project verified this raw asset. */
|
|
71
|
+
pathSvgAdapter?(specifier: string): string | null;
|
|
72
|
+
/** With pathSvgAdapter, supplies a native SVG adapter with the requested fixed root color. */
|
|
73
|
+
fixedColorSvgAdapter?(specifier: string, color: string): string | null;
|
|
74
|
+
/** This file uses React's automatic JSX runtime and the component resolves to its original React package. */
|
|
75
|
+
reactStoreComponent?(specifier: string): boolean;
|
|
76
|
+
}
|
|
77
|
+
/** A React package with no Octane port, and how migrated code uses it. */
|
|
78
|
+
export interface Unported {
|
|
79
|
+
name: string;
|
|
80
|
+
/** Elements now rendered in a `ReactCompat` island. */
|
|
81
|
+
islands: number;
|
|
82
|
+
/** Uses that only a port can support. */
|
|
83
|
+
blocked: number;
|
|
84
|
+
}
|
|
85
|
+
export interface TransformResult {
|
|
86
|
+
code: string;
|
|
87
|
+
changed: boolean;
|
|
88
|
+
/** Whether the module becomes `.tsrx`: it holds a template, keeps JSX, or was a JSX module, so no `.tsx` module is left outside Strong mode's reach. */
|
|
89
|
+
template: boolean;
|
|
90
|
+
notes: Note[];
|
|
91
|
+
/** How often each rule fired. */
|
|
92
|
+
rules: Record<string, number>;
|
|
93
|
+
/** What the module exports that other modules' rewrites depend on. */
|
|
94
|
+
exports: {
|
|
95
|
+
elementChildren?: {
|
|
96
|
+
start: number;
|
|
97
|
+
exported?: string;
|
|
98
|
+
}[];
|
|
99
|
+
descriptorChildren?: string[];
|
|
100
|
+
contexts: string[];
|
|
101
|
+
reexports: {
|
|
102
|
+
specifier: string;
|
|
103
|
+
names: [string, string][] | null;
|
|
104
|
+
}[];
|
|
105
|
+
};
|
|
106
|
+
unported: Unported[];
|
|
107
|
+
}
|
|
108
|
+
export interface CodemodOptions {
|
|
109
|
+
/** Bare SVG components render the authored geometry and ?raw loads the same asset as text. */
|
|
110
|
+
svgPaths?: boolean;
|
|
111
|
+
target?: 'tsrx' | 'tsx';
|
|
112
|
+
/** Migrate to Strong mode where the compiler accepts it, falling back to compatibility mode per module. */
|
|
113
|
+
strong?: boolean;
|
|
114
|
+
/** Keep `"use strong"` even on modules with Strong errors left. */
|
|
115
|
+
forceStrong?: boolean;
|
|
116
|
+
/** Compile every migrated module (default true). */
|
|
117
|
+
verify?: boolean;
|
|
118
|
+
/** Resolve types through the project graph (default true). False uses only module-local type information. */
|
|
119
|
+
projectTypes?: boolean;
|
|
120
|
+
/** Write the result to disk (default true). */
|
|
121
|
+
write?: boolean;
|
|
122
|
+
/** The project root, for resolving its compiler. */
|
|
123
|
+
root?: string;
|
|
124
|
+
}
|
|
125
|
+
export interface Finding {
|
|
126
|
+
code: string;
|
|
127
|
+
severity: string;
|
|
128
|
+
message: string;
|
|
129
|
+
line: number;
|
|
130
|
+
}
|
|
131
|
+
export interface Compiler {
|
|
132
|
+
/** Where the compiler was loaded. */
|
|
133
|
+
from: string;
|
|
134
|
+
collectDiagnostics: typeof import('octane/compiler').collectDiagnostics;
|
|
135
|
+
}
|
|
136
|
+
export interface FileResult {
|
|
137
|
+
/** The module's path, relative to the root. */
|
|
138
|
+
file: string;
|
|
139
|
+
/** Where it is written, relative to the root. */
|
|
140
|
+
output: string;
|
|
141
|
+
changed: boolean;
|
|
142
|
+
rules: Record<string, number>;
|
|
143
|
+
/** The codemod's own findings, at the original file's lines. */
|
|
144
|
+
notes: Note[];
|
|
145
|
+
/** Whether it is Strong, or null when Strong was not requested or does not apply. */
|
|
146
|
+
strong: boolean | null;
|
|
147
|
+
strongFixes: string[];
|
|
148
|
+
/** What the compiler reported for the migrated module. */
|
|
149
|
+
diagnostics: Finding[];
|
|
150
|
+
code: string;
|
|
151
|
+
}
|
|
152
|
+
export interface CodemodResult {
|
|
153
|
+
root: string;
|
|
154
|
+
files: FileResult[];
|
|
155
|
+
/** React packages the migrated code now imports as Octane bindings. */
|
|
156
|
+
packages: {
|
|
157
|
+
from: string;
|
|
158
|
+
to: string;
|
|
159
|
+
}[];
|
|
160
|
+
/** Bindings added to package.json, which the project did not declare. */
|
|
161
|
+
dependencies: {
|
|
162
|
+
name: string;
|
|
163
|
+
range: string;
|
|
164
|
+
}[];
|
|
165
|
+
/** Patches of replaced React packages, which do not reach their bindings. */
|
|
166
|
+
patched: {
|
|
167
|
+
name: string;
|
|
168
|
+
patch: string;
|
|
169
|
+
}[];
|
|
170
|
+
/** React packages the code imports that have no Octane port, and how it uses them. */
|
|
171
|
+
missing: (Unported & {
|
|
172
|
+
modules: number;
|
|
173
|
+
})[];
|
|
174
|
+
/** Entry pages whose script reference was updated. */
|
|
175
|
+
html: string[];
|
|
176
|
+
/** Where the verifying compiler came from. */
|
|
177
|
+
compiler: string | null;
|
|
178
|
+
}
|
|
179
|
+
export interface StrongResult {
|
|
180
|
+
/** The module after every fix the compiler allowed. */
|
|
181
|
+
code: string;
|
|
182
|
+
/** Whether it kept `"use strong"`. */
|
|
183
|
+
strong: boolean;
|
|
184
|
+
/** The diagnostic codes a fix resolved. */
|
|
185
|
+
fixed: string[];
|
|
186
|
+
/** The Strong errors left, when it did not. */
|
|
187
|
+
blockers: Finding[];
|
|
188
|
+
}
|
|
189
|
+
/** A named import used by the scoped signal source or its destination adapter. */
|
|
190
|
+
export interface ScopedSignalBinding {
|
|
191
|
+
module: string;
|
|
192
|
+
name: string;
|
|
193
|
+
}
|
|
194
|
+
export interface ScopedSignalOptions {
|
|
195
|
+
sourceRoot: string;
|
|
196
|
+
tsconfig: string;
|
|
197
|
+
sourceModule: string;
|
|
198
|
+
scope: ScopedSignalBinding;
|
|
199
|
+
factory: ScopedSignalBinding;
|
|
200
|
+
reader: ScopedSignalBinding;
|
|
201
|
+
/** Source-root-relative paths and reviewed SHA-256 hashes of imperative consumers. */
|
|
202
|
+
reviewedAccess?: Record<string, string>;
|
|
203
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@octanejs/codemod",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.22.2"
|
|
@@ -27,27 +27,40 @@
|
|
|
27
27
|
"tsrx"
|
|
28
28
|
],
|
|
29
29
|
"files": [
|
|
30
|
-
"
|
|
30
|
+
"dist",
|
|
31
|
+
"templates",
|
|
32
|
+
"src/path-svg.ts",
|
|
31
33
|
"README.md",
|
|
32
34
|
"LICENSE"
|
|
33
35
|
],
|
|
34
36
|
"bin": {
|
|
35
|
-
"octane-codemod": "
|
|
37
|
+
"octane-codemod": "dist/bin.js"
|
|
36
38
|
},
|
|
37
39
|
"exports": {
|
|
38
|
-
".":
|
|
40
|
+
".": {
|
|
41
|
+
"types": "./dist/index.d.ts",
|
|
42
|
+
"import": "./dist/index.js",
|
|
43
|
+
"default": "./dist/index.js"
|
|
44
|
+
},
|
|
39
45
|
"./package.json": "./package.json"
|
|
40
46
|
},
|
|
41
47
|
"dependencies": {
|
|
42
|
-
"@octanejs/cli": "0.3.
|
|
43
|
-
"
|
|
48
|
+
"@octanejs/cli": "0.3.4",
|
|
49
|
+
"@tsrx/oxc": "0.23.0",
|
|
50
|
+
"octane": "0.17.0",
|
|
44
51
|
"picocolors": "^1.1.1",
|
|
45
52
|
"typescript": "^5.9.3"
|
|
46
53
|
},
|
|
47
54
|
"devDependencies": {
|
|
55
|
+
"clsx": "^2.1.1",
|
|
56
|
+
"react": "19.2.7",
|
|
57
|
+
"react-dom": "19.2.7",
|
|
58
|
+
"react-intl": "7.1.14",
|
|
48
59
|
"vitest": "^4.1.10"
|
|
49
60
|
},
|
|
50
61
|
"scripts": {
|
|
62
|
+
"build": "tsc -p tsconfig.json",
|
|
63
|
+
"typecheck": "octane-tsc -p tsconfig.typecheck.json",
|
|
51
64
|
"test": "cd ../.. && vitest run --project codemod"
|
|
52
65
|
}
|
|
53
66
|
}
|
package/src/path-svg.ts
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
const space = /^[ \t\r\n]*$/;
|
|
2
|
+
const number = '[+-]?(?:[0-9]+(?:\\.[0-9]*)?|\\.[0-9]+)(?:[eE][+-]?[0-9]+)?';
|
|
3
|
+
const length = new RegExp('^' + number + '(?:px|em|rem|%)?$');
|
|
4
|
+
const extent = new RegExp('^(?!-)' + number + '(?:px|em|rem|%)?$');
|
|
5
|
+
const separator = '(?: *, *| +)';
|
|
6
|
+
const paint =
|
|
7
|
+
/^(?:[a-zA-Z]+|#[\da-fA-F]{3,4}|#[\da-fA-F]{6}(?:[\da-fA-F]{2})?|var\(--[A-Za-z_][A-Za-z0-9_-]*\))$/;
|
|
8
|
+
const opacity = /^(?:0|0?\.\d+|1)$/;
|
|
9
|
+
const nextNumber = '(?:' + separator + number + ')';
|
|
10
|
+
const operation =
|
|
11
|
+
'(?:(?:scale|translate)\\(' +
|
|
12
|
+
number +
|
|
13
|
+
nextNumber +
|
|
14
|
+
'?\\)|matrix\\(' +
|
|
15
|
+
number +
|
|
16
|
+
nextNumber +
|
|
17
|
+
'{5}\\))';
|
|
18
|
+
const transform = new RegExp('^' + operation + '(?:' + separator + operation + ')*$');
|
|
19
|
+
const transformNumbers = new RegExp(number, 'g');
|
|
20
|
+
const localPaint = /^url\(#[A-Za-z_][A-Za-z0-9_-]*\)$/;
|
|
21
|
+
const tileSize = /^[1-9]\d*$/;
|
|
22
|
+
const rootAttributes = {
|
|
23
|
+
preserveAspectRatio: /^none$/,
|
|
24
|
+
xmlns: /^http:\/\/www\.w3\.org\/2000\/svg$/,
|
|
25
|
+
width: length,
|
|
26
|
+
height: length,
|
|
27
|
+
viewBox: new RegExp('^' + number + '(?:' + separator + number + '){3}$'),
|
|
28
|
+
fill: paint,
|
|
29
|
+
role: /^img$/,
|
|
30
|
+
focusable: /^(?:true|false)$/,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export function svgLiteralRootOverride(name: string, value: string) {
|
|
34
|
+
return (name === 'fill' || name === 'focusable') && rootAttributes[name].test(value);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const fillRule = /^(?:evenodd|nonzero)$/;
|
|
38
|
+
const presentation = {
|
|
39
|
+
fill: paint,
|
|
40
|
+
stroke: paint,
|
|
41
|
+
'stroke-width': length,
|
|
42
|
+
'stroke-linecap': /^(?:butt|round|square)$/,
|
|
43
|
+
'stroke-linejoin': /^(?:miter|round|bevel)$/,
|
|
44
|
+
opacity,
|
|
45
|
+
};
|
|
46
|
+
const pathAttributes = {
|
|
47
|
+
...presentation,
|
|
48
|
+
transform,
|
|
49
|
+
d: /^[Mm] *[+\-.\d][MmLlHhVvCcSsQqTtAaZzEe0-9.,+\- ]+$/,
|
|
50
|
+
'fill-rule': fillRule,
|
|
51
|
+
'clip-rule': fillRule,
|
|
52
|
+
'stroke-opacity': opacity,
|
|
53
|
+
};
|
|
54
|
+
const rectAttributes = {
|
|
55
|
+
...presentation,
|
|
56
|
+
fill: new RegExp(paint.source + '|' + localPaint.source),
|
|
57
|
+
x: length,
|
|
58
|
+
y: length,
|
|
59
|
+
width: extent,
|
|
60
|
+
height: extent,
|
|
61
|
+
rx: extent,
|
|
62
|
+
ry: extent,
|
|
63
|
+
};
|
|
64
|
+
const shapeAttributes = {
|
|
65
|
+
path: pathAttributes,
|
|
66
|
+
rect: rectAttributes,
|
|
67
|
+
g: { transform, 'clip-path': /^url\(#[A-Za-z_][A-Za-z0-9_-]*\)$/ },
|
|
68
|
+
defs: {},
|
|
69
|
+
clipPath: { id: /^[A-Za-z_][A-Za-z0-9_-]*$/ },
|
|
70
|
+
pattern: {
|
|
71
|
+
id: /^[A-Za-z_][A-Za-z0-9_-]*$/,
|
|
72
|
+
width: tileSize,
|
|
73
|
+
height: tileSize,
|
|
74
|
+
patternUnits: /^userSpaceOnUse$/,
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
function attributes(text: string, allowed: Record<string, RegExp>) {
|
|
79
|
+
const values: Record<string, string> = {};
|
|
80
|
+
const field =
|
|
81
|
+
/[ \t\r\n]+([A-Za-z][A-Za-z-]*)[ \t\r\n]*=[ \t\r\n]*(?:"([^"&<>\t\r\n]*)"|'([^'&<>\t\r\n]*)')/gy;
|
|
82
|
+
let end = 0;
|
|
83
|
+
for (let match; (match = field.exec(text)); end = field.lastIndex) {
|
|
84
|
+
const name = match[1]!;
|
|
85
|
+
let value = (match[2] ?? match[3])!;
|
|
86
|
+
if (name in values || !Object.prototype.hasOwnProperty.call(allowed, name)) return null;
|
|
87
|
+
const rule = allowed[name]!;
|
|
88
|
+
if (!rule.test(value)) return null;
|
|
89
|
+
// SVGR emits a numeric literal for a leading-dot opacity.
|
|
90
|
+
if (rule === opacity && value.startsWith('.')) value = '0' + value;
|
|
91
|
+
// SVG component loaders can emit a numeric literal for unitless lengths.
|
|
92
|
+
if (rule === length || rule === extent || rule === opacity || rule === tileSize) {
|
|
93
|
+
const n = Number(value);
|
|
94
|
+
if (
|
|
95
|
+
(!Number.isNaN(n) && String(n) !== value) ||
|
|
96
|
+
(rule === extent && !Number.isFinite(parseFloat(value)))
|
|
97
|
+
)
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
if (
|
|
101
|
+
rule === transform &&
|
|
102
|
+
value.match(transformNumbers)!.some((n) => !Number.isFinite(Number(n)))
|
|
103
|
+
)
|
|
104
|
+
return null;
|
|
105
|
+
values[name] = value;
|
|
106
|
+
}
|
|
107
|
+
return space.test(text.slice(end)) ? values : null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Hydration and React interop use reserved data attributes.
|
|
111
|
+
export function svgDataAttribute(name: string): name is `data-${string}` {
|
|
112
|
+
return /^data-(?!oct(?:ane)?(?:-|$)|react-compat$)[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/.test(name);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
type StaticSvgNode = {
|
|
116
|
+
tag: keyof typeof shapeAttributes;
|
|
117
|
+
attributes: Record<string, string>;
|
|
118
|
+
children: StaticSvgNode[];
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/** Read authored geometry with a single local clip or paint definition. */
|
|
122
|
+
export function parseStaticSvg(source: string) {
|
|
123
|
+
if (typeof source !== 'string') return null;
|
|
124
|
+
let root: Record<string, string> | null = null;
|
|
125
|
+
const children: StaticSvgNode[] = [];
|
|
126
|
+
const open: { tag: 'svg' | StaticSvgNode['tag']; children: StaticSvgNode[] }[] = [];
|
|
127
|
+
let clipId: string | undefined;
|
|
128
|
+
let reference: string | undefined;
|
|
129
|
+
let patternId: string | undefined;
|
|
130
|
+
let paintId: string | undefined;
|
|
131
|
+
const token =
|
|
132
|
+
/[ \t\r\n]*(?:<!--([\s\S]*?)-->|<\/(svg|g|defs|clipPath|pattern)[ \t\r\n]*>|<(svg|g|rect|path|defs|clipPath|pattern)([^<>]*?)(\/?)>)/gy;
|
|
133
|
+
let hasShape = false;
|
|
134
|
+
let end = 0;
|
|
135
|
+
for (let match; (match = token.exec(source)); end = token.lastIndex) {
|
|
136
|
+
const [, comment, closing, tag, fields, selfClosing] = match;
|
|
137
|
+
const parent = open[open.length - 1];
|
|
138
|
+
if (comment !== undefined) {
|
|
139
|
+
if (/--|-$|[^\t\r\n\u0020-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/u.test(comment))
|
|
140
|
+
return null;
|
|
141
|
+
} else if (closing) {
|
|
142
|
+
if (!parent || closing !== parent.tag) return null;
|
|
143
|
+
if (
|
|
144
|
+
(closing === 'defs' || closing === 'clipPath' || closing === 'pattern') &&
|
|
145
|
+
parent.children.length !== 1
|
|
146
|
+
)
|
|
147
|
+
return null;
|
|
148
|
+
open.pop();
|
|
149
|
+
} else if (tag === 'svg') {
|
|
150
|
+
if (root || selfClosing) return null;
|
|
151
|
+
root = attributes(fields!, rootAttributes);
|
|
152
|
+
if (!root) return null;
|
|
153
|
+
open.push({ tag, children });
|
|
154
|
+
} else {
|
|
155
|
+
if (!parent) return null;
|
|
156
|
+
const name = tag as StaticSvgNode['tag'];
|
|
157
|
+
if (
|
|
158
|
+
(name === 'defs' && parent.tag !== 'svg') ||
|
|
159
|
+
(name === 'clipPath' || name === 'pattern') !== (parent.tag === 'defs') ||
|
|
160
|
+
(parent.tag === 'clipPath' && name !== 'rect') ||
|
|
161
|
+
(parent.tag === 'pattern' && name !== 'path')
|
|
162
|
+
)
|
|
163
|
+
return null;
|
|
164
|
+
const container = name !== 'path' && name !== 'rect';
|
|
165
|
+
if (container ? selfClosing || open.length > 32 : !selfClosing) return null;
|
|
166
|
+
const props = attributes(fields!, shapeAttributes[name]);
|
|
167
|
+
if (
|
|
168
|
+
!props ||
|
|
169
|
+
(tag === 'path' && !props.d) ||
|
|
170
|
+
(tag === 'rect' && !(props.width && props.height))
|
|
171
|
+
)
|
|
172
|
+
return null;
|
|
173
|
+
if (name === 'clipPath') {
|
|
174
|
+
if (!props.id || clipId !== undefined || patternId !== undefined) return null;
|
|
175
|
+
clipId = props.id;
|
|
176
|
+
}
|
|
177
|
+
if (props['clip-path']) {
|
|
178
|
+
const id = props['clip-path'].slice(5, -1);
|
|
179
|
+
if (parent.tag !== 'svg' || (reference !== undefined && reference !== id)) return null;
|
|
180
|
+
reference = id;
|
|
181
|
+
}
|
|
182
|
+
if (name === 'pattern') {
|
|
183
|
+
if (
|
|
184
|
+
patternId !== undefined ||
|
|
185
|
+
clipId !== undefined ||
|
|
186
|
+
!(props.id && props.width && props.height && props.patternUnits)
|
|
187
|
+
)
|
|
188
|
+
return null;
|
|
189
|
+
patternId = props.id;
|
|
190
|
+
}
|
|
191
|
+
if (name === 'rect' && props.fill?.startsWith('url(')) {
|
|
192
|
+
const id = props.fill.slice(5, -1);
|
|
193
|
+
if (parent.tag !== 'svg' || (paintId !== undefined && paintId !== id)) return null;
|
|
194
|
+
paintId = id;
|
|
195
|
+
}
|
|
196
|
+
const node: StaticSvgNode = { tag: name, attributes: props, children: [] };
|
|
197
|
+
parent.children.push(node);
|
|
198
|
+
if (container) open.push(node);
|
|
199
|
+
else if (parent.tag !== 'clipPath' && parent.tag !== 'pattern') hasShape = true;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return root &&
|
|
203
|
+
hasShape &&
|
|
204
|
+
!open.length &&
|
|
205
|
+
clipId === reference &&
|
|
206
|
+
patternId === paintId &&
|
|
207
|
+
space.test(source.slice(end))
|
|
208
|
+
? { root, children }
|
|
209
|
+
: null;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Existing generated helpers expose this path-only result to their importers.
|
|
213
|
+
export function parsePathSvg(source: string) {
|
|
214
|
+
const svg = parseStaticSvg(source);
|
|
215
|
+
return svg && svg.children.every((node) => node.tag === 'path')
|
|
216
|
+
? { root: svg.root, paths: svg.children.map((node) => node.attributes) }
|
|
217
|
+
: null;
|
|
218
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { createElement, type OctaneNode } from 'octane';
|
|
2
|
+
|
|
3
|
+
function reactClassName(value: unknown) {
|
|
4
|
+
if (
|
|
5
|
+
value == null ||
|
|
6
|
+
typeof value === 'boolean' ||
|
|
7
|
+
typeof value === 'symbol' ||
|
|
8
|
+
typeof value === 'function'
|
|
9
|
+
)
|
|
10
|
+
return undefined;
|
|
11
|
+
return '' + value;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Prepare static props once and create fresh elements for each mounted icon.
|
|
15
|
+
function prepareShape(node: StaticSvgNode, key: number): () => OctaneNode {
|
|
16
|
+
const props = { ...node.attributes, key };
|
|
17
|
+
if (node.tag === 'path' || node.tag === 'rect') return () => createElement(node.tag, props);
|
|
18
|
+
const children = node.children.map(prepareShape);
|
|
19
|
+
return () => createElement(node.tag, { ...props, children: children.map((render) => render()) });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function pathSvg(source: string, fixedStyle?: string) {
|
|
23
|
+
const geometry = parseStaticSvg(source);
|
|
24
|
+
if (!geometry) throw new Error('Expected a raw SVG containing only supported static geometry.');
|
|
25
|
+
const root = fixedStyle === undefined ? geometry.root : { ...geometry.root, style: fixedStyle };
|
|
26
|
+
const children = geometry.children.map(prepareShape);
|
|
27
|
+
return function PathSvg(props: {
|
|
28
|
+
className?: unknown;
|
|
29
|
+
'aria-hidden'?: string | boolean;
|
|
30
|
+
fill?: string;
|
|
31
|
+
focusable?: string;
|
|
32
|
+
[name: `data-${string}`]: string | undefined;
|
|
33
|
+
style?: { transform: string };
|
|
34
|
+
}) {
|
|
35
|
+
const attributes: Record<string, unknown> = {
|
|
36
|
+
...root,
|
|
37
|
+
className: reactClassName(props.className),
|
|
38
|
+
'aria-hidden': props['aria-hidden'],
|
|
39
|
+
children: children.map((render) => render()),
|
|
40
|
+
};
|
|
41
|
+
for (const name in props) {
|
|
42
|
+
if (
|
|
43
|
+
(name === 'fill' || name === 'focusable' || svgDataAttribute(name)) &&
|
|
44
|
+
Object.prototype.hasOwnProperty.call(props, name)
|
|
45
|
+
) {
|
|
46
|
+
const value = props[name];
|
|
47
|
+
if (typeof value === 'string') attributes[name] = value;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (Object.prototype.hasOwnProperty.call(props, 'style')) {
|
|
51
|
+
const transform = props.style?.transform;
|
|
52
|
+
if (typeof transform === 'string') attributes.style = { __proto__: null, transform };
|
|
53
|
+
}
|
|
54
|
+
return createElement('svg', attributes);
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strong';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createContext,
|
|
5
|
+
useContext,
|
|
6
|
+
useMemo,
|
|
7
|
+
useCallback,
|
|
8
|
+
useRef,
|
|
9
|
+
useLayoutEffect,
|
|
10
|
+
useSyncExternalStore,
|
|
11
|
+
type Context,
|
|
12
|
+
type OctaneNode,
|
|
13
|
+
} from 'octane';
|
|
14
|
+
import type { ChildrenBlock } from 'octane/tsrx-boundary';
|
|
15
|
+
import type { Context as ReactContext } from 'react';
|
|
16
|
+
|
|
17
|
+
// The consuming compiler selects the client or server hooks for this module.
|
|
18
|
+
export const storeHooks = { useMemo, useCallback, useRef, useLayoutEffect, useSyncExternalStore };
|
|
19
|
+
|
|
20
|
+
const absent = Symbol('no native store provider');
|
|
21
|
+
const stores = new WeakMap<object, Context<unknown>>();
|
|
22
|
+
|
|
23
|
+
function nativeFor(context: object): Context<unknown> {
|
|
24
|
+
let native = stores.get(context);
|
|
25
|
+
if (!native) {
|
|
26
|
+
native = createContext<unknown>(absent);
|
|
27
|
+
stores.set(context, native);
|
|
28
|
+
}
|
|
29
|
+
return native;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function useStoreContext<T>(context: ReactContext<T>): T {
|
|
33
|
+
const value = useContext(nativeFor(context));
|
|
34
|
+
// The matching provider stores a T for this exact React context.
|
|
35
|
+
return value === absent ? useContext(context) : (value as T);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function NativeStoreProvider<T>({
|
|
39
|
+
context,
|
|
40
|
+
value,
|
|
41
|
+
children,
|
|
42
|
+
}: {
|
|
43
|
+
context: ReactContext<T>;
|
|
44
|
+
value: NoInfer<T>;
|
|
45
|
+
children?: OctaneNode | ChildrenBlock;
|
|
46
|
+
}) @{
|
|
47
|
+
const Provider = nativeFor(context);
|
|
48
|
+
<Provider value={value}>{children}</Provider>
|
|
49
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use no memo';
|
|
2
|
+
|
|
3
|
+
import { storeHooks } from './octane-store-context.tsrx';
|
|
4
|
+
|
|
5
|
+
let ordinal: number | undefined;
|
|
6
|
+
|
|
7
|
+
function nextSlot() {
|
|
8
|
+
if (ordinal === undefined) throw new Error('A store hook must run inside its bound entry.');
|
|
9
|
+
return ordinal++;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** The compiled caller supplies the outer slot. React orders the base hooks within it. */
|
|
13
|
+
export function bindStoreHook<F extends (...args: any[]) => any>(hook: F): F {
|
|
14
|
+
return function (this: unknown, ...args: Parameters<F>) {
|
|
15
|
+
const previous = ordinal;
|
|
16
|
+
ordinal = 0;
|
|
17
|
+
try {
|
|
18
|
+
return hook.apply(this, args);
|
|
19
|
+
} finally {
|
|
20
|
+
ordinal = previous;
|
|
21
|
+
}
|
|
22
|
+
} as F;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
type Deps = readonly unknown[];
|
|
26
|
+
|
|
27
|
+
export function useMemo<T>(create: () => T, deps?: Deps): T {
|
|
28
|
+
return storeHooks.useMemo(() => create(), deps ?? null, nextSlot());
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function useCallback<F extends (...args: any[]) => any>(fn: F, deps?: Deps): F {
|
|
32
|
+
return storeHooks.useCallback(fn, deps ?? null, nextSlot());
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function useRef<T>(value: T): { current: T };
|
|
36
|
+
export function useRef<T>(value: T | null): { current: T | null };
|
|
37
|
+
export function useRef<T>(value: T | undefined): { current: T | undefined };
|
|
38
|
+
export function useRef<T>(value: T) {
|
|
39
|
+
return storeHooks.useRef(value, nextSlot());
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function useLayoutEffect(setup: () => void | (() => void), deps?: Deps): void {
|
|
43
|
+
storeHooks.useLayoutEffect(() => setup(), deps ?? null, nextSlot());
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function missingServerSnapshot(): never {
|
|
47
|
+
throw new Error('Missing getServerSnapshot, which is required for server-rendered content.');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function useSyncExternalStore<T>(
|
|
51
|
+
subscribe: (notify: () => void) => () => void,
|
|
52
|
+
getSnapshot: () => T,
|
|
53
|
+
getServerSnapshot?: () => T,
|
|
54
|
+
): T {
|
|
55
|
+
return storeHooks.useSyncExternalStore(
|
|
56
|
+
subscribe,
|
|
57
|
+
getSnapshot,
|
|
58
|
+
getServerSnapshot ?? missingServerSnapshot,
|
|
59
|
+
nextSlot(),
|
|
60
|
+
);
|
|
61
|
+
}
|