Skip to content

Commit

Permalink
Chore: Refactor to use messageId in a number of rules (#12715)
Browse files Browse the repository at this point in the history
* Chore: Refactor to use messageId in a number of rules

* Chore: use variable for customMessage in no-restricted-global test
  • Loading branch information
bradzacher authored and ilyavolodin committed Jan 7, 2020
1 parent 1118fce commit 9dfc850
Show file tree
Hide file tree
Showing 79 changed files with 3,611 additions and 709 deletions.
14 changes: 12 additions & 2 deletions lib/rules/no-func-assign.js
Expand Up @@ -22,7 +22,11 @@ module.exports = {
url: "https://eslint.org/docs/rules/no-func-assign"
},

schema: []
schema: [],

messages: {
isAFunction: "'{{name}}' is a function."
}
},

create(context) {
Expand All @@ -34,7 +38,13 @@ module.exports = {
*/
function checkReference(references) {
astUtils.getModifyingReferences(references).forEach(reference => {
context.report({ node: reference.identifier, message: "'{{name}}' is a function.", data: { name: reference.identifier.name } });
context.report({
node: reference.identifier,
messageId: "isAFunction",
data: {
name: reference.identifier.name
}
});
});
}

Expand Down
12 changes: 9 additions & 3 deletions lib/rules/no-global-assign.js
Expand Up @@ -32,7 +32,11 @@ module.exports = {
},
additionalProperties: false
}
]
],

messages: {
globalShouldNotBeModified: "Read-only global '{{name}}' should not be modified."
}
},

create(context) {
Expand Down Expand Up @@ -60,8 +64,10 @@ module.exports = {
) {
context.report({
node: identifier,
message: "Read-only global '{{name}}' should not be modified.",
data: identifier
messageId: "globalShouldNotBeModified",
data: {
name: identifier.name
}
});
}
}
Expand Down
8 changes: 6 additions & 2 deletions lib/rules/no-implicit-coercion.js
Expand Up @@ -187,7 +187,11 @@ module.exports = {
}
},
additionalProperties: false
}]
}],

messages: {
useRecommendation: "use `{{recommendation}}` instead."
}
},

create(context) {
Expand All @@ -204,7 +208,7 @@ module.exports = {
function report(node, recommendation, shouldFix) {
context.report({
node,
message: "use `{{recommendation}}` instead.",
messageId: "useRecommendation",
data: {
recommendation
},
Expand Down
11 changes: 9 additions & 2 deletions lib/rules/no-implied-eval.js
Expand Up @@ -20,7 +20,11 @@ module.exports = {
url: "https://eslint.org/docs/rules/no-implied-eval"
},

schema: []
schema: [],

messages: {
impliedEval: "Implied eval. Consider passing a function instead of a string."
}
},

create(context) {
Expand Down Expand Up @@ -107,7 +111,10 @@ module.exports = {
// remove the entire substack, to avoid duplicate reports
const substack = impliedEvalAncestorsStack.pop();

context.report({ node: substack[0], message: "Implied eval. Consider passing a function instead of a string." });
context.report({
node: substack[0],
messageId: "impliedEval"
});
}
}

Expand Down
11 changes: 9 additions & 2 deletions lib/rules/no-inline-comments.js
Expand Up @@ -21,7 +21,11 @@ module.exports = {
url: "https://eslint.org/docs/rules/no-inline-comments"
},

schema: []
schema: [],

messages: {
unexpectedInlineComment: "Unexpected comment inline with code."
}
},

create(context) {
Expand Down Expand Up @@ -64,7 +68,10 @@ module.exports = {
return;
}

context.report({ node, message: "Unexpected comment inline with code." });
context.report({
node,
messageId: "unexpectedInlineComment"
});
}

//--------------------------------------------------------------------------
Expand Down
8 changes: 6 additions & 2 deletions lib/rules/no-inner-declarations.js
Expand Up @@ -24,7 +24,11 @@ module.exports = {
{
enum: ["functions", "both"]
}
]
],

messages: {
moveDeclToRoot: "Move {{type}} declaration to {{body}} root."
}
},

create(context) {
Expand Down Expand Up @@ -68,7 +72,7 @@ module.exports = {
if (!valid) {
context.report({
node,
message: "Move {{type}} declaration to {{body}} root.",
messageId: "moveDeclToRoot",
data: {
type: (node.type === "FunctionDeclaration" ? "function" : "variable"),
body: (body.type === "Program" ? "program" : "function body")
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/no-invalid-regexp.js
Expand Up @@ -40,6 +40,8 @@ module.exports = {
},
additionalProperties: false
}]

// no messages, because the error text comes directly from the regexpp module
},

create(context) {
Expand Down
11 changes: 9 additions & 2 deletions lib/rules/no-invalid-this.js
Expand Up @@ -37,7 +37,11 @@ module.exports = {
},
additionalProperties: false
}
]
],

messages: {
unexpectedThis: "Unexpected 'this'."
}
},

create(context) {
Expand Down Expand Up @@ -130,7 +134,10 @@ module.exports = {
const current = stack.getCurrent();

if (current && !current.valid) {
context.report({ node, message: "Unexpected 'this'." });
context.report({
node,
messageId: "unexpectedThis"
});
}
}
};
Expand Down
18 changes: 15 additions & 3 deletions lib/rules/no-irregular-whitespace.js
Expand Up @@ -59,7 +59,11 @@ module.exports = {
},
additionalProperties: false
}
]
],

