diff --git a/transforms/magic-redirect.ts b/transforms/magic-redirect.ts index eb988c3..0463158 100644 --- a/transforms/magic-redirect.ts +++ b/transforms/magic-redirect.ts @@ -14,7 +14,8 @@ import { recursiveParent } from '../utils/recursiveParent' const unifiedMagicString = (path: ASTPath, projectRequestName: string) => { const pathArguments = path.value.arguments - if (pathArguments.length === 1 && pathArguments[0].type === 'StringLiteral' && pathArguments[0].value === 'back') { + + if (pathArguments.length === 1 && pathArguments[0]?.type === 'StringLiteral' && pathArguments[0].value === 'back') { path.value.arguments = [ logicalExpression( '||', @@ -39,6 +40,7 @@ export default function transformer(file: FileInfo, _api: API) { }, }) .map((path) => unifiedMagicString(path, recursiveParent(path.parentPath) || 'req')) + parsedFile .find(CallExpression, { callee: { diff --git a/utils/recursiveParent.ts b/utils/recursiveParent.ts index ba904eb..bae9f09 100644 --- a/utils/recursiveParent.ts +++ b/utils/recursiveParent.ts @@ -7,9 +7,9 @@ export const recursiveParent = ( paramIndex = 0, parentExpressionType = defaultParentExpressionType, ): string | null => { - if (parentExpressionType.some((type) => parent.value.type === type)) { + if (parentExpressionType.some((type) => parent.value?.type === type)) { const foundNode = parent.value as unknown as ArrowFunctionExpression | FunctionExpression - if (foundNode.params[paramIndex].type === 'Identifier') { + if (foundNode.params[paramIndex]?.type === 'Identifier') { return foundNode.params[paramIndex].name } return null