Skip to content

Commit

Permalink
Chore: Update rules starting with 'c' or 'no-c' to use messageIds
Browse files Browse the repository at this point in the history
  • Loading branch information
j-f1 committed Sep 4, 2017
1 parent 29e50da commit d8dfb7c
Show file tree
Hide file tree
Showing 50 changed files with 889 additions and 646 deletions.
8 changes: 6 additions & 2 deletions lib/rules/callback-return.js
Expand Up @@ -19,7 +19,11 @@ module.exports = {
schema: [{
type: "array",
items: { type: "string" }
}]
}],

messages: {
missingReturn: "Expected return with your callback function."
}
},

create(context) {
Expand Down Expand Up @@ -164,7 +168,7 @@ module.exports = {

// as long as you're the child of a function at this point you should be asked to return
if (findClosestParentOfType(node, ["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"])) {
context.report({ node, message: "Expected return with your callback function." });
context.report({ node, messageId: "missingReturn" });
}

}
Expand Down
8 changes: 6 additions & 2 deletions lib/rules/camelcase.js
Expand Up @@ -27,7 +27,11 @@ module.exports = {
},
additionalProperties: false
}
]
],

messages: {
notCamelCase: "Identifier '{{name}}' is not in camel case."
}
},

create(context) {
Expand Down Expand Up @@ -61,7 +65,7 @@ module.exports = {
function report(node) {
if (reported.indexOf(node) < 0) {
reported.push(node);
context.report({ node, message: "Identifier '{{name}}' is not in camel case.", data: { name: node.name } });
context.report({ node, messageId: "notCamelCase", data: { name: node.name } });
}
}

Expand Down
19 changes: 11 additions & 8 deletions lib/rules/capitalized-comments.js
Expand Up @@ -15,9 +15,7 @@ const astUtils = require("../ast-utils");
// Helpers
//------------------------------------------------------------------------------

const ALWAYS_MESSAGE = "Comments should not begin with a lowercase character",
NEVER_MESSAGE = "Comments should not begin with an uppercase character",
DEFAULT_IGNORE_PATTERN = astUtils.COMMENTS_IGNORE_PATTERN,
const DEFAULT_IGNORE_PATTERN = astUtils.COMMENTS_IGNORE_PATTERN,
WHITESPACE = /\s/g,
MAYBE_URL = /^\s*[^:/?#\s]+:\/\/[^?#]/, // TODO: Combine w/ max-len pattern?
DEFAULTS = {
Expand Down Expand Up @@ -131,7 +129,12 @@ module.exports = {
}
]
}
]
],

messages: {
lowercase: "Comments should not begin with a lowercase character",
uppercase: "Comments should not begin with an uppercase character"
}
},

create(context) {
Expand Down Expand Up @@ -265,14 +268,14 @@ module.exports = {
commentValid = isCommentValid(comment, options);

if (!commentValid) {
const message = capitalize === "always"
? ALWAYS_MESSAGE
: NEVER_MESSAGE;
const messageId = capitalize === "always"
? "lowercase"
: "uppercase";

context.report({
node: null, // Intentionally using loc instead
loc: comment.loc,
message,
messageId,
fix(fixer) {
const match = comment.value.match(LETTER_PATTERN);

Expand Down
10 changes: 7 additions & 3 deletions lib/rules/class-methods-use-this.js
Expand Up @@ -27,7 +27,11 @@ module.exports = {
}
},
additionalProperties: false
}]
}],

messages: {
missingThis: "Expected 'this' to be used by class method '{{name}}'."
}
},
create(context) {
const config = context.options[0] ? Object.assign({}, context.options[0]) : {};
Expand Down Expand Up @@ -79,9 +83,9 @@ module.exports = {
if (isIncludedInstanceMethod(node.parent) && !methodUsesThis) {
context.report({
node,
message: "Expected 'this' to be used by class method '{{classMethod}}'.",
messageId: "missingThis",
data: {
classMethod: node.parent.key.name
name: node.parent.key.name
}
});
}
Expand Down
11 changes: 7 additions & 4 deletions lib/rules/comma-dangle.js
Expand Up @@ -123,14 +123,17 @@ module.exports = {
]
}
]
},

messages: {
unexpected: "Unexpected trailing comma.",
missing: "Missing trailing comma."
}
},

create(context) {
const options = normalizeOptions(context.options[0]);
const sourceCode = context.getSourceCode();
const UNEXPECTED_MESSAGE = "Unexpected trailing comma.";
const MISSING_MESSAGE = "Missing trailing comma.";

/**
* Gets the last item of the given node.
Expand Down Expand Up @@ -229,7 +232,7 @@ module.exports = {
context.report({
node: lastItem,
loc: trailingToken.loc.start,
message: UNEXPECTED_MESSAGE,
messageId: "unexpected",
fix(fixer) {
return fixer.remove(trailingToken);
}
Expand Down Expand Up @@ -266,7 +269,7 @@ module.exports = {
context.report({
node: lastItem,
loc: trailingToken.loc.end,
message: MISSING_MESSAGE,
messageId: "missing",
fix(fixer) {
return fixer.insertTextAfter(trailingToken, ",");
}
Expand Down
23 changes: 13 additions & 10 deletions lib/rules/comma-spacing.js
Expand Up @@ -33,7 +33,12 @@ module.exports = {
},
additionalProperties: false
}
]
],

messages: {
missing: "A space is required {{loc}} ','.",
unexpected: "There should be no space {{loc}} ','."
}
},

create(context) {
Expand All @@ -56,17 +61,17 @@ module.exports = {
/**
* Reports a spacing error with an appropriate message.
* @param {ASTNode} node The binary expression node to report.
* @param {string} dir Is the error "before" or "after" the comma?
* @param {string} loc Is the error "before" or "after" the comma?
* @param {ASTNode} otherNode The node at the left or right of `node`
* @returns {void}
* @private
*/
function report(node, dir, otherNode) {
function report(node, loc, otherNode) {
context.report({
node,
fix(fixer) {
if (options[dir]) {
if (dir === "before") {
if (options[loc]) {
if (loc === "before") {
return fixer.insertTextBefore(node, " ");
}
return fixer.insertTextAfter(node, " ");
Expand All @@ -75,7 +80,7 @@ module.exports = {
let start, end;
const newText = "";

if (dir === "before") {
if (loc === "before") {
start = otherNode.range[1];
end = node.range[0];
} else {
Expand All @@ -86,11 +91,9 @@ module.exports = {
return fixer.replaceTextRange([start, end], newText);

},
message: options[dir]
? "A space is required {{dir}} ','."
: "There should be no space {{dir}} ','.",
messageId: options[loc] ? "missing" : "unexpected",
data: {
dir
loc
}
});
}
Expand Down
13 changes: 9 additions & 4 deletions lib/rules/comma-style.js
Expand Up @@ -35,7 +35,12 @@ module.exports = {
},
additionalProperties: false
}
]
],
messages: {
beforeAndAfter: "Bad line breaking before and after ','.",
after: "',' should be placed first.",
before: "',' should be placed last."
}
},

create(context) {
Expand Down Expand Up @@ -133,15 +138,15 @@ module.exports = {
line: commaToken.loc.end.line,
column: commaToken.loc.start.column
},
message: "Bad line breaking before and after ','.",
messageId: "beforeAndAfter",
fix: getFixerFunction("between", previousItemToken, commaToken, currentItemToken)
});

} else if (style === "first" && !astUtils.isTokenOnSameLine(commaToken, currentItemToken)) {

context.report({
node: reportItem,
message: "',' should be placed first.",
messageId: "after",
fix: getFixerFunction(style, previousItemToken, commaToken, currentItemToken)
});

Expand All @@ -153,7 +158,7 @@ module.exports = {
line: commaToken.loc.end.line,
column: commaToken.loc.end.column
},
message: "',' should be placed last.",
messageId: "before",
fix: getFixerFunction(style, previousItemToken, commaToken, currentItemToken)
});
}
Expand Down
8 changes: 6 additions & 2 deletions lib/rules/complexity.js
Expand Up @@ -49,7 +49,11 @@ module.exports = {
}
]
}
]
],

messages: {
complex: "{{name}} has a complexity of {{complexity}}."
}
},

create(context) {
Expand Down Expand Up @@ -95,7 +99,7 @@ module.exports = {
if (complexity > THRESHOLD) {
context.report({
node,
message: "{{name}} has a complexity of {{complexity}}.",
messageId: "complex",
data: { name, complexity }
});
}
Expand Down
18 changes: 13 additions & 5 deletions lib/rules/computed-property-spacing.js
Expand Up @@ -24,7 +24,15 @@ module.exports = {
{
enum: ["always", "never"]
}
]
],

messages: {
before: "There should be no space before '{{tokenValue}}'.",
after: "There should be no space after '{{tokenValue}}'.",

missingBefore: "A space is required before '{{tokenValue}}'.",
missingAfter: "A space is required after '{{tokenValue}}'."
}
},

create(context) {
Expand All @@ -46,7 +54,7 @@ module.exports = {
context.report({
node,
loc: token.loc.start,
message: "There should be no space after '{{tokenValue}}'.",
messageId: "after",
data: {
tokenValue: token.value
},
Expand All @@ -67,7 +75,7 @@ module.exports = {
context.report({
node,
loc: token.loc.start,
message: "There should be no space before '{{tokenValue}}'.",
messageId: "before",
data: {
tokenValue: token.value
},
Expand All @@ -87,7 +95,7 @@ module.exports = {
context.report({
node,
loc: token.loc.start,
message: "A space is required after '{{tokenValue}}'.",
messageId: "missingAfter",
data: {
tokenValue: token.value
},
Expand All @@ -107,7 +115,7 @@ module.exports = {
context.report({
node,
loc: token.loc.start,
message: "A space is required before '{{tokenValue}}'.",
messageId: "missingBefore",
data: {
tokenValue: token.value
},
Expand Down
19 changes: 12 additions & 7 deletions lib/rules/consistent-return.js
Expand Up @@ -67,7 +67,13 @@ module.exports = {
}
},
additionalProperties: false
}]
}],

messages: {
missingReturn: "Expected to return a value at the end of {{name}}.",
missingReturnValue: "{{name}} expected a return value.",
unexpectedReturnValue: "{{name}} expected no return value."
}
},

create(context) {
Expand Down Expand Up @@ -128,7 +134,7 @@ module.exports = {
context.report({
node,
loc,
message: "Expected to return a value at the end of {{name}}.",
messageId: "missingReturn",
data: { name }
});
}
Expand All @@ -142,7 +148,7 @@ module.exports = {
codePath,
hasReturn: false,
hasReturnValue: false,
message: "",
messageId: "",
node
};
},
Expand All @@ -162,17 +168,16 @@ module.exports = {
if (!funcInfo.hasReturn) {
funcInfo.hasReturn = true;
funcInfo.hasReturnValue = hasReturnValue;
funcInfo.message = "{{name}} expected {{which}} return value.";
funcInfo.messageId = hasReturnValue ? "missingReturnValue" : "unexpectedReturnValue";
funcInfo.data = {
name: funcInfo.node.type === "Program"
? "Program"
: lodash.upperFirst(astUtils.getFunctionNameWithKind(funcInfo.node)),
which: hasReturnValue ? "a" : "no"
: lodash.upperFirst(astUtils.getFunctionNameWithKind(funcInfo.node))
};
} else if (funcInfo.hasReturnValue !== hasReturnValue) {
context.report({
node,
message: funcInfo.message,
messageId: funcInfo.messageId,
data: funcInfo.data
});
}
Expand Down

0 comments on commit d8dfb7c

Please sign in to comment.