import * as ts from 'typescript';
import { findNodes } from '@nrwl/workspace/src/utilities/typescript/find-nodes';
import {
ChangeType,
logger,
StringChange,
StringInsertion,
} from '@nrwl/devkit';
export function addImport(
source: ts.SourceFile,
statement: string
): StringChange[] {
const allImports = findNodes(source, ts.SyntaxKind.ImportDeclaration);
if (allImports.length > 0) {
const lastImport = allImports[allImports.length - 1];
return [
{
type: ChangeType.Insert,
index: lastImport.end + 1,
text: `\n${statement}\n`,
},
];
} else {
return [
{
type: ChangeType.Insert,
index: 0,
text: `\n${statement}\n`,
},
];
}
}
export function findMainRenderStatement(
source: ts.SourceFile
): ts.CallExpression | null {
// 1. Try to find ReactDOM.render.
const calls = findNodes(
source,
ts.SyntaxKind.CallExpression
) as ts.CallExpression[];
for (const expr of calls) {
const inner = expr.expression;
if (
ts.isPropertyAccessExpression(inner) &&
/ReactDOM/i.test(inner.expression.getText()) &&
inner.name.getText() === 'render'
) {
return expr;
}
}
// 2. Try to find render from 'react-dom'.
const imports = findNodes(
source,
ts.SyntaxKind.ImportDeclaration
) as ts.ImportDeclaration[];
const hasRenderImport = imports.some(
(i) =>
i.moduleSpecifier.getText().includes('react-dom') &&
/\brender\b/.test(i.importClause.namedBindings.getText())
);
if (hasRenderImport) {
const calls = findNodes(
source,
ts.SyntaxKind.CallExpression
) as ts.CallExpression[];
for (const expr of calls) {
if (expr.expression.getText() === 'render') {
return expr;
}
}
}
return null;
}
export function findDefaultExport(source: ts.SourceFile): ts.Node | null {
return (
findDefaultExportDeclaration(source) || findDefaultClassOrFunction(source)
);
}
export function findDefaultExportDeclaration(
source: ts.SourceFile
): ts.Node | null {
const identifier = findDefaultExportIdentifier(source);
if (identifier) {
const variables = findNodes(source, ts.SyntaxKind.VariableDeclaration);
const fns = findNodes(source, ts.SyntaxKind.FunctionDeclaration);
const cls = findNodes(source, ts.SyntaxKind.ClassDeclaration);
const all = [...variables, ...fns, ...cls] as Array<
ts.VariableDeclaration | ts.FunctionDeclaration | ts.ClassDeclaration
>;
const exported = all
.filter((x) => x.name.kind === ts.SyntaxKind.Identifier)
.find((x) => (x.name as ts.Identifier).text === identifier.text);
return exported || null;
} else {
return null;
}
}
export function findDefaultExportIdentifier(
source: ts.SourceFile
): ts.Identifier | null {
const exports = findNodes(
source,
ts.SyntaxKind.ExportAssignment
) as ts.ExportAssignment[];
const identifier = exports
.map((x) => x.expression)
.find((x) => x.kind === ts.SyntaxKind.Identifier) as ts.Identifier;
return identifier || null;
}
export function findDefaultClassOrFunction(
source: ts.SourceFile
): ts.FunctionDeclaration | ts.ClassDeclaration | null {
const fns = findNodes(
source,
ts.SyntaxKind.FunctionDeclaration
) as ts.FunctionDeclaration[];
const cls = findNodes(
source,
ts.SyntaxKind.ClassDeclaration
) as ts.ClassDeclaration[];
return (
fns.find(hasDefaultExportModifier) ||
cls.find(hasDefaultExportModifier) ||
null
);
}
function hasDefaultExportModifier(
x: ts.ClassDeclaration | ts.FunctionDeclaration
) {
return (
x.modifiers &&
x.modifiers.some((m) => m.kind === ts.SyntaxKind.ExportKeyword) &&
x.modifiers.some((m) => m.kind === ts.SyntaxKind.DefaultKeyword)
);
}
export function findComponentImportPath(
componentName: string,
source: ts.SourceFile
) {
const allImports = findNodes(
source,
ts.SyntaxKind.ImportDeclaration
) as ts.ImportDeclaration[];
const matching = allImports.filter((i: ts.ImportDeclaration) => {
return (
i.importClause &&
i.importClause.name &&
i.importClause.name.getText() === componentName
);
});
if (matching.length === 0) {
return null;
}
const appImport = matching[0];
return appImport.moduleSpecifier.getText().replace(/['"]/g, '');
}
export function findElements(source: ts.SourceFile, tagName: string) {
const nodes = findNodes(source, [
ts.SyntaxKind.JsxSelfClosingElement,
ts.SyntaxKind.JsxOpeningElement,
]);
return nodes.filter((node) => isTag(tagName, node));
}
export function findClosestOpening(tagName: string, node: ts.Node) {
if (!node) {
return null;
}
if (isTag(tagName, node)) {
return node;
} else {
return findClosestOpening(tagName, node.parent);
}
}
export function isTag(tagName: string, node: ts.Node) {
if (ts.isJsxOpeningLikeElement(node)) {
return (
node.tagName.kind === ts.SyntaxKind.Identifier &&
node.tagName.text === tagName
);
}
if (ts.isJsxElement(node) && node.openingElement) {
return (
node.openingElement.tagName.kind === ts.SyntaxKind.Identifier &&
node.openingElement.tagName.getText() === tagName
);
}
return false;
}
export function addInitialRoutes(
sourcePath: string,
source: ts.SourceFile
): StringChange[] {
const jsxClosingElements = findNodes(source, [
ts.SyntaxKind.JsxClosingElement,
ts.SyntaxKind.JsxClosingFragment,
]);
const outerMostJsxClosing = jsxClosingElements[jsxClosingElements.length - 1];
if (!outerMostJsxClosing) {
logger.warn(
`Could not find JSX elements in ${sourcePath}; Skipping insert routes`
);
return [];
}
const insertRoutes: StringInsertion = {
type: ChangeType.Insert,
index: outerMostJsxClosing.getStart(),
text: `
{/* START: routes */}
{/* These routes and navigation have been generated for you */}
{/* Feel free to move and update them to fit your needs */}