Skip to content

Commit

Permalink
Chore: Rule messages use internal rule message format (fixes eslint#6977
Browse files Browse the repository at this point in the history
  • Loading branch information
platinumazure authored and ilyavolodin committed Sep 2, 2016
1 parent 46cb690 commit 4126f12
Show file tree
Hide file tree
Showing 49 changed files with 475 additions and 134 deletions.
20 changes: 16 additions & 4 deletions lib/rules/array-bracket-spacing.js
Expand Up @@ -75,7 +75,10 @@ module.exports = {
context.report({
node,
loc: token.loc.start,
message: "There should be no space after '" + token.value + "'.",
message: "There should be no space after '{{tokenValue}}'.",
data: {
tokenValue: token.value
},
fix(fixer) {
const nextToken = sourceCode.getTokenAfter(token);

Expand All @@ -94,7 +97,10 @@ module.exports = {
context.report({
node,
loc: token.loc.start,
message: "There should be no space before '" + token.value + "'.",
message: "There should be no space before '{{tokenValue}}'.",
data: {
tokenValue: token.value
},
fix(fixer) {
const previousToken = sourceCode.getTokenBefore(token);

Expand All @@ -113,7 +119,10 @@ module.exports = {
context.report({
node,
loc: token.loc.start,
message: "A space is required after '" + token.value + "'.",
message: "A space is required after '{{tokenValue}}'.",
data: {
tokenValue: token.value
},
fix(fixer) {
return fixer.insertTextAfter(token, " ");
}
Expand All @@ -130,7 +139,10 @@ module.exports = {
context.report({
node,
loc: token.loc.start,
message: "A space is required before '" + token.value + "'.",
message: "A space is required before '{{tokenValue}}'.",
data: {
tokenValue: token.value
},
fix(fixer) {
return fixer.insertTextBefore(token, " ");
}
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/arrow-parens.js
Expand Up @@ -87,7 +87,7 @@ module.exports = {
node,
message: requireForBlockBodyNoParensMessage,
fix(fixer) {
return fixer.replaceText(token, "(" + token.value + ")");
return fixer.replaceText(token, `(${token.value})`);
}
});
}
Expand Down Expand Up @@ -123,7 +123,7 @@ module.exports = {
node,
message,
fix(fixer) {
return fixer.replaceText(token, "(" + token.value + ")");
return fixer.replaceText(token, `(${token.value})`);
}
});
}
Expand Down
10 changes: 8 additions & 2 deletions lib/rules/block-spacing.js
Expand Up @@ -97,7 +97,10 @@ module.exports = {
context.report({
node,
loc: openBrace.loc.start,
message: message + " after '{'.",
message: "{{message}} after '{'.",
data: {
message
},
fix(fixer) {
if (always) {
return fixer.insertTextBefore(firstToken, " ");
Expand All @@ -111,7 +114,10 @@ module.exports = {
context.report({
node,
loc: closeBrace.loc.start,
message: message + " before '}'.",
message: "{{message}} before '}'.",
data: {
message
},
fix(fixer) {
if (always) {
return fixer.insertTextAfter(lastToken, " ");
Expand Down
8 changes: 7 additions & 1 deletion lib/rules/class-methods-use-this.js
Expand Up @@ -53,7 +53,13 @@ module.exports = {
const methodUsesThis = stack.pop();

if (isInstanceMethod(node.parent) && !methodUsesThis) {
context.report(node, "Expected 'this' to be used by class method '" + node.parent.key.name + "'.");
context.report({
node,
message: "Expected 'this' to be used by class method '{{classMethod}}'.",
data: {
classMethod: node.parent.key.name
}
});
}
}

Expand Down
7 changes: 5 additions & 2 deletions lib/rules/comma-spacing.js
Expand Up @@ -97,8 +97,11 @@ module.exports = {
}
},
message: options[dir] ?
"A space is required " + dir + " ','." :
"There should be no space " + dir + " ','."
"A space is required {{dir}} ','." :
"There should be no space {{dir}} ','.",
data: {
dir
}
});
}

Expand Down
20 changes: 16 additions & 4 deletions lib/rules/computed-property-spacing.js
Expand Up @@ -46,7 +46,10 @@ module.exports = {
context.report({
node,
loc: token.loc.start,
message: "There should be no space after '" + token.value + "'.",
message: "There should be no space after '{{tokenValue}}'.",
data: {
tokenValue: token.value
},
fix(fixer) {
return fixer.removeRange([token.range[1], tokenAfter.range[0]]);
}
Expand All @@ -64,7 +67,10 @@ module.exports = {
context.report({
node,
loc: token.loc.start,
message: "There should be no space before '" + token.value + "'.",
message: "There should be no space before '{{tokenValue}}'.",
data: {
tokenValue: token.value
},
fix(fixer) {
return fixer.removeRange([tokenBefore.range[1], token.range[0]]);
}
Expand All @@ -81,7 +87,10 @@ module.exports = {
context.report({
node,
loc: token.loc.start,
message: "A space is required after '" + token.value + "'.",
message: "A space is required after '{{tokenValue}}'.",
data: {
tokenValue: token.value
},
fix(fixer) {
return fixer.insertTextAfter(token, " ");
}
Expand All @@ -98,7 +107,10 @@ module.exports = {
context.report({
node,
loc: token.loc.start,
message: "A space is required before '" + token.value + "'.",
message: "A space is required before '{{tokenValue}}'.",
data: {
tokenValue: token.value
},
fix(fixer) {
return fixer.insertTextBefore(token, " ");
}
Expand Down
11 changes: 9 additions & 2 deletions lib/rules/consistent-return.js
Expand Up @@ -145,9 +145,16 @@ module.exports = {
if (!funcInfo.hasReturn) {
funcInfo.hasReturn = true;
funcInfo.hasReturnValue = hasReturnValue;
funcInfo.message = "Expected " + (hasReturnValue ? "a" : "no") + " return value.";
funcInfo.message = "Expected {{which}} return value.";
funcInfo.data = {
which: hasReturnValue ? "a" : "no"
};
} else if (funcInfo.hasReturnValue !== hasReturnValue) {
context.report({node, message: funcInfo.message});
context.report({
node,
message: funcInfo.message,
data: funcInfo.data
});
}
},

Expand Down
4 changes: 2 additions & 2 deletions lib/rules/curly.js
Expand Up @@ -149,7 +149,7 @@ module.exports = {
message: "Expected { after '{{name}}'{{suffix}}.",
data: {
name,
suffix: (suffix ? " " + suffix : "")
suffix: (suffix ? ` ${suffix}` : "")
}
});
}
Expand All @@ -169,7 +169,7 @@ module.exports = {
message: "Unnecessary { after '{{name}}'{{suffix}}.",
data: {
name,
suffix: (suffix ? " " + suffix : "")
suffix: (suffix ? ` ${suffix}` : "")
}
});
}
Expand Down
20 changes: 16 additions & 4 deletions lib/rules/dot-notation.js
Expand Up @@ -51,18 +51,30 @@ module.exports = {
node.computed &&
node.property.type === "Literal" &&
validIdentifier.test(node.property.value) &&
(allowKeywords || keywords.indexOf("" + node.property.value) === -1)
(allowKeywords || keywords.indexOf(String(node.property.value)) === -1)
) {
if (!(allowPattern && allowPattern.test(node.property.value))) {
context.report(node.property, "[" + JSON.stringify(node.property.value) + "] is better written in dot notation.");
context.report({
node: node.property,
message: "[{{propertyValue}}] is better written in dot notation.",
data: {
propertyValue: JSON.stringify(node.property.value)
}
});
}
}
if (
!allowKeywords &&
!node.computed &&
keywords.indexOf("" + node.property.name) !== -1
keywords.indexOf(String(node.property.name)) !== -1
) {
context.report(node.property, "." + node.property.name + " is a syntax error.");
context.report({
node: node.property,
message: ".{{propertyName}} is a syntax error.",
data: {
propertyName: node.property.name
}
});
}
}
};
Expand Down
7 changes: 6 additions & 1 deletion lib/rules/generator-star-spacing.js
Expand Up @@ -87,11 +87,16 @@ module.exports = {
const spaceRequired = mode[side];
const node = after ? leftToken : rightToken;
const type = spaceRequired ? "Missing" : "Unexpected";
const message = type + " space " + side + " *.";
const message = "{{type}} space {{side}} *.";
const data = {
type,
side
};

context.report({
node,
message,
data,
fix(fixer) {
if (spaceRequired) {
if (after) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/indent.js
Expand Up @@ -168,7 +168,7 @@ module.exports = {
let rangeToFix = [];

if (needed > gotten) {
const spaces = "" + new Array(needed - gotten + 1).join(indentChar); // replace with repeat in future
const spaces = indentChar.repeat(needed - gotten);

if (isLastNodeCheck === true) {
rangeToFix = [
Expand Down
16 changes: 14 additions & 2 deletions lib/rules/init-declarations.js
Expand Up @@ -114,9 +114,21 @@ module.exports = {
}

if (mode === MODE_ALWAYS && !initialized) {
context.report(declaration, "Variable '" + id.name + "' should be initialized on declaration.");
context.report({
node: declaration,
message: "Variable '{{idName}}' should be initialized on declaration.",
data: {
idName: id.name
}
});
} else if (mode === MODE_NEVER && kind !== "const" && initialized && !isIgnoredForLoop) {
context.report(declaration, "Variable '" + id.name + "' should not be initialized on declaration.");
context.report({
node: declaration,
message: "Variable '{{idName}}' should not be initialized on declaration.",
data: {
idName: id.name
}
});
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/rules/jsx-quotes.js
Expand Up @@ -74,7 +74,10 @@ module.exports = {
if (attributeValue && astUtils.isStringLiteral(attributeValue) && !usesExpectedQuotes(attributeValue)) {
context.report({
node: attributeValue,
message: "Unexpected usage of " + setting.description + ".",
message: "Unexpected usage of {{description}}.",
data: {
description: setting.description
},
fix(fixer) {
return fixer.replaceText(attributeValue, setting.convert(attributeValue.raw));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/keyword-spacing.js
Expand Up @@ -30,7 +30,7 @@ const KEYS = keywords.concat(["as", "await", "from", "get", "let", "of", "set",
KEYS.sort();
for (let i = 1; i < KEYS.length; ++i) {
if (KEYS[i] === KEYS[i - 1]) {
throw new Error("Duplication was found in the keyword list: " + KEYS[i]);
throw new Error(`Duplication was found in the keyword list: ${KEYS[i]}`);
}
}
}());
Expand Down
20 changes: 18 additions & 2 deletions lib/rules/max-len.js
Expand Up @@ -242,9 +242,25 @@ module.exports = {
}

if (lineIsComment && lineLength > maxCommentLength) {
context.report(node, { line: lineNumber, column: 0 }, "Line " + (i + 1) + " exceeds the maximum comment line length of " + maxCommentLength + ".");
context.report({
node,
loc: { line: lineNumber, column: 0 },
message: "Line {{lineNumber}} exceeds the maximum comment line length of {{maxCommentLength}}.",
data: {
lineNumber: i + 1,
maxCommentLength
}
});
} else if (lineLength > maxLength) {
context.report(node, { line: lineNumber, column: 0 }, "Line " + (i + 1) + " exceeds the maximum line length of " + maxLength + ".");
context.report({
node,
loc: { line: lineNumber, column: 0 },
message: "Line {{lineNumber}} exceeds the maximum line length of {{maxLength}}.",
data: {
lineNumber: i + 1,
maxLength
}
});
}
});
}
Expand Down
8 changes: 5 additions & 3 deletions lib/rules/max-statements-per-line.js
Expand Up @@ -35,7 +35,9 @@ module.exports = {
const sourceCode = context.getSourceCode(),
options = context.options[0] || {},
maxStatementsPerLine = typeof options.max !== "undefined" ? options.max : 1,
message = "This line has too many statements. Maximum allowed is " + maxStatementsPerLine + ".";
message = "This line has too many statements. Maximum allowed is {{maxStatementsPerLine}}.",
data = { maxStatementsPerLine };

let lastStatementLine = 0,
numberOfStatementsOnThisLine = 0;

Expand Down Expand Up @@ -89,7 +91,7 @@ module.exports = {

// Reports if the node violated this rule.
if (numberOfStatementsOnThisLine === maxStatementsPerLine + 1) {
context.report({node, message});
context.report({node, message, data});
}
}

Expand Down Expand Up @@ -166,7 +168,7 @@ module.exports = {
// Empty blocks should be warned if `{max: 0}` was given.
BlockStatement: function reportIfZero(node) {
if (maxStatementsPerLine === 0 && node.body.length === 0) {
context.report({node, message});
context.report({node, message, data});
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/new-cap.js
Expand Up @@ -37,7 +37,7 @@ function checkArray(obj, key, fallback) {

/* istanbul ignore if */
if (Object.prototype.hasOwnProperty.call(obj, key) && !Array.isArray(obj[key])) {
throw new TypeError(key + ", if provided, must be an Array");
throw new TypeError(`${key}, if provided, must be an Array`);
}
return obj[key] || fallback;
}
Expand Down
13 changes: 8 additions & 5 deletions lib/rules/newline-per-chained-call.js
Expand Up @@ -69,11 +69,14 @@ module.exports = {
}

if (depth > ignoreChainWithDepth && callee.property.loc.start.line === callee.object.loc.end.line) {
context.report(
callee.property,
callee.property.loc.start,
"Expected line break before `" + getPropertyText(callee) + "`."
);
context.report({
node: callee.property,
loc: callee.property.loc.start,
message: "Expected line break before `{{callee}}`.",
data: {
callee: getPropertyText(callee)
}
});
}
}
};
Expand Down

0 comments on commit 4126f12

Please sign in to comment.