From be5629265095615511f5b53a2a5c43a16343c775 Mon Sep 17 00:00:00 2001 From: Pat Herlihy Date: Tue, 26 Nov 2019 08:31:44 -0500 Subject: [PATCH 1/4] Fix the `custom-error-definition` rule to support constructors without a body. --- rules/custom-error-definition.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rules/custom-error-definition.js b/rules/custom-error-definition.js index e2b4ce56cf..f710fa8091 100644 --- a/rules/custom-error-definition.js +++ b/rules/custom-error-definition.js @@ -80,6 +80,12 @@ const customErrorDefinition = (context, node) => { } const constructorBodyNode = constructor.value.body; + + // Verify the constructor has a body + if (!constructorBodyNode) { + return; + } + const constructorBody = constructorBodyNode.body; const superExpression = constructorBody.find(isSuperExpression); From b9cef910f771e460ab41c0368cee7f01f1fa451b Mon Sep 17 00:00:00 2001 From: futpib Date: Tue, 26 Nov 2019 21:43:24 +0300 Subject: [PATCH 2/4] Add a typescript parser test --- package.json | 2 ++ test/custom-error-definition.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/package.json b/package.json index 216e5a5a4f..5ed28dc39b 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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": { diff --git a/test/custom-error-definition.js b/test/custom-error-definition.js index 816cf5dc5e..5ba9a1bb5a 100644 --- a/test/custom-error-definition.js +++ b/test/custom-error-definition.js @@ -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.'}; @@ -433,3 +437,15 @@ 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: [ + ] +}); From b79c3e7f8776dfd9cac76ed7cbd620757bd3f07c Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 27 Nov 2019 11:29:37 +0700 Subject: [PATCH 3/4] Update custom-error-definition.js --- rules/custom-error-definition.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/custom-error-definition.js b/rules/custom-error-definition.js index f710fa8091..ba71c8098f 100644 --- a/rules/custom-error-definition.js +++ b/rules/custom-error-definition.js @@ -81,7 +81,7 @@ const customErrorDefinition = (context, node) => { const constructorBodyNode = constructor.value.body; - // Verify the constructor has a body + // Verify the constructor has a body (TypeScript) if (!constructorBodyNode) { return; } From b1ccb9d22dbb3be1123e5427abbe771117ac1db8 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 27 Nov 2019 11:29:57 +0700 Subject: [PATCH 4/4] Update custom-error-definition.js --- test/custom-error-definition.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/custom-error-definition.js b/test/custom-error-definition.js index 5ba9a1bb5a..7b5325cda7 100644 --- a/test/custom-error-definition.js +++ b/test/custom-error-definition.js @@ -446,6 +446,5 @@ typescriptRuleTester.run('custom-error-definition', rule, { } ` ], - invalid: [ - ] + invalid: [] });