Skip to content

Commit

Permalink
Chore: Use messageIds in some of the core rules (#9648)
Browse files Browse the repository at this point in the history
* Chore: Update accessor-pairs to use messageIds

* Chore: Convert no-alert report() to new-style

* Chore: Update rules starting with 'a' or 'no-a' to use messageIds

* Chore: Update rules starting with 'b' or 'no-b' to use messageIds

* Chore: specify type in no-control-regex.getRegExp

* Chore: Update rules starting with 'c' or 'no-c' to use messageIds

* Chore: Update rules starting with 'd' or 'no-d' to use messageIds

Exception: no-duplicate-imports has more-dynamic rule message generation, which I don’t want to tamper with.

* Chore: Update rules starting with 'e' or 'no-e' to use messageIds

* Chore: Correct the invalid tests in no-empty-function for use with messageIds

* Chore: Fix lint errors

* Inline functions only used once

* Update message names as suggested by @platinumazure

* Fix error

* preferable
  • Loading branch information
j-f1 authored and platinumazure committed Feb 3, 2018
1 parent 1aa1970 commit bb213dc
Show file tree
Hide file tree
Showing 135 changed files with 2,490 additions and 1,872 deletions.
10 changes: 7 additions & 3 deletions lib/rules/accessor-pairs.js
Expand Up @@ -89,7 +89,11 @@ module.exports = {
}
},
additionalProperties: false
}]
}],
messages: {
getter: "Getter is not present.",
setter: "Setter is not present."
}
},
create(context) {
const config = context.options[0] || {};
Expand Down Expand Up @@ -140,9 +144,9 @@ module.exports = {
}

if (checkSetWithoutGet && isSetPresent && !isGetPresent) {
context.report({ node, message: "Getter is not present." });
context.report({ node, messageId: "getter" });
} else if (checkGetWithoutSet && isGetPresent && !isSetPresent) {
context.report({ node, message: "Setter is not present." });
context.report({ node, messageId: "setter" });
}
}

Expand Down
16 changes: 11 additions & 5 deletions lib/rules/array-bracket-newline.js
Expand Up @@ -41,7 +41,13 @@ module.exports = {
}
]
}
]
],
messages: {
unexpectedOpeningLinebreak: "There should be no linebreak after '['.",
unexpectedClosingLinebreak: "There should be no linebreak before ']'.",
missingOpeningLinebreak: "A linebreak is required after '['.",
missingClosingLinebreak: "A linebreak is required before ']'."
}
},

