|
| 1 | +import * as ts from 'typescript'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as edit from './change'; |
| 4 | + |
| 5 | +export function removeRouteFromParent(){ |
| 6 | + |
| 7 | +} |
| 8 | + |
| 9 | +export function findParentRouteFile(){ |
| 10 | + |
| 11 | +} |
| 12 | + |
| 13 | +export function addRoutesToParent(){ |
| 14 | + |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | +* Add Import `import { symbolName } from fileName` if the import doesn't exit |
| 19 | +* already. Assumes fileToEdit can be resolved and accessed. |
| 20 | +* @param fileToEdit (file we want to add import to) |
| 21 | +* @param symbolName (item to import) |
| 22 | +* @param fileName (path to the file) |
| 23 | +*/ |
| 24 | + |
| 25 | +export function insertImport(fileToEdit: string, symbolName: string, fileName: string): Promise<void> { |
| 26 | + let rootNode = ts.createSourceFile(fileToEdit, fs.readFileSync(fileToEdit).toString(), |
| 27 | + ts.ScriptTarget.ES6, true); |
| 28 | + let allImports = findNodes(rootNode, ts.SyntaxKind.ImportDeclaration); |
| 29 | + |
| 30 | + // get nodes that map to import statements from the file fileName |
| 31 | + let relevantImports = allImports.filter(node => { |
| 32 | + // StringLiteral of the ImportDeclaration is the import file (fileName in this case). |
| 33 | + let importFiles = node.getChildren().filter(child => child.kind === ts.SyntaxKind.StringLiteral) |
| 34 | + .map(n => (<ts.StringLiteralTypeNode>n).text); |
| 35 | + return importFiles.filter(file => file === fileName).length === 1; |
| 36 | + }); |
| 37 | + |
| 38 | + if (relevantImports.length > 0) { |
| 39 | + |
| 40 | + var importsAsterisk: boolean = false; |
| 41 | + // imports from import file |
| 42 | + let imports: ts.Node[] = []; |
| 43 | + relevantImports.forEach(n => { |
| 44 | + Array.prototype.push.apply(imports, findNodes(n, ts.SyntaxKind.Identifier)); |
| 45 | + if (findNodes(n, ts.SyntaxKind.AsteriskToken).length > 0) { |
| 46 | + importsAsterisk = true; |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + // if imports * from fileName, don't add symbolName |
| 51 | + if (importsAsterisk) { |
| 52 | + return Promise.resolve(); |
| 53 | + } |
| 54 | + |
| 55 | + let importTextNodes = imports.filter(n => (<ts.Identifier>n).text === symbolName); |
| 56 | + |
| 57 | + // insert import if it's not there |
| 58 | + if (importTextNodes.length === 0) { |
| 59 | + let fallbackPos = findNodes(relevantImports[0], ts.SyntaxKind.CloseBraceToken)[0].pos || |
| 60 | + findNodes(relevantImports[0], ts.SyntaxKind.FromKeyword)[0].pos; |
| 61 | + return insertAfterLastOccurence(imports, ', ', symbolName, fileToEdit, fallbackPos); |
| 62 | + } |
| 63 | + return Promise.resolve(); |
| 64 | + } |
| 65 | + // no such import declaration exists |
| 66 | + let useStrict = findNodes(rootNode, ts.SyntaxKind.StringLiteral).filter(n => n.text === 'use strict'); |
| 67 | + let fallbackPos: number = 0; |
| 68 | + if(useStrict.length > 0){ |
| 69 | + fallbackPos = useStrict[0].end; |
| 70 | + } |
| 71 | + return insertAfterLastOccurence(allImports, ';\n', 'import { ' + symbolName + ' } from \'' + fileName + '\'', |
| 72 | + fileToEdit, fallbackPos, ts.SyntaxKind.StringLiteral) |
| 73 | +}; |
| 74 | + |
| 75 | +/** |
| 76 | +* Find all nodes from the AST in the subtree of node of SyntaxKind kind. |
| 77 | +* @param node |
| 78 | +* @param kind (a valid index of ts.SyntaxKind enum, eg ts.SyntaxKind.ImportDeclaration) |
| 79 | +* @return all nodes of kind kind, or [] if none is found |
| 80 | +*/ |
| 81 | +export function findNodes (node: ts.Node, kind: number, arr: ts.Node[] = []): ts.Node[]{ |
| 82 | + if (node) { |
| 83 | + if(node.kind === kind){ |
| 84 | + arr.push(node); |
| 85 | + } |
| 86 | + node.getChildren().forEach(child => findNodes(child, kind, arr)); |
| 87 | + } |
| 88 | + return arr; |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * @param nodes (nodes to sort) |
| 93 | + * @return (nodes sorted by their position from the source file |
| 94 | + * or [] if nodes is empty) |
| 95 | + */ |
| 96 | +export function sortNodesByPosition(nodes: ts.Node[]): ts.Node[]{ |
| 97 | + if (nodes) { |
| 98 | + return nodes.sort((first, second) => {return first.pos - second.pos}); |
| 99 | + } |
| 100 | + return []; |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * |
| 105 | + * Insert toInsert after the last occurence of ts.SyntaxKind[nodes[i].kind] |
| 106 | + * or after the last of occurence of syntaxKind if the last occurence is a sub child |
| 107 | + * of ts.SyntaxKind[nodes[i].kind] |
| 108 | + * @param nodes (insert after the last occurence of nodes) |
| 109 | + * @param toInsert (string to insert) |
| 110 | + * @param separator (separator between existing text that comes before |
| 111 | + * the new text and toInsert) |
| 112 | + * @param file (file to write the changes to) |
| 113 | + * @param fallbackPos (position to insert if toInsert happens to be the first occurence) |
| 114 | + * @param syntaxKind (the ts.SyntaxKind of the last subchild of the last |
| 115 | + * occurence of nodes, after which we want to insert) |
| 116 | + * @throw Error if toInsert is first occurence but fall back is not set |
| 117 | + */ |
| 118 | +export function insertAfterLastOccurence(nodes: ts.Node[], separator: string, toInsert:string, |
| 119 | + file: string, fallbackPos?: number, syntaxKind?: ts.SyntaxKind): Promise<void> { |
| 120 | + var lastItem = sortNodesByPosition(nodes).pop(); |
| 121 | + |
| 122 | + if (syntaxKind) { |
| 123 | + lastItem = sortNodesByPosition(findNodes(lastItem, syntaxKind)).pop(; |
| 124 | + } |
| 125 | + if (!lastItem && fallbackPos === undefined) { |
| 126 | + return Promise.reject(new Error(`tried to insert ${toInsert} as first occurence with no fallback position`)); |
| 127 | + } |
| 128 | + let lastItemPosition: number = lastItem? lastItem.end : fallbackPos; |
| 129 | + |
| 130 | + let editFile = new edit.InsertChange(file, lastItemPosition, separator + toInsert); |
| 131 | + return editFile.apply(); |
| 132 | +} |
| 133 | + |
0 commit comments