Skip to content

Commit

Permalink
fix(no-throw-default-error): suggest more errors, correct syntax and …
Browse files Browse the repository at this point in the history
…for new package (#96)

This was missing a few error types and also suggested the
`ValidationError` syntax for `ToolkitError`.
  • Loading branch information
mrgrain authored Jan 20, 2025
1 parent c9945d3 commit 37b20ab
Showing 1 changed file with 38 additions and 58 deletions.
96 changes: 38 additions & 58 deletions src/rules/no-throw-default-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ export const meta = {
hasSuggestions: true,
};

// list of paths that should trigger the toolkit error suggestions
const toolkitErrorPaths = ['packages/aws-cdk/', 'packages/@aws-cdk/toolkit/'];

export function create(context: Rule.RuleContext): Rule.NodeListener {
const fileName = context.filename;
const isCliFile = fileName.includes('packages/aws-cdk/');
const isToolkitFile = toolkitErrorPaths.some((path) =>
fileName.includes(path),
);

return {
ThrowStatement(node: ThrowStatement) {
Expand All @@ -21,13 +26,40 @@ export function create(context: Rule.RuleContext): Rule.NodeListener {
newExpr.callee.type === 'Identifier' &&
newExpr.callee.name === 'Error'
) {
const suggestions = [
{
const suggestions = [];

const replaceErrorClassSuggestion = (suggested: string) => {
return {
desc: `Replace with \`${suggested}\``,
fix: (fixer: Rule.RuleFixer) => {
// no args
if (newExpr.arguments.length === 0) {
return fixer.replaceText(newExpr, `new ${suggested}('<insert error message>')`);
}
return [fixer.replaceText(newExpr.callee, suggested)];
},
};
};


// Adds ToolkitError and AuthenticationError suggestions for CLI files.
if (isToolkitFile) {
suggestions.push(
replaceErrorClassSuggestion('ToolkitError'),
replaceErrorClassSuggestion('AuthenticationError'),
replaceErrorClassSuggestion('AssemblyError'),
replaceErrorClassSuggestion('ContextProviderError'),
);
} else {
suggestions.push({
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)");
return fixer.replaceText(
newExpr,
"new ValidationError('<insert error message>', this)",
);
}

const fixes = [
Expand All @@ -36,64 +68,12 @@ export function create(context: Rule.RuleContext): Rule.NodeListener {

const last = newExpr.arguments.at(-1)?.range;
if (last) {
fixes.push(
fixer.insertTextAfterRange(last, ', this'),
);
fixes.push(fixer.insertTextAfterRange(last, ', this'));
}

return fixes;
},
},
];

// Adds ToolkitError and AuthenticationError suggestions for CLI files.
if (isCliFile) {
suggestions.push(
{
desc: 'Replace with `ToolkitError`',
fix: (fixer: Rule.RuleFixer) => {
// no existing args
if (newExpr.arguments.length === 0) {
return fixer.replaceText(newExpr, "new ToolkitError('<insert error message>')");
}

const fixes = [
fixer.replaceText(newExpr.callee, 'ToolkitError'),
];

const last = newExpr.arguments.at(-1)?.range;
if (last) {
fixes.push(
fixer.insertTextAfterRange(last, ', this'),
);
}

return fixes;
},
},
{
desc: 'Replace with `AuthenticationError`',
fix: (fixer: Rule.RuleFixer) => {
// no existing args
if (newExpr.arguments.length === 0) {
return fixer.replaceText(newExpr, "new AuthenticationError('<insert error message>')");
}

const fixes = [
fixer.replaceText(newExpr.callee, 'AuthenticationError'),
];

const last = newExpr.arguments.at(-1)?.range;
if (last) {
fixes.push(
fixer.insertTextAfterRange(last, ', this'),
);
}

return fixes;
},
},
);
});
}

context.report({
Expand Down

0 comments on commit 37b20ab

Please sign in to comment.