@octanejs/codemod 0.2.1 → 0.2.2

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/dist/types.d.ts CHANGED
@@ -33,18 +33,32 @@ export interface TransformOptions {
33
33
  bindingExports?(from: string, to: string): ReadonlySet<string> | undefined;
34
34
  /** Record a successful rewrite that was checked against a partial binding's catalog. */
35
35
  recordBindingRewrite?(from: string, to: string): void;
36
+ /** Resolve a direct relative import of an unselected source in a single-file run. */
37
+ retainedRegistry?(specifier: string): ts.SourceFile | undefined;
36
38
  /** The issuer resolves the original installed React runtime and types. */
37
39
  originalReact?: boolean;
38
- /** A React DOM resource hint called by an imported plain TypeScript initializer. */
39
- initializerResourceHint?(module: string, name: string): string | undefined;
40
+ /** The authored React bridge and its direct component import still resolve to React. */
41
+ retainedReactBridgeComponent?(bridge: string, module: string, name: string): boolean;
42
+ /** The issuer resolves the original React DOM server entry that this call will migrate. */
43
+ originalReactServerEntry?(module: string): boolean;
44
+ /** A React DOM resource hint called by an imported plain TypeScript function. */
45
+ initializerResourceHint?(module: string, name: string, retainedOnly?: boolean): string | undefined;
40
46
  /** Renderer work called by an unchanged local TypeScript export. */
41
47
  localReactHook?(module: string, name: string, nativeOnly: boolean): string | {
42
48
  from: string;
43
49
  } | {
44
50
  privateContext: true;
51
+ } | {
52
+ retainedStore: true;
45
53
  } | undefined;
46
54
  /** A dispatcher hook directly called by an unchanged component at a migrated React DOM root. */
47
55
  localReactRootComponent?(module: string, name: string): string | undefined;
56
+ /** Whether a retained local export returns JSX under the project's React JSX settings. */
57
+ localReactTemplateComponent?(module: string, name: string): boolean;
58
+ /** A directly imported context declaration that remains in React. */
59
+ localReactContext?(module: string, name: string, member: boolean): boolean;
60
+ /** Severity for a direct render of a local React class component. */
61
+ localReactClass?(module: string, name: string): 'error' | 'warning' | undefined;
48
62
  /** Resolve a source-backed workspace hook after verifying its implementation and its call sites. */
49
63
  resolveStoreHook?(module: string, name: string, safeUses: (sharedInput: StoreInputCheck, unchangedHelper?: (module: string, name: string) => boolean, retainedArgument?: (module: string, name: string) => boolean) => boolean): {
50
64
  from: string;
@@ -54,11 +68,11 @@ export interface TransformOptions {
54
68
  reactIntlAdapter?: string;
55
69
  /** Whether a directly imported, selected component forwards this prop only to text attributes. */
56
70
  forwardedText?(module: string, exported: string, prop: string, ordinaryCaller?: boolean, through?: boolean): boolean;
57
- /** Whether the selected component at this local declaration offset, or this imported name, accepts one explicit element. */
71
+ /** Whether this selected component accepts one explicit element, with separate review for path aliases. */
58
72
  elementChild?(binding: number | {
59
73
  module: string;
60
74
  imported: string;
61
- }): boolean;
75
+ }): boolean | 'review-alias';
62
76
  /** Whether a selected, directly imported component inspects its child descriptors. */
63
77
  descriptorChild?(binding: {
64
78
  module: string;
@@ -66,7 +80,7 @@ export interface TransformOptions {
66
80
  }): boolean;
67
81
  /** Whether an imported name is a context, with separate review for selected alias targets. */
68
82
  isContext?(specifier: string, name: string, local: string): boolean | 'review-alias' | null;
69
- /** The React package an import specifier names when that package has no Octane port, or null. */
83
+ /** The React package an import specifier names when the codemod keeps it on React, or null. */
70
84
  unported?(specifier: string): string | null;
71
85
  /** 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. */
72
86
  reactCompat?: boolean;
@@ -80,6 +94,8 @@ export interface TransformOptions {
80
94
  cssModuleFile?(specifier: string): string | undefined;
81
95
  /** The generated native SVG helper, if the project verified this raw asset. */
82
96
  pathSvgAdapter?(specifier: string): string | null;
97
+ /** Whether a native server SVG entry resolves to the adapter's installed Octane. */
98
+ originalSvgServer?: boolean;
83
99
  /** With pathSvgAdapter, supplies a native SVG adapter with the requested fixed root color. */
84
100
  fixedColorSvgAdapter?(specifier: string, color: string): string | null;
85
101
  /** The project compiles this file with the original React automatic JSX runtime. */
@@ -87,12 +103,12 @@ export interface TransformOptions {
87
103
  /** This file uses React's automatic JSX runtime and the component resolves to its original React package. */
88
104
  reactStoreComponent?(specifier: string): boolean;
89
105
  }
90
- /** A React package with no Octane port, and how migrated code uses it. */
106
+ /** A retained React package import and how migrated code uses it. */
91
107
  export interface Unported {
92
108
  name: string;
93
109
  /** Elements now rendered in a `ReactCompat` island. */
94
110
  islands: number;
95
- /** Uses that only a port can support. */
111
+ /** Uses that need review before migrating to an Octane renderer. */
96
112
  blocked: number;
97
113
  }
98
114
  export interface TransformResult {
@@ -181,7 +197,7 @@ export interface CodemodResult {
181
197
  name: string;
182
198
  patch: string;
183
199
  }[];
184
- /** React packages the code imports that have no Octane port, and how it uses them. */
200
+ /** React package imports the codemod keeps on React, and how the code uses them. */
185
201
  missing: (Unported & {
186
202
  modules: number;
187
203
  })[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octanejs/codemod",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=22.22.2"
@@ -45,9 +45,10 @@
45
45
  "./package.json": "./package.json"
46
46
  },
47
47
  "dependencies": {
48
- "@octanejs/cli": "0.3.5",
48
+ "@octanejs/cli": "0.3.6",
49
49
  "@tsrx/oxc": "0.23.0",
50
- "octane": "0.18.0",
50
+ "octane": "0.19.0",
51
+ "parse5": "8.0.1",
51
52
  "picocolors": "^1.1.1",
52
53
  "typescript": "^5.9.3"
53
54
  },
package/src/path-svg.ts CHANGED
@@ -220,6 +220,7 @@ function closedContainer(tag: SvgParent, children: readonly StaticSvgNode[]) {
220
220
  export function parseStaticSvg(source: string) {
221
221
  if (typeof source !== 'string') return null;
222
222
  let root: Record<string, string> | null = null;
223
+ let title: string | undefined;
223
224
  const children: StaticSvgNode[] = [];
224
225
  const open: { tag: SvgParent; children: StaticSvgNode[] }[] = [];
225
226
  const definitions = new Map<string, SvgTag>();
@@ -244,6 +245,21 @@ export function parseStaticSvg(source: string) {
244
245
  root = attributes(fields!, rootAttributes, true);
245
246
  if (!root) return null;
246
247
  open.push({ tag, children });
248
+ } else if (tag === 'title') {
249
+ if (parent?.tag !== 'svg' || children.length || title !== undefined || fields || selfClosing)
250
+ return null;
251
+ // Keep one literal text child. XML entities, markup, and line endings require loader rules.
252
+ const titleToken = /([^<&]+)<\/title>/uy;
253
+ titleToken.lastIndex = token.lastIndex;
254
+ const text = titleToken.exec(source);
255
+ if (
256
+ !text ||
257
+ /\]\]>|[^\u0020-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/u.test(text[1]) ||
258
+ !text[1].trim()
259
+ )
260
+ return null;
261
+ title = text[1];
262
+ token.lastIndex = titleToken.lastIndex;
247
263
  } else {
248
264
  if (!parent || !tag || !Object.prototype.hasOwnProperty.call(shapeAttributes, tag))
249
265
  return null;
@@ -308,14 +324,16 @@ export function parseStaticSvg(source: string) {
308
324
  definitions.get(id) === target || (rootRect && definitions.get(id) === 'pattern'),
309
325
  ) &&
310
326
  space.test(source.slice(end))
311
- ? { root, children }
327
+ ? title === undefined
328
+ ? { root, children }
329
+ : { root, children, title }
312
330
  : null;
313
331
  }
314
332
 
315
333
  // Existing generated helpers expose this path-only result to their importers.
316
334
  export function parsePathSvg(source: string) {
317
335
  const svg = parseStaticSvg(source);
318
- return svg && svg.children.every((node) => node.tag === 'path')
336
+ return svg && svg.title === undefined && svg.children.every((node) => node.tag === 'path')
319
337
  ? { root: svg.root, paths: svg.children.map((node) => node.attributes) }
320
338
  : null;
321
339
  }
@@ -24,6 +24,10 @@ export function pathSvg(source: string, fixedStyle?: string) {
24
24
  if (!geometry) throw new Error('Expected a raw SVG containing only supported static geometry.');
25
25
  const root = fixedStyle === undefined ? geometry.root : { ...geometry.root, style: fixedStyle };
26
26
  const children = geometry.children.map(prepareShape);
27
+ if (geometry.title !== undefined) {
28
+ const props = { key: -1, children: geometry.title };
29
+ children.unshift(() => createElement('title', props));
30
+ }
27
31
  return function PathSvg(props: {
28
32
  className?: unknown;
29
33
  'aria-hidden'?: string | boolean;
@@ -1,6 +1,6 @@
1
1
  'use strong';
2
2
 
3
- import { createContext, useContext, useMemo, type OctaneNode } from 'octane';
3
+ import { createContext, memo, useContext, useMemo, type OctaneNode } from 'octane';
4
4
  import { ReactCompat } from 'octane/react';
5
5
  import type { ChildrenBlock } from 'octane/tsrx-boundary';
6
6
  import {
@@ -87,19 +87,47 @@ export function IntlProvider({
87
87
  }
88
88
 
89
89
  type MessageProps = MessageDescriptor &
90
- Pick<ComponentProps<typeof ReactMessage>, 'values' | 'ignoreTag'>;
90
+ Pick<ComponentProps<typeof ReactMessage>, 'values' | 'ignoreTag'> & {
91
+ inlineHost?: typeof ReactCompat;
92
+ };
91
93
 
92
94
  function ReactContent({ content }: { content: ReactNode }) {
93
95
  return content;
94
96
  }
95
97
 
96
- export function FormattedMessage({
97
- id,
98
- defaultMessage,
99
- description,
100
- values,
101
- ignoreTag,
102
- }: MessageProps) @{
98
+ type MessagePart = string | number | bigint | boolean | null | undefined;
99
+
100
+ function isMessagePart(value: unknown): value is MessagePart {
101
+ return (
102
+ typeof value === 'string' ||
103
+ typeof value === 'number' ||
104
+ typeof value === 'bigint' ||
105
+ typeof value === 'boolean' ||
106
+ value === null ||
107
+ value === undefined
108
+ );
109
+ }
110
+
111
+ function plainMessageParts(value: unknown): MessagePart[] | undefined {
112
+ if (
113
+ !Array.isArray(value) ||
114
+ Object.getPrototypeOf(value) !== Array.prototype ||
115
+ Object.hasOwn(value, '$$typeof') ||
116
+ Object.hasOwn(value, 'then')
117
+ )
118
+ return;
119
+ // Copy dense data parts without calling methods or getters on custom formatter output.
120
+ const parts: MessagePart[] = [];
121
+ for (let i = 0, length = value.length; i < length; i++) {
122
+ const part = Object.getOwnPropertyDescriptor(value, i);
123
+ if (!part || !Object.hasOwn(part, 'value') || !isMessagePart(part.value)) return;
124
+ parts.push(part.value);
125
+ }
126
+ return parts;
127
+ }
128
+
129
+ function NativeFormattedMessage(props: MessageProps) @{
130
+ const { id, defaultMessage, description, values, ignoreTag } = props;
103
131
  const intl = useIntl();
104
132
  const descriptor = { id, defaultMessage, description };
105
133
  const { textComponent, formatMessage } = intl;
@@ -109,8 +137,33 @@ export function FormattedMessage({
109
137
  const content = requiresReact
110
138
  ? reactElement(ReactMessage, { ...descriptor, values, ignoreTag })
111
139
  : formatMessage(descriptor, values, { ignoreTag });
112
- if (typeof content === 'string') return content;
140
+ if (isMessagePart(content)) return content;
141
+ const parts = plainMessageParts(content);
142
+ if (parts) return parts;
143
+ const Island = Object.hasOwn(props, 'inlineHost')
144
+ ? (props.inlineHost ?? ReactCompat)
145
+ : ReactCompat;
113
146
  <Locale value={intl}>
114
- <ReactCompat component={ReactContent} props={{ content }} contexts={intlContexts} />
147
+ <Island component={ReactContent} props={{ content }} contexts={intlContexts} />
115
148
  </Locale>
116
149
  }
150
+
151
+ function equalOwnValues<T extends object>(before: T | undefined, after: T | undefined): boolean {
152
+ if (before === after) return true;
153
+ if (!before || !after) return false;
154
+ const keys = Object.keys(before) as (keyof T)[];
155
+ if (keys.length !== Object.keys(after).length) return false;
156
+ for (const key of keys) {
157
+ if (before[key] !== after[key] || !Object.hasOwn(after, key)) return false;
158
+ }
159
+ return true;
160
+ }
161
+
162
+ function equalMessages(previous: Readonly<MessageProps>, next: Readonly<MessageProps>): boolean {
163
+ const { values, ...message } = previous;
164
+ const { values: nextValues, ...nextMessage } = next;
165
+ // React Intl compares its values separately with strict equality (including ±0 and NaN).
166
+ return equalOwnValues(nextValues, values) && equalOwnValues(message, nextMessage);
167
+ }
168
+
169
+ export const FormattedMessage = memo(NativeFormattedMessage, equalMessages);