Support ConditionalExpressions in dry-error-messages rule (#11917)

* Support ConditionalExpressions in dry-error-messages rule

* tests
This commit is contained in:
Brian Ng 2020-08-05 15:28:35 -05:00 committed by GitHub
parent 50b3262063
commit c0f6f0394d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 70 additions and 11 deletions

View File

@ -41,6 +41,25 @@ function findIdNode(node) {
return null; 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) { function findReference(node, scope) {
let currentScope = scope; let currentScope = scope;
@ -128,11 +147,13 @@ export default {
node, node,
) { ) {
const [, errorMsgNode] = node.arguments; const [, errorMsgNode] = node.arguments;
const nodeToCheck = findIdNode(errorMsgNode); const nodesToCheck = findIdNodes(errorMsgNode);
if ( if (
nodeToCheck && Array.isArray(nodesToCheck) &&
referencesImportedBinding(nodeToCheck, getScope(), importedBindings) nodesToCheck.every(node =>
referencesImportedBinding(node, getScope(), importedBindings),
)
) { ) {
return; return;
} }

View File

@ -261,6 +261,14 @@ ruleTester.run("dry-error-messages", rule, {
code: "this.raise(loc);", code: "this.raise(loc);",
options: [{ errorModule: ERRORS_MODULE }], 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: [ 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 },
},
],
},
], ],
}); });

View File

@ -366,7 +366,6 @@ export default class LValParser extends NodeUtils {
? isStrictBindReservedWord(expr.name, this.inModule) ? isStrictBindReservedWord(expr.name, this.inModule)
: isStrictBindOnlyReservedWord(expr.name)) : isStrictBindOnlyReservedWord(expr.name))
) { ) {
/* eslint-disable @babel/development-internal/dry-error-messages */
this.raise( this.raise(
expr.start, expr.start,
bindingType === BIND_NONE bindingType === BIND_NONE
@ -374,7 +373,6 @@ export default class LValParser extends NodeUtils {
: Errors.StrictEvalArgumentsBinding, : Errors.StrictEvalArgumentsBinding,
expr.name, expr.name,
); );
/* eslint-enable @babel/development-internal/dry-error-messages */
} }
if (checkClashes) { if (checkClashes) {
@ -471,7 +469,6 @@ export default class LValParser extends NodeUtils {
break; break;
default: { default: {
/* eslint-disable @babel/development-internal/dry-error-messages */
this.raise( this.raise(
expr.start, expr.start,
bindingType === BIND_NONE bindingType === BIND_NONE
@ -479,7 +476,6 @@ export default class LValParser extends NodeUtils {
: Errors.InvalidLhsBinding, : Errors.InvalidLhsBinding,
contextDescription, contextDescription,
); );
/* eslint-enable @babel/development-internal/dry-error-messages */
} }
} }
} }

View File

@ -2042,7 +2042,6 @@ export default class StatementParser extends ExpressionParser {
name: string, name: string,
): void { ): void {
if (this.state.exportedIdentifiers.indexOf(name) > -1) { if (this.state.exportedIdentifiers.indexOf(name) > -1) {
/* eslint-disable @babel/development-internal/dry-error-messages */
this.raise( this.raise(
node.start, node.start,
name === "default" name === "default"
@ -2050,7 +2049,6 @@ export default class StatementParser extends ExpressionParser {
: Errors.DuplicateExport, : Errors.DuplicateExport,
name, name,
); );
/* eslint-enable @babel/development-internal/dry-error-messages */
} }
this.state.exportedIdentifiers.push(name); this.state.exportedIdentifiers.push(name);
} }

View File

@ -420,14 +420,12 @@ export default class Tokenizer extends ParserErrors {
// misleading // misleading
this.expectPlugin("recordAndTuple"); this.expectPlugin("recordAndTuple");
if (this.getPluginOption("recordAndTuple", "syntaxType") !== "hash") { if (this.getPluginOption("recordAndTuple", "syntaxType") !== "hash") {
/* eslint-disable @babel/development-internal/dry-error-messages */
throw this.raise( throw this.raise(
this.state.pos, this.state.pos,
next === charCodes.leftCurlyBrace next === charCodes.leftCurlyBrace
? Errors.RecordExpressionHashIncorrectStartSyntaxType ? Errors.RecordExpressionHashIncorrectStartSyntaxType
: Errors.TupleExpressionHashIncorrectStartSyntaxType, : Errors.TupleExpressionHashIncorrectStartSyntaxType,
); );
/* eslint-enable @babel/development-internal/dry-error-messages */
} }
if (next === charCodes.leftCurlyBrace) { if (next === charCodes.leftCurlyBrace) {