UNPKG

ng-matero

Version:
99 lines 4.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findRouteNode = findRouteNode; exports.findRouteNodeByKey = findRouteNodeByKey; exports.addRouteDeclarationToModule = addRouteDeclarationToModule; const ts = require("@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript"); const ast_utils_1 = require("@schematics/angular/utility/ast-utils"); function findRouteNode(node, kind, textKey, textValue) { if (node.kind === kind && node.getText() === textKey && node.parent.initializer.text === textValue) { // throw new Error(node.getText()); return node.parent.parent; } let foundNode = null; ts.forEachChild(node, childNode => { foundNode = foundNode || findRouteNode(childNode, kind, textKey, textValue); }); return foundNode; } function findRouteNodeByKey(node, kind, textKey) { let foundNode = null; ts.forEachChild(node, (childNode) => { if (childNode.initializer.kind === kind && childNode.name.text === textKey) { foundNode = childNode.initializer; } }); return foundNode; } /** * Adds a new route declaration to the router module or routes. * * - module: `*-routing.module.ts` * - standalone: `*.routes.ts` */ function addRouteDeclarationToModule(source, fileToAdd, routeLiteral, standalone, subModule) { let routesArg; if (standalone) { const routesVarNode = (0, ast_utils_1.getSourceNodes)(source).find(node => node.kind == ts.SyntaxKind.Identifier && node.getText() == 'routes'); if (!routesVarNode) { throw new Error(`Couldn't find a routes variable in ${fileToAdd}.`); } routesArg = routesVarNode; } else { const routerModuleExpr = (0, ast_utils_1.getRouterModuleDeclaration)(source); if (!routerModuleExpr) { throw new Error(`Couldn't find a route declaration in ${fileToAdd}.`); } const scopeConfigMethodArgs = routerModuleExpr.arguments; if (!scopeConfigMethodArgs.length) { const { line } = source.getLineAndCharacterOfPosition(routerModuleExpr.getStart()); throw new Error(`The router module method doesn't have arguments ` + `at line ${line} in ${fileToAdd}`); } routesArg = scopeConfigMethodArgs[0]; } let routesArr; // Check if the route declarations array is // an inlined argument of RouterModule or a standalone variable if (ts.isArrayLiteralExpression(routesArg)) { routesArr = routesArg; } else { const routesVarName = routesArg.getText(); let routesVar; if (routesArg.kind === ts.SyntaxKind.Identifier) { routesVar = source.statements .filter((s) => s.kind === ts.SyntaxKind.VariableStatement) .find((v) => v.declarationList.declarations[0].name.getText() === routesVarName); } if (!routesVar) { const { line } = source.getLineAndCharacterOfPosition(routesArg.getStart()); throw new Error(`No route declaration array was found that corresponds ` + `to router module at line ${line} in ${fileToAdd}`); } routesArr = (0, ast_utils_1.findNodes)(routesVar, ts.SyntaxKind.ArrayLiteralExpression, 1)[0]; } const occurencesCount = routesArr.elements.length; const text = routesArr.getFullText(source); let route = routeLiteral; if (occurencesCount > 0) { const identation = text.match(/\r?\n(\r?)\s*/) || []; route = `,${identation[0] || ' '}${routeLiteral}`; } // `module` -> insert to main module // `page` -> insert to sub module if (!subModule) { // Find a route which `path` equals to `''` const routeNodeInsertedTo = findRouteNode(routesArr, ts.SyntaxKind.Identifier, 'path', ''); if (!routeNodeInsertedTo) { throw new Error(`Couldn't find a route definition which path is empty string`); } const routeNodeChildren = findRouteNodeByKey(routeNodeInsertedTo, ts.SyntaxKind.ArrayLiteralExpression, 'children'); // reset variable with new value routesArr = routeNodeChildren; } return (0, ast_utils_1.insertAfterLastOccurrence)(routesArr.elements, route, fileToAdd, routesArr.elements.pos, ts.SyntaxKind.ObjectLiteralExpression); } //# sourceMappingURL=ast-utils.js.map