Support ConditionalExpressions in dry-error-messages rule (#11917)
* Support ConditionalExpressions in dry-error-messages rule * tests
This commit is contained in:
parent
50b3262063
commit
c0f6f0394d
@ -41,6 +41,25 @@ function findIdNode(node) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function findIdNodes(node) {
|
||||
if (node.type === "ConditionalExpression") {
|
||||
const consequent = findIdNode(node.consequent);
|
||||
const alternate = findIdNode(node.alternate);
|
||||
|
||||
if (consequent && alternate) {
|
||||
return [consequent, alternate];
|
||||
}
|
||||
}
|
||||
|
||||
const idNode = findIdNode(node);
|
||||
|
||||
if (idNode) {
|
||||
return [idNode];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function findReference(node, scope) {
|
||||
let currentScope = scope;
|
||||
|
||||
@ -128,11 +147,13 @@ export default {
|
||||
node,
|
||||
) {
|
||||
const [, errorMsgNode] = node.arguments;
|
||||
const nodeToCheck = findIdNode(errorMsgNode);
|
||||
const nodesToCheck = findIdNodes(errorMsgNode);
|
||||
|
||||
if (
|
||||
nodeToCheck &&
|
||||
referencesImportedBinding(nodeToCheck, getScope(), importedBindings)
|
||||
Array.isArray(nodesToCheck) &&
|
||||
nodesToCheck.every(node =>
|
||||
referencesImportedBinding(node, getScope(), importedBindings),
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -261,6 +261,14 @@ ruleTester.run("dry-error-messages", rule, {
|
||||
code: "this.raise(loc);",
|
||||
options: [{ errorModule: ERRORS_MODULE }],
|
||||
},
|
||||
|
||||
// Support ternary as second argument
|
||||
{
|
||||
filename: FILENAME,
|
||||
code:
|
||||
"import Errors, { NotErrors } from 'errorsModule'; this.raise(loc, a ? Errors.someErrorMessage : Errors.someOtherErrorMessage);",
|
||||
options: [{ errorModule: ERRORS_MODULE }],
|
||||
},
|
||||
],
|
||||
invalid: [
|
||||
{
|
||||
@ -691,5 +699,43 @@ ruleTester.run("dry-error-messages", rule, {
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
// Should error if either part of a ternary isn't from error module
|
||||
{
|
||||
filename: FILENAME,
|
||||
code:
|
||||
"import Errors, { NotErrors } from 'errorsModule'; this.raise(loc, a ? Errors.someErrorMessage : 'hello');",
|
||||
options: [{ errorModule: ERRORS_MODULE }],
|
||||
errors: [
|
||||
{
|
||||
messageId: "mustBeImported",
|
||||
data: { errorModule: ERRORS_MODULE },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
filename: FILENAME,
|
||||
code:
|
||||
"import Errors, { NotErrors } from 'errorsModule'; this.raise(loc, a ? 'hello' : Errors.someErrorMessage);",
|
||||
options: [{ errorModule: ERRORS_MODULE }],
|
||||
errors: [
|
||||
{
|
||||
messageId: "mustBeImported",
|
||||
data: { errorModule: ERRORS_MODULE },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
filename: FILENAME,
|
||||
code:
|
||||
"import Errors, { NotErrors } from 'errorsModule'; this.raise(loc, a ? 'hello' : 'world');",
|
||||
options: [{ errorModule: ERRORS_MODULE }],
|
||||
errors: [
|
||||
{
|
||||
messageId: "mustBeImported",
|
||||
data: { errorModule: ERRORS_MODULE },
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
@ -366,7 +366,6 @@ export default class LValParser extends NodeUtils {
|
||||
? isStrictBindReservedWord(expr.name, this.inModule)
|
||||
: isStrictBindOnlyReservedWord(expr.name))
|
||||
) {
|
||||
/* eslint-disable @babel/development-internal/dry-error-messages */
|
||||
this.raise(
|
||||
expr.start,
|
||||
bindingType === BIND_NONE
|
||||
@ -374,7 +373,6 @@ export default class LValParser extends NodeUtils {
|
||||
: Errors.StrictEvalArgumentsBinding,
|
||||
expr.name,
|
||||
);
|
||||
/* eslint-enable @babel/development-internal/dry-error-messages */
|
||||
}
|
||||
|
||||
if (checkClashes) {
|
||||
@ -471,7 +469,6 @@ export default class LValParser extends NodeUtils {
|
||||
break;
|
||||
|
||||
default: {
|
||||
/* eslint-disable @babel/development-internal/dry-error-messages */
|
||||
this.raise(
|
||||
expr.start,
|
||||
bindingType === BIND_NONE
|
||||
@ -479,7 +476,6 @@ export default class LValParser extends NodeUtils {
|
||||
: Errors.InvalidLhsBinding,
|
||||
contextDescription,
|
||||
);
|
||||
/* eslint-enable @babel/development-internal/dry-error-messages */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2042,7 +2042,6 @@ export default class StatementParser extends ExpressionParser {
|
||||
name: string,
|
||||
): void {
|
||||
if (this.state.exportedIdentifiers.indexOf(name) > -1) {
|
||||
/* eslint-disable @babel/development-internal/dry-error-messages */
|
||||
this.raise(
|
||||
node.start,
|
||||
name === "default"
|
||||
@ -2050,7 +2049,6 @@ export default class StatementParser extends ExpressionParser {
|
||||
: Errors.DuplicateExport,
|
||||
name,
|
||||
);
|
||||
/* eslint-enable @babel/development-internal/dry-error-messages */
|
||||
}
|
||||
this.state.exportedIdentifiers.push(name);
|
||||
}
|
||||
|
||||
@ -420,14 +420,12 @@ export default class Tokenizer extends ParserErrors {
|
||||
// misleading
|
||||
this.expectPlugin("recordAndTuple");
|
||||
if (this.getPluginOption("recordAndTuple", "syntaxType") !== "hash") {
|
||||
/* eslint-disable @babel/development-internal/dry-error-messages */
|
||||
throw this.raise(
|
||||
this.state.pos,
|
||||
next === charCodes.leftCurlyBrace
|
||||
? Errors.RecordExpressionHashIncorrectStartSyntaxType
|
||||
: Errors.TupleExpressionHashIncorrectStartSyntaxType,
|
||||
);
|
||||
/* eslint-enable @babel/development-internal/dry-error-messages */
|
||||
}
|
||||
|
||||
if (next === charCodes.leftCurlyBrace) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user