@octanejs/codemod 0.2.0 → 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.
Files changed (40) hide show
  1. package/README.md +140 -12
  2. package/dist/bin.js +2 -2
  3. package/dist/effect-receivers.d.ts +14 -2
  4. package/dist/effect-receivers.js +171 -22
  5. package/dist/expression-wrappers.d.ts +2 -0
  6. package/dist/expression-wrappers.js +19 -0
  7. package/dist/forwarded-props.d.ts +4 -1
  8. package/dist/forwarded-props.js +18 -3
  9. package/dist/linked-state.d.ts +1 -1
  10. package/dist/linked-state.js +7 -3
  11. package/dist/local-react-hook.d.ts +13 -6
  12. package/dist/local-react-hook.js +316 -53
  13. package/dist/local-uses.d.ts +10 -1
  14. package/dist/local-uses.js +87 -5
  15. package/dist/mappings.d.ts +7 -1
  16. package/dist/mappings.js +21 -3
  17. package/dist/path-svg.d.ts +68 -4
  18. package/dist/path-svg.js +162 -56
  19. package/dist/project.js +1589 -147
  20. package/dist/react-intl.d.ts +1 -1
  21. package/dist/react-intl.js +43 -6
  22. package/dist/react-island-sources.d.ts +53 -0
  23. package/dist/react-island-sources.js +979 -0
  24. package/dist/react-renderer.d.ts +2 -0
  25. package/dist/react-renderer.js +94 -0
  26. package/dist/scoped-store-contract.d.ts +9 -2
  27. package/dist/scoped-store-contract.js +209 -33
  28. package/dist/store-hook-uses.d.ts +7 -5
  29. package/dist/store-hook-uses.js +218 -62
  30. package/dist/store-inputs.d.ts +9 -2
  31. package/dist/store-inputs.js +213 -16
  32. package/dist/strong.d.ts +2 -2
  33. package/dist/strong.js +72 -18
  34. package/dist/transform.d.ts +14 -0
  35. package/dist/transform.js +2856 -292
  36. package/dist/types.d.ts +40 -10
  37. package/package.json +4 -3
  38. package/src/path-svg.ts +183 -62
  39. package/templates/octane-path-svg.ts +5 -1
  40. package/templates/react-intl.tsrx +64 -11
package/dist/path-svg.js CHANGED
@@ -16,6 +16,7 @@ const operation = '(?:(?:scale|translate)\\(' +
16
16
  const transform = new RegExp('^' + operation + '(?:' + separator + operation + ')*$');
17
17
  const transformNumbers = new RegExp(number, 'g');
