@ecopages/codemod 0.9.0 → 0.10.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 +58 -6
- package/package.json +31 -1
- package/src/cli.d.ts +12 -0
- package/src/cli.js +96 -2
- package/src/transforms/migrate-middleware-locals.d.ts +75 -0
- package/src/transforms/migrate-middleware-locals.js +949 -0
- package/src/transforms/migrate-radiant-bindings.d.ts +49 -0
- package/src/transforms/migrate-radiant-bindings.js +613 -0
- package/src/transforms/migrate-radiant-refs.d.ts +43 -0
- package/src/transforms/migrate-radiant-refs.js +503 -0
- package/src/transforms/print-options.d.ts +7 -4
- package/src/transforms/print-options.js +9 -3
- package/src/transforms/source-edits.d.ts +61 -0
- package/src/transforms/source-edits.js +93 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Source-splicing helpers shared by the Radiant transforms.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* These transforms do not reprint through Recast. A change to a class or JSX node makes Recast reprint the
|
|
6
|
+
* whole node, which re-indents it and drops whitespace in JSX text. They record each change as a replacement
|
|
7
|
+
* between two offsets of the original source and apply them to the text, so every other character stays as
|
|
8
|
+
* written: tabs or spaces, line endings and JSX text.
|
|
9
|
+
*/
|
|
10
|
+
export const RADIANT_SOURCE = /^@ecopages\/radiant(\/|$)/;
|
|
11
|
+
/**
|
|
12
|
+
* Recast parse options under which a node's line and column match the original text.
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* A new object on every call: jscodeshift writes its parser into the options object it is given, and a shared
|
|
16
|
+
* one then carries that parser into the next file.
|
|
17
|
+
*/
|
|
18
|
+
export function parseOptions() {
|
|
19
|
+
return { tabWidth: 1 };
|
|
20
|
+
}
|
|
21
|
+
export function isNode(value) {
|
|
22
|
+
return typeof value === 'object' && value !== null && typeof value.type === 'string';
|
|
23
|
+
}
|
|
24
|
+
export function lineOf(node) {
|
|
25
|
+
return isNode(node) ? node.loc?.start.line : undefined;
|
|
26
|
+
}
|
|
27
|
+
export function keyName(node) {
|
|
28
|
+
const key = node.key;
|
|
29
|
+
if (node.computed || !isNode(key))
|
|
30
|
+
return undefined;
|
|
31
|
+
if (key.type === 'Identifier')
|
|
32
|
+
return key.name;
|
|
33
|
+
if (key.type === 'StringLiteral')
|
|
34
|
+
return key.value;
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
export function stringValue(node) {
|
|
38
|
+
return isNode(node) && node.type === 'StringLiteral' ? node.value : undefined;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Reads node offsets from their line and column.
|
|
42
|
+
*
|
|
43
|
+
* @remarks
|
|
44
|
+
* Recast parses a copy of the source with its line endings normalized and its indentation re-measured, so the
|
|
45
|
+
* parser's own `start` and `end` drift from the original text on CRLF or tab-indented files. Line and column
|
|
46
|
+
* stay true when the source is parsed with {@link parseOptions}.
|
|
47
|
+
*/
|
|
48
|
+
export function offsetsOf(source) {
|
|
49
|
+
const lineStarts = [0];
|
|
50
|
+
for (let index = source.indexOf('\n'); index !== -1; index = source.indexOf('\n', index + 1)) {
|
|
51
|
+
lineStarts.push(index + 1);
|
|
52
|
+
}
|
|
53
|
+
const at = (position) => lineStarts[position.line - 1] + position.column;
|
|
54
|
+
return {
|
|
55
|
+
start: (node) => at(node.loc.start),
|
|
56
|
+
end: (node) => at(node.loc.end),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export function applyEdits(source, edits) {
|
|
60
|
+
let result = source;
|
|
61
|
+
for (const edit of [...edits].sort((a, b) => b.start - a.start)) {
|
|
62
|
+
result = `${result.slice(0, edit.start)}${edit.text}${result.slice(edit.end)}`;
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
/** Removes a node with its leading comments, and its line when nothing else is on it. */
|
|
67
|
+
export function removeNode(source, pos, node) {
|
|
68
|
+
const comments = (node.leadingComments ?? []).filter((comment) => pos.start(comment) < pos.start(node));
|
|
69
|
+
let start = pos.start(comments.length > 0 ? comments[0] : node);
|
|
70
|
+
let end = pos.end(node);
|
|
71
|
+
const lineStart = source.lastIndexOf('\n', start - 1) + 1;
|
|
72
|
+
const lineEnd = source.indexOf('\n', end);
|
|
73
|
+
const restOfLine = source.slice(end, lineEnd === -1 ? undefined : lineEnd);
|
|
74
|
+
if (source.slice(lineStart, start).trim() === '' && restOfLine.trim() === '') {
|
|
75
|
+
start = lineStart;
|
|
76
|
+
end = lineEnd === -1 ? source.length : lineEnd + 1;
|
|
77
|
+
}
|
|
78
|
+
return { start, end, text: '' };
|
|
79
|
+
}
|
|
80
|
+
/** Maps each local name imported from `@ecopages/radiant` or one of its subpaths to its exported name. */
|
|
81
|
+
export function readRadiantImports(j, root) {
|
|
82
|
+
const imports = new Map();
|
|
83
|
+
root.find(j.ImportDeclaration)
|
|
84
|
+
.filter((path) => RADIANT_SOURCE.test(String(path.node.source.value)))
|
|
85
|
+
.forEach((path) => {
|
|
86
|
+
for (const specifier of path.node.specifiers ?? []) {
|
|
87
|
+
if (specifier.type !== 'ImportSpecifier')
|
|
88
|
+
continue;
|
|
89
|
+
imports.set(String(specifier.local?.name ?? specifier.imported.name), String(specifier.imported.name));
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
return imports;
|
|
93
|
+
}
|