@cornerstonejs/codemods 6.0.0-beta.1 → 6.0.0-beta.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/.oxlintrc.json +4 -0
- package/package.json +1 -1
- package/registry/cornerstone3d/6/README.md +46 -0
- package/registry/cornerstone3d/6/classes-and-types/README.md +16 -0
- package/registry/cornerstone3d/6/classes-and-types/scripts/classes-and-types.ts +32 -90
- package/registry/cornerstone3d/6/classes-and-types/scripts/lib.ts +864 -16
- package/registry/cornerstone3d/6/classes-and-types/tests/tsx/jsx-in-a-js-file/expected.js +31 -0
- package/registry/cornerstone3d/6/classes-and-types/tests/tsx/jsx-in-a-js-file/input.js +31 -0
- package/registry/cornerstone3d/6/classes-and-types/tests/tsx/jsx-in-a-js-file/metrics.json +46 -0
- package/registry/cornerstone3d/6/classes-and-types/tests/tsx/not-analysed-a-name-declared-twice/expected.tsx +34 -0
- package/registry/cornerstone3d/6/classes-and-types/tests/tsx/not-analysed-a-name-declared-twice/input.tsx +34 -0
- package/registry/cornerstone3d/6/classes-and-types/tests/tsx/not-analysed-a-name-declared-twice/metrics.json +12 -0
- package/registry/cornerstone3d/6/classes-and-types/tests/typescript/positive-classes/metrics.json +10 -0
- package/registry/cornerstone3d/6/classes-and-types/tests/typescript/renamed-classes-make-two-operands-one/expected.ts +61 -0
- package/registry/cornerstone3d/6/classes-and-types/tests/typescript/renamed-classes-make-two-operands-one/input.ts +65 -0
- package/registry/cornerstone3d/6/classes-and-types/tests/typescript/renamed-classes-make-two-operands-one/metrics.json +36 -0
- package/registry/cornerstone3d/6/display-sets/README.md +4 -0
- package/registry/cornerstone3d/6/display-sets/scripts/display-sets.ts +4 -2
- package/registry/cornerstone3d/6/display-sets/scripts/lib.ts +864 -16
- package/registry/cornerstone3d/6/display-sets/tests/tsx/jsx-in-a-js-file/expected.js +45 -0
- package/registry/cornerstone3d/6/display-sets/tests/tsx/jsx-in-a-js-file/input.js +39 -0
- package/registry/cornerstone3d/6/display-sets/tests/tsx/jsx-in-a-js-file/metrics.json +84 -0
- package/registry/cornerstone3d/6/display-sets/tests/tsx/not-analysed-a-name-declared-twice/expected.tsx +42 -0
- package/registry/cornerstone3d/6/display-sets/tests/tsx/not-analysed-a-name-declared-twice/input.tsx +42 -0
- package/registry/cornerstone3d/6/display-sets/tests/tsx/not-analysed-a-name-declared-twice/metrics.json +12 -0
- package/registry/cornerstone3d/6/events/README.md +4 -0
- package/registry/cornerstone3d/6/events/scripts/events.ts +5 -3
- package/registry/cornerstone3d/6/events/scripts/lib.ts +864 -16
- package/registry/cornerstone3d/6/events/tests/tsx/jsx-in-a-js-file/expected.js +18 -0
- package/registry/cornerstone3d/6/events/tests/tsx/jsx-in-a-js-file/input.js +18 -0
- package/registry/cornerstone3d/6/events/tests/tsx/jsx-in-a-js-file/metrics.json +38 -0
- package/registry/cornerstone3d/6/events/tests/tsx/not-analysed-a-name-declared-twice/expected.tsx +21 -0
- package/registry/cornerstone3d/6/events/tests/tsx/not-analysed-a-name-declared-twice/input.tsx +21 -0
- package/registry/cornerstone3d/6/events/tests/tsx/not-analysed-a-name-declared-twice/metrics.json +12 -0
- package/registry/cornerstone3d/6/settings/README.md +4 -0
- package/registry/cornerstone3d/6/settings/scripts/lib.ts +864 -16
- package/registry/cornerstone3d/6/settings/scripts/settings.ts +5 -3
- package/registry/cornerstone3d/6/settings/tests/tsx/jsx-in-a-js-file/expected.js +18 -0
- package/registry/cornerstone3d/6/settings/tests/tsx/jsx-in-a-js-file/input.js +18 -0
- package/registry/cornerstone3d/6/settings/tests/tsx/jsx-in-a-js-file/metrics.json +46 -0
- package/registry/cornerstone3d/6/settings/tests/tsx/not-analysed-a-name-declared-twice/expected.tsx +21 -0
- package/registry/cornerstone3d/6/settings/tests/tsx/not-analysed-a-name-declared-twice/input.tsx +21 -0
- package/registry/cornerstone3d/6/settings/tests/tsx/not-analysed-a-name-declared-twice/metrics.json +12 -0
- package/registry/cornerstone3d/6/tools/README.md +5 -0
- package/registry/cornerstone3d/6/tools/scripts/lib.ts +864 -16
- package/registry/cornerstone3d/6/tools/scripts/tools.ts +6 -3
- package/registry/cornerstone3d/6/tools/tests/tsx/jsx-in-a-js-file/expected.js +35 -0
- package/registry/cornerstone3d/6/tools/tests/tsx/jsx-in-a-js-file/input.js +35 -0
- package/registry/cornerstone3d/6/tools/tests/tsx/jsx-in-a-js-file/metrics.json +52 -0
- package/registry/cornerstone3d/6/tools/tests/tsx/not-analysed-a-name-declared-twice/expected.tsx +38 -0
- package/registry/cornerstone3d/6/tools/tests/tsx/not-analysed-a-name-declared-twice/input.tsx +38 -0
- package/registry/cornerstone3d/6/tools/tests/tsx/not-analysed-a-name-declared-twice/metrics.json +12 -0
- package/registry/cornerstone3d/6/viewport-members/README.md +5 -0
- package/registry/cornerstone3d/6/viewport-members/scripts/lib.ts +864 -16
- package/registry/cornerstone3d/6/viewport-members/scripts/viewport-members.ts +5 -3
- package/registry/cornerstone3d/6/viewport-members/tests/tsx/jsx-in-a-js-file/expected.js +41 -0
- package/registry/cornerstone3d/6/viewport-members/tests/tsx/jsx-in-a-js-file/input.js +41 -0
- package/registry/cornerstone3d/6/viewport-members/tests/tsx/jsx-in-a-js-file/metrics.json +108 -0
- package/registry/cornerstone3d/6/viewport-members/tests/tsx/not-analysed-a-name-declared-twice/expected.tsx +44 -0
- package/registry/cornerstone3d/6/viewport-members/tests/tsx/not-analysed-a-name-declared-twice/input.tsx +44 -0
- package/registry/cornerstone3d/6/viewport-members/tests/tsx/not-analysed-a-name-declared-twice/metrics.json +12 -0
- package/registry/cornerstone3d/6/viewport-types/README.md +35 -0
- package/registry/cornerstone3d/6/viewport-types/scripts/lib.ts +864 -16
- package/registry/cornerstone3d/6/viewport-types/scripts/viewport-types.ts +192 -4
- package/registry/cornerstone3d/6/viewport-types/tests/tsx/jsx-in-a-js-file/expected.js +76 -0
- package/registry/cornerstone3d/6/viewport-types/tests/tsx/jsx-in-a-js-file/input.js +76 -0
- package/registry/cornerstone3d/6/viewport-types/tests/tsx/jsx-in-a-js-file/metrics.json +102 -0
- package/registry/cornerstone3d/6/viewport-types/tests/tsx/not-analysed-a-name-declared-twice/expected.tsx +79 -0
- package/registry/cornerstone3d/6/viewport-types/tests/tsx/not-analysed-a-name-declared-twice/input.tsx +79 -0
- package/registry/cornerstone3d/6/viewport-types/tests/tsx/not-analysed-a-name-declared-twice/metrics.json +12 -0
- package/registry/cornerstone3d/6/viewport-types/tests/tsx/not-analysed-a-name-declared-twice-in-a-js-file/expected.js +78 -0
- package/registry/cornerstone3d/6/viewport-types/tests/tsx/not-analysed-a-name-declared-twice-in-a-js-file/input.js +78 -0
- package/registry/cornerstone3d/6/viewport-types/tests/tsx/not-analysed-a-name-declared-twice-in-a-js-file/metrics.json +12 -0
- package/registry/cornerstone3d/6/viewport-types/tests/typescript/ohif-returns-of-two-types-become-one/expected.ts +173 -0
- package/registry/cornerstone3d/6/viewport-types/tests/typescript/ohif-returns-of-two-types-become-one/input.ts +173 -0
- package/registry/cornerstone3d/6/viewport-types/tests/typescript/ohif-returns-of-two-types-become-one/metrics.json +166 -0
- package/registry/cornerstone3d/6/viewport-types/tests/typescript/ohif-two-operands-become-one/expected.ts +103 -0
- package/registry/cornerstone3d/6/viewport-types/tests/typescript/ohif-two-operands-become-one/input.ts +103 -0
- package/registry/cornerstone3d/6/viewport-types/tests/typescript/ohif-two-operands-become-one/metrics.json +78 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Edit, SgNode, SgRoot } from 'codemod:ast-grep';
|
|
1
|
+
import type { Codemod, Edit, SgNode, SgRoot } from 'codemod:ast-grep';
|
|
2
2
|
import type TSX from 'codemod:ast-grep/langs/tsx';
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -13,6 +13,51 @@ export type Node = SgNode<TSX>;
|
|
|
13
13
|
/** The package whose names the transforms rewrite. */
|
|
14
14
|
export const CORE_PACKAGE = '@cornerstonejs/core';
|
|
15
15
|
|
|
16
|
+
/** The extensions of the files that hold JavaScript without types. */
|
|
17
|
+
const JAVASCRIPT_FILE = /\.(js|jsx|mjs|cjs)$/;
|
|
18
|
+
|
|
19
|
+
/** The part of an error of the tool's semantic analysis before its reason. */
|
|
20
|
+
const PARSE_FAILURE = /^[^]*Failed to parse file '[^]*?': /;
|
|
21
|
+
|
|
22
|
+
/** The node kinds of a name that can refer to a binding. */
|
|
23
|
+
const REFERENCE_KINDS = [
|
|
24
|
+
'identifier',
|
|
25
|
+
'shorthand_property_identifier',
|
|
26
|
+
'shorthand_property_identifier_pattern',
|
|
27
|
+
'type_identifier',
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
/** The node kinds of a name that can declare a binding. */
|
|
31
|
+
const BINDING_KINDS = [
|
|
32
|
+
'identifier',
|
|
33
|
+
'shorthand_property_identifier_pattern',
|
|
34
|
+
'type_identifier',
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
/** The node kinds of a JSX tag, whose lower-case name is an element. */
|
|
38
|
+
const JSX_TAG_KINDS = new Set([
|
|
39
|
+
'jsx_opening_element',
|
|
40
|
+
'jsx_closing_element',
|
|
41
|
+
'jsx_self_closing_element',
|
|
42
|
+
]);
|
|
43
|
+
|
|
44
|
+
/** The node kinds that hold the name of a parameter. */
|
|
45
|
+
const PARAMETER_KINDS = new Set([
|
|
46
|
+
'required_parameter',
|
|
47
|
+
'optional_parameter',
|
|
48
|
+
'formal_parameters',
|
|
49
|
+
'arrow_function',
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The files of this run that the tool's semantic analysis could not read.
|
|
54
|
+
* Their names are looked up on the syntax tree.
|
|
55
|
+
*/
|
|
56
|
+
const syntaxFiles = new Set<string>();
|
|
57
|
+
|
|
58
|
+
/** The declarations of each name of the file in work, by file and name. */
|
|
59
|
+
const bindingsByName = new Map<string, Declaration[]>();
|
|
60
|
+
|
|
16
61
|
/** The longest line that a regenerated import statement keeps on one line. */
|
|
17
62
|
const IMPORT_LINE_LENGTH = 80;
|
|
18
63
|
|
|
@@ -178,6 +223,74 @@ const LOOSE_PARENTS = new Set([
|
|
|
178
223
|
'ternary_expression',
|
|
179
224
|
]);
|
|
180
225
|
|
|
226
|
+
/**
|
|
227
|
+
* The expressions around a value that keep it: parentheses, casts and
|
|
228
|
+
* non-null assertions.
|
|
229
|
+
*/
|
|
230
|
+
const KEEPING_KINDS = new Set([
|
|
231
|
+
'parenthesized_expression',
|
|
232
|
+
'as_expression',
|
|
233
|
+
'satisfies_expression',
|
|
234
|
+
'non_null_expression',
|
|
235
|
+
'type_assertion',
|
|
236
|
+
]);
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* The parents of a name that write it in another way than `name = value`:
|
|
240
|
+
* an operator assignment, `++` or `--`, a `for in` or `for of`, or a
|
|
241
|
+
* destructuring target.
|
|
242
|
+
*/
|
|
243
|
+
const OTHER_WRITES = new Set([
|
|
244
|
+
'augmented_assignment_expression',
|
|
245
|
+
'update_expression',
|
|
246
|
+
'for_in_statement',
|
|
247
|
+
'object_pattern',
|
|
248
|
+
'array_pattern',
|
|
249
|
+
'pair_pattern',
|
|
250
|
+
'assignment_pattern',
|
|
251
|
+
'object_assignment_pattern',
|
|
252
|
+
'rest_pattern',
|
|
253
|
+
]);
|
|
254
|
+
|
|
255
|
+
/** The operators whose operands a chain of tests joins. */
|
|
256
|
+
const CHAIN_OPERATORS = new Set(['||', '&&']);
|
|
257
|
+
|
|
258
|
+
/** The nodes whose text is compared as one token. */
|
|
259
|
+
const TOKEN_KINDS = new Set(['string', 'template_string', 'regex', 'number']);
|
|
260
|
+
|
|
261
|
+
/** The nodes that only TypeScript reads, wherever they stand. */
|
|
262
|
+
const TYPE_PART_KINDS = new Set(['type_arguments', 'type_annotation']);
|
|
263
|
+
|
|
264
|
+
/** What to read at two operands that a run makes the same, by their place. */
|
|
265
|
+
const MERGED_NOTES: Record<MergedPlace, string> = {
|
|
266
|
+
'||': 'two operands of one || are the same now, read what each tested in 5.x',
|
|
267
|
+
'&&': 'two operands of one && are the same now, read what each tested in 5.x',
|
|
268
|
+
case: 'two case labels of one switch are the same now, read what each chose in 5.x',
|
|
269
|
+
includes:
|
|
270
|
+
'two entries of one list used with includes are the same now, read what each named in 5.x',
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Where two operands stand: in a chain of `||` or of `&&`, as `case` labels
|
|
275
|
+
* of one `switch`, or as entries of an array read with `includes`.
|
|
276
|
+
*/
|
|
277
|
+
export type MergedPlace = '||' | '&&' | 'case' | 'includes';
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* An operand that the edits of a run make the same expression as an operand
|
|
281
|
+
* before it in the same place, where the two differed.
|
|
282
|
+
*/
|
|
283
|
+
export interface MergedOperand {
|
|
284
|
+
/** The later of the two operands. */
|
|
285
|
+
node: Node;
|
|
286
|
+
/** Where the two operands stand. */
|
|
287
|
+
place: MergedPlace;
|
|
288
|
+
/** What a person reads there, for the rule of the report. */
|
|
289
|
+
note: string;
|
|
290
|
+
/** The texts that the edits write into the two operands. */
|
|
291
|
+
inserted: string[];
|
|
292
|
+
}
|
|
293
|
+
|
|
181
294
|
/** One named specifier of an import statement. */
|
|
182
295
|
interface ImportSpecifierInfo {
|
|
183
296
|
name: string;
|
|
@@ -212,6 +325,17 @@ interface Declaration {
|
|
|
212
325
|
scope: Node;
|
|
213
326
|
}
|
|
214
327
|
|
|
328
|
+
/** The declaration that a name refers to, as the tool answers it. */
|
|
329
|
+
export interface Definition {
|
|
330
|
+
kind: string;
|
|
331
|
+
node: Node;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** A metric of the tool, as the transforms use it. */
|
|
335
|
+
interface Metric {
|
|
336
|
+
increment(cardinality?: Record<string, string>): void;
|
|
337
|
+
}
|
|
338
|
+
|
|
215
339
|
/**
|
|
216
340
|
* Returns the module that an import statement reads from.
|
|
217
341
|
*/
|
|
@@ -957,6 +1081,291 @@ export function bindsAt(rootNode: Node, declaration: Node, at: Node): boolean {
|
|
|
957
1081
|
);
|
|
958
1082
|
}
|
|
959
1083
|
|
|
1084
|
+
/**
|
|
1085
|
+
* Returns the reason of an error without the path of the file, in one line.
|
|
1086
|
+
*/
|
|
1087
|
+
function failureReason(error: unknown): string {
|
|
1088
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1089
|
+
|
|
1090
|
+
return message.replace(PARSE_FAILURE, '').split('\n')[0].trim();
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
/**
|
|
1094
|
+
* Notes that the tool's semantic analysis could not read a file. The names
|
|
1095
|
+
* of a JavaScript file are looked up on the syntax tree from then on. Any
|
|
1096
|
+
* other file is not analysed: the error goes on to `analysed`.
|
|
1097
|
+
*/
|
|
1098
|
+
function leaveTool(file: string, error: unknown): void {
|
|
1099
|
+
if (!JAVASCRIPT_FILE.test(file)) {
|
|
1100
|
+
throw new Error(failureReason(error));
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
syntaxFiles.add(file);
|
|
1104
|
+
}
|
|
1105
|
+
|
|
1106
|
+
/**
|
|
1107
|
+
* Returns the outermost node that covers the same text as a node. The tool
|
|
1108
|
+
* answers with this node: the import specifier of a name without an alias,
|
|
1109
|
+
* the parameter of a plain name, the export specifier of an exported name.
|
|
1110
|
+
*/
|
|
1111
|
+
function outermost(node: Node): Node {
|
|
1112
|
+
let current = node;
|
|
1113
|
+
let parent = current.parent();
|
|
1114
|
+
|
|
1115
|
+
while (parent && contains(current, parent)) {
|
|
1116
|
+
current = parent;
|
|
1117
|
+
parent = current.parent();
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
return current;
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
/**
|
|
1124
|
+
* Returns the node that bounds the scope of a name that a node declares, as
|
|
1125
|
+
* `declarationScope` does. The left side of a `for in` or a `for of` without
|
|
1126
|
+
* `const`, `let` or `var` writes a name and declares none.
|
|
1127
|
+
*/
|
|
1128
|
+
function bindingScope(node: Node): Node | undefined {
|
|
1129
|
+
const holder = bindingHolder(node);
|
|
1130
|
+
|
|
1131
|
+
if (holder?.kind() === 'for_in_statement' && !holder.field('kind')) {
|
|
1132
|
+
return undefined;
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
return declarationScope(node);
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
/**
|
|
1139
|
+
* Returns the declarations of a name in a file, each with its scope, with
|
|
1140
|
+
* the names of classes. The answer is kept for the file in work.
|
|
1141
|
+
*/
|
|
1142
|
+
function bindingsOf(rootNode: Node, name: string): Declaration[] {
|
|
1143
|
+
const key = `${rootNode.getRoot().filename()}\n${name}`;
|
|
1144
|
+
const kept = bindingsByName.get(key);
|
|
1145
|
+
|
|
1146
|
+
if (kept) {
|
|
1147
|
+
return kept;
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
const found: Declaration[] = [];
|
|
1151
|
+
|
|
1152
|
+
for (const node of rootNode.findAll({
|
|
1153
|
+
rule: {
|
|
1154
|
+
any: BINDING_KINDS.map((kind) => ({ kind })),
|
|
1155
|
+
regex: `^${escapeName(name)}$`,
|
|
1156
|
+
},
|
|
1157
|
+
})) {
|
|
1158
|
+
const scope = bindingScope(node);
|
|
1159
|
+
|
|
1160
|
+
if (scope) {
|
|
1161
|
+
found.push({ node, scope });
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
bindingsByName.set(key, found);
|
|
1166
|
+
|
|
1167
|
+
return found;
|
|
1168
|
+
}
|
|
1169
|
+
|
|
1170
|
+
/**
|
|
1171
|
+
* Tells whether a declaration may repeat a name of its scope: a `var`, a
|
|
1172
|
+
* function declaration or a parameter.
|
|
1173
|
+
*/
|
|
1174
|
+
function mayRepeat(declaration: Node): boolean {
|
|
1175
|
+
const holder = bindingHolder(declaration);
|
|
1176
|
+
const kind = holder?.kind() ?? '';
|
|
1177
|
+
|
|
1178
|
+
return (
|
|
1179
|
+
PARAMETER_KINDS.has(kind) ||
|
|
1180
|
+
kind === 'function_declaration' ||
|
|
1181
|
+
kind === 'generator_function_declaration' ||
|
|
1182
|
+
(kind === 'variable_declarator' &&
|
|
1183
|
+
holder.parent()?.kind() === 'variable_declaration')
|
|
1184
|
+
);
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
/**
|
|
1188
|
+
* Tells whether a name stands where JavaScript reads a binding. The alias of
|
|
1189
|
+
* an export, a name of an export from another module and the lower-case
|
|
1190
|
+
* name of a JSX tag do not.
|
|
1191
|
+
*/
|
|
1192
|
+
function readsBinding(node: Node): boolean {
|
|
1193
|
+
const parent = node.parent();
|
|
1194
|
+
const kind = parent?.kind() ?? '';
|
|
1195
|
+
|
|
1196
|
+
if (JSX_TAG_KINDS.has(kind)) {
|
|
1197
|
+
return !/^[a-z]/.test(node.text());
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
if (kind !== 'export_specifier' && kind !== 'namespace_export') {
|
|
1201
|
+
return true;
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
const statement = parent
|
|
1205
|
+
.ancestors()
|
|
1206
|
+
.find((ancestor) => ancestor.kind() === 'export_statement');
|
|
1207
|
+
|
|
1208
|
+
return (
|
|
1209
|
+
kind === 'export_specifier' &&
|
|
1210
|
+
parent.field('alias')?.id() !== node.id() &&
|
|
1211
|
+
!statement?.field('source')
|
|
1212
|
+
);
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
/**
|
|
1216
|
+
* Returns the declared name that a name refers to at its place, read from
|
|
1217
|
+
* the syntax tree: the declaration of the innermost scope that holds the
|
|
1218
|
+
* node. The name of a function expression gives way to a parameter or a
|
|
1219
|
+
* variable of that function. Throws when two declarations of one scope
|
|
1220
|
+
* cannot both stand, as the binding is then unknown.
|
|
1221
|
+
*/
|
|
1222
|
+
function bindingAt(node: Node): Node | undefined {
|
|
1223
|
+
const name = node.text();
|
|
1224
|
+
const ownScope = bindingScope(node);
|
|
1225
|
+
|
|
1226
|
+
if (ownScope && NAMED_EXPRESSION_KINDS.has(node.parent()?.kind() ?? '')) {
|
|
1227
|
+
return node;
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
const inScope = bindingsOf(node.getRoot().root(), name).filter(({ scope }) =>
|
|
1231
|
+
contains(scope, node)
|
|
1232
|
+
);
|
|
1233
|
+
const innermost = inScope.find(({ scope }) =>
|
|
1234
|
+
ownScope
|
|
1235
|
+
? scope.id() === ownScope.id()
|
|
1236
|
+
: inScope.every((other) => contains(other.scope, scope))
|
|
1237
|
+
);
|
|
1238
|
+
|
|
1239
|
+
if (!innermost) {
|
|
1240
|
+
return undefined;
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
const sameScope = inScope.filter(
|
|
1244
|
+
({ scope }) => scope.id() === innermost.scope.id()
|
|
1245
|
+
);
|
|
1246
|
+
const own = sameScope.filter(
|
|
1247
|
+
({ node: declaration }) =>
|
|
1248
|
+
!NAMED_EXPRESSION_KINDS.has(declaration.parent()?.kind() ?? '')
|
|
1249
|
+
);
|
|
1250
|
+
const candidates = own.length ? own : sameScope;
|
|
1251
|
+
|
|
1252
|
+
if (
|
|
1253
|
+
candidates.length > 1 &&
|
|
1254
|
+
!candidates.every(({ node: declaration }) => mayRepeat(declaration))
|
|
1255
|
+
) {
|
|
1256
|
+
throw new Error(`${name} is declared twice in one scope`);
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
return candidates[0].node;
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
/**
|
|
1263
|
+
* Returns the definition of a name read from the syntax tree, in the shape
|
|
1264
|
+
* of the tool's answer: a name of an import statement gives its specifier
|
|
1265
|
+
* with the kind `import`, any other name its declaration with the kind
|
|
1266
|
+
* `local`. A second declaration of a name gives nothing.
|
|
1267
|
+
*/
|
|
1268
|
+
function syntaxDefinition(node: Node): Definition | undefined {
|
|
1269
|
+
const ancestors = node.ancestors();
|
|
1270
|
+
|
|
1271
|
+
if (ancestors.some((ancestor) => ancestor.kind() === 'import_statement')) {
|
|
1272
|
+
const specifier = ancestors.find(
|
|
1273
|
+
(ancestor) =>
|
|
1274
|
+
ancestor.kind() === 'import_specifier' ||
|
|
1275
|
+
ancestor.kind() === 'namespace_import'
|
|
1276
|
+
);
|
|
1277
|
+
|
|
1278
|
+
return { kind: 'import', node: outermost(specifier ?? node) };
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
if (!readsBinding(node)) {
|
|
1282
|
+
return undefined;
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
const binding = bindingAt(node);
|
|
1286
|
+
|
|
1287
|
+
if (!binding || (bindingScope(node) && binding.id() !== node.id())) {
|
|
1288
|
+
return undefined;
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
return { kind: 'local', node: outermost(binding) };
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
/**
|
|
1295
|
+
* Returns the nodes of the file that refer to a declared name, read from the
|
|
1296
|
+
* syntax tree, in the shape of the tool's answer. A node that is not the
|
|
1297
|
+
* declaration of its name gives none.
|
|
1298
|
+
*/
|
|
1299
|
+
function syntaxReferences(declaration: Node): Node[] {
|
|
1300
|
+
if (!bindingScope(declaration)) {
|
|
1301
|
+
return [];
|
|
1302
|
+
}
|
|
1303
|
+
|
|
1304
|
+
if (bindingAt(declaration)?.id() !== declaration.id()) {
|
|
1305
|
+
return [];
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
return declaration
|
|
1309
|
+
.getRoot()
|
|
1310
|
+
.root()
|
|
1311
|
+
.findAll({
|
|
1312
|
+
rule: {
|
|
1313
|
+
any: REFERENCE_KINDS.map((kind) => ({ kind })),
|
|
1314
|
+
regex: `^${escapeName(declaration.text())}$`,
|
|
1315
|
+
},
|
|
1316
|
+
})
|
|
1317
|
+
.filter(
|
|
1318
|
+
(node) =>
|
|
1319
|
+
!bindingScope(node) &&
|
|
1320
|
+
readsBinding(node) &&
|
|
1321
|
+
!node
|
|
1322
|
+
.ancestors()
|
|
1323
|
+
.some((ancestor) => ancestor.kind() === 'import_statement') &&
|
|
1324
|
+
bindingAt(node)?.id() === declaration.id()
|
|
1325
|
+
)
|
|
1326
|
+
.map(outermost);
|
|
1327
|
+
}
|
|
1328
|
+
|
|
1329
|
+
/**
|
|
1330
|
+
* Returns the definition of a name: the answer of the tool's semantic
|
|
1331
|
+
* analysis, or the one read from the syntax tree when the tool cannot read
|
|
1332
|
+
* a JavaScript file (it reads JSX only in `.jsx` and `.tsx` files).
|
|
1333
|
+
*/
|
|
1334
|
+
export function definitionAt(node: Node): Definition | undefined {
|
|
1335
|
+
const file = node.getRoot().filename();
|
|
1336
|
+
|
|
1337
|
+
if (!syntaxFiles.has(file)) {
|
|
1338
|
+
try {
|
|
1339
|
+
return node.definition() ?? undefined;
|
|
1340
|
+
} catch (error) {
|
|
1341
|
+
leaveTool(file, error);
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
|
|
1345
|
+
return syntaxDefinition(node);
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
/**
|
|
1349
|
+
* Returns the nodes of the file that refer to a declared name, from the tool
|
|
1350
|
+
* or from the syntax tree as `definitionAt` does.
|
|
1351
|
+
*/
|
|
1352
|
+
function referencesAt(declaration: Node): Node[] {
|
|
1353
|
+
const file = declaration.getRoot().filename();
|
|
1354
|
+
|
|
1355
|
+
if (!syntaxFiles.has(file)) {
|
|
1356
|
+
try {
|
|
1357
|
+
return declaration
|
|
1358
|
+
.references()
|
|
1359
|
+
.filter((entry) => entry.root.filename() === file)
|
|
1360
|
+
.flatMap((entry) => entry.nodes);
|
|
1361
|
+
} catch (error) {
|
|
1362
|
+
leaveTool(file, error);
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
|
|
1366
|
+
return syntaxReferences(declaration);
|
|
1367
|
+
}
|
|
1368
|
+
|
|
960
1369
|
/**
|
|
961
1370
|
* Returns the definition of an identifier, with the kind of the definition.
|
|
962
1371
|
* A declarator with a type annotation comes back as its name, as one without.
|
|
@@ -964,7 +1373,7 @@ export function bindsAt(rootNode: Node, declaration: Node, at: Node): boolean {
|
|
|
964
1373
|
function definitionOf(
|
|
965
1374
|
identifier: Node
|
|
966
1375
|
): { kind: string; node: Node } | undefined {
|
|
967
|
-
const definition = identifier
|
|
1376
|
+
const definition = definitionAt(identifier);
|
|
968
1377
|
|
|
969
1378
|
if (!definition) {
|
|
970
1379
|
return undefined;
|
|
@@ -1008,20 +1417,15 @@ export function declarationNodeOf(identifier: Node): Node | undefined {
|
|
|
1008
1417
|
* the import statements.
|
|
1009
1418
|
*/
|
|
1010
1419
|
export function referencesOf(declaration: Node): Node[] {
|
|
1011
|
-
const file = declaration.getRoot().filename();
|
|
1012
1420
|
const start = declaration.range().start.index;
|
|
1013
1421
|
|
|
1014
|
-
return declaration
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
!node
|
|
1022
|
-
.ancestors()
|
|
1023
|
-
.some((ancestor) => ancestor.kind() === 'import_statement')
|
|
1024
|
-
);
|
|
1422
|
+
return referencesAt(declaration).filter(
|
|
1423
|
+
(node) =>
|
|
1424
|
+
node.range().start.index !== start &&
|
|
1425
|
+
!node
|
|
1426
|
+
.ancestors()
|
|
1427
|
+
.some((ancestor) => ancestor.kind() === 'import_statement')
|
|
1428
|
+
);
|
|
1025
1429
|
}
|
|
1026
1430
|
|
|
1027
1431
|
/**
|
|
@@ -1115,7 +1519,7 @@ export function coreImportOf(
|
|
|
1115
1519
|
identifier: Node,
|
|
1116
1520
|
subpaths = false
|
|
1117
1521
|
): { name: string; specifier: Node } | undefined {
|
|
1118
|
-
const definition = identifier
|
|
1522
|
+
const definition = definitionAt(identifier);
|
|
1119
1523
|
const target = definition?.node;
|
|
1120
1524
|
const specifier = target?.kind() === 'identifier' ? target.parent() : target;
|
|
1121
1525
|
|
|
@@ -1146,7 +1550,7 @@ export function coreImportOf(
|
|
|
1146
1550
|
* Tells whether an identifier is the namespace import of `@cornerstonejs/core`.
|
|
1147
1551
|
*/
|
|
1148
1552
|
export function isCoreNamespace(identifier: Node): boolean {
|
|
1149
|
-
const definition = identifier
|
|
1553
|
+
const definition = definitionAt(identifier);
|
|
1150
1554
|
|
|
1151
1555
|
if (!definition) {
|
|
1152
1556
|
return false;
|
|
@@ -1529,6 +1933,62 @@ export function isBoundTo(node: Node, declaration: Node): boolean {
|
|
|
1529
1933
|
);
|
|
1530
1934
|
}
|
|
1531
1935
|
|
|
1936
|
+
/**
|
|
1937
|
+
* Returns the outermost expression that keeps the value of a node through
|
|
1938
|
+
* parentheses, casts and non-null assertions.
|
|
1939
|
+
*/
|
|
1940
|
+
export function keptValue(node: Node): Node {
|
|
1941
|
+
let current = node;
|
|
1942
|
+
let parent = node.parent();
|
|
1943
|
+
|
|
1944
|
+
while (parent && KEEPING_KINDS.has(parent.kind())) {
|
|
1945
|
+
current = parent;
|
|
1946
|
+
parent = parent.parent();
|
|
1947
|
+
}
|
|
1948
|
+
|
|
1949
|
+
return current;
|
|
1950
|
+
}
|
|
1951
|
+
|
|
1952
|
+
/**
|
|
1953
|
+
* Returns the values that a declared name gets: the value of its declaration
|
|
1954
|
+
* and the value of each `name = value`. Undefined when the file writes the
|
|
1955
|
+
* name in another way, such as `+=` or a destructuring.
|
|
1956
|
+
*/
|
|
1957
|
+
export function writtenValues(declaration: Node): Node[] | undefined {
|
|
1958
|
+
const holder = declaration.parent();
|
|
1959
|
+
const initial =
|
|
1960
|
+
holder?.kind() === 'variable_declarator' &&
|
|
1961
|
+
holder.field('name')?.id() === declaration.id()
|
|
1962
|
+
? holder.field('value')
|
|
1963
|
+
: undefined;
|
|
1964
|
+
const values = initial ? [initial] : [];
|
|
1965
|
+
|
|
1966
|
+
for (const reference of referencesOf(declaration)) {
|
|
1967
|
+
let child = reference;
|
|
1968
|
+
let parent = reference.parent();
|
|
1969
|
+
|
|
1970
|
+
while (parent?.kind() === 'parenthesized_expression') {
|
|
1971
|
+
child = parent;
|
|
1972
|
+
parent = parent.parent();
|
|
1973
|
+
}
|
|
1974
|
+
|
|
1975
|
+
const kind = parent?.kind() ?? '';
|
|
1976
|
+
const right =
|
|
1977
|
+
kind === 'assignment_expression' &&
|
|
1978
|
+
parent.field('left')?.id() === child.id()
|
|
1979
|
+
? parent.field('right')
|
|
1980
|
+
: undefined;
|
|
1981
|
+
|
|
1982
|
+
if (right) {
|
|
1983
|
+
values.push(right);
|
|
1984
|
+
} else if (OTHER_WRITES.has(kind)) {
|
|
1985
|
+
return undefined;
|
|
1986
|
+
}
|
|
1987
|
+
}
|
|
1988
|
+
|
|
1989
|
+
return values;
|
|
1990
|
+
}
|
|
1991
|
+
|
|
1532
1992
|
/**
|
|
1533
1993
|
* Returns what a declaration gives to the binding that an identifier refers
|
|
1534
1994
|
* to, when that binding holds one value for its whole scope: the core types
|
|
@@ -2335,6 +2795,84 @@ export function site(
|
|
|
2335
2795
|
};
|
|
2336
2796
|
}
|
|
2337
2797
|
|
|
2798
|
+
/**
|
|
2799
|
+
* Holds the entries of a metric until the transform of a file is done, so
|
|
2800
|
+
* that a file that could not be analysed leaves none of them.
|
|
2801
|
+
*/
|
|
2802
|
+
export class HeldMetric {
|
|
2803
|
+
private readonly metric: Metric;
|
|
2804
|
+
|
|
2805
|
+
private held: Record<string, string>[] = [];
|
|
2806
|
+
|
|
2807
|
+
constructor(metric: Metric) {
|
|
2808
|
+
this.metric = metric;
|
|
2809
|
+
}
|
|
2810
|
+
|
|
2811
|
+
/**
|
|
2812
|
+
* Keeps one entry.
|
|
2813
|
+
*/
|
|
2814
|
+
increment(cardinality: Record<string, string>): void {
|
|
2815
|
+
this.held.push(cardinality);
|
|
2816
|
+
}
|
|
2817
|
+
|
|
2818
|
+
/**
|
|
2819
|
+
* Gives the kept entries to the metric, in their order.
|
|
2820
|
+
*/
|
|
2821
|
+
release(): void {
|
|
2822
|
+
for (const cardinality of this.held) {
|
|
2823
|
+
this.metric.increment(cardinality);
|
|
2824
|
+
}
|
|
2825
|
+
|
|
2826
|
+
this.held = [];
|
|
2827
|
+
}
|
|
2828
|
+
|
|
2829
|
+
/**
|
|
2830
|
+
* Forgets the kept entries.
|
|
2831
|
+
*/
|
|
2832
|
+
drop(): void {
|
|
2833
|
+
this.held = [];
|
|
2834
|
+
}
|
|
2835
|
+
}
|
|
2836
|
+
|
|
2837
|
+
/**
|
|
2838
|
+
* Wraps the transform of a step so that it never fails on a file. When the
|
|
2839
|
+
* transform throws (the tool could not read a TypeScript file, or a name is
|
|
2840
|
+
* declared twice in one scope), the file stays as it is, the entries of the
|
|
2841
|
+
* file are dropped, and the file is reported once in `manual` with the
|
|
2842
|
+
* reason.
|
|
2843
|
+
*/
|
|
2844
|
+
export function analysed(
|
|
2845
|
+
transform: Codemod<TSX>,
|
|
2846
|
+
manual: HeldMetric,
|
|
2847
|
+
...others: HeldMetric[]
|
|
2848
|
+
): Codemod<TSX> {
|
|
2849
|
+
return (root, options) => {
|
|
2850
|
+
const metrics = [manual, ...others];
|
|
2851
|
+
|
|
2852
|
+
bindingsByName.clear();
|
|
2853
|
+
|
|
2854
|
+
try {
|
|
2855
|
+
const result = transform(root, options);
|
|
2856
|
+
|
|
2857
|
+
metrics.forEach((metric) => metric.release());
|
|
2858
|
+
|
|
2859
|
+
return result;
|
|
2860
|
+
} catch (error) {
|
|
2861
|
+
metrics.forEach((metric) => metric.drop());
|
|
2862
|
+
manual.increment(
|
|
2863
|
+
site(
|
|
2864
|
+
root,
|
|
2865
|
+
`the file was not analysed: ${failureReason(error)}`,
|
|
2866
|
+
root.root()
|
|
2867
|
+
)
|
|
2868
|
+
);
|
|
2869
|
+
manual.release();
|
|
2870
|
+
|
|
2871
|
+
return null;
|
|
2872
|
+
}
|
|
2873
|
+
};
|
|
2874
|
+
}
|
|
2875
|
+
|
|
2338
2876
|
/**
|
|
2339
2877
|
* Keeps edits that do not overlap, so a transform can skip a site that
|
|
2340
2878
|
* another rewrite of the same run touches.
|
|
@@ -2384,3 +2922,313 @@ export class EditSet {
|
|
|
2384
2922
|
return this.list;
|
|
2385
2923
|
}
|
|
2386
2924
|
}
|
|
2925
|
+
|
|
2926
|
+
/**
|
|
2927
|
+
* Tells whether an edit lies inside a node.
|
|
2928
|
+
*/
|
|
2929
|
+
function isEditInside(node: Node, edit: Edit): boolean {
|
|
2930
|
+
const { start, end } = node.range();
|
|
2931
|
+
|
|
2932
|
+
return start.index <= edit.startPos && edit.endPos <= end.index;
|
|
2933
|
+
}
|
|
2934
|
+
|
|
2935
|
+
/**
|
|
2936
|
+
* Returns the tokens of the value of a node: its leaves, where a string, a
|
|
2937
|
+
* template, a regular expression and a number are one token each. The
|
|
2938
|
+
* comments and the parts that only TypeScript reads (the type of a cast, a
|
|
2939
|
+
* non-null `!`, type arguments, a type annotation, and the parentheses
|
|
2940
|
+
* around a cast of a name, a member or a call) are not tokens: they go to
|
|
2941
|
+
* `skipped`.
|
|
2942
|
+
*/
|
|
2943
|
+
function tokensOf(node: Node, skipped: Node[]): Node[] {
|
|
2944
|
+
const kind = node.kind();
|
|
2945
|
+
|
|
2946
|
+
if (kind === 'comment' || TYPE_PART_KINDS.has(kind)) {
|
|
2947
|
+
skipped.push(node);
|
|
2948
|
+
|
|
2949
|
+
return [];
|
|
2950
|
+
}
|
|
2951
|
+
|
|
2952
|
+
const children = node.children();
|
|
2953
|
+
|
|
2954
|
+
if (!children.length || TOKEN_KINDS.has(kind)) {
|
|
2955
|
+
return [node];
|
|
2956
|
+
}
|
|
2957
|
+
|
|
2958
|
+
const named = children.filter(
|
|
2959
|
+
(child) => child.isNamed() && child.kind() !== 'comment'
|
|
2960
|
+
);
|
|
2961
|
+
const isCastInParentheses =
|
|
2962
|
+
kind === 'parenthesized_expression' &&
|
|
2963
|
+
KEEPING_KINDS.has(named[0]?.kind() ?? '') &&
|
|
2964
|
+
named[0].kind() !== kind &&
|
|
2965
|
+
PRIMARY_KINDS.has(unwrap(named[0]).kind());
|
|
2966
|
+
|
|
2967
|
+
if (
|
|
2968
|
+
!KEEPING_KINDS.has(kind) ||
|
|
2969
|
+
(kind === 'parenthesized_expression' && !isCastInParentheses)
|
|
2970
|
+
) {
|
|
2971
|
+
return children.flatMap((child) => tokensOf(child, skipped));
|
|
2972
|
+
}
|
|
2973
|
+
|
|
2974
|
+
const value = kind === 'type_assertion' ? named[named.length - 1] : named[0];
|
|
2975
|
+
|
|
2976
|
+
skipped.push(...children.filter((child) => child.id() !== value?.id()));
|
|
2977
|
+
|
|
2978
|
+
return value ? tokensOf(value, skipped) : [];
|
|
2979
|
+
}
|
|
2980
|
+
|
|
2981
|
+
/**
|
|
2982
|
+
* A node as a list of edits leaves it: the text of its value, to compare
|
|
2983
|
+
* two nodes, and the texts that the edits write into that value.
|
|
2984
|
+
*/
|
|
2985
|
+
interface EditedText {
|
|
2986
|
+
text: string;
|
|
2987
|
+
inserted: string[];
|
|
2988
|
+
}
|
|
2989
|
+
|
|
2990
|
+
/**
|
|
2991
|
+
* Returns the value of a node as a list of edits leaves it: its text without
|
|
2992
|
+
* comments, without the parts that only TypeScript reads and without the
|
|
2993
|
+
* white space around its punctuation. Undefined when an edit crosses the
|
|
2994
|
+
* border of the node or of one of its tokens.
|
|
2995
|
+
*/
|
|
2996
|
+
function editedText(node: Node, edits: Edit[]): EditedText | undefined {
|
|
2997
|
+
const { start, end } = node.range();
|
|
2998
|
+
const pieces: { at: number; order: number; text: string }[] = [];
|
|
2999
|
+
const inside: Edit[] = [];
|
|
3000
|
+
const skipped: Node[] = [];
|
|
3001
|
+
const tokens = tokensOf(node, skipped);
|
|
3002
|
+
|
|
3003
|
+
for (const edit of edits) {
|
|
3004
|
+
const isApart =
|
|
3005
|
+
edit.startPos === edit.endPos
|
|
3006
|
+
? !isEditInside(node, edit)
|
|
3007
|
+
: edit.endPos <= start.index || end.index <= edit.startPos;
|
|
3008
|
+
|
|
3009
|
+
if (isApart || skipped.some((part) => isEditInside(part, edit))) {
|
|
3010
|
+
continue;
|
|
3011
|
+
}
|
|
3012
|
+
|
|
3013
|
+
if (!isEditInside(node, edit)) {
|
|
3014
|
+
return undefined;
|
|
3015
|
+
}
|
|
3016
|
+
|
|
3017
|
+
inside.push(edit);
|
|
3018
|
+
pieces.push({ at: edit.startPos, order: 0, text: edit.insertedText });
|
|
3019
|
+
}
|
|
3020
|
+
|
|
3021
|
+
for (const token of tokens) {
|
|
3022
|
+
const range = token.range();
|
|
3023
|
+
const over = inside.filter(
|
|
3024
|
+
(edit) =>
|
|
3025
|
+
edit.startPos < range.end.index && range.start.index < edit.endPos
|
|
3026
|
+
);
|
|
3027
|
+
|
|
3028
|
+
if (
|
|
3029
|
+
over.some(
|
|
3030
|
+
(edit) =>
|
|
3031
|
+
edit.startPos > range.start.index || edit.endPos < range.end.index
|
|
3032
|
+
)
|
|
3033
|
+
) {
|
|
3034
|
+
return undefined;
|
|
3035
|
+
}
|
|
3036
|
+
|
|
3037
|
+
if (!over.length) {
|
|
3038
|
+
pieces.push({ at: range.start.index, order: 1, text: token.text() });
|
|
3039
|
+
}
|
|
3040
|
+
}
|
|
3041
|
+
|
|
3042
|
+
return {
|
|
3043
|
+
text: pieces
|
|
3044
|
+
.sort((a, b) => a.at - b.at || a.order - b.order)
|
|
3045
|
+
.map((piece) => piece.text)
|
|
3046
|
+
.join(' ')
|
|
3047
|
+
.replace(/\s+/g, ' ')
|
|
3048
|
+
.replace(/ ?([^\w$ ]) ?/g, '$1')
|
|
3049
|
+
.trim(),
|
|
3050
|
+
inserted: inside.map((edit) => edit.insertedText),
|
|
3051
|
+
};
|
|
3052
|
+
}
|
|
3053
|
+
|
|
3054
|
+
/**
|
|
3055
|
+
* Returns the text of the value of a node as a list of edits leaves it,
|
|
3056
|
+
* without comments, without the parts that only TypeScript reads and
|
|
3057
|
+
* without the white space around its punctuation, so that two nodes can be
|
|
3058
|
+
* compared. Undefined when an edit crosses the border of the node or of one
|
|
3059
|
+
* of its tokens.
|
|
3060
|
+
*/
|
|
3061
|
+
export function textAfterEdits(node: Node, edits: Edit[]): string | undefined {
|
|
3062
|
+
return editedText(node, edits)?.text;
|
|
3063
|
+
}
|
|
3064
|
+
|
|
3065
|
+
/**
|
|
3066
|
+
* Tells whether a node is a test that joins two operands with an operator.
|
|
3067
|
+
*/
|
|
3068
|
+
function isChain(node: Node | null | undefined, operator: string): boolean {
|
|
3069
|
+
return (
|
|
3070
|
+
node?.kind() === 'binary_expression' &&
|
|
3071
|
+
node.field('operator')?.text() === operator
|
|
3072
|
+
);
|
|
3073
|
+
}
|
|
3074
|
+
|
|
3075
|
+
/**
|
|
3076
|
+
* Returns the operands of a chain of one operator, through parentheses and
|
|
3077
|
+
* casts: `a || (b || c)` gives `a`, `b` and `c`.
|
|
3078
|
+
*/
|
|
3079
|
+
function chainOperands(binary: Node, operator: string): Node[] {
|
|
3080
|
+
return ['left', 'right'].flatMap((side) => {
|
|
3081
|
+
const operand = binary.field(side);
|
|
3082
|
+
|
|
3083
|
+
if (!operand) {
|
|
3084
|
+
return [];
|
|
3085
|
+
}
|
|
3086
|
+
|
|
3087
|
+
const inner = unwrap(operand);
|
|
3088
|
+
|
|
3089
|
+
return isChain(inner, operator)
|
|
3090
|
+
? chainOperands(inner, operator)
|
|
3091
|
+
: [operand];
|
|
3092
|
+
});
|
|
3093
|
+
}
|
|
3094
|
+
|
|
3095
|
+
/**
|
|
3096
|
+
* Tells whether a node is the object of a read of `includes`, through
|
|
3097
|
+
* parentheses and casts.
|
|
3098
|
+
*/
|
|
3099
|
+
function isIncludesObject(node: Node): boolean {
|
|
3100
|
+
const value = keptValue(node);
|
|
3101
|
+
const parent = value.parent();
|
|
3102
|
+
|
|
3103
|
+
return (
|
|
3104
|
+
parent?.kind() === 'member_expression' &&
|
|
3105
|
+
parent.field('object')?.id() === value.id() &&
|
|
3106
|
+
parent.field('property')?.text() === 'includes'
|
|
3107
|
+
);
|
|
3108
|
+
}
|
|
3109
|
+
|
|
3110
|
+
/**
|
|
3111
|
+
* Tells whether an array literal is read with `includes`: directly, or
|
|
3112
|
+
* through a `const` that holds it.
|
|
3113
|
+
*/
|
|
3114
|
+
function isReadWithIncludes(array: Node): boolean {
|
|
3115
|
+
if (isIncludesObject(array)) {
|
|
3116
|
+
return true;
|
|
3117
|
+
}
|
|
3118
|
+
|
|
3119
|
+
const value = keptValue(array);
|
|
3120
|
+
const declarator = value.parent();
|
|
3121
|
+
const name =
|
|
3122
|
+
declarator?.kind() === 'variable_declarator' &&
|
|
3123
|
+
declarator.field('value')?.id() === value.id()
|
|
3124
|
+
? declarator.field('name')
|
|
3125
|
+
: undefined;
|
|
3126
|
+
|
|
3127
|
+
return Boolean(
|
|
3128
|
+
name?.kind() === 'identifier' &&
|
|
3129
|
+
isStableBinding(name) &&
|
|
3130
|
+
referencesOf(name).some(isIncludesObject)
|
|
3131
|
+
);
|
|
3132
|
+
}
|
|
3133
|
+
|
|
3134
|
+
/**
|
|
3135
|
+
* Returns the operands of a file that stand in one place, place by place:
|
|
3136
|
+
* the operands of each chain of `||` or of `&&`, the `case` labels of each
|
|
3137
|
+
* `switch`, and the entries of each array literal read with `includes`.
|
|
3138
|
+
*/
|
|
3139
|
+
function operandGroups(
|
|
3140
|
+
rootNode: Node
|
|
3141
|
+
): { place: MergedPlace; operands: Node[] }[] {
|
|
3142
|
+
const groups: { place: MergedPlace; operands: Node[] }[] = [];
|
|
3143
|
+
|
|
3144
|
+
for (const binary of rootNode.findAll({
|
|
3145
|
+
rule: { kind: 'binary_expression' },
|
|
3146
|
+
})) {
|
|
3147
|
+
const operator = binary.field('operator')?.text() ?? '';
|
|
3148
|
+
|
|
3149
|
+
if (
|
|
3150
|
+
CHAIN_OPERATORS.has(operator) &&
|
|
3151
|
+
!isChain(keptValue(binary).parent(), operator)
|
|
3152
|
+
) {
|
|
3153
|
+
groups.push({
|
|
3154
|
+
place: operator as MergedPlace,
|
|
3155
|
+
operands: chainOperands(binary, operator),
|
|
3156
|
+
});
|
|
3157
|
+
}
|
|
3158
|
+
}
|
|
3159
|
+
|
|
3160
|
+
for (const body of rootNode.findAll({ rule: { kind: 'switch_body' } })) {
|
|
3161
|
+
groups.push({
|
|
3162
|
+
place: 'case',
|
|
3163
|
+
operands: body
|
|
3164
|
+
.children()
|
|
3165
|
+
.filter((child) => child.kind() === 'switch_case')
|
|
3166
|
+
.flatMap((child) => child.field('value') ?? []),
|
|
3167
|
+
});
|
|
3168
|
+
}
|
|
3169
|
+
|
|
3170
|
+
for (const array of rootNode.findAll({ rule: { kind: 'array' } })) {
|
|
3171
|
+
if (isReadWithIncludes(array)) {
|
|
3172
|
+
groups.push({
|
|
3173
|
+
place: 'includes',
|
|
3174
|
+
operands: array
|
|
3175
|
+
.children()
|
|
3176
|
+
.filter((child) => child.isNamed() && child.kind() !== 'comment'),
|
|
3177
|
+
});
|
|
3178
|
+
}
|
|
3179
|
+
}
|
|
3180
|
+
|
|
3181
|
+
return groups;
|
|
3182
|
+
}
|
|
3183
|
+
|
|
3184
|
+
/**
|
|
3185
|
+
* Returns each operand that a list of edits makes the same expression as an
|
|
3186
|
+
* operand before it in the same place, where the two differed: in a chain
|
|
3187
|
+
* of `||` or of `&&`, among the `case` labels of one `switch`, or among the
|
|
3188
|
+
* entries of an array literal read with `includes`. Such a rewrite is right
|
|
3189
|
+
* name by name, and what the two tests told apart in 5.x is gone.
|
|
3190
|
+
*/
|
|
3191
|
+
export function mergedOperands(rootNode: Node, edits: Edit[]): MergedOperand[] {
|
|
3192
|
+
if (!edits.length) {
|
|
3193
|
+
return [];
|
|
3194
|
+
}
|
|
3195
|
+
|
|
3196
|
+
const found: MergedOperand[] = [];
|
|
3197
|
+
|
|
3198
|
+
for (const { place, operands } of operandGroups(rootNode)) {
|
|
3199
|
+
const isEdited = operands.some((operand) =>
|
|
3200
|
+
edits.some((edit) => isEditInside(operand, edit))
|
|
3201
|
+
);
|
|
3202
|
+
|
|
3203
|
+
if (!isEdited) {
|
|
3204
|
+
continue;
|
|
3205
|
+
}
|
|
3206
|
+
|
|
3207
|
+
const texts = operands.map((operand) => ({
|
|
3208
|
+
before: textAfterEdits(unwrap(operand), []),
|
|
3209
|
+
after: editedText(unwrap(operand), edits),
|
|
3210
|
+
}));
|
|
3211
|
+
|
|
3212
|
+
operands.forEach((operand, index) => {
|
|
3213
|
+
const { before, after } = texts[index];
|
|
3214
|
+
const twin = texts.find(
|
|
3215
|
+
(other, at) =>
|
|
3216
|
+
at < index &&
|
|
3217
|
+
after !== undefined &&
|
|
3218
|
+
other.after?.text === after.text &&
|
|
3219
|
+
other.before !== before
|
|
3220
|
+
);
|
|
3221
|
+
|
|
3222
|
+
if (after && twin?.after) {
|
|
3223
|
+
found.push({
|
|
3224
|
+
node: operand,
|
|
3225
|
+
place,
|
|
3226
|
+
note: MERGED_NOTES[place],
|
|
3227
|
+
inserted: [...after.inserted, ...twin.after.inserted],
|
|
3228
|
+
});
|
|
3229
|
+
}
|
|
3230
|
+
});
|
|
3231
|
+
}
|
|
3232
|
+
|
|
3233
|
+
return found;
|
|
3234
|
+
}
|