Skip to content

Commit

Permalink
Update: Uniform messages for the rules in "complexity" section (#11759)
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorNovozhilov authored and ilyavolodin committed May 25, 2019
1 parent 0a801d7 commit 53f7f4c
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 92 deletions.
4 changes: 2 additions & 2 deletions lib/rules/complexity.js
Expand Up @@ -55,7 +55,7 @@ module.exports = {
],

messages: {
complex: "{{name}} has a complexity of {{complexity}}."
complex: "{{name}} has a complexity of {{complexity}}. Maximum allowed is {{max}}."
}
},

Expand Down Expand Up @@ -102,7 +102,7 @@ module.exports = {
context.report({
node,
messageId: "complex",
data: { name, complexity }
data: { name, complexity, max: THRESHOLD }
});
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/max-classes-per-file.js
Expand Up @@ -32,7 +32,7 @@ module.exports = {
],

messages: {
maximumExceeded: "Number of classes per file must not exceed {{ max }}."
maximumExceeded: "File has too many classes ({{ classCount }}). Maximum allowed is {{ max }}."
}
},
create(context) {
Expand All @@ -51,6 +51,7 @@ module.exports = {
node,
messageId: "maximumExceeded",
data: {
classCount,
max: maxClasses
}
});
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/max-depth.js
Expand Up @@ -45,7 +45,7 @@ module.exports = {
}
],
messages: {
tooDeeply: "Blocks are nested too deeply ({{depth}})."
tooDeeply: "Blocks are nested too deeply ({{depth}}). Maximum allowed is {{maxDepth}}."
}
},

Expand Down Expand Up @@ -97,7 +97,7 @@ module.exports = {
const len = ++functionStack[functionStack.length - 1];

if (len > maxDepth) {
context.report({ node, messageId: "tooDeeply", data: { depth: len } });
context.report({ node, messageId: "tooDeeply", data: { depth: len, maxDepth } });
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/rules/max-len.js
Expand Up @@ -80,8 +80,8 @@ module.exports = {
OPTIONS_SCHEMA
],
messages: {
max: "Line {{lineNumber}} exceeds the maximum line length of {{maxLength}}.",
maxComment: "Line {{lineNumber}} exceeds the maximum comment line length of {{maxCommentLength}}."
max: "This line has a length of {{lineLength}}. Maximum allowed is {{maxLength}}.",
maxComment: "This line has a comment length of {{lineLength}}. Maximum allowed is {{maxCommentLength}}."
}
},

Expand Down Expand Up @@ -346,7 +346,7 @@ module.exports = {
loc: { line: lineNumber, column: 0 },
messageId: "maxComment",
data: {
lineNumber: i + 1,
lineLength,
maxCommentLength
}
});
Expand All @@ -357,7 +357,7 @@ module.exports = {
loc: { line: lineNumber, column: 0 },
messageId: "max",
data: {
lineNumber: i + 1,
lineLength,
maxLength
}
});
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/max-lines-per-function.js
Expand Up @@ -10,6 +10,8 @@

const astUtils = require("./utils/ast-utils");

const lodash = require("lodash");

//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -192,7 +194,7 @@ module.exports = {
}

if (lineCount > maxLines) {
const name = astUtils.getFunctionNameWithKind(funcNode);
const name = lodash.upperFirst(astUtils.getFunctionNameWithKind(funcNode));

context.report({
node,
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/max-lines.js
Expand Up @@ -53,7 +53,7 @@ module.exports = {
}
],
messages: {
exceed: "File must be at most {{max}} lines long. It's {{actual}} lines long."
exceed: "File has too many lines ({{actual}}). Maximum allowed is {{max}}."
}
},

Expand Down
2 changes: 1 addition & 1 deletion tests/lib/linter/linter.js
Expand Up @@ -2685,7 +2685,7 @@ describe("Linter", () => {

assert.strictEqual(messages.length, 1);
assert.strictEqual(messages[0].ruleId, "max-len");
assert.strictEqual(messages[0].message, "Line 1 exceeds the maximum line length of 100.");
assert.strictEqual(messages[0].message, "This line has a length of 122. Maximum allowed is 100.");
assert.include(messages[0].nodeType, "Program");
});
});
Expand Down
23 changes: 12 additions & 11 deletions tests/lib/rules/complexity.js
Expand Up @@ -38,12 +38,13 @@ function createComplexity(complexity) {
* Create an expected error object
* @param {string} name The name of the symbol being tested
* @param {number} complexity The cyclomatic complexity value of the symbol
* @param {number} max The maximum cyclomatic complexity value of the symbol
* @returns {Object} The error object
*/
function makeError(name, complexity) {
function makeError(name, complexity, max) {
return {
messageId: "complex",
data: { name, complexity }
data: { name, complexity, max }
};
}

Expand Down Expand Up @@ -76,10 +77,10 @@ ruleTester.run("complexity", rule, {
{ code: "function b(x) {}", options: [{ max: 1 }] }
],
invalid: [
{ code: "function a(x) {}", options: [0], errors: [makeError("Function 'a'", 1)] },
{ code: "var func = function () {}", options: [0], errors: [makeError("Function", 1)] },
{ code: "var obj = { a(x) {} }", options: [0], parserOptions: { ecmaVersion: 6 }, errors: [makeError("Method 'a'", 1)] },
{ code: "class Test { a(x) {} }", options: [0], parserOptions: { ecmaVersion: 6 }, errors: [makeError("Method 'a'", 1)] },
{ code: "function a(x) {}", options: [0], errors: [makeError("Function 'a'", 1, 0)] },
{ code: "var func = function () {}", options: [0], errors: [makeError("Function", 1, 0)] },
{ code: "var obj = { a(x) {} }", options: [0], parserOptions: { ecmaVersion: 6 }, errors: [makeError("Method 'a'", 1, 0)] },
{ code: "class Test { a(x) {} }", options: [0], parserOptions: { ecmaVersion: 6 }, errors: [makeError("Method 'a'", 1, 0)] },
{ code: "var a = (x) => {if (true) {return x;}}", options: [1], errors: 1, settings: { ecmascript: 6 } },
{ code: "function a(x) {if (true) {return x;}}", options: [1], errors: 1 },
{ code: "function a(x) {if (true) {return x;} else {return x+1;}}", options: [1], errors: 1 },
Expand All @@ -100,19 +101,19 @@ ruleTester.run("complexity", rule, {
{ code: "function a(x) {do {'foo';} while (true)}", options: [1], errors: 1 },
{ code: "function a(x) {(function() {while(true){'foo';}})(); (function() {while(true){'bar';}})();}", options: [1], errors: 2 },
{ code: "function a(x) {(function() {while(true){'foo';}})(); (function() {'bar';})();}", options: [1], errors: 1 },
{ code: "var obj = { a(x) { return x ? 0 : 1; } };", options: [1], parserOptions: { ecmaVersion: 6 }, errors: [makeError("Method 'a'", 2)] },
{ code: "var obj = { a: function b(x) { return x ? 0 : 1; } };", options: [1], errors: [makeError("Method 'b'", 2)] },
{ code: "var obj = { a(x) { return x ? 0 : 1; } };", options: [1], parserOptions: { ecmaVersion: 6 }, errors: [makeError("Method 'a'", 2, 1)] },
{ code: "var obj = { a: function b(x) { return x ? 0 : 1; } };", options: [1], errors: [makeError("Method 'b'", 2, 1)] },
{
code: createComplexity(21),
errors: [makeError("Function 'test'", 21)]
errors: [makeError("Function 'test'", 21, 20)]
},
{
code: createComplexity(21),
options: [{}],
errors: [makeError("Function 'test'", 21)]
errors: [makeError("Function 'test'", 21, 20)]
},

// object property options
{ code: "function a(x) {}", options: [{ max: 0 }], errors: [makeError("Function 'a'", 1)] }
{ code: "function a(x) {}", options: [{ max: 0 }], errors: [makeError("Function 'a'", 1, 0)] }
]
});
20 changes: 10 additions & 10 deletions tests/lib/rules/max-depth.js
Expand Up @@ -29,18 +29,18 @@ ruleTester.run("max-depth", rule, {
{ code: "function foo() { if (true) { if (false) { if (true) { } } } }", options: [{ max: 3 }] }
],
invalid: [
{ code: "function foo() { if (true) { if (false) { if (true) { } } } }", options: [2], errors: [{ messageId: "tooDeeply", data: { depth: 3 }, type: "IfStatement" }] },
{ code: "var foo = () => { if (true) { if (false) { if (true) { } } } }", options: [2], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "tooDeeply", data: { depth: 3 }, type: "IfStatement" }] },
{ code: "function foo() { if (true) {} else { for(;;) {} } }", options: [1], errors: [{ messageId: "tooDeeply", data: { depth: 2 }, type: "ForStatement" }] },
{ code: "function foo() { while (true) { if (true) {} } }", options: [1], errors: [{ messageId: "tooDeeply", data: { depth: 2 }, type: "IfStatement" }] },
{ code: "function foo() { for (let x of foo) { if (true) {} } }", options: [1], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "tooDeeply", data: { depth: 2 }, type: "IfStatement" }] },
{ code: "function foo() { while (true) { if (true) { if (false) { } } } }", options: [1], errors: [{ messageId: "tooDeeply", data: { depth: 2 }, type: "IfStatement" }, { messageId: "tooDeeply", data: { depth: 3 }, type: "IfStatement" }] },
{ code: "function foo() { if (true) { if (false) { if (true) { if (false) { if (true) { } } } } } }", errors: [{ messageId: "tooDeeply", data: { depth: 5 }, type: "IfStatement" }] },
{ code: "function foo() { if (true) { if (false) { if (true) { } } } }", options: [2], errors: [{ messageId: "tooDeeply", data: { depth: 3, maxDepth: 2 }, type: "IfStatement" }] },
{ code: "var foo = () => { if (true) { if (false) { if (true) { } } } }", options: [2], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "tooDeeply", data: { depth: 3, maxDepth: 2 }, type: "IfStatement" }] },
{ code: "function foo() { if (true) {} else { for(;;) {} } }", options: [1], errors: [{ messageId: "tooDeeply", data: { depth: 2, maxDepth: 1 }, type: "ForStatement" }] },
{ code: "function foo() { while (true) { if (true) {} } }", options: [1], errors: [{ messageId: "tooDeeply", data: { depth: 2, maxDepth: 1 }, type: "IfStatement" }] },
{ code: "function foo() { for (let x of foo) { if (true) {} } }", options: [1], parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "tooDeeply", data: { depth: 2, maxDepth: 1 }, type: "IfStatement" }] },
{ code: "function foo() { while (true) { if (true) { if (false) { } } } }", options: [1], errors: [{ messageId: "tooDeeply", data: { depth: 2, maxDepth: 1 }, type: "IfStatement" }, { messageId: "tooDeeply", data: { depth: 3, maxDepth: 1 }, type: "IfStatement" }] },
{ code: "function foo() { if (true) { if (false) { if (true) { if (false) { if (true) { } } } } } }", errors: [{ messageId: "tooDeeply", data: { depth: 5, maxDepth: 4 }, type: "IfStatement" }] },

// object property options
{ code: "function foo() { if (true) { if (false) { if (true) { } } } }", options: [{ max: 2 }], errors: [{ messageId: "tooDeeply", data: { depth: 3 }, type: "IfStatement" }] },
{ code: "function foo() { if (true) { if (false) { if (true) { } } } }", options: [{ max: 2 }], errors: [{ messageId: "tooDeeply", data: { depth: 3, maxDepth: 2 }, type: "IfStatement" }] },

{ code: "function foo() { if (a) { if (b) { if (c) { if (d) { if (e) {} } } } } }", options: [{}], errors: [{ messageId: "tooDeeply", data: { depth: 5 } }] },
{ code: "function foo() { if (true) {} }", options: [{ max: 0 }], errors: [{ messageId: "tooDeeply", data: { depth: 1 } }] }
{ code: "function foo() { if (a) { if (b) { if (c) { if (d) { if (e) {} } } } } }", options: [{}], errors: [{ messageId: "tooDeeply", data: { depth: 5, maxDepth: 4 } }] },
{ code: "function foo() { if (true) {} }", options: [{ max: 0 }], errors: [{ messageId: "tooDeeply", data: { depth: 1, maxDepth: 0 } }] }
]
});

0 comments on commit 53f7f4c

Please sign in to comment.