Skip to content
This repository was archived by the owner on Apr 10, 2023. It is now read-only.

Commit c10cfab

Browse files
committed
feat(serialize): ✨Safe call serialize withou parent
1 parent 6a9c599 commit c10cfab

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

lib/serialize.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ const serializeTypes = {
3636
' ' +
3737
serialize(ast.right, ast);
3838
if (
39-
parent.type === astTypes.UNARY_EXPRESSION ||
40-
(parent.type === astTypes.BINARY_EXPRESSION &&
41-
(binaryOperatorPrecedences[parent.operator] >
42-
binaryOperatorPrecedences[ast.operator] ||
43-
(binaryOperatorPrecedences[parent.operator] ===
44-
binaryOperatorPrecedences[ast.operator] &&
45-
ast === parent.right)))
39+
parent &&
40+
(parent.type === astTypes.UNARY_EXPRESSION ||
41+
(parent.type === astTypes.BINARY_EXPRESSION &&
42+
(binaryOperatorPrecedences[parent.operator] >
43+
binaryOperatorPrecedences[ast.operator] ||
44+
(binaryOperatorPrecedences[parent.operator] ===
45+
binaryOperatorPrecedences[ast.operator] &&
46+
ast === parent.right))))
4647
) {
4748
return addParen(serialized);
4849
}
@@ -70,9 +71,10 @@ const serializeTypes = {
7071
' ' +
7172
serialize(ast.right, ast);
7273
if (
73-
parent.type === astTypes.UNARY_EXPRESSION ||
74-
parent.type === astTypes.BINARY_EXPRESSION ||
75-
(parent.type === astTypes.LOGICAL_EXPRESSION && ast.operator === '||')
74+
parent &&
75+
(parent.type === astTypes.UNARY_EXPRESSION ||
76+
parent.type === astTypes.BINARY_EXPRESSION ||
77+
(parent.type === astTypes.LOGICAL_EXPRESSION && ast.operator === '||'))
7678
) {
7779
return addParen(serialized);
7880
}
@@ -93,9 +95,10 @@ const serializeTypes = {
9395
' : ' +
9496
serialize(ast.alternate, ast);
9597
if (
96-
parent.type === astTypes.UNARY_EXPRESSION ||
97-
parent.type === astTypes.LOGICAL_EXPRESSION ||
98-
parent.type === astTypes.BINARY_EXPRESSION
98+
parent &&
99+
(parent.type === astTypes.UNARY_EXPRESSION ||
100+
parent.type === astTypes.LOGICAL_EXPRESSION ||
101+
parent.type === astTypes.BINARY_EXPRESSION)
99102
) {
100103
return addParen(serialized);
101104
}

0 commit comments

Comments
 (0)