Skip to content

Commit 47ba3f8

Browse files
committed
Fix custom-error-definition
1 parent 3b3ff59 commit 47ba3f8

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

rules/custom-error-definition.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import {upperFirst} from './utils/index.js';
1+
import {
2+
upperFirst,
3+
getParenthesizedText,
4+
} from './utils/index.js';
25

36
const MESSAGE_ID_INVALID_EXPORT = 'invalidExport';
47
const messages = {
@@ -121,7 +124,12 @@ function * customErrorDefinition(context, node) {
121124
if (superExpression.expression.arguments.length === 0) {
122125
const rhs = expression.expression.right;
123126
const [start] = sourceCode.getRange(superExpression);
124-
yield fixer.insertTextAfterRange([start, start + 6], rhs.raw || rhs.name);
127+
// This part crashes on ESLint 10, but it's still not correct.
128+
// There can be spaces, comments after `super`
129+
yield fixer.insertTextAfterRange(
130+
[start, start + 6],
131+
getParenthesizedText(rhs, context),
132+
);
125133
}
126134

127135
const start = messageExpressionIndex === 0
@@ -136,7 +144,6 @@ function * customErrorDefinition(context, node) {
136144
const nameExpression = constructorBody.find(x => isAssignmentExpression(x, 'name'));
137145
if (!nameExpression) {
138146
const nameProperty = body.find(node => isPropertyDefinition(node, 'name'));
139-
140147
if (!nameProperty?.value || nameProperty.value.value !== name) {
141148
yield {
142149
node: nameProperty?.value ?? constructorBodyNode,

test/custom-error-definition.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,28 @@ const tests = {
457457
`,
458458
errors: [passMessageToSuperError],
459459
},
460+
{
461+
code: outdent`
462+
class FooError extends Error {
463+
constructor() {
464+
super();
465+
this.message = foo.error;
466+
this.name = 'FooError';
467+
}
468+
}
469+
`,
470+
output: outdent`
471+
class FooError extends Error {
472+
constructor() {
473+
super(foo.error);
474+
this.name = 'FooError';
475+
}
476+
}
477+
`,
478+
errors: [
479+
passMessageToSuperError,
480+
],
481+
},
460482
],
461483
};
462484

0 commit comments

Comments
 (0)