@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.
Files changed (58) hide show
  1. package/README.md +305 -5
  2. package/dist/bin.d.ts +2 -0
  3. package/dist/bin.js +162 -0
  4. package/dist/compiler.d.ts +11 -0
  5. package/dist/compiler.js +48 -0
  6. package/dist/effect-receivers.d.ts +11 -0
  7. package/dist/effect-receivers.js +315 -0
  8. package/dist/expression-wrappers.d.ts +6 -0
  9. package/dist/expression-wrappers.js +16 -0
  10. package/dist/forwarded-props.d.ts +27 -0
  11. package/dist/forwarded-props.js +489 -0
  12. package/dist/index.d.ts +6 -0
  13. package/{src → dist}/index.js +1 -0
  14. package/dist/linked-state.d.ts +17 -0
  15. package/dist/linked-state.js +126 -0
  16. package/dist/local-calls.d.ts +20 -0
  17. package/dist/local-calls.js +143 -0
  18. package/dist/local-react-hook.d.ts +15 -0
  19. package/dist/local-react-hook.js +353 -0
  20. package/dist/local-uses.d.ts +13 -0
  21. package/dist/local-uses.js +987 -0
  22. package/dist/mappings.d.ts +57 -0
  23. package/dist/mappings.js +343 -0
  24. package/dist/path-svg.d.ts +60 -0
  25. package/dist/path-svg.js +207 -0
  26. package/dist/project.d.ts +9 -0
  27. package/dist/project.js +1568 -0
  28. package/dist/react-intl.d.ts +4 -0
  29. package/dist/react-intl.js +389 -0
  30. package/dist/react-renderer.d.ts +9 -0
  31. package/dist/react-renderer.js +106 -0
  32. package/dist/scoped-signal.d.ts +4 -0
  33. package/dist/scoped-signal.js +480 -0
  34. package/dist/scoped-store-contract.d.ts +13 -0
  35. package/dist/scoped-store-contract.js +741 -0
  36. package/dist/store-hook-uses.d.ts +18 -0
  37. package/dist/store-hook-uses.js +403 -0
  38. package/dist/store-inputs.d.ts +16 -0
  39. package/dist/store-inputs.js +391 -0
  40. package/dist/strong.d.ts +24 -0
  41. package/dist/strong.js +142 -0
  42. package/dist/transform.d.ts +27 -0
  43. package/dist/transform.js +4890 -0
  44. package/dist/types.d.ts +203 -0
  45. package/dist/types.js +1 -0
  46. package/package.json +19 -6
  47. package/src/path-svg.ts +218 -0
  48. package/templates/octane-path-svg.ts +56 -0
  49. package/templates/octane-store-context.tsrx +49 -0
  50. package/templates/octane-store-hooks.ts +61 -0
  51. package/templates/react-intl.tsrx +116 -0
  52. package/src/bin.js +0 -151
  53. package/src/compiler.js +0 -61
  54. package/src/index.d.ts +0 -95
  55. package/src/mappings.js +0 -429
  56. package/src/project.js +0 -670
  57. package/src/strong.js +0 -149
  58. package/src/transform.js +0 -3425
