|
9 | 9 | import * as ts from 'typescript'; |
10 | 10 | import { Change, InsertChange, NoopChange } from './change'; |
11 | 11 |
|
12 | | -/** |
13 | | - * Find all nodes from the AST in the subtree of node of SyntaxKind kind. |
14 | | - * @param node |
15 | | - * @param kind |
16 | | - * @param max The maximum number of items to return. |
17 | | - * @return all nodes of kind, or [] if none is found |
18 | | - */ |
19 | | -export function findNodes( |
20 | | - node: ts.Node, |
21 | | - kind: ts.SyntaxKind, |
22 | | - max = Infinity, |
23 | | -): ts.Node[] { |
24 | | - if (!node || max == 0) { |
25 | | - return []; |
26 | | - } |
27 | | - |
28 | | - const arr: ts.Node[] = []; |
29 | | - if (node.kind === kind) { |
30 | | - arr.push(node); |
31 | | - max--; |
32 | | - } |
33 | | - if (max > 0) { |
34 | | - for (const child of node.getChildren()) { |
35 | | - findNodes(child, kind, max).forEach((node) => { |
36 | | - if (max > 0) { |
37 | | - arr.push(node); |
38 | | - } |
39 | | - max--; |
40 | | - }); |
41 | | - |
42 | | - if (max <= 0) { |
43 | | - break; |
44 | | - } |
45 | | - } |
46 | | - } |
47 | | - |
48 | | - return arr; |
49 | | -} |
50 | | - |
51 | | -/** |
52 | | - * Helper for sorting nodes. |
53 | | - * @return function to sort nodes in increasing order of position in sourceFile |
54 | | - */ |
55 | | -function nodesByPosition(first: ts.Node, second: ts.Node): number { |
56 | | - return first.getStart() - second.getStart(); |
57 | | -} |
58 | | - |
59 | | -/** |
60 | | - * Insert `toInsert` after the last occurence of `ts.SyntaxKind[nodes[i].kind]` |
61 | | - * or after the last of occurence of `syntaxKind` if the last occurence is a sub child |
62 | | - * of ts.SyntaxKind[nodes[i].kind] and save the changes in file. |
63 | | - * |
64 | | - * @param nodes insert after the last occurence of nodes |
65 | | - * @param toInsert string to insert |
66 | | - * @param file file to insert changes into |
67 | | - * @param fallbackPos position to insert if toInsert happens to be the first occurence |
68 | | - * @param syntaxKind the ts.SyntaxKind of the subchildren to insert after |
69 | | - * @return Change instance |
70 | | - * @throw Error if toInsert is first occurence but fall back is not set |
71 | | - */ |
72 | | -export function insertAfterLastOccurrence( |
73 | | - nodes: ts.Node[], |
74 | | - toInsert: string, |
75 | | - file: string, |
76 | | - fallbackPos: number, |
77 | | - syntaxKind?: ts.SyntaxKind, |
78 | | -): Change { |
79 | | - // sort() has a side effect, so make a copy so that we won't overwrite the parent's object. |
80 | | - let lastItem = [...nodes].sort(nodesByPosition).pop(); |
81 | | - if (!lastItem) { |
82 | | - throw new Error(); |
83 | | - } |
84 | | - if (syntaxKind) { |
85 | | - lastItem = findNodes(lastItem, syntaxKind).sort(nodesByPosition).pop(); |
86 | | - } |
87 | | - if (!lastItem && fallbackPos == undefined) { |
88 | | - throw new Error( |
89 | | - `tried to insert ${toInsert} as first occurence with no fallback position`, |
90 | | - ); |
91 | | - } |
92 | | - const lastItemPosition: number = lastItem ? lastItem.getEnd() : fallbackPos; |
93 | | - |
94 | | - return new InsertChange(file, lastItemPosition, toInsert); |
95 | | -} |
96 | | - |
97 | 12 | /** |
98 | 13 | * Add Import `import { symbolName } from fileName` if the import doesn't exit |
99 | 14 | * already. Assumes fileToEdit can be resolved and accessed. |
@@ -213,6 +128,45 @@ export function insertImport( |
213 | 128 | ); |
214 | 129 | } |
215 | 130 |
|
| 131 | +/** |
| 132 | + * Find all nodes from the AST in the subtree of node of SyntaxKind kind. |
| 133 | + * @param node |
| 134 | + * @param kind |
| 135 | + * @param max The maximum number of items to return. |
| 136 | + * @return all nodes of kind, or [] if none is found |
| 137 | + */ |
| 138 | +export function findNodes( |
| 139 | + node: ts.Node, |
| 140 | + kind: ts.SyntaxKind, |
| 141 | + max = Infinity, |
| 142 | +): ts.Node[] { |
| 143 | + if (!node || max == 0) { |
| 144 | + return []; |
| 145 | + } |
| 146 | + |
| 147 | + const arr: ts.Node[] = []; |
| 148 | + if (node.kind === kind) { |
| 149 | + arr.push(node); |
| 150 | + max--; |
| 151 | + } |
| 152 | + if (max > 0) { |
| 153 | + for (const child of node.getChildren()) { |
| 154 | + findNodes(child, kind, max).forEach((node) => { |
| 155 | + if (max > 0) { |
| 156 | + arr.push(node); |
| 157 | + } |
| 158 | + max--; |
| 159 | + }); |
| 160 | + |
| 161 | + if (max <= 0) { |
| 162 | + break; |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return arr; |
| 168 | +} |
| 169 | + |
216 | 170 | /** |
217 | 171 | * Get all the nodes from a source. |
218 | 172 | * @param sourceFile The source file object. |
@@ -254,6 +208,52 @@ export function findNode( |
254 | 208 | return foundNode; |
255 | 209 | } |
256 | 210 |
|
| 211 | +/** |
| 212 | + * Helper for sorting nodes. |
| 213 | + * @return function to sort nodes in increasing order of position in sourceFile |
| 214 | + */ |
| 215 | +function nodesByPosition(first: ts.Node, second: ts.Node): number { |
| 216 | + return first.getStart() - second.getStart(); |
| 217 | +} |
| 218 | + |
| 219 | +/** |
| 220 | + * Insert `toInsert` after the last occurence of `ts.SyntaxKind[nodes[i].kind]` |
| 221 | + * or after the last of occurence of `syntaxKind` if the last occurence is a sub child |
| 222 | + * of ts.SyntaxKind[nodes[i].kind] and save the changes in file. |
| 223 | + * |
| 224 | + * @param nodes insert after the last occurence of nodes |
| 225 | + * @param toInsert string to insert |
| 226 | + * @param file file to insert changes into |
| 227 | + * @param fallbackPos position to insert if toInsert happens to be the first occurence |
| 228 | + * @param syntaxKind the ts.SyntaxKind of the subchildren to insert after |
| 229 | + * @return Change instance |
| 230 | + * @throw Error if toInsert is first occurence but fall back is not set |
| 231 | + */ |
| 232 | +export function insertAfterLastOccurrence( |
| 233 | + nodes: ts.Node[], |
| 234 | + toInsert: string, |
| 235 | + file: string, |
| 236 | + fallbackPos: number, |
| 237 | + syntaxKind?: ts.SyntaxKind, |
| 238 | +): Change { |
| 239 | + // sort() has a side effect, so make a copy so that we won't overwrite the parent's object. |
| 240 | + let lastItem = [...nodes].sort(nodesByPosition).pop(); |
| 241 | + if (!lastItem) { |
| 242 | + throw new Error(); |
| 243 | + } |
| 244 | + if (syntaxKind) { |
| 245 | + lastItem = findNodes(lastItem, syntaxKind).sort(nodesByPosition).pop(); |
| 246 | + } |
| 247 | + if (!lastItem && fallbackPos == undefined) { |
| 248 | + throw new Error( |
| 249 | + `tried to insert ${toInsert} as first occurence with no fallback position`, |
| 250 | + ); |
| 251 | + } |
| 252 | + const lastItemPosition: number = lastItem ? lastItem.getEnd() : fallbackPos; |
| 253 | + |
| 254 | + return new InsertChange(file, lastItemPosition, toInsert); |
| 255 | +} |
| 256 | + |
257 | 257 | export function getContentOfKeyLiteral( |
258 | 258 | _source: ts.SourceFile, |
259 | 259 | node: ts.Node, |
@@ -515,8 +515,7 @@ export function addSymbolToNgModuleMetadata( |
515 | 515 | } |
516 | 516 |
|
517 | 517 | if (Array.isArray(node)) { |
518 | | - // eslint-disable-next-line @typescript-eslint/ban-types |
519 | | - const nodeArray = (node as {}) as Array<ts.Node>; |
| 518 | + const nodeArray = (node as unknown) as Array<ts.Node>; |
520 | 519 | const symbolsArray = nodeArray.map((node) => node.getText()); |
521 | 520 | if (symbolsArray.includes(symbolName)) { |
522 | 521 | return []; |
|
0 commit comments