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