messages: {
noIrregularWhitespace: "Irregular whitespace not allowed."
}
},

create(context) {
Expand Down Expand Up @@ -161,7 +165,11 @@ module.exports = {
column: match.index
};

errors.push({ node, message: "Irregular whitespace not allowed.", loc: location });
errors.push({
node,
messageId: "noIrregularWhitespace",
loc: location
});
}
});
}
Expand All @@ -186,7 +194,11 @@ module.exports = {
column: sourceLines[lineIndex].length
};

errors.push({ node, message: "Irregular whitespace not allowed.", loc: location });
errors.push({
node,
messageId: "noIrregularWhitespace",
loc: location
});
lastLineIndex = lineIndex;
}
}
Expand Down
11 changes: 9 additions & 2 deletions lib/rules/no-iterator.js
Expand Up @@ -20,7 +20,11 @@ module.exports = {
url: "https://eslint.org/docs/rules/no-iterator"
},

schema: []
schema: [],

messages: {
noIterator: "Reserved name '__iterator__'."
}
},

create(context) {
Expand All @@ -32,7 +36,10 @@ module.exports = {
if (node.property &&
(node.property.type === "Identifier" && node.property.name === "__iterator__" && !node.computed) ||
(node.property.type === "Literal" && node.property.value === "__iterator__")) {
context.report({ node, message: "Reserved name '__iterator__'." });
context.report({
node,
messageId: "noIterator"
});
}
}
};
Expand Down
11 changes: 9 additions & 2 deletions lib/rules/no-label-var.js
Expand Up @@ -26,7 +26,11 @@ module.exports = {
url: "https://eslint.org/docs/rules/no-label-var"
},

schema: []
schema: [],

messages: {
identifierClashWithLabel: "Found identifier with same name as label."
}
},

create(context) {
Expand Down Expand Up @@ -62,7 +66,10 @@ module.exports = {
* with the innermost scope.
*/
if (findIdentifier(scope, node.label.name)) {
context.report({ node, message: "Found identifier with same name as label." });
context.report({
node,
messageId: "identifierClashWithLabel"
});
}
}

Expand Down
14 changes: 10 additions & 4 deletions lib/rules/no-labels.js
Expand Up @@ -40,7 +40,13 @@ module.exports = {
},
additionalProperties: false
}
]
],

messages: {
unexpectedLabel: "Unexpected labeled statement.",
unexpectedLabelInBreak: "Unexpected label in break statement.",
unexpectedLabelInContinue: "Unexpected label in continue statement."
}
},

create(context) {
Expand Down Expand Up @@ -113,7 +119,7 @@ module.exports = {
if (!isAllowed(scopeInfo.kind)) {
context.report({
node,
message: "Unexpected labeled statement."
messageId: "unexpectedLabel"
});
}

Expand All @@ -124,7 +130,7 @@ module.exports = {
if (node.label && !isAllowed(getKind(node.label.name))) {
context.report({
node,
message: "Unexpected label in break statement."
messageId: "unexpectedLabelInBreak"
});
}
},
Expand All @@ -133,7 +139,7 @@ module.exports = {
if (node.label && !isAllowed(getKind(node.label.name))) {
context.report({
node,
message: "Unexpected label in continue statement."
messageId: "unexpectedLabelInContinue"
});
}
}
Expand Down
14 changes: 11 additions & 3 deletions lib/rules/no-lone-blocks.js
Expand Up @@ -20,7 +20,12 @@ module.exports = {
url: "https://eslint.org/docs/rules/no-lone-blocks"
},

schema: []
schema: [],

messages: {
redundantBlock: "Block is redundant.",
redundantNestedBlock: "Nested block is redundant."
}
},

create(context) {
Expand All @@ -35,9 +40,12 @@ module.exports = {
* @returns {void}
*/
function report(node) {
const message = node.parent.type === "BlockStatement" ? "Nested block is redundant." : "Block is redundant.";
const messageId = node.parent.type === "BlockStatement" ? "redundantNestedBlock" : "redundantBlock";

context.report({ node, message });
context.report({
node,
messageId
});
}

/**
Expand Down
8 changes: 6 additions & 2 deletions lib/rules/no-lonely-if.js
Expand Up @@ -20,7 +20,11 @@ module.exports = {
},

schema: [],
fixable: "code"
fixable: "code",

messages: {
unexpectedLonelyIf: "Unexpected if as the only statement in an else block."
}
},

create(context) {
Expand All @@ -38,7 +42,7 @@ module.exports = {
parent === grandparent.alternate) {
context.report({
node,
message: "Unexpected if as the only statement in an else block.",
messageId: "unexpectedLonelyIf",
fix(fixer) {
const openingElseCurly = sourceCode.getFirstToken(parent);
const closingElseCurly = sourceCode.getLastToken(parent);
Expand Down

0 comments on commit 9dfc850

Please sign in to comment.