package/src/strong.js DELETED
@@ -1,149 +0,0 @@
1
- import { applyFixes, pruneImports } from '@octanejs/cli/fix';
2
- import { finding, thrown } from './compiler.js';
3
-
4
- /**
5
- * @typedef {import('./compiler.js').Compiler} Compiler
6
- * @typedef {import('./compiler.js').Finding} Finding
7
- *
8
- * @typedef {Object} StrongResult
9
- * @property {string} code the module after every fix the compiler allowed
10
- * @property {boolean} strong whether it kept `"use strong"`
11
- * @property {string[]} fixed the diagnostic codes a fix resolved
12
- * @property {Finding[]} blockers the Strong errors left, when it did not
13
- */
14
-
15
- /**
16
- * The source without the directive the codemod added: its own line after any
17
- * leading comments and `"use client"`, or, at the very top, that line and the
18
- * blank line after it. `line` is where it stood and `lines` how many went.
19
- *
20
- * @param {string} code
21
- */
22
- function withoutDirective(code) {
23
- const top = '"use strong";\n\n';
24
- if (code.startsWith(top)) return { code: code.slice(top.length), line: 1, lines: 2 };
25
- const match = /^"use strong";\n/m.exec(code);
26
- if (!match) return { code, line: 0, lines: 0 };
27
- return {
28
- code: code.slice(0, match.index) + code.slice(match.index + match[0].length),
29
- line: code.slice(0, match.index).split('\n').length,
30
- lines: 1,
31
- };
32
- }
33
- const MAX_ROUNDS = 6;
34
-
35
- /**
36
- * Compile a module the codemod emitted in Strong mode and repair what the
37
- * compiler reports, until it compiles clean or nothing more can be fixed.
38
- *
39
- * The repairs are the compiler's own suggested edits, as `octane analyze --fix`
40
- * applies them, plus the rewrites that keep React's behavior exactly:
41
- *
42
- * - an explicit dependency list the compiler reports as equivalent to the one
43
- * it infers is dropped;
44
- * - state initialized from a prop is captured lazily, `useState(() => prop)`,
45
- * which is what `useState(prop)` meant in React.
46
- *
47
- * Strong errors that remain mean the module needs a person. With `force`
48
- * unset the module falls back to compatibility mode so the migrated app still
49
- * builds, and the errors are reported as the work left to make it Strong.
50
- *
51
- * @param {Compiler} compiler
52
- * @param {string} code
53
- * @param {string} filename
54
- * @param {{ force?: boolean }} [options]
55
- * @returns {StrongResult}
56
- */
57
- export function strengthen(compiler, code, filename, { force = false } = {}) {
58
- /** @type {string[]} */
59
- const fixed = [];
60
- let diagnostics = [];
61
- for (let round = 0; round < MAX_ROUNDS; round++) {
62
- const result = compiler.collectDiagnostics(code, filename, { strong: true });
63
- if (result.error != null) {
64
- // A module that compatibility mode cannot compile either has no
65
- // Strong blocker to report; the compile failure is its own error.
66
- const compat = compiler.collectDiagnostics(withoutDirective(code).code, filename, {});
67
- return fallback(code, compat.error == null ? [thrown(result.error)] : [], fixed, force);
68
- }
69
- diagnostics = result.diagnostics;
70
- const fixes = diagnostics.flatMap((diagnostic) => fixesFor(code, diagnostic));
71
- if (fixes.length === 0) break;
72
- const { text, applied } = applyFixes(code, fixes);
73
- if (applied.length === 0) break;
74
- code = pruneImports(text);
75
- for (const fix of applied) fixed.push(fix.code);
76
- }
77
- const blockers = diagnostics.filter((diagnostic) => diagnostic.severity === 'error').map(finding);
78
- if (blockers.length === 0) return { code, strong: true, fixed, blockers };
79
- return fallback(code, blockers, fixed, force);
80
- }
81
-
82
- /**
83
- * @param {string} code
84
- * @param {Finding[]} blockers
85
- * @param {string[]} fixed
86
- * @param {boolean} force
87
- * @returns {StrongResult}
88
- */
89
- function fallback(code, blockers, fixed, force) {
90
- if (force) return { code, strong: true, fixed, blockers };
91
- // The module builds in compatibility mode; what kept it there is work
92
- // left, not a failure. Its lines are the written file's, without the
93
- // directive.
94
- const compat = withoutDirective(code);
95
- const kept = blockers.map((blocker) => ({
96
- ...blocker,
97
- severity: 'warning',
98
- line: blocker.line > compat.line ? blocker.line - compat.lines : blocker.line,
99
- }));
100
- return { code: compat.code, strong: false, fixed, blockers: kept };
101
- }
102
-
103
- /**
104
- * The edits that resolve one diagnostic, as `applyFixes` takes them.
105
- *
106
- * @param {string} code
107
- * @param {any} diagnostic
108
- * @returns {{ code: string, edits: { start: number, end: number, text: string }[] }[]}
109
- */
110
- function fixesFor(code, diagnostic) {
111
- /** @type {{ code: string, edits: { start: number, end: number, text: string }[] }[]} */
112
- const fixes = [];
113
- for (const suggestion of diagnostic.suggestions ?? []) {
114
- if (Array.isArray(suggestion?.edits) && suggestion.edits.length > 0) {
115
- fixes.push({ code: diagnostic.code, edits: suggestion.edits });
116
- break;
117
- }
118
- }
119
- if (fixes.length > 0) return fixes;
120
- const start = diagnostic.start?.offset;
121
- const end = diagnostic.end?.offset;
122
- if (typeof start !== 'number' || typeof end !== 'number') return fixes;
123
-
124
- if (diagnostic.code === 'OCTANE_STRONG_EXPLICIT_DEPENDENCIES' && diagnostic.severity === 'hint') {
125
- // `useEffect(fn, [a])` -> `useEffect(fn)`: the compiler infers `[a]`.
126
- const comma = /,\s*$/.exec(code.slice(0, start));
127
- if (comma && code[start] === '[') {
128
- fixes.push({ code: diagnostic.code, edits: [{ start: comma.index, end, text: '' }] });
129
- }
130
- }
131
- if (diagnostic.code === 'OCTANE_STRONG_UNLINKED_PROP_STATE') {
132
- const before = /\S\s*$/.exec(code.slice(0, start))?.[0][0];
133
- const value = code.slice(start, end);
134
- if (before === '(') {
135
- // useState(prop): React captured the value once, as a lazy
136
- // initializer does.
137
- const body = /^[\w$.]+$/.test(value) ? value : `(${value})`;
138
- fixes.push({ code: diagnostic.code, edits: [{ start, end, text: `() => ${body}` }] });
139
- } else if (before === ',') {
140
- // useReducer(reducer, prop): an explicit initializer is the
141
- // deliberate capture.
142
- fixes.push({
143
- code: diagnostic.code,
144
- edits: [{ start: end, end, text: ', (initial) => initial' }],
145
- });
146
- }
147
- }
148
- return fixes;
149
- }