Skip to content

Commit 6da565a

Browse files
committed
Fix crash with prefer-type-error rule
Fixes #82
1 parent afa6c53 commit 6da565a

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

rules/prefer-type-error.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ const throwsErrorObject = node =>
5858
node.argument.callee.type === 'Identifier' &&
5959
node.argument.callee.name === 'Error';
6060

61-
const isLone = node =>
62-
node.parent !== null &&
63-
node.parent.body !== null &&
64-
node.parent.body.length === 1;
61+
const isLone = node => node.parent && node.parent.body && node.parent.body.length === 1;
6562

6663
const isTypecheckingMemberExpression = (node, callExpression) => {
6764
if (isTypecheckingIdentifier(node.property, callExpression, true)) {
@@ -106,7 +103,7 @@ const create = context => {
106103
ThrowStatement: node => {
107104
if (throwsErrorObject(node) &&
108105
isLone(node) &&
109-
node.parent.parent !== null &&
106+
node.parent.parent &&
110107
isTypechecking(node.parent.parent)) {
111108
context.report({
112109
node,

test/prefer-type-error.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ ruleTester.run('prefer-type-error', rule, {
194194
}`,
195195
`if (!_.isFudge(x)) {
196196
throw new Error('x is no fudge!');
197+
}`,
198+
// Should not crash:
199+
`switch (something) {
200+
case 1:
201+
break;
202+
default:
203+
throw new Error('Unknown');
197204
}`
198205
],
199206
invalid: [

0 commit comments

Comments
 (0)