Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
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'.
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 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 all = variables.concat(fns) as Array<
ts.VariableDeclaration | ts.FunctionDeclaration
>;
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 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
);
}
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 all = variables.concat(fns) as Array<
ts.VariableDeclaration | ts.FunctionDeclaration
>;
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;
}
}