Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Use messageIds in some of the core rules #9648

Merged
merged 15 commits into from Feb 3, 2018
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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: {
noOpen: "There should be no linebreak after '['.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm struggling with these messageIds, partially because a name like noOpen might imply either that there is a linebreak but should not be (which is what "noOpen" means here), or that there is no linebreak but should be. I would suggest a name like shouldNotHaveLinebreakOpen or doesNotHaveLinebreakOpen (or slightly less verbose) to make it clear what "no" means here. I like the idea of having messageIds close to what the messages say, so of the two, I would probably favor shouldNotHaveLinebreakOpen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unexpectedOpeningLinebreak?

noClose: "There should be no linebreak before ']'.",
open: "A linebreak is required after '['.",
close: "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: "noOpen",
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: "noClose",
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: "open",
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: "close",
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: {
noAfter: "There should be no space after '{{tokenValue}}'.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See notes in array-bracket-newline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unexpectedSpaceAfter?

noBefore: "There should be no space before '{{tokenValue}}'.",
after: "A space is required after '{{tokenValue}}'.",
before: "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: "noAfter",
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: "noBefore",
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: "after",
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: "before",
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: {
noLineBreak: "There should be no linebreak here.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See notes in array-bracket-newline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unexpectedLineBreak?

lineBreak: "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: "noLineBreak",
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: "lineBreak",
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