Skip to content

Commit

Permalink
Fix custom-error-definition to support constructors without a body (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pat841 authored and sindresorhus committed Nov 27, 2019
1 parent 3554c17 commit a496e96
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -52,6 +52,7 @@
},
"devDependencies": {
"@lubien/fixture-beta-package": "^1.0.0-beta.1",
"@typescript-eslint/parser": "^2.9.0",
"ava": "^2.4.0",
"babel-eslint": "^10.0.3",
"chalk": "^2.4.2",
Expand All @@ -66,6 +67,7 @@
"outdent": "^0.7.0",
"pify": "^4.0.1",
"tempy": "^0.3.0",
"typescript": "^3.7.2",
"xo": "^0.25.3"
},
"peerDependencies": {
Expand Down
6 changes: 6 additions & 0 deletions rules/custom-error-definition.js
Expand Up @@ -80,6 +80,12 @@ const customErrorDefinition = (context, node) => {
}

const constructorBodyNode = constructor.value.body;

// Verify the constructor has a body (TypeScript)
if (!constructorBodyNode) {
return;
}

const constructorBody = constructorBodyNode.body;

const superExpression = constructorBody.find(isSuperExpression);
Expand Down
15 changes: 15 additions & 0 deletions test/custom-error-definition.js
Expand Up @@ -12,6 +12,10 @@ const ruleTester = avaRuleTester(test, {
}
});

const typescriptRuleTester = avaRuleTester(test, {
parser: require.resolve('@typescript-eslint/parser')
});

const invalidClassNameError = {ruleId: 'custom-error-definition', message: 'Invalid class name, use `FooError`.'};
const constructorError = {ruleId: 'custom-error-definition', message: 'Add a constructor to your error.'};
const noSuperCallError = {ruleId: 'custom-error-definition', message: 'Missing call to `super()` in constructor.'};
Expand Down Expand Up @@ -433,3 +437,14 @@ ruleTester.run('custom-error-definition', rule, {
}
]
});

typescriptRuleTester.run('custom-error-definition', rule, {
valid: [
outdent`
class CustomError extends Error {
constructor(type: string, text: string, reply?: any);
}
`
],
invalid: []
});

0 comments on commit a496e96

Please sign in to comment.