create(context) {
Expand Down Expand Up @@ -106,7 +112,7 @@ module.exports = {
context.report({
node,
loc: token.loc,
message: "There should be no linebreak after '['.",
messageId: "unexpectedOpeningLinebreak",
fix(fixer) {
const nextToken = sourceCode.getTokenAfter(token, { includeComments: true });

Expand All @@ -129,7 +135,7 @@ module.exports = {
context.report({
node,
loc: token.loc,
message: "There should be no linebreak before ']'.",
messageId: "unexpectedClosingLinebreak",
fix(fixer) {
const previousToken = sourceCode.getTokenBefore(token, { includeComments: true });

Expand All @@ -152,7 +158,7 @@ module.exports = {
context.report({
node,
loc: token.loc,
message: "A linebreak is required after '['.",
messageId: "missingOpeningLinebreak",
fix(fixer) {
return fixer.insertTextAfter(token, "\n");
}
Expand All @@ -169,7 +175,7 @@ module.exports = {
context.report({
node,
loc: token.loc,
message: "A linebreak is required before ']'.",
messageId: "missingClosingLinebreak",
fix(fixer) {
return fixer.insertTextBefore(token, "\n");
}
Expand Down
16 changes: 11 additions & 5 deletions lib/rules/array-bracket-spacing.js
Expand Up @@ -38,7 +38,13 @@ module.exports = {
},
additionalProperties: false
}
]
],
messages: {
unexpectedSpaceAfter: "There should be no space after '{{tokenValue}}'.",
unexpectedSpaceBefore: "There should be no space before '{{tokenValue}}'.",
missingSpaceAfter: "A space is required after '{{tokenValue}}'.",
missingSpaceBefore: "A space is required before '{{tokenValue}}'."
}
},
create(context) {
const spaced = context.options[0] === "always",
Expand Down Expand Up @@ -76,7 +82,7 @@ module.exports = {
context.report({
node,
loc: token.loc.start,
message: "There should be no space after '{{tokenValue}}'.",
messageId: "unexpectedSpaceAfter",
data: {
tokenValue: token.value
},
Expand All @@ -98,7 +104,7 @@ module.exports = {
context.report({
node,
loc: token.loc.start,
message: "There should be no space before '{{tokenValue}}'.",
messageId: "unexpectedSpaceBefore",
data: {
tokenValue: token.value
},
Expand All @@ -120,7 +126,7 @@ module.exports = {
context.report({
node,
loc: token.loc.start,
message: "A space is required after '{{tokenValue}}'.",
messageId: "missingSpaceAfter",
data: {
tokenValue: token.value
},
Expand All @@ -140,7 +146,7 @@ module.exports = {
context.report({
node,
loc: token.loc.start,
message: "A space is required before '{{tokenValue}}'.",
messageId: "missingSpaceBefore",
data: {
tokenValue: token.value
},
Expand Down
16 changes: 11 additions & 5 deletions lib/rules/array-callback-return.js
Expand Up @@ -156,7 +156,13 @@ module.exports = {
},
additionalProperties: false
}
]
],

messages: {
expectedAtEnd: "Expected to return a value at the end of {{name}}.",
expectedInside: "Expected to return a value in {{name}}.",
expectedReturnValue: "{{name}} expected a return value."
}
},

create(context) {
Expand Down Expand Up @@ -188,9 +194,9 @@ module.exports = {
context.report({
node,
loc: getLocation(node, context.getSourceCode()).loc.start,
message: funcInfo.hasReturn
? "Expected to return a value at the end of {{name}}."
: "Expected to return a value in {{name}}.",
messageId: funcInfo.hasReturn
? "expectedAtEnd"
: "expectedInside",
data: {
name: astUtils.getFunctionNameWithKind(funcInfo.node)
}
Expand Down Expand Up @@ -230,7 +236,7 @@ module.exports = {
if (!options.allowImplicit && !node.argument) {
context.report({
node,
message: "{{name}} expected a return value.",
messageId: "expectedReturnValue",
data: {
name: lodash.upperFirst(astUtils.getFunctionNameWithKind(funcInfo.node))
}
Expand Down
11 changes: 8 additions & 3 deletions lib/rules/array-element-newline.js
Expand Up @@ -41,7 +41,12 @@ module.exports = {
}
]
}
]
],

messages: {
unexpectedLineBreak: "There should be no linebreak here.",
missingLineBreak: "There should be a linebreak after this element."
}
},

create(context) {
Expand Down Expand Up @@ -100,7 +105,7 @@ module.exports = {
start: tokenBefore.loc.end,
end: token.loc.start
},
message: "There should be no linebreak here.",
messageId: "unexpectedLineBreak",
fix(fixer) {
if (astUtils.isCommentToken(tokenBefore)) {
return null;
Expand Down Expand Up @@ -149,7 +154,7 @@ module.exports = {
start: tokenBefore.loc.end,
end: token.loc.start
},
message: "There should be a linebreak after this element.",
messageId: "missingLineBreak",
fix(fixer) {
return fixer.replaceTextRange([tokenBefore.range[1], token.range[0]], "\n");
}
Expand Down
24 changes: 16 additions & 8 deletions lib/rules/arrow-body-style.js
Expand Up @@ -55,7 +55,15 @@ module.exports = {
]
},

fixable: "code"
fixable: "code",

messages: {
unexpectedOtherBlock: "Unexpected block statement surrounding arrow body.",
unexpectedEmptyBlock: "Unexpected block statement surrounding arrow body; put a value of `undefined` immediately after the `=>`.",
unexpectedObjectBlock: "Unexpected block statement surrounding arrow body; parenthesize the returned value and move it immediately after the `=>`.",
unexpectedSingleBlock: "Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`.",
expectedBlock: "Expected block statement surrounding arrow body."
}
},

create(context) {
Expand Down Expand Up @@ -110,22 +118,22 @@ module.exports = {
}

if (never || asNeeded && blockBody[0].type === "ReturnStatement") {
let message;
let messageId;

if (blockBody.length === 0) {
message = "Unexpected block statement surrounding arrow body; put a value of `undefined` immediately after the `=>`.";
messageId = "unexpectedEmptyBlock";
} else if (blockBody.length > 1) {
message = "Unexpected block statement surrounding arrow body.";
messageId = "unexpectedOtherBlock";
} else if (astUtils.isOpeningBraceToken(sourceCode.getFirstToken(blockBody[0], { skip: 1 }))) {
message = "Unexpected block statement surrounding arrow body; parenthesize the returned value and move it immediately after the `=>`.";
messageId = "unexpectedObjectBlock";
} else {
message = "Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`.";
messageId = "unexpectedSingleBlock";
}

context.report({
node,
loc: arrowBody.loc.start,
message,
messageId,
fix(fixer) {
const fixes = [];

Expand Down Expand Up @@ -190,7 +198,7 @@ module.exports = {
context.report({
node,
loc: arrowBody.loc.start,
message: "Expected block statement surrounding arrow body.",
messageId: "expectedBlock",
fix(fixer) {
const fixes = [];
const arrowToken = sourceCode.getTokenBefore(arrowBody, astUtils.isArrowToken);
Expand Down
22 changes: 13 additions & 9 deletions lib/rules/arrow-parens.js
Expand Up @@ -38,15 +38,19 @@ module.exports = {
},
additionalProperties: false
}
]
],

messages: {
unexpectedParens: "Unexpected parentheses around single function argument.",
expectedParens: "Expected parentheses around arrow function argument.",

unexpectedParensInline: "Unexpected parentheses around single function argument having a body with no curly braces.",
expectedParensBlock: "Expected parentheses around arrow function argument having a body with curly braces."
}
},

create(context) {
const message = "Expected parentheses around arrow function argument.";
const asNeededMessage = "Unexpected parentheses around single function argument.";
const asNeeded = context.options[0] === "as-needed";
const requireForBlockBodyMessage = "Unexpected parentheses around single function argument having a body with no curly braces";
const requireForBlockBodyNoParensMessage = "Expected parentheses around arrow function argument having a body with curly braces.";
const requireForBlockBody = asNeeded && context.options[1] && context.options[1].requireForBlockBody === true;

const sourceCode = context.getSourceCode();
Expand Down Expand Up @@ -94,7 +98,7 @@ module.exports = {
if (astUtils.isOpeningParenToken(firstTokenOfParam)) {
context.report({
node,
message: requireForBlockBodyMessage,
messageId: "unexpectedParensInline",
fix: fixParamsWithParenthesis
});
}
Expand All @@ -108,7 +112,7 @@ module.exports = {
if (!astUtils.isOpeningParenToken(firstTokenOfParam)) {
context.report({
node,
message: requireForBlockBodyNoParensMessage,
messageId: "expectedParensBlock",
fix(fixer) {
return fixer.replaceText(firstTokenOfParam, `(${firstTokenOfParam.value})`);
}
Expand All @@ -127,7 +131,7 @@ module.exports = {
if (astUtils.isOpeningParenToken(firstTokenOfParam)) {
context.report({
node,
message: asNeededMessage,
messageId: "unexpectedParens",
fix: fixParamsWithParenthesis
});
}
Expand All @@ -141,7 +145,7 @@ module.exports = {
if (after.value !== ")") {
context.report({
node,
message,
messageId: "expectedParens",
fix(fixer) {
return fixer.replaceText(firstTokenOfParam, `(${firstTokenOfParam.value})`);
}
Expand Down
18 changes: 13 additions & 5 deletions lib/rules/arrow-spacing.js
Expand Up @@ -38,7 +38,15 @@ module.exports = {
},
additionalProperties: false
}
]
],

messages: {
expectedBefore: "Missing space before =>.",
unexpectedBefore: "Unexpected space before =>.",

expectedAfter: "Missing space after =>.",
unexpectedAfter: "Unexpected space after =>."
}
},

create(context) {
Expand Down Expand Up @@ -96,7 +104,7 @@ module.exports = {
if (countSpace.before === 0) {
context.report({
node: tokens.before,
message: "Missing space before =>.",
messageId: "expectedBefore",
fix(fixer) {
return fixer.insertTextBefore(tokens.arrow, " ");
}
Expand All @@ -108,7 +116,7 @@ module.exports = {
if (countSpace.before > 0) {
context.report({
node: tokens.before,
message: "Unexpected space before =>.",
messageId: "unexpectedBefore",
fix(fixer) {
return fixer.removeRange([tokens.before.range[1], tokens.arrow.range[0]]);
}
Expand All @@ -122,7 +130,7 @@ module.exports = {
if (countSpace.after === 0) {
context.report({
node: tokens.after,
message: "Missing space after =>.",
messageId: "expectedAfter",
fix(fixer) {
return fixer.insertTextAfter(tokens.arrow, " ");
}
Expand All @@ -134,7 +142,7 @@ module.exports = {
if (countSpace.after > 0) {
context.report({
node: tokens.after,
message: "Unexpected space after =>.",
messageId: "unexpectedAfter",
fix(fixer) {
return fixer.removeRange([tokens.arrow.range[1], tokens.after.range[0]]);
}
Expand Down
8 changes: 6 additions & 2 deletions lib/rules/block-scoped-var.js
Expand Up @@ -17,7 +17,11 @@ module.exports = {
url: "https://eslint.org/docs/rules/block-scoped-var"
},

schema: []
schema: [],

messages: {
outOfScope: "'{{name}}' used outside of binding context."
}
},

create(context) {
Expand Down Expand Up @@ -48,7 +52,7 @@ module.exports = {
function report(reference) {
const identifier = reference.identifier;

context.report({ node: identifier, message: "'{{name}}' used outside of binding context.", data: { name: identifier.name } });
context.report({ node: identifier, messageId: "outOfScope", data: { name: identifier.name } });
}

/**
Expand Down

0 comments on commit bb213dc

Please sign in to comment.