Skip to content

Commit

Permalink
fix: add optional chaining to prevent runtime errors in magic-redirec…
Browse files Browse the repository at this point in the history
…t and recursiveParent functions (#16)
  • Loading branch information
bjohansebas authored Jan 11, 2025
1 parent 4d6a100 commit d16b91f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion transforms/magic-redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import { recursiveParent } from '../utils/recursiveParent'

const unifiedMagicString = (path: ASTPath<CallExpression>, 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(
'||',
Expand All @@ -39,6 +40,7 @@ export default function transformer(file: FileInfo, _api: API) {
},
})
.map((path) => unifiedMagicString(path, recursiveParent(path.parentPath) || 'req'))

parsedFile
.find(CallExpression, {
callee: {
Expand Down
4 changes: 2 additions & 2 deletions utils/recursiveParent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d16b91f

Please sign in to comment.