-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A new rule to enforce using custom error types instead of the default error object. <img width="802" alt="image" src="https://github.com/user-attachments/assets/b5e09529-7123-491a-af4c-942c5394e5d3"> Related to aws/aws-cdk#32324
- Loading branch information
Showing
10 changed files
with
104 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Rule } from 'eslint'; | ||
import type { NewExpression, ThrowStatement } from 'estree'; | ||
|
||
export const meta = { | ||
hasSuggestions: true, | ||
}; | ||
|
||
export function create(context: Rule.RuleContext): Rule.NodeListener { | ||
return { | ||
ThrowStatement(node: ThrowStatement) { | ||
if (node.argument.type !== 'NewExpression') { | ||
return; | ||
} | ||
|
||
const newExpr = node.argument as NewExpression; | ||
if ( | ||
newExpr.callee && | ||
newExpr.callee.type === 'Identifier' && | ||
newExpr.callee.name === 'Error' | ||
) { | ||
context.report({ | ||
node: newExpr, | ||
message: 'Expected a non-default error object to be thrown.', | ||
suggest: [ | ||
{ | ||
desc: 'Replace with `ValidationError`', | ||
fix: (fixer: Rule.RuleFixer) => { | ||
// no existing args | ||
if (newExpr.arguments.length === 0) { | ||
return fixer.replaceText(newExpr, "new ValidationError('<insert error message>', this)"); | ||
} | ||
|
||
const fixes = [ | ||
fixer.replaceText(newExpr.callee, 'ValidationError'), | ||
]; | ||
|
||
const last = newExpr.arguments.at(-1)?.range; | ||
if (last) { | ||
fixes.push( | ||
fixer.insertTextAfterRange(last, ', this'), | ||
); | ||
} | ||
|
||
return fixes; | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
test/rules/fixtures/no-throw-default-error/error-no-args.suggested.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
throw new ValidationError('<insert error message>', this); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
throw new Error(); |
1 change: 1 addition & 0 deletions
1
test/rules/fixtures/no-throw-default-error/error-one-arg.suggested.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
throw new ValidationError('My error', this); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
throw new Error('My error'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
plugins: ['local'], | ||
rules: { | ||
'local/no-throw-default-error': [ 'error' ], | ||
} | ||
} | ||
|
||
|
1 change: 1 addition & 0 deletions
1
test/rules/fixtures/no-throw-default-error/throw-error.error.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Expected a non-default error object to be thrown. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
throw new Error('Some error'); |