18
18
  const localPaint = /^url\(#[A-Za-z_][A-Za-z0-9_-]*\)$/;
19
+ const identifier = /^[A-Za-z_][A-Za-z0-9_-]*$/;
19
20
  const tileSize = /^[1-9]\d*$/;
20
21
  const rootAttributes = {
21
22
  preserveAspectRatio: /^none$/,
@@ -31,8 +32,12 @@ export function svgLiteralRootOverride(name, value) {
31
32
  return (name === 'fill' || name === 'focusable') && rootAttributes[name].test(value);
32
33
  }
33
34
  const fillRule = /^(?:evenodd|nonzero)$/;
35
+ const referencedPaint = new RegExp(paint.source + '|' + localPaint.source);
36
+ const userSpace = /^userSpaceOnUse$/;
37
+ const bounds = { x: length, y: length, width: extent, height: extent };
38
+ const blur = /^(?:0|[1-9]\d*)(?:\.\d+)?$/;
34
39
  const presentation = {
35
- fill: paint,
40
+ fill: referencedPaint,
36
41
  stroke: paint,
37
42
  'stroke-width': length,
38
43
  'stroke-linecap': /^(?:butt|round|square)$/,
@@ -49,47 +54,73 @@ const pathAttributes = {
49
54
  };
50
55
  const rectAttributes = {
51
56
  ...presentation,
52
- fill: new RegExp(paint.source + '|' + localPaint.source),
53
- x: length,
54
- y: length,
55
- width: extent,
56
- height: extent,
57
+ ...bounds,
57
58
  rx: extent,
58
59
  ry: extent,
60
+ transform,
59
61
  };
60
62
  const shapeAttributes = {
61
63
  path: pathAttributes,
62
64
  rect: rectAttributes,
63
- g: { transform, 'clip-path': /^url\(#[A-Za-z_][A-Za-z0-9_-]*\)$/ },
65
+ circle: { ...presentation, cx: length, cy: length, r: extent },
66
+ g: { transform, 'clip-path': localPaint, mask: localPaint, filter: localPaint },
64
67
  defs: {},
65
- clipPath: { id: /^[A-Za-z_][A-Za-z0-9_-]*$/ },
68
+ clipPath: { id: identifier },
66
69
  pattern: {
67
- id: /^[A-Za-z_][A-Za-z0-9_-]*$/,
70
+ id: identifier,
68
71
  width: tileSize,
69
72
  height: tileSize,
70
- patternUnits: /^userSpaceOnUse$/,
73
+ patternUnits: userSpace,
74
+ },
75
+ linearGradient: {
76
+ id: identifier,
77
+ gradientUnits: userSpace,
78
+ x1: length,
79
+ y1: length,
80
+ x2: length,
81
+ y2: length,
82
+ },
83
+ stop: { offset: opacity, 'stop-color': paint, 'stop-opacity': opacity },
84
+ mask: { id: identifier, ...bounds, maskUnits: userSpace, style: /^mask-type:alpha$/ },
85
+ filter: {
86
+ id: identifier,
87
+ ...bounds,
88
+ filterUnits: userSpace,
89
+ 'color-interpolation-filters': /^(?:sRGB|linearRGB)$/,
71
90
  },
91
+ feFlood: { 'flood-opacity': opacity, result: identifier },
92
+ feBlend: { in: identifier, in2: identifier, mode: /^normal$/, result: identifier },
93
+ feGaussianBlur: { stdDeviation: blur, result: identifier },
72
94
  };
73
- function attributes(text, allowed) {
95
+ function attributes(text, allowed, allowData = false) {
74
96
  const values = {};
75
- const field = /[ \t\r\n]+([A-Za-z][A-Za-z-]*)[ \t\r\n]*=[ \t\r\n]*(?:"([^"&<>\t\r\n]*)"|'([^'&<>\t\r\n]*)')/gy;
97
+ const field = /[ \t\r\n]+([A-Za-z][A-Za-z0-9-]*)[ \t\r\n]*=[ \t\r\n]*(?:"([^"&<>\t\r\n]*)"|'([^'&<>\t\r\n]*)')/gy;
76
98
  let end = 0;
77
99
  for (let match; (match = field.exec(text)); end = field.lastIndex) {
78
100
  const name = match[1];
79
101
  let value = (match[2] ?? match[3]);
80
- if (name in values || !Object.prototype.hasOwnProperty.call(allowed, name))
102
+ if (name in values)
81
103
  return null;
82
- const rule = allowed[name];
83
- if (!rule.test(value))
104
+ const rule = Object.prototype.hasOwnProperty.call(allowed, name) ? allowed[name] : undefined;
105
+ // SVG JSX loaders can normalize numeric strings or escape punctuation differently.
106
+ if (rule
107
+ ? !rule.test(value)
108
+ : !(allowData &&
109
+ svgDataAttribute(name) &&
110
+ /^(?:[a-z][a-z0-9_-]*(?: [a-z0-9_-]+)*)?$/.test(value)))
84
111
  return null;
85
112
  // SVGR emits a numeric literal for a leading-dot opacity.
86
113
  if (rule === opacity && value.startsWith('.'))
87
114
  value = '0' + value;
88
115
  // SVG component loaders can emit a numeric literal for unitless lengths.
89
- if (rule === length || rule === extent || rule === opacity || rule === tileSize) {
116
+ if (rule === length ||
117
+ rule === extent ||
118
+ rule === opacity ||
119
+ rule === tileSize ||
120
+ rule === blur) {
90
121
  const n = Number(value);
91
122
  if ((!Number.isNaN(n) && String(n) !== value) ||
92
- (rule === extent && !Number.isFinite(parseFloat(value))))
123
+ ((rule === extent || rule === blur) && !Number.isFinite(parseFloat(value))))
93
124
  return null;
94
125
  }
95
126
  if (rule === transform &&
@@ -103,18 +134,70 @@ function attributes(text, allowed) {
103
134
  export function svgDataAttribute(name) {
104
135
  return /^data-(?!oct(?:ane)?(?:-|$)|react-compat$)[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/.test(name);
105
136
  }
106
- /** Read authored geometry with a single local clip or paint definition. */
137
+ const containerTags = new Set([
138
+ 'g',
139
+ 'defs',
140
+ 'clipPath',
141
+ 'pattern',
142
+ 'linearGradient',
143
+ 'mask',
144
+ 'filter',
145
+ ]);
146
+ const definitionTags = new Set(['clipPath', 'pattern', 'linearGradient', 'mask', 'filter']);
147
+ const filterPrimitives = new Set(['feFlood', 'feBlend', 'feGaussianBlur']);
148
+ const geometryTags = new Set(['path', 'rect', 'circle']);
149
+ function allowedChild(parent, child) {
150
+ switch (parent) {
151
+ case 'defs':
152
+ return definitionTags.has(child) && child !== 'mask';
153
+ case 'linearGradient':
154
+ return child === 'stop';
155
+ case 'filter':
156
+ return filterPrimitives.has(child);
157
+ case 'clipPath':
158
+ return child === 'rect';
159
+ case 'pattern':
160
+ return child === 'path';
161
+ case 'mask':
162
+ return child === 'path' || child === 'rect';
163
+ default:
164
+ return (geometryTags.has(child) ||
165
+ child === 'g' ||
166
+ (parent === 'svg' && (child === 'defs' || child === 'mask')));
167
+ }
168
+ }
169
+ function closedContainer(tag, children) {
170
+ if (tag === 'clipPath' || tag === 'pattern')
171
+ return children.length === 1;
172
+ if (tag === 'linearGradient')
173
+ return children.length >= 2;
174
+ if (tag !== 'filter')
175
+ return (tag !== 'defs' && tag !== 'mask') || children.length > 0;
176
+ const inputs = new Set(['SourceGraphic', 'SourceAlpha']);
177
+ return (children.length > 0 &&
178
+ children.every(({ tag, attributes }) => {
179
+ const { in: input, in2, result, stdDeviation } = attributes;
180
+ if ((tag === 'feBlend' && !(input && in2 && inputs.has(input) && inputs.has(in2))) ||
181
+ (tag === 'feGaussianBlur' && stdDeviation === undefined) ||
182
+ !result ||
183
+ inputs.has(result))
184
+ return false;
185
+ inputs.add(result);
186
+ return true;
187
+ }));
188
+ }
189
+ /** Read authored geometry with bounded local paint and effect definitions. */
107
190
  export function parseStaticSvg(source) {
108
191
  if (typeof source !== 'string')
109
192
  return null;
110
193
  let root = null;
194
+ let title;
111
195
  const children = [];
112
196
  const open = [];
113
- let clipId;
114
- let reference;
115
- let patternId;
116
- let paintId;
117
- const token = /[ \t\r\n]*(?:<!--([\s\S]*?)-->|<\/(svg|g|defs|clipPath|pattern)[ \t\r\n]*>|<(svg|g|rect|path|defs|clipPath|pattern)([^<>]*?)(\/?)>)/gy;
197
+ const definitions = new Map();
198
+ const references = [];
199
+ let legacyDefinition = false;
200
+ const token = /[ \t\r\n]*(?:<!--([\s\S]*?)-->|<\/([A-Za-z][A-Za-z0-9]*)[ \t\r\n]*>|<([A-Za-z][A-Za-z0-9]*)([^<>]*?)(\/?)>)/gy;
118
201
  let hasShape = false;
119
202
  let end = 0;
120
203
  for (let match; (match = token.exec(source)); end = token.lastIndex) {
@@ -125,83 +208,106 @@ export function parseStaticSvg(source) {
125
208
  return null;
126
209
  }
127
210
  else if (closing) {
128
- if (!parent || closing !== parent.tag)
129
- return null;
130
- if ((closing === 'defs' || closing === 'clipPath' || closing === 'pattern') &&
131
- parent.children.length !== 1)
211
+ if (!parent || closing !== parent.tag || !closedContainer(parent.tag, parent.children))
132
212
  return null;
133
213
  open.pop();
134
214
  }
135
215
  else if (tag === 'svg') {
136
216
  if (root || selfClosing)
137
217
  return null;
138
- root = attributes(fields, rootAttributes);
218
+ root = attributes(fields, rootAttributes, true);
139
219
  if (!root)
140
220
  return null;
141
221
  open.push({ tag, children });
142
222
  }
223
+ else if (tag === 'title') {
224
+ if (parent?.tag !== 'svg' || children.length || title !== undefined || fields || selfClosing)
225
+ return null;
226
+ // Keep one literal text child. XML entities, markup, and line endings require loader rules.
227
+ const titleToken = /([^<&]+)<\/title>/uy;
228
+ titleToken.lastIndex = token.lastIndex;
229
+ const text = titleToken.exec(source);
230
+ if (!text ||
231
+ /\]\]>|[^\u0020-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/u.test(text[1]) ||
232
+ !text[1].trim())
233
+ return null;
234
+ title = text[1];
235
+ token.lastIndex = titleToken.lastIndex;
236
+ }
143
237
  else {
144
- if (!parent)
238
+ if (!parent || !tag || !Object.prototype.hasOwnProperty.call(shapeAttributes, tag))
145
239
  return null;
146
240
  const name = tag;
147
- if ((name === 'defs' && parent.tag !== 'svg') ||
148
- (name === 'clipPath' || name === 'pattern') !== (parent.tag === 'defs') ||
149
- (parent.tag === 'clipPath' && name !== 'rect') ||
150
- (parent.tag === 'pattern' && name !== 'path'))
241
+ if (!allowedChild(parent.tag, name))
242
+ return null;
243
+ if (name === 'defs' && children.some((node) => node.tag === 'defs'))
151
244
  return null;
152
- const container = name !== 'path' && name !== 'rect';
245
+ const container = containerTags.has(name);
153
246
  if (container ? selfClosing || open.length > 32 : !selfClosing)
154
247
  return null;
155
248
  const props = attributes(fields, shapeAttributes[name]);
156
249
  if (!props ||
157
250
  (tag === 'path' && !props.d) ||
158
- (tag === 'rect' && !(props.width && props.height)))
251
+ (tag === 'rect' && !(props.width && props.height)) ||
252
+ (tag === 'circle' && !props.r) ||
253
+ (tag === 'stop' && !props['stop-color']))
159
254
  return null;
160
- if (name === 'clipPath') {
161
- if (!props.id || clipId !== undefined || patternId !== undefined)
255
+ if (definitionTags.has(name)) {
256
+ if (!props.id || definitions.has(props.id))
162
257
  return null;
163
- clipId = props.id;
258
+ legacyDefinition ||= name === 'clipPath' || name === 'pattern';
259
+ definitions.set(props.id, name);
164
260
  }
165
261
  if (props['clip-path']) {
166
- const id = props['clip-path'].slice(5, -1);
167
- if (parent.tag !== 'svg' || (reference !== undefined && reference !== id))
262
+ if (parent.tag !== 'svg')
168
263
  return null;
169
- reference = id;
264
+ references.push({ id: props['clip-path'].slice(5, -1), target: 'clipPath' });
170
265
  }
171
- if (name === 'pattern') {
172
- if (patternId !== undefined ||
173
- clipId !== undefined ||
174
- !(props.id && props.width && props.height && props.patternUnits))
175
- return null;
176
- patternId = props.id;
266
+ if (name === 'pattern' && !(props.width && props.height && props.patternUnits))
267
+ return null;
268
+ if (name === 'linearGradient' &&
269
+ !(props.gradientUnits && props.x1 && props.y1 && props.x2 && props.y2))
270
+ return null;
271
+ if ((name === 'mask' && !(props.maskUnits && props.style && props.width && props.height)) ||
272
+ (name === 'filter' && !(props.filterUnits && props.width && props.height)))
273
+ return null;
274
+ for (const target of ['mask', 'filter']) {
275
+ if (props[target])
276
+ references.push({ id: props[target].slice(5, -1), target });
177
277
  }
178
- if (name === 'rect' && props.fill?.startsWith('url(')) {
179
- const id = props.fill.slice(5, -1);
180
- if (parent.tag !== 'svg' || (paintId !== undefined && paintId !== id))
278
+ if (props.fill?.startsWith('url(')) {
279
+ if (parent.tag !== 'svg' && parent.tag !== 'g')
181
280
  return null;
182
- paintId = id;
281
+ references.push({
282
+ id: props.fill.slice(5, -1),
283
+ target: 'linearGradient',
284
+ rootRect: name === 'rect' && parent.tag === 'svg',
285
+ });
183
286
  }
184
287
  const node = { tag: name, attributes: props, children: [] };
185
288
  parent.children.push(node);
186
289
  if (container)
187
290
  open.push(node);
188
- else if (parent.tag !== 'clipPath' && parent.tag !== 'pattern')
291
+ else if (parent.tag === 'svg' || parent.tag === 'g')
189
292
  hasShape = true;
190
293
  }
191
294
  }
192
295
  return root &&
193
296
  hasShape &&
194
297
  !open.length &&
195
- clipId === reference &&
196
- patternId === paintId &&
298
+ (!legacyDefinition || definitions.size === 1) &&
299
+ new Set(references.map(({ id }) => id)).size === definitions.size &&
300
+ references.every(({ id, target, rootRect }) => definitions.get(id) === target || (rootRect && definitions.get(id) === 'pattern')) &&
197
301
  space.test(source.slice(end))
198
- ? { root, children }
302
+ ? title === undefined
303
+ ? { root, children }
304
+ : { root, children, title }
199
305
  : null;
200
306
  }
201
307
  // Existing generated helpers expose this path-only result to their importers.
202
308
  export function parsePathSvg(source) {
203
309
  const svg = parseStaticSvg(source);
204
- return svg && svg.children.every((node) => node.tag === 'path')
310
+ return svg && svg.title === undefined && svg.children.every((node) => node.tag === 'path')
205
311
  ? { root: svg.root, paths: svg.children.map((node) => node.attributes) }
206
312
  : null;
207
313
  }