Skip to content

Commit

Permalink
error-message: Handle shadowed Error constructor (#1496)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanHessell committed Aug 24, 2021
1 parent 8da2aec commit fdadd88
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions rules/error-message.js
@@ -1,5 +1,6 @@
'use strict';
const {getStaticValue} = require('eslint-utils');
const isShadowed = require('./utils/is-shadowed.js');
const {callOrNewExpressionSelector} = require('./selectors/index.js');

const MESSAGE_ID_MISSING_MESSAGE = 'missing-message';
Expand All @@ -26,6 +27,10 @@ const selector = callOrNewExpressionSelector([

const create = context => ({
[selector](expression) {
if (isShadowed(context.getScope(), expression.callee)) {
return;
}

const constructorName = expression.callee.name;
const messageArgumentIndex = constructorName === 'AggregateError' ? 1 : 0;
const callArguments = expression.arguments;
Expand Down
7 changes: 7 additions & 0 deletions test/error-message.mjs
Expand Up @@ -28,6 +28,13 @@ test.snapshot({
const a = x;
throw x;
`,
// #1431: Do not fail if Error is shadowed
outdent`
const Error = function () {};
const err = new Error({
name: 'Unauthorized',
});
`,
],
invalid: [
'throw new Error()',
Expand Down

0 comments on commit fdadd88

Please sign in to comment.