Skip to content

Commit

Permalink
Chore: use meta.messages in some rules (1/4) (#10764)
Browse files Browse the repository at this point in the history
* New: consistent-meta-messages rule

* Chore: use messageId in rule for-direction

* Chore: use messageId in rule no-magic-numbers

* Chore: use messageId in rule func-call-spacing

* Chore: use messageId in rule func-name-matching

* Chore: use messageId in rule func-names

* Chore: use messageId in rule func-style

* Chore: use messageId in rule function-paren-newline

* Chore: use messageId in rule generator-star-spacing

* Chore: use messageId in rule getter-return

* Chore: separate messages for i18n

* Chore: fix linting errors

* Chore: temporally disable consistent-meta-messages

* Chore: accept review suggestions
  • Loading branch information
aladdin-add committed Aug 18, 2018
1 parent a857cd9 commit cb946af
Show file tree
Hide file tree
Showing 21 changed files with 479 additions and 708 deletions.
5 changes: 5 additions & 0 deletions .eslintrc.js
Expand Up @@ -32,6 +32,11 @@ module.exports = {
rules: {
"rulesdir/no-invalid-meta": "error",
"rulesdir/consistent-docs-description": "error"

/*
* TODO: enable it when all the rules using meta.messages
* "rulesdir/consistent-meta-messages": "error"
*/
}
}, {
files: ["lib/rules/*"],
Expand Down
7 changes: 5 additions & 2 deletions lib/rules/for-direction.js
Expand Up @@ -18,7 +18,10 @@ module.exports = {
url: "https://eslint.org/docs/rules/for-direction"
},
fixable: null,
schema: []
schema: [],
messages: {
incorrectDirection: "The update clause in this loop moves the variable in the wrong direction."
}
},

create(context) {
Expand All @@ -31,7 +34,7 @@ module.exports = {
function report(node) {
context.report({
node,
message: "The update clause in this loop moves the variable in the wrong direction."
messageId: "incorrectDirection"
});
}

Expand Down
10 changes: 7 additions & 3 deletions lib/rules/func-call-spacing.js
Expand Up @@ -57,6 +57,10 @@ module.exports = {
maxItems: 2
}
]
},
messages: {
unexpected: "Unexpected newline between function name and paren.",
missing: "Missing space between function name and paren."
}
},

Expand Down Expand Up @@ -116,7 +120,7 @@ module.exports = {
context.report({
node,
loc: lastCalleeToken.loc.start,
message: "Unexpected space between function name and paren.",
messageId: "unexpected",
fix(fixer) {

/*
Expand All @@ -134,7 +138,7 @@ module.exports = {
context.report({
node,
loc: lastCalleeToken.loc.start,
message: "Missing space between function name and paren.",
messageId: "missing",
fix(fixer) {
return fixer.insertTextBefore(parenToken, " ");
}
Expand All @@ -143,7 +147,7 @@ module.exports = {
context.report({
node,
loc: lastCalleeToken.loc.start,
message: "Unexpected newline between function name and paren.",
messageId: "unexpected",
fix(fixer) {
return fixer.replaceTextRange([prevToken.range[1], parenToken.range[0]], " ");
}
Expand Down
18 changes: 12 additions & 6 deletions lib/rules/func-name-matching.js
Expand Up @@ -87,6 +87,12 @@ module.exports = {
additionalItems: false,
items: [optionsObject]
}]
},
messages: {
matchProperty: "Function name `{{funcName}}` should match property name `{{name}}`",
matchVariable: "Function name `{{funcName}}` should match variable name `{{name}}`",
notMatchProperty: "Function name `{{funcName}}` should not match property name `{{name}}`",
notMatchVariable: "Function name `{{funcName}}` should not match variable name `{{name}}`"
}
},

Expand Down Expand Up @@ -132,20 +138,20 @@ module.exports = {
* @returns {void}
*/
function report(node, name, funcName, isProp) {
let message;
let messageId;

if (nameMatches === "always" && isProp) {
message = "Function name `{{funcName}}` should match property name `{{name}}`";
messageId = "matchProperty";
} else if (nameMatches === "always") {
message = "Function name `{{funcName}}` should match variable name `{{name}}`";
messageId = "matchVariable";
} else if (isProp) {
message = "Function name `{{funcName}}` should not match property name `{{name}}`";
messageId = "notMatchProperty";
} else {
message = "Function name `{{funcName}}` should not match variable name `{{name}}`";
messageId = "notMatchVariable";
}
context.report({
node,
message,
messageId,
data: {
name,
funcName
Expand Down
10 changes: 7 additions & 3 deletions lib/rules/func-names.js
Expand Up @@ -37,7 +37,11 @@ module.exports = {
{
enum: ["always", "as-needed", "never"]
}
]
],
messages: {
unnamed: "Unexpected unnamed {{name}}.",
named: "Unexpected named {{name}}."
}
},

create(context) {
Expand Down Expand Up @@ -96,15 +100,15 @@ module.exports = {
if (hasName) {
context.report({
node,
message: "Unexpected named {{name}}.",
messageId: "named",
data: { name }
});
}
} else {
if (!hasName && (asNeeded ? !hasInferredName(node) : !isObjectOrClassMethod(node))) {
context.report({
node,
message: "Unexpected unnamed {{name}}.",
messageId: "unnamed",
data: { name }
});
}
Expand Down
12 changes: 8 additions & 4 deletions lib/rules/func-style.js
Expand Up @@ -30,7 +30,11 @@ module.exports = {
},
additionalProperties: false
}
]
],
messages: {
expression: "Expected a function expression.",
declaration: "Expected a function declaration."
}
},

create(context) {
Expand All @@ -45,7 +49,7 @@ module.exports = {
stack.push(false);

if (!enforceDeclarations && node.parent.type !== "ExportDefaultDeclaration") {
context.report({ node, message: "Expected a function expression." });
context.report({ node, messageId: "expression" });
}
},
"FunctionDeclaration:exit"() {
Expand All @@ -56,7 +60,7 @@ module.exports = {
stack.push(false);

if (enforceDeclarations && node.parent.type === "VariableDeclarator") {
context.report({ node: node.parent, message: "Expected a function declaration." });
context.report({ node: node.parent, messageId: "declaration" });
}
},
"FunctionExpression:exit"() {
Expand All @@ -79,7 +83,7 @@ module.exports = {
const hasThisExpr = stack.pop();

if (enforceDeclarations && !hasThisExpr && node.parent.type === "VariableDeclarator") {
context.report({ node: node.parent, message: "Expected a function declaration." });
context.report({ node: node.parent, messageId: "declaration" });
}
};
}
Expand Down
16 changes: 11 additions & 5 deletions lib/rules/function-paren-newline.js
Expand Up @@ -41,7 +41,13 @@ module.exports = {
}
]
}
]
],
messages: {
expectedBefore: "Expected newline before ')'.",
expectedAfter: "Expected newline after '('.",
unexpectedBefore: "Unexpected newline before '('.",
unexpectedAfter: "Unexpected newline after ')'."
}
},

create(context) {
Expand Down Expand Up @@ -99,7 +105,7 @@ module.exports = {
if (hasLeftNewline && !needsNewlines) {
context.report({
node: leftParen,
message: "Unexpected newline after '('.",
messageId: "unexpectedAfter",
fix(fixer) {
return sourceCode.getText().slice(leftParen.range[1], tokenAfterLeftParen.range[0]).trim()

Expand All @@ -111,15 +117,15 @@ module.exports = {
} else if (!hasLeftNewline && needsNewlines) {
context.report({
node: leftParen,
message: "Expected a newline after '('.",
messageId: "expectedAfter",
fix: fixer => fixer.insertTextAfter(leftParen, "\n")
});
}

if (hasRightNewline && !needsNewlines) {
context.report({
node: rightParen,
message: "Unexpected newline before ')'.",
messageId: "unexpectedBefore",
fix(fixer) {
return sourceCode.getText().slice(tokenBeforeRightParen.range[1], rightParen.range[0]).trim()

Expand All @@ -131,7 +137,7 @@ module.exports = {
} else if (!hasRightNewline && needsNewlines) {
context.report({
node: rightParen,
message: "Expected a newline before ')'.",
messageId: "expectedBefore",
fix: fixer => fixer.insertTextBefore(rightParen, "\n")
});
}
Expand Down
27 changes: 18 additions & 9 deletions lib/rules/generator-star-spacing.js
Expand Up @@ -55,7 +55,13 @@ module.exports = {
}
]
}
]
],
messages: {
missingBefore: "Missing space before *.",
missingAfter: "Missing space after *.",
unexpectedBefore: "Unexpected space before *.",
unexpectedAfter: "Unexpected space after *."
}
},

create(context) {
Expand Down Expand Up @@ -119,6 +125,15 @@ module.exports = {
);
}

/**
* capitalize a given string.
* @param {string} str the given string.
* @returns {string} the capitalized string.
*/
function capitalize(str) {
return str[0].toUpperCase() + str.slice(1);
}

/**
* Checks the spacing between two tokens before or after the star token.
*
Expand All @@ -135,17 +150,11 @@ module.exports = {
const after = leftToken.value === "*";
const spaceRequired = modes[kind][side];
const node = after ? leftToken : rightToken;
const type = spaceRequired ? "Missing" : "Unexpected";
const message = "{{type}} space {{side}} *.";
const data = {
type,
side
};
const messageId = `${spaceRequired ? "missing" : "unexpected"}${capitalize(side)}`;

context.report({
node,
message,
data,
messageId,
fix(fixer) {
if (spaceRequired) {
if (after) {
Expand Down
12 changes: 7 additions & 5 deletions lib/rules/getter-return.js
Expand Up @@ -61,7 +61,11 @@ module.exports = {
},
additionalProperties: false
}
]
],
messages: {
expected: "Expected to return a value in {{name}}.",
expectedAlways: "Expected {{name}} to always return a value."
}
},

create(context) {
Expand Down Expand Up @@ -93,9 +97,7 @@ module.exports = {
context.report({
node,
loc: getId(node).loc.start,
message: funcInfo.hasReturn
? "Expected {{name}} to always return a value."
: "Expected to return a value in {{name}}.",
messageId: funcInfo.hasReturn ? "expectedAlways" : "expected",
data: {
name: astUtils.getFunctionNameWithKind(funcInfo.node)
}
Expand Down Expand Up @@ -161,7 +163,7 @@ module.exports = {
if (!options.allowImplicit && !node.argument) {
context.report({
node,
message: "Expected to return a value in {{name}}.",
messageId: "expected",
data: {
name: astUtils.getFunctionNameWithKind(funcInfo.node)
}
Expand Down
10 changes: 7 additions & 3 deletions lib/rules/no-magic-numbers.js
Expand Up @@ -39,7 +39,11 @@ module.exports = {
}
},
additionalProperties: false
}]
}],
messages: {
useConst: "Number constants declarations must use 'const'.",
noMagic: "No magic number: {{raw}}."
}
},

create(context) {
Expand Down Expand Up @@ -136,7 +140,7 @@ module.exports = {
if (enforceConst && parent.parent.kind !== "const") {
context.report({
node: fullNumberNode,
message: "Number constants declarations must use 'const'."
messageId: "useConst"
});
}
} else if (
Expand All @@ -145,7 +149,7 @@ module.exports = {
) {
context.report({
node: fullNumberNode,
message: "No magic number: {{raw}}.",
messageId: "noMagic",
data: {
raw
}
Expand Down
21 changes: 11 additions & 10 deletions tests/lib/rules/for-direction.js
Expand Up @@ -17,6 +17,7 @@ const RuleTester = require("../../../lib/testers/rule-tester");
//------------------------------------------------------------------------------

const ruleTester = new RuleTester();
const incorrectDirection = { messageId: "incorrectDirection" };

ruleTester.run("for-direction", rule, {
valid: [
Expand Down Expand Up @@ -52,17 +53,17 @@ ruleTester.run("for-direction", rule, {
invalid: [

// test if '++', '--'
{ code: "for(var i = 0; i < 10; i--){}", errors: [{ message: "The update clause in this loop moves the variable in the wrong direction." }] },
{ code: "for(var i = 0; i <= 10; i--){}", errors: [{ message: "The update clause in this loop moves the variable in the wrong direction." }] },
{ code: "for(var i = 10; i > 10; i++){}", errors: [{ message: "The update clause in this loop moves the variable in the wrong direction." }] },
{ code: "for(var i = 10; i >= 0; i++){}", errors: [{ message: "The update clause in this loop moves the variable in the wrong direction." }] },
{ code: "for(var i = 0; i < 10; i--){}", errors: [incorrectDirection] },
{ code: "for(var i = 0; i <= 10; i--){}", errors: [incorrectDirection] },
{ code: "for(var i = 10; i > 10; i++){}", errors: [incorrectDirection] },
{ code: "for(var i = 10; i >= 0; i++){}", errors: [incorrectDirection] },

// test if '+=', '-='
{ code: "for(var i = 0; i < 10; i-=1){}", errors: [{ message: "The update clause in this loop moves the variable in the wrong direction." }] },
{ code: "for(var i = 0; i <= 10; i-=1){}", errors: [{ message: "The update clause in this loop moves the variable in the wrong direction." }] },
{ code: "for(var i = 10; i > 10; i+=1){}", errors: [{ message: "The update clause in this loop moves the variable in the wrong direction." }] },
{ code: "for(var i = 10; i >= 0; i+=1){}", errors: [{ message: "The update clause in this loop moves the variable in the wrong direction." }] },
{ code: "for (var i = 0; i < MAX; i -= STEP_SIZE);", errors: [{ message: "The update clause in this loop moves the variable in the wrong direction." }] },
{ code: "for (var i = 0; i > MIN; i += STEP_SIZE);", errors: [{ message: "The update clause in this loop moves the variable in the wrong direction." }] }
{ code: "for(var i = 0; i < 10; i-=1){}", errors: [incorrectDirection] },
{ code: "for(var i = 0; i <= 10; i-=1){}", errors: [incorrectDirection] },
{ code: "for(var i = 10; i > 10; i+=1){}", errors: [incorrectDirection] },
{ code: "for(var i = 10; i >= 0; i+=1){}", errors: [incorrectDirection] },
{ code: "for (var i = 0; i < MAX; i -= STEP_SIZE);", errors: [incorrectDirection] },
{ code: "for (var i = 0; i > MIN; i += STEP_SIZE);", errors: [incorrectDirection] }
]
});

0 comments on commit cb946af

Please sign in to comment.