Skip to content

Commit d16b91f

Browse files
authored
fix: add optional chaining to prevent runtime errors in magic-redirect and recursiveParent functions (#16)
1 parent 4d6a100 commit d16b91f

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

transforms/magic-redirect.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import { recursiveParent } from '../utils/recursiveParent'
1414

1515
const unifiedMagicString = (path: ASTPath<CallExpression>, projectRequestName: string) => {
1616
const pathArguments = path.value.arguments
17-
if (pathArguments.length === 1 && pathArguments[0].type === 'StringLiteral' && pathArguments[0].value === 'back') {
17+
18+
if (pathArguments.length === 1 && pathArguments[0]?.type === 'StringLiteral' && pathArguments[0].value === 'back') {
1819
path.value.arguments = [
1920
logicalExpression(
2021
'||',
@@ -39,6 +40,7 @@ export default function transformer(file: FileInfo, _api: API) {
3940
},
4041
})
4142
.map((path) => unifiedMagicString(path, recursiveParent(path.parentPath) || 'req'))
43+
4244
parsedFile
4345
.find(CallExpression, {
4446
callee: {

utils/recursiveParent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ export const recursiveParent = (
77
paramIndex = 0,
88
parentExpressionType = defaultParentExpressionType,
99
): string | null => {
10-
if (parentExpressionType.some((type) => parent.value.type === type)) {
10+
if (parentExpressionType.some((type) => parent.value?.type === type)) {
1111
const foundNode = parent.value as unknown as ArrowFunctionExpression | FunctionExpression
12-
if (foundNode.params[paramIndex].type === 'Identifier') {
12+
if (foundNode.params[paramIndex]?.type === 'Identifier') {
1313
return foundNode.params[paramIndex].name
1414
}
1515
return null

0 commit comments

Comments
 (0)