@trackunit/react-table-helpers 3.0.10-alpha-1872e1df7fb.0 → 3.0.10
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/index.cjs.js +0 -388
- package/index.esm.js +2 -387
- package/migrations/assets/custom-field-table-helpers/package.json +31 -0
- package/migrations/utils/jsx-utils.js +233 -0
- package/migrations/utils/move-named-imports.js +409 -0
- package/migrations/utils/package-json-utils.js +73 -0
- package/migrations/v3-0-0/move-custom-field-table-helpers.js +70 -0
- package/migrations.json +7 -1
- package/package.json +6 -15
- package/src/index.d.ts +0 -4
- package/src/generated/graphql-api/fragment-masking.d.ts +0 -19
- package/src/generated/graphql-api/gql.d.ts +0 -35
- package/src/generated/graphql-api/graphql.d.ts +0 -323
- package/src/generated/graphql-api/index.d.ts +0 -2
- package/src/useColumnDefinitionsFromCustomFieldDefinition/customFieldToCell.d.ts +0 -8
- package/src/useColumnDefinitionsFromCustomFieldDefinition/customFieldToExportString.d.ts +0 -8
- package/src/useColumnDefinitionsFromCustomFieldDefinition/types.d.ts +0 -165
- package/src/useColumnDefinitionsFromCustomFieldDefinition/useColumnDefinitionsFromCustomFieldDefinition.d.ts +0 -18
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.insertAttributeIntoOpeningTag = exports.logSummary = exports.parseTsx = exports.hasSpreadAttribute = exports.findJsxAttribute = exports.findJsxElements = exports.getLocalAliasFor = exports.getImportedAliases = exports.visitTsFiles = exports.visitTsxFiles = void 0;
|
|
37
|
+
const devkit_1 = require("@nx/devkit");
|
|
38
|
+
const ts = __importStar(require("typescript"));
|
|
39
|
+
const TSX_EXTENSIONS = [".tsx", ".jsx"];
|
|
40
|
+
const TS_EXTENSIONS = [".ts", ".tsx", ".jsx"];
|
|
41
|
+
const isTsxFile = (filePath) => TSX_EXTENSIONS.some(ext => filePath.endsWith(ext));
|
|
42
|
+
const isTsFile = (filePath) => TS_EXTENSIONS.some(ext => filePath.endsWith(ext));
|
|
43
|
+
const isUnderNodeModules = (filePath) => filePath.includes("/node_modules/") || filePath.startsWith("node_modules/");
|
|
44
|
+
const isUnderDist = (filePath) => filePath.includes("/dist/") || filePath.startsWith("dist/") || filePath.includes("/.nx/");
|
|
45
|
+
/**
|
|
46
|
+
* Visits every `.tsx`/`.jsx` file under the workspace root and invokes the
|
|
47
|
+
* callback with the file path and its current contents. Files that don't
|
|
48
|
+
* include the marker substring are skipped, which makes large workspaces
|
|
49
|
+
* cheap to scan.
|
|
50
|
+
*/
|
|
51
|
+
const visitTsxFiles = (tree, marker, callback) => {
|
|
52
|
+
let touched = 0;
|
|
53
|
+
(0, devkit_1.visitNotIgnoredFiles)(tree, "/", filePath => {
|
|
54
|
+
if (!isTsxFile(filePath))
|
|
55
|
+
return;
|
|
56
|
+
if (isUnderNodeModules(filePath) || isUnderDist(filePath))
|
|
57
|
+
return;
|
|
58
|
+
const content = tree.read(filePath, "utf-8");
|
|
59
|
+
if (content === null)
|
|
60
|
+
return;
|
|
61
|
+
if (!content.includes(marker))
|
|
62
|
+
return;
|
|
63
|
+
const updated = callback(filePath, content);
|
|
64
|
+
if (updated !== null && updated !== undefined && updated !== content) {
|
|
65
|
+
tree.write(filePath, updated);
|
|
66
|
+
touched += 1;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
return touched;
|
|
70
|
+
};
|
|
71
|
+
exports.visitTsxFiles = visitTsxFiles;
|
|
72
|
+
/**
|
|
73
|
+
* Same as {@link visitTsxFiles} but also includes plain `.ts` files. Use this
|
|
74
|
+
* when the codemod also rewrites non-JSX files such as helpers or hooks.
|
|
75
|
+
*/
|
|
76
|
+
const visitTsFiles = (tree, marker, callback) => {
|
|
77
|
+
let touched = 0;
|
|
78
|
+
(0, devkit_1.visitNotIgnoredFiles)(tree, "/", filePath => {
|
|
79
|
+
if (!isTsFile(filePath))
|
|
80
|
+
return;
|
|
81
|
+
if (isUnderNodeModules(filePath) || isUnderDist(filePath))
|
|
82
|
+
return;
|
|
83
|
+
const content = tree.read(filePath, "utf-8");
|
|
84
|
+
if (content === null)
|
|
85
|
+
return;
|
|
86
|
+
if (!content.includes(marker))
|
|
87
|
+
return;
|
|
88
|
+
const updated = callback(filePath, content);
|
|
89
|
+
if (updated !== null && updated !== undefined && updated !== content) {
|
|
90
|
+
tree.write(filePath, updated);
|
|
91
|
+
touched += 1;
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
return touched;
|
|
95
|
+
};
|
|
96
|
+
exports.visitTsFiles = visitTsFiles;
|
|
97
|
+
/**
|
|
98
|
+
* Mapping of imported component aliases to their original names for a given
|
|
99
|
+
* package. For `import { IconButton as Btn } from "@trackunit/react-components"`
|
|
100
|
+
* this returns `{ Btn: "IconButton" }`.
|
|
101
|
+
*
|
|
102
|
+
* Returns `null` when the file imports nothing from the package, which lets
|
|
103
|
+
* callers short-circuit before parsing JSX.
|
|
104
|
+
*/
|
|
105
|
+
const getImportedAliases = (sourceFile, packageName) => {
|
|
106
|
+
const result = {};
|
|
107
|
+
let found = false;
|
|
108
|
+
for (const stmt of sourceFile.statements) {
|
|
109
|
+
if (!ts.isImportDeclaration(stmt))
|
|
110
|
+
continue;
|
|
111
|
+
const moduleSpecifier = stmt.moduleSpecifier;
|
|
112
|
+
if (!ts.isStringLiteral(moduleSpecifier))
|
|
113
|
+
continue;
|
|
114
|
+
if (moduleSpecifier.text !== packageName)
|
|
115
|
+
continue;
|
|
116
|
+
const namedBindings = stmt.importClause?.namedBindings;
|
|
117
|
+
if (namedBindings === undefined || !ts.isNamedImports(namedBindings))
|
|
118
|
+
continue;
|
|
119
|
+
for (const element of namedBindings.elements) {
|
|
120
|
+
const localName = element.name.text;
|
|
121
|
+
const importedName = element.propertyName?.text ?? localName;
|
|
122
|
+
result[localName] = importedName;
|
|
123
|
+
found = true;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return found ? result : null;
|
|
127
|
+
};
|
|
128
|
+
exports.getImportedAliases = getImportedAliases;
|
|
129
|
+
/**
|
|
130
|
+
* Returns the local alias used in this file for `originalName` when imported
|
|
131
|
+
* from `packageName`, or `null` if the component is not imported.
|
|
132
|
+
*/
|
|
133
|
+
const getLocalAliasFor = (sourceFile, packageName, originalName) => {
|
|
134
|
+
const aliases = (0, exports.getImportedAliases)(sourceFile, packageName);
|
|
135
|
+
if (aliases === null)
|
|
136
|
+
return null;
|
|
137
|
+
for (const [local, original] of Object.entries(aliases)) {
|
|
138
|
+
if (original === originalName)
|
|
139
|
+
return local;
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
};
|
|
143
|
+
exports.getLocalAliasFor = getLocalAliasFor;
|
|
144
|
+
/**
|
|
145
|
+
* Finds every JSX element whose tag name resolves to one of `tagNames`
|
|
146
|
+
* (typically the local aliases returned by {@link getImportedAliases}).
|
|
147
|
+
*
|
|
148
|
+
* The result intentionally includes both opening and full elements so callers
|
|
149
|
+
* can manipulate attributes (via `openingElement`) or wrap/replace the whole
|
|
150
|
+
* element (via `element`).
|
|
151
|
+
*/
|
|
152
|
+
const findJsxElements = (sourceFile, tagNames) => {
|
|
153
|
+
const matches = [];
|
|
154
|
+
const tagSet = new Set(tagNames);
|
|
155
|
+
const visit = (node) => {
|
|
156
|
+
if (ts.isJsxSelfClosingElement(node)) {
|
|
157
|
+
if (ts.isIdentifier(node.tagName) && tagSet.has(node.tagName.text)) {
|
|
158
|
+
matches.push({ openingElement: node, element: node });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
else if (ts.isJsxElement(node)) {
|
|
162
|
+
const opening = node.openingElement;
|
|
163
|
+
if (ts.isIdentifier(opening.tagName) && tagSet.has(opening.tagName.text)) {
|
|
164
|
+
matches.push({ openingElement: opening, element: node });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
ts.forEachChild(node, visit);
|
|
168
|
+
};
|
|
169
|
+
visit(sourceFile);
|
|
170
|
+
return matches;
|
|
171
|
+
};
|
|
172
|
+
exports.findJsxElements = findJsxElements;
|
|
173
|
+
/**
|
|
174
|
+
* Looks up a named JSX attribute on the opening element. Returns `null` when
|
|
175
|
+
* the attribute is missing or part of a spread attribute (which we cannot
|
|
176
|
+
* statically inspect).
|
|
177
|
+
*/
|
|
178
|
+
const findJsxAttribute = (openingElement, attributeName) => {
|
|
179
|
+
for (const attr of openingElement.attributes.properties) {
|
|
180
|
+
if (!ts.isJsxAttribute(attr))
|
|
181
|
+
continue;
|
|
182
|
+
if (!ts.isIdentifier(attr.name))
|
|
183
|
+
continue;
|
|
184
|
+
if (attr.name.text === attributeName)
|
|
185
|
+
return attr;
|
|
186
|
+
}
|
|
187
|
+
return null;
|
|
188
|
+
};
|
|
189
|
+
exports.findJsxAttribute = findJsxAttribute;
|
|
190
|
+
/**
|
|
191
|
+
* `true` when the element forwards a spread expression such as `{...rest}`,
|
|
192
|
+
* which means we can't be sure which attributes are actually set.
|
|
193
|
+
*/
|
|
194
|
+
const hasSpreadAttribute = (openingElement) => {
|
|
195
|
+
return openingElement.attributes.properties.some(prop => ts.isJsxSpreadAttribute(prop));
|
|
196
|
+
};
|
|
197
|
+
exports.hasSpreadAttribute = hasSpreadAttribute;
|
|
198
|
+
/**
|
|
199
|
+
* Parses the file as TSX so JSX is recognised. We do not need a full program
|
|
200
|
+
* here; tsquery's `ast` helper produces a script-kind source file which loses
|
|
201
|
+
* JSX context, so we go through `createSourceFile` directly.
|
|
202
|
+
*/
|
|
203
|
+
const parseTsx = (content, fileName = "file.tsx") => ts.createSourceFile(fileName, content, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
|
204
|
+
exports.parseTsx = parseTsx;
|
|
205
|
+
/**
|
|
206
|
+
* Helper for codemods to log a one-line summary at the end of a run. Migration
|
|
207
|
+
* runners aggregate logs per migration, so this keeps output focused.
|
|
208
|
+
*/
|
|
209
|
+
const logSummary = (migrationName, touched, manualReviewNeeded) => {
|
|
210
|
+
if (touched === 0) {
|
|
211
|
+
devkit_1.logger.info(` ${migrationName}: no files changed`);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const reviewSuffix = manualReviewNeeded !== undefined && manualReviewNeeded > 0
|
|
215
|
+
? ` (${manualReviewNeeded} location(s) need manual review)`
|
|
216
|
+
: "";
|
|
217
|
+
devkit_1.logger.info(` ${migrationName}: updated ${touched} file(s)${reviewSuffix}`);
|
|
218
|
+
};
|
|
219
|
+
exports.logSummary = logSummary;
|
|
220
|
+
/**
|
|
221
|
+
* Inserts an attribute string immediately after the tag name (so the new
|
|
222
|
+
* attribute appears first). Returns the resulting source content.
|
|
223
|
+
*
|
|
224
|
+
* The added attribute is rendered verbatim, so callers are responsible for
|
|
225
|
+
* including the leading space (e.g. ` title="Close"`).
|
|
226
|
+
*/
|
|
227
|
+
const insertAttributeIntoOpeningTag = (content, openingElement, attributeText) => {
|
|
228
|
+
const tagName = openingElement.tagName;
|
|
229
|
+
const insertPos = tagName.getEnd();
|
|
230
|
+
return `${content.slice(0, insertPos)} ${attributeText}${content.slice(insertPos)}`;
|
|
231
|
+
};
|
|
232
|
+
exports.insertAttributeIntoOpeningTag = insertAttributeIntoOpeningTag;
|
|
233
|
+
//# sourceMappingURL=jsx-utils.js.map
|
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.logManualReview = exports.moveNamedImports = void 0;
|
|
37
|
+
const devkit_1 = require("@nx/devkit");
|
|
38
|
+
const ts = __importStar(require("typescript"));
|
|
39
|
+
const jsx_utils_1 = require("./jsx-utils");
|
|
40
|
+
const MOCK_OBJECTS = new Set(["vi", "jest"]);
|
|
41
|
+
const MOCK_METHODS = new Set([
|
|
42
|
+
"mock",
|
|
43
|
+
"doMock",
|
|
44
|
+
"unmock",
|
|
45
|
+
"doUnmock",
|
|
46
|
+
"importActual",
|
|
47
|
+
"importMock",
|
|
48
|
+
"requireActual",
|
|
49
|
+
"requireMock",
|
|
50
|
+
]);
|
|
51
|
+
const toRenameMap = (names) => Array.isArray(names) ? new Map(names.map(name => [name, name])) : new Map(Object.entries(names));
|
|
52
|
+
const parseSource = (filePath, content) => ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true, filePath.endsWith(".ts") ? ts.ScriptKind.TS : ts.ScriptKind.TSX);
|
|
53
|
+
const isModule = (node, moduleName) => node !== undefined &&
|
|
54
|
+
(ts.isStringLiteral(node) || ts.isNoSubstitutionTemplateLiteral(node)) &&
|
|
55
|
+
node.text === moduleName;
|
|
56
|
+
/** Renders `moduleName` with the quote style of an existing specifier. */
|
|
57
|
+
const quoteLike = (specifier, moduleName) => {
|
|
58
|
+
const quote = specifier.getText().startsWith("'") ? "'" : '"';
|
|
59
|
+
return `${quote}${moduleName}${quote}`;
|
|
60
|
+
};
|
|
61
|
+
const renderSpecifier = ({ imported, local, isTypeOnly }, withTypePrefix) => `${withTypePrefix && isTypeOnly ? "type " : ""}${imported === local ? imported : `${imported} as ${local}`}`;
|
|
62
|
+
const statementTerminator = (statement) => (statement.getText().endsWith(";") ? ";" : "");
|
|
63
|
+
const lineOf = (sourceFile, node) => sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1;
|
|
64
|
+
/** `true` when `node` is a property-like key (`a.X`, `{ X: 1 }`, `<C X="" />`) rather than a reference to a binding. */
|
|
65
|
+
const isPropertyKey = (node) => {
|
|
66
|
+
const parent = node.parent;
|
|
67
|
+
if (ts.isPropertyAccessExpression(parent) || ts.isQualifiedName(parent)) {
|
|
68
|
+
return ts.isPropertyAccessExpression(parent) ? parent.name === node : parent.right === node;
|
|
69
|
+
}
|
|
70
|
+
if (ts.isBindingElement(parent))
|
|
71
|
+
return parent.propertyName === node;
|
|
72
|
+
if (ts.isPropertyAssignment(parent) ||
|
|
73
|
+
ts.isPropertyDeclaration(parent) ||
|
|
74
|
+
ts.isPropertySignature(parent) ||
|
|
75
|
+
ts.isMethodDeclaration(parent) ||
|
|
76
|
+
ts.isMethodSignature(parent) ||
|
|
77
|
+
ts.isGetAccessorDeclaration(parent) ||
|
|
78
|
+
ts.isSetAccessorDeclaration(parent) ||
|
|
79
|
+
ts.isEnumMember(parent) ||
|
|
80
|
+
ts.isJsxAttribute(parent)) {
|
|
81
|
+
return parent.name === node;
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
};
|
|
85
|
+
/** `true` when `node` declares a new binding (variable, parameter, function, class, type...). */
|
|
86
|
+
const isBindingDeclaration = (node) => {
|
|
87
|
+
const parent = node.parent;
|
|
88
|
+
if (ts.isBindingElement(parent))
|
|
89
|
+
return parent.name === node;
|
|
90
|
+
if (ts.isVariableDeclaration(parent) ||
|
|
91
|
+
ts.isParameter(parent) ||
|
|
92
|
+
ts.isFunctionDeclaration(parent) ||
|
|
93
|
+
ts.isFunctionExpression(parent) ||
|
|
94
|
+
ts.isClassDeclaration(parent) ||
|
|
95
|
+
ts.isClassExpression(parent) ||
|
|
96
|
+
ts.isInterfaceDeclaration(parent) ||
|
|
97
|
+
ts.isTypeAliasDeclaration(parent) ||
|
|
98
|
+
ts.isEnumDeclaration(parent) ||
|
|
99
|
+
ts.isTypeParameterDeclaration(parent) ||
|
|
100
|
+
ts.isImportSpecifier(parent) ||
|
|
101
|
+
ts.isImportClause(parent) ||
|
|
102
|
+
ts.isNamespaceImport(parent) ||
|
|
103
|
+
ts.isImportEqualsDeclaration(parent)) {
|
|
104
|
+
return parent.name === node;
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
};
|
|
108
|
+
/** Import and re-export statements are rewritten as a whole, so their identifiers are never edited one by one. */
|
|
109
|
+
const isInsideModuleStatement = (node) => ts.findAncestor(node, ancestor => ts.isImportDeclaration(ancestor) || (ts.isExportDeclaration(ancestor) && ancestor.moduleSpecifier !== undefined)) !== undefined;
|
|
110
|
+
const collectIdentifiers = (sourceFile) => {
|
|
111
|
+
const identifiers = [];
|
|
112
|
+
const visit = (node) => {
|
|
113
|
+
if (ts.isIdentifier(node))
|
|
114
|
+
identifiers.push(node);
|
|
115
|
+
ts.forEachChild(node, visit);
|
|
116
|
+
};
|
|
117
|
+
visit(sourceFile);
|
|
118
|
+
return identifiers;
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Edits that rename every reference to the local binding `oldName` to
|
|
122
|
+
* `newName`, or `null` when that can't be done safely (the new name is
|
|
123
|
+
* already used in the file, or `oldName` is redeclared and might be shadowed).
|
|
124
|
+
*/
|
|
125
|
+
const renameReferenceEdits = (sourceFile, identifiers, oldName, newName) => {
|
|
126
|
+
if (identifiers.some(id => id.text === newName))
|
|
127
|
+
return null;
|
|
128
|
+
const outside = identifiers.filter(id => id.text === oldName && !isInsideModuleStatement(id));
|
|
129
|
+
if (outside.some(isBindingDeclaration))
|
|
130
|
+
return null;
|
|
131
|
+
const edits = [];
|
|
132
|
+
for (const id of outside) {
|
|
133
|
+
if (isPropertyKey(id))
|
|
134
|
+
continue;
|
|
135
|
+
const start = id.getStart(sourceFile);
|
|
136
|
+
const end = id.getEnd();
|
|
137
|
+
const parent = id.parent;
|
|
138
|
+
if (ts.isShorthandPropertyAssignment(parent)) {
|
|
139
|
+
edits.push({ start, end, text: `${oldName}: ${newName}` });
|
|
140
|
+
}
|
|
141
|
+
else if (ts.isExportSpecifier(parent)) {
|
|
142
|
+
if (parent.propertyName === undefined)
|
|
143
|
+
edits.push({ start, end, text: `${newName} as ${oldName}` });
|
|
144
|
+
else if (parent.propertyName === id)
|
|
145
|
+
edits.push({ start, end, text: newName });
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
edits.push({ start, end, text: newName });
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return edits;
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Usages of `fromModule` the codemod can't rewrite statically. They are only
|
|
155
|
+
* reported when the file references a moved name at all, so e.g. a
|
|
156
|
+
* `vi.mock(fromModule, ...)` in a spec that never touches a moved name is not
|
|
157
|
+
* noise. `export *` is always reported: the moved names silently drop out of it.
|
|
158
|
+
*/
|
|
159
|
+
const findManualReviewItems = (sourceFile, identifiers, fromModule, toModule, renames) => {
|
|
160
|
+
const items = [];
|
|
161
|
+
const file = sourceFile.fileName;
|
|
162
|
+
const mentionsMovedName = identifiers.some(id => renames.has(id.text)) || sourceFile.text.split(/["'`]/).some(chunk => renames.has(chunk));
|
|
163
|
+
const movedList = [...renames.keys()].join(", ");
|
|
164
|
+
const hint = `moved names (${movedList}) now come from "${toModule}"`;
|
|
165
|
+
const report = (node, what) => {
|
|
166
|
+
items.push({ file, line: lineOf(sourceFile, node), reason: `${what} of "${fromModule}"; ${hint}` });
|
|
167
|
+
};
|
|
168
|
+
const visit = (node) => {
|
|
169
|
+
if (ts.isImportDeclaration(node) && isModule(node.moduleSpecifier, fromModule)) {
|
|
170
|
+
const bindings = node.importClause?.namedBindings;
|
|
171
|
+
if (bindings !== undefined && ts.isNamespaceImport(bindings) && mentionsMovedName) {
|
|
172
|
+
report(node, `namespace import \`* as ${bindings.name.text}\``);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
else if (ts.isExportDeclaration(node) && isModule(node.moduleSpecifier, fromModule)) {
|
|
176
|
+
if (node.exportClause === undefined)
|
|
177
|
+
report(node, "`export *`");
|
|
178
|
+
else if (ts.isNamespaceExport(node.exportClause))
|
|
179
|
+
report(node, "`export * as`");
|
|
180
|
+
}
|
|
181
|
+
else if (ts.isImportEqualsDeclaration(node) && mentionsMovedName) {
|
|
182
|
+
const reference = node.moduleReference;
|
|
183
|
+
if (ts.isExternalModuleReference(reference) && isModule(reference.expression, fromModule)) {
|
|
184
|
+
report(node, "`import = require()`");
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
else if (ts.isImportTypeNode(node) && mentionsMovedName) {
|
|
188
|
+
const argument = node.argument;
|
|
189
|
+
if (ts.isLiteralTypeNode(argument) && isModule(argument.literal, fromModule))
|
|
190
|
+
report(node, "`import()` type");
|
|
191
|
+
}
|
|
192
|
+
else if (ts.isCallExpression(node) && mentionsMovedName && isModule(node.arguments[0], fromModule)) {
|
|
193
|
+
const callee = node.expression;
|
|
194
|
+
if (callee.kind === ts.SyntaxKind.ImportKeyword) {
|
|
195
|
+
report(node, "dynamic import()");
|
|
196
|
+
}
|
|
197
|
+
else if (ts.isIdentifier(callee) && callee.text === "require") {
|
|
198
|
+
report(node, "require()");
|
|
199
|
+
}
|
|
200
|
+
else if (ts.isPropertyAccessExpression(callee) &&
|
|
201
|
+
ts.isIdentifier(callee.expression) &&
|
|
202
|
+
MOCK_OBJECTS.has(callee.expression.text) &&
|
|
203
|
+
MOCK_METHODS.has(callee.name.text)) {
|
|
204
|
+
report(node, `${callee.expression.text}.${callee.name.text}()`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
ts.forEachChild(node, visit);
|
|
208
|
+
};
|
|
209
|
+
visit(sourceFile);
|
|
210
|
+
return items;
|
|
211
|
+
};
|
|
212
|
+
const findTargetImport = (sourceFile, toModule, typeOnly) => {
|
|
213
|
+
for (const statement of sourceFile.statements) {
|
|
214
|
+
if (!ts.isImportDeclaration(statement) || !isModule(statement.moduleSpecifier, toModule))
|
|
215
|
+
continue;
|
|
216
|
+
const clause = statement.importClause;
|
|
217
|
+
if (clause === undefined || clause.isTypeOnly !== typeOnly)
|
|
218
|
+
continue;
|
|
219
|
+
const bindings = clause.namedBindings;
|
|
220
|
+
if (bindings === undefined || !ts.isNamedImports(bindings))
|
|
221
|
+
continue;
|
|
222
|
+
return { statement, elements: bindings.elements, additions: [] };
|
|
223
|
+
}
|
|
224
|
+
return null;
|
|
225
|
+
};
|
|
226
|
+
const specifierKey = ({ imported, local }) => `${imported} as ${local}`;
|
|
227
|
+
const specifierOf = (element, parentTypeOnly) => ({
|
|
228
|
+
imported: (element.propertyName ?? element.name).text,
|
|
229
|
+
local: element.name.text,
|
|
230
|
+
isTypeOnly: parentTypeOnly || element.isTypeOnly,
|
|
231
|
+
});
|
|
232
|
+
const rewriteFile = (filePath, content, { fromModule, toModule }, renames) => {
|
|
233
|
+
const sourceFile = parseSource(filePath, content);
|
|
234
|
+
const identifiers = collectIdentifiers(sourceFile);
|
|
235
|
+
const manualReview = findManualReviewItems(sourceFile, identifiers, fromModule, toModule, renames);
|
|
236
|
+
const edits = [];
|
|
237
|
+
const valueTarget = findTargetImport(sourceFile, toModule, false);
|
|
238
|
+
const typeTarget = findTargetImport(sourceFile, toModule, true);
|
|
239
|
+
const targetFor = (spec) => {
|
|
240
|
+
if (!spec.isTypeOnly)
|
|
241
|
+
return valueTarget;
|
|
242
|
+
return typeTarget ?? valueTarget;
|
|
243
|
+
};
|
|
244
|
+
const replaceStatement = (statement, kept, added) => {
|
|
245
|
+
const parts = [...(kept === null ? [] : [kept]), ...added];
|
|
246
|
+
const start = statement.getStart(sourceFile);
|
|
247
|
+
let end = statement.getEnd();
|
|
248
|
+
if (parts.length === 0) {
|
|
249
|
+
if (content.startsWith("\r\n", end))
|
|
250
|
+
end += 2;
|
|
251
|
+
else if (content.startsWith("\n", end))
|
|
252
|
+
end += 1;
|
|
253
|
+
const leading = content.slice(statement.getFullStart(), start);
|
|
254
|
+
if (leading.includes("eslint-disable")) {
|
|
255
|
+
manualReview.push({
|
|
256
|
+
file: filePath,
|
|
257
|
+
line: lineOf(sourceFile, statement),
|
|
258
|
+
reason: `removed import of "${fromModule}" had an eslint-disable comment, which now applies to the next line`,
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
edits.push({ start, end, text: parts.join("\n") });
|
|
263
|
+
};
|
|
264
|
+
const createdImports = new Map();
|
|
265
|
+
const deferredImports = [];
|
|
266
|
+
for (const statement of sourceFile.statements) {
|
|
267
|
+
if (ts.isImportDeclaration(statement) && isModule(statement.moduleSpecifier, fromModule)) {
|
|
268
|
+
const clause = statement.importClause;
|
|
269
|
+
const bindings = clause?.namedBindings;
|
|
270
|
+
if (clause === undefined || bindings === undefined || !ts.isNamedImports(bindings))
|
|
271
|
+
continue;
|
|
272
|
+
const moved = bindings.elements.filter(element => renames.has((element.propertyName ?? element.name).text));
|
|
273
|
+
if (moved.length === 0)
|
|
274
|
+
continue;
|
|
275
|
+
const kept = bindings.elements.filter(element => !moved.includes(element));
|
|
276
|
+
const createdKind = clause.isTypeOnly;
|
|
277
|
+
const addCreated = (spec) => {
|
|
278
|
+
const created = createdImports.get(createdKind) ?? { anchor: statement, specs: [] };
|
|
279
|
+
createdImports.set(createdKind, created);
|
|
280
|
+
if (!created.specs.some(existing => specifierKey(existing) === specifierKey(spec)))
|
|
281
|
+
created.specs.push(spec);
|
|
282
|
+
};
|
|
283
|
+
for (const element of moved) {
|
|
284
|
+
const original = specifierOf(element, clause.isTypeOnly);
|
|
285
|
+
const newName = renames.get(original.imported) ?? original.imported;
|
|
286
|
+
let local = original.local;
|
|
287
|
+
if (newName !== original.imported && original.local === original.imported) {
|
|
288
|
+
const renameEdits = renameReferenceEdits(sourceFile, identifiers, original.local, newName);
|
|
289
|
+
if (renameEdits !== null) {
|
|
290
|
+
edits.push(...renameEdits);
|
|
291
|
+
local = newName;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
const spec = { imported: newName, local, isTypeOnly: original.isTypeOnly };
|
|
295
|
+
const target = targetFor(spec);
|
|
296
|
+
if (target === null) {
|
|
297
|
+
addCreated(spec);
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
const existing = new Set([
|
|
301
|
+
...target.elements.map(e => specifierKey(specifierOf(e, false))),
|
|
302
|
+
...target.additions.map(specifierKey),
|
|
303
|
+
]);
|
|
304
|
+
if (!existing.has(specifierKey(spec)))
|
|
305
|
+
target.additions.push(spec);
|
|
306
|
+
}
|
|
307
|
+
const terminator = statementTerminator(statement);
|
|
308
|
+
const defaultName = clause.name?.text;
|
|
309
|
+
const keptText = kept.length === 0 && defaultName === undefined
|
|
310
|
+
? null
|
|
311
|
+
: `import ${clause.isTypeOnly ? "type " : ""}${[
|
|
312
|
+
...(defaultName === undefined ? [] : [defaultName]),
|
|
313
|
+
...(kept.length === 0 ? [] : [`{ ${kept.map(e => e.getText(sourceFile)).join(", ")} }`]),
|
|
314
|
+
].join(", ")} from ${statement.moduleSpecifier.getText(sourceFile)}${terminator}`;
|
|
315
|
+
deferredImports.push({ statement, keptText });
|
|
316
|
+
}
|
|
317
|
+
else if (ts.isExportDeclaration(statement) &&
|
|
318
|
+
isModule(statement.moduleSpecifier, fromModule) &&
|
|
319
|
+
statement.moduleSpecifier !== undefined &&
|
|
320
|
+
statement.exportClause !== undefined &&
|
|
321
|
+
ts.isNamedExports(statement.exportClause)) {
|
|
322
|
+
const elements = statement.exportClause.elements;
|
|
323
|
+
const moved = elements.filter(element => renames.has((element.propertyName ?? element.name).text));
|
|
324
|
+
if (moved.length === 0)
|
|
325
|
+
continue;
|
|
326
|
+
const kept = elements.filter(element => !moved.includes(element));
|
|
327
|
+
const typePrefix = statement.isTypeOnly ? "type " : "";
|
|
328
|
+
const terminator = statementTerminator(statement);
|
|
329
|
+
const specs = moved.map(element => {
|
|
330
|
+
const imported = (element.propertyName ?? element.name).text;
|
|
331
|
+
return renderSpecifier({ imported: renames.get(imported) ?? imported, local: element.name.text, isTypeOnly: element.isTypeOnly }, true);
|
|
332
|
+
});
|
|
333
|
+
replaceStatement(statement, kept.length === 0
|
|
334
|
+
? null
|
|
335
|
+
: `export ${typePrefix}{ ${kept.map(e => e.getText(sourceFile)).join(", ")} } from ${statement.moduleSpecifier.getText(sourceFile)}${terminator}`, [
|
|
336
|
+
`export ${typePrefix}{ ${specs.join(", ")} } from ${quoteLike(statement.moduleSpecifier, toModule)}${terminator}`,
|
|
337
|
+
]);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
for (const { statement, keptText } of deferredImports) {
|
|
341
|
+
const createdText = [...createdImports.entries()]
|
|
342
|
+
.filter(([, created]) => created.anchor === statement)
|
|
343
|
+
.map(([typeOnly, created]) => `import ${typeOnly ? "type " : ""}{ ${created.specs
|
|
344
|
+
.map(spec => renderSpecifier(spec, !typeOnly))
|
|
345
|
+
.join(", ")} } from ${quoteLike(statement.moduleSpecifier, toModule)}${statementTerminator(statement)}`);
|
|
346
|
+
replaceStatement(statement, keptText, createdText);
|
|
347
|
+
}
|
|
348
|
+
for (const target of [valueTarget, typeTarget]) {
|
|
349
|
+
if (target === null || target.additions.length === 0)
|
|
350
|
+
continue;
|
|
351
|
+
const typeOnly = target.statement.importClause?.isTypeOnly === true;
|
|
352
|
+
const bindings = target.statement.importClause?.namedBindings;
|
|
353
|
+
if (bindings === undefined)
|
|
354
|
+
continue;
|
|
355
|
+
const merged = [
|
|
356
|
+
...target.elements.map(e => e.getText(sourceFile)),
|
|
357
|
+
...target.additions.map(spec => renderSpecifier(spec, !typeOnly)),
|
|
358
|
+
];
|
|
359
|
+
edits.push({ start: bindings.getStart(sourceFile), end: bindings.getEnd(), text: `{ ${merged.join(", ")} }` });
|
|
360
|
+
}
|
|
361
|
+
if (edits.length === 0)
|
|
362
|
+
return { content, manualReview };
|
|
363
|
+
edits.sort((a, b) => b.start - a.start);
|
|
364
|
+
let updated = content;
|
|
365
|
+
for (const { start, end, text } of edits) {
|
|
366
|
+
updated = updated.slice(0, start) + text + updated.slice(end);
|
|
367
|
+
}
|
|
368
|
+
return { content: updated, manualReview };
|
|
369
|
+
};
|
|
370
|
+
/**
|
|
371
|
+
* Moves named imports and named re-exports of `names` from `fromModule` to
|
|
372
|
+
* `toModule` across every `.ts`/`.tsx`/`.jsx` file in the tree.
|
|
373
|
+
*
|
|
374
|
+
* - Mixed imports are split; names that didn't move stay on `fromModule`.
|
|
375
|
+
* - Aliases (`as`), `import type` and inline `type` modifiers are preserved.
|
|
376
|
+
* - Moved names are merged into an existing named import of `toModule`, or
|
|
377
|
+
* else into one new import per kind (value / `import type`).
|
|
378
|
+
* - With a rename record, an unaliased import has its references renamed too;
|
|
379
|
+
* when that isn't safe (name clash or shadowing) it is imported as
|
|
380
|
+
* `{ NewName as OldName }` instead. Re-exports keep their public name.
|
|
381
|
+
* - Namespace imports, `export *`, dynamic `import()`, `require()` and
|
|
382
|
+
* `vi`/`jest` mocks of `fromModule` are left untouched and returned in
|
|
383
|
+
* `manualReview`, never skipped silently.
|
|
384
|
+
*
|
|
385
|
+
* Synchronous and format-free, so it can run inside a `run-migrations`
|
|
386
|
+
* migration; callers format changed files themselves (e.g. `formatFiles`).
|
|
387
|
+
*/
|
|
388
|
+
const moveNamedImports = (tree, options) => {
|
|
389
|
+
const renames = toRenameMap(options.names);
|
|
390
|
+
const changedFiles = [];
|
|
391
|
+
const manualReview = [];
|
|
392
|
+
(0, jsx_utils_1.visitTsFiles)(tree, options.fromModule, (filePath, content) => {
|
|
393
|
+
const result = rewriteFile(filePath, content, options, renames);
|
|
394
|
+
manualReview.push(...result.manualReview);
|
|
395
|
+
if (result.content !== content)
|
|
396
|
+
changedFiles.push(filePath);
|
|
397
|
+
return result.content;
|
|
398
|
+
});
|
|
399
|
+
return { changedFiles, manualReview };
|
|
400
|
+
};
|
|
401
|
+
exports.moveNamedImports = moveNamedImports;
|
|
402
|
+
/** Logs each manual-review item as a warning, prefixed with the migration name. */
|
|
403
|
+
const logManualReview = (migrationName, items) => {
|
|
404
|
+
for (const { file, line, reason } of items) {
|
|
405
|
+
devkit_1.logger.warn(` ${migrationName}: manual review needed at ${file}:${line} - ${reason}`);
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
exports.logManualReview = logManualReview;
|
|
409
|
+
//# sourceMappingURL=move-named-imports.js.map
|