Skip to content

Commit

Permalink
Chore: use messages in some rules (refs eslint#9870)
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Mar 25, 2019
1 parent 4fe7eb7 commit 73f6f9e
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 54 deletions.
7 changes: 5 additions & 2 deletions lib/rules/no-func-assign.js
Expand Up @@ -22,7 +22,10 @@ module.exports = {
url: "https://eslint.org/docs/rules/no-func-assign"
},

schema: []
schema: [],
messages: {
unexpected: "'{{name}}' is a function."
}
},

create(context) {
Expand All @@ -34,7 +37,7 @@ 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: "unexpected", data: { name: reference.identifier.name } });
});
}

Expand Down
7 changes: 5 additions & 2 deletions lib/rules/no-global-assign.js
Expand Up @@ -32,7 +32,10 @@ module.exports = {
},
additionalProperties: false
}
]
],
messages: {
unexpected: "Read-only global '{{name}}' should not be modified."
}
},

create(context) {
Expand Down Expand Up @@ -60,7 +63,7 @@ module.exports = {
) {
context.report({
node: identifier,
message: "Read-only global '{{name}}' should not be modified.",
messageId: "unexpected",
data: identifier
});
}
Expand Down
5 changes: 4 additions & 1 deletion lib/rules/no-implicit-coercion.js
Expand Up @@ -187,7 +187,10 @@ module.exports = {
}
},
additionalProperties: false
}]
}],
messages: {
unexpected: "use `{{recommendation}}` instead."
}
},

create(context) {
Expand Down
7 changes: 5 additions & 2 deletions lib/rules/no-implied-eval.js
Expand Up @@ -20,7 +20,10 @@ module.exports = {
url: "https://eslint.org/docs/rules/no-implied-eval"
},

schema: []
schema: [],
messages: {
unexpected: "Implied eval. Consider passing a function instead of a string."
}
},

create(context) {
Expand Down Expand Up @@ -108,7 +111,7 @@ 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: "unexpected" });
}
}

Expand Down
7 changes: 5 additions & 2 deletions lib/rules/no-inner-declarations.js
Expand Up @@ -24,7 +24,10 @@ module.exports = {
{
enum: ["functions", "both"]
}
]
],
messages: {
unexpected: "Move {{type}} declaration to {{body}} root."
}
},

create(context) {
Expand Down Expand Up @@ -68,7 +71,7 @@ module.exports = {
if (!valid) {
context.report({
node,
message: "Move {{type}} declaration to {{body}} root.",
messageId: "unexpected",
data: {
type: (node.type === "FunctionDeclaration" ? "function" : "variable"),
body: (body.type === "Program" ? "program" : "function body")
Expand Down
16 changes: 11 additions & 5 deletions lib/rules/object-curly-newline.js
Expand Up @@ -162,7 +162,13 @@ module.exports = {
}
]
}
]
],
messages: {
expectedBeforeClosing: "Expected a line break before this closing brace.",
expectedAfterOpening: "Expected a line break after this opening brace.",
unexpectedBeforeClosing: "Unexpected line break before this closing brace.",
unexpectedAfterOpening: "Unexpected line break after this opening brace."
}
},

create(context) {
Expand Down Expand Up @@ -218,7 +224,7 @@ module.exports = {
if (needsLineBreaks) {
if (astUtils.isTokenOnSameLine(openBrace, first)) {
context.report({
message: "Expected a line break after this opening brace.",
messageId: "expectedAfterOpening",
node,
loc: openBrace.loc.start,
fix(fixer) {
Expand All @@ -232,7 +238,7 @@ module.exports = {
}
if (astUtils.isTokenOnSameLine(last, closeBrace)) {
context.report({
message: "Expected a line break before this closing brace.",
messageId: "expectedBeforeClosing",
node,
loc: closeBrace.loc.start,
fix(fixer) {
Expand All @@ -254,7 +260,7 @@ module.exports = {
(consistent && hasLineBreakBetweenOpenBraceAndFirst && !hasLineBreakBetweenCloseBraceAndLast)
) {
context.report({
message: "Unexpected line break after this opening brace.",
messageId: "unexpectedAfterOpening",
node,
loc: openBrace.loc.start,
fix(fixer) {
Expand All @@ -274,7 +280,7 @@ module.exports = {
(consistent && !hasLineBreakBetweenOpenBraceAndFirst && hasLineBreakBetweenCloseBraceAndLast)
) {
context.report({
message: "Unexpected line break before this closing brace.",
messageId: "unexpectedBeforeClosing",
node,
loc: closeBrace.loc.start,
fix(fixer) {
Expand Down
7 changes: 5 additions & 2 deletions lib/rules/require-await.js
Expand Up @@ -40,7 +40,10 @@ module.exports = {
url: "https://eslint.org/docs/rules/require-await"
},

schema: []
schema: [],
messages: {
expected: "{{name}} has no 'await' expression."
}
},

create(context) {
Expand Down Expand Up @@ -71,7 +74,7 @@ module.exports = {
context.report({
node,
loc: astUtils.getFunctionHeadLoc(node, sourceCode),
message: "{{name}} has no 'await' expression.",
messageId: "expected",
data: {
name: capitalizeFirstLetter(
astUtils.getFunctionNameWithKind(node)
Expand Down
16 changes: 11 additions & 5 deletions lib/rules/semi-spacing.js
Expand Up @@ -39,7 +39,13 @@ module.exports = {
},
additionalProperties: false
}
]
],
messages: {
missingBefore: "Missing whitespace before semicolon.",
missingAfter: "Missing whitespace after semicolon.",
unexpectedBefore: "Unexpected whitespace before semicolon.",
unexpectedAfter: "Unexpected whitespace after semicolon."
}
},

create(context) {
Expand Down Expand Up @@ -124,7 +130,7 @@ module.exports = {
context.report({
node,
loc: location,
message: "Unexpected whitespace before semicolon.",
messageId: "unexpectedBefore",
fix(fixer) {
const tokenBefore = sourceCode.getTokenBefore(token);

Expand All @@ -137,7 +143,7 @@ module.exports = {
context.report({
node,
loc: location,
message: "Missing whitespace before semicolon.",
messageId: "missingBefore",
fix(fixer) {
return fixer.insertTextBefore(token, " ");
}
Expand All @@ -151,7 +157,7 @@ module.exports = {
context.report({
node,
loc: location,
message: "Unexpected whitespace after semicolon.",
messageId: "unexpectedAfter",
fix(fixer) {
const tokenAfter = sourceCode.getTokenAfter(token);

Expand All @@ -164,7 +170,7 @@ module.exports = {
context.report({
node,
loc: location,
message: "Missing whitespace after semicolon.",
messageId: "missingAfter",
fix(fixer) {
return fixer.insertTextAfter(token, " ");
}
Expand Down
11 changes: 5 additions & 6 deletions lib/rules/semi-style.js
Expand Up @@ -75,6 +75,10 @@ module.exports = {
},

schema: [{ enum: ["last", "first"] }],
messages: {
expectedBeginning: "Expected this semicolon to be at the beginning of the next line.",
expectedEnd: "Expected this semicolon to be at the end of the previous line."
},
fixable: "whitespace"
},

Expand All @@ -97,12 +101,7 @@ module.exports = {
if ((expected === "last" && !prevIsSameLine) || (expected === "first" && !nextIsSameLine)) {
context.report({
loc: semiToken.loc,
message: "Expected this semicolon to be at {{pos}}.",
data: {
pos: (expected === "last")
? "the end of the previous line"
: "the beginning of the next line"
},
messageId: expected === "last" ? "expectedEnd" : "expectedBeginning",
fix(fixer) {
if (prevToken && nextToken && sourceCode.commentsExistBetween(prevToken, nextToken)) {
return null;
Expand Down
12 changes: 8 additions & 4 deletions lib/rules/semi.js
Expand Up @@ -67,6 +67,10 @@ module.exports = {
maxItems: 2
}
]
},
messages: {
missing: "Missing semicolon.",
unexpected: "Extra semicolon."
}
},

Expand All @@ -91,18 +95,18 @@ module.exports = {
*/
function report(node, missing) {
const lastToken = sourceCode.getLastToken(node);
let message,
let messageId,
fix,
loc = lastToken.loc;

if (!missing) {
message = "Missing semicolon.";
messageId = "missing";
loc = loc.end;
fix = function(fixer) {
return fixer.insertTextAfter(lastToken, ";");
};
} else {
message = "Extra semicolon.";
messageId = "unexpected";
loc = loc.start;
fix = function(fixer) {

Expand All @@ -120,7 +124,7 @@ module.exports = {
context.report({
node,
loc,
message,
messageId,
fix
});

Expand Down
11 changes: 8 additions & 3 deletions lib/rules/sort-imports.js
Expand Up @@ -49,6 +49,11 @@ module.exports = {
additionalProperties: false
}
],
messages: {
unsortedSyntax: "Expected '{{syntaxA}}' syntax before '{{syntaxB}}' syntax.",
unsortedImports: "Imports should be sorted alphabetically.",
unsortedMember: "Member '{{memberName}}' of the import declaration should be sorted alphabetically."
},

fixable: "code"
},
Expand Down Expand Up @@ -133,7 +138,7 @@ module.exports = {
if (currentMemberSyntaxGroupIndex < previousMemberSyntaxGroupIndex) {
context.report({
node,
message: "Expected '{{syntaxA}}' syntax before '{{syntaxB}}' syntax.",
messageId: "unsortedSyntax",
data: {
syntaxA: memberSyntaxSortOrder[currentMemberSyntaxGroupIndex],
syntaxB: memberSyntaxSortOrder[previousMemberSyntaxGroupIndex]
Expand All @@ -147,7 +152,7 @@ module.exports = {
) {
context.report({
node,
message: "Imports should be sorted alphabetically."
messageId: "unsortedImports"
});
}
}
Expand All @@ -164,7 +169,7 @@ module.exports = {
if (firstUnsortedIndex !== -1) {
context.report({
node: importSpecifiers[firstUnsortedIndex],
message: "Member '{{memberName}}' of the import declaration should be sorted alphabetically.",
messageId: "unsortedMember",
data: { memberName: importSpecifiers[firstUnsortedIndex].local.name },
fix(fixer) {
if (importSpecifiers.some(specifier =>
Expand Down
5 changes: 4 additions & 1 deletion lib/rules/sort-vars.js
Expand Up @@ -32,6 +32,9 @@ module.exports = {
additionalProperties: false
}
],
messages: {
unsorted: "Variables within the same declaration block should be sorted alphabetically."
},

fixable: "code"
},
Expand All @@ -56,7 +59,7 @@ module.exports = {
if (currentVariableName < lastVariableName) {
context.report({
node: decl,
message: "Variables within the same declaration block should be sorted alphabetically.",
messageId: "unsorted",
fix(fixer) {
if (unfixable || fixed) {
return null;
Expand Down
10 changes: 7 additions & 3 deletions lib/rules/space-before-blocks.js
Expand Up @@ -47,7 +47,11 @@ module.exports = {
}
]
}
]
],
messages: {
missing: "Missing space before opening brace.",
unexpected: "Unexpected space before opening brace."
}
},

create(context) {
Expand Down Expand Up @@ -115,15 +119,15 @@ module.exports = {
if (requireSpace && !hasSpace) {
context.report({
node,
message: "Missing space before opening brace.",
messageId: "missing",
fix(fixer) {
return fixer.insertTextBefore(node, " ");
}
});
} else if (requireNoSpace && hasSpace) {
context.report({
node,
message: "Unexpected space before opening brace.",
messageId: "unexpected",
fix(fixer) {
return fixer.removeRange([precedingToken.range[1], node.range[0]]);
}
Expand Down

0 comments on commit 73f6f9e

Please sign in to comment.