Skip to content

user defined error message #7

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ Instead of a string to be checked for exact matching you can also supply a regul
}
```

### 3 arguments
In the 3 argument, the last is optional string error message, defined by user.
```json
{
"plugins": [
"header"
],
"rules": {
"header/header": [2, "line", {"pattern": "^ Copyright \\d{4}\\n My Company$"}, "Copyright information error. Please add below line to your file.\n\tCopyright (c) 2017 ABC Inc. All rights reserved""]
}
}
```

## Examples

The following examples are all valid.
Expand Down
18 changes: 12 additions & 6 deletions lib/rules/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ function match(actual, expected) {
}
}


module.exports = function(context) {
var options = context.options;

// get error message form user config
var errorMessage = options[2] && typeof options[2] === 'string' ? options[2] : 'incorrect header';

// If just one option then read comment from file
if (options.length === 1) {
var text = fs.readFileSync(context.options[0], "utf8");
Expand All @@ -33,9 +37,11 @@ module.exports = function(context) {
headerLines = options[1].map(function(line) {
return isPattern(line) ? new RegExp(line.pattern) : line;
});
} else if (isPattern(options[1])) {
}
else if (isPattern(options[1])) {
headerLines = [new RegExp(options[1].pattern)];
} else {
}
else {
// TODO split on \r as well
headerLines = options[1].split("\n");
}
Expand All @@ -59,24 +65,24 @@ module.exports = function(context) {
}

if (!leadingComments.length) {
context.report(node, "missing header");
context.report(node, errorMessage);
} else if (leadingComments[0].type.toLowerCase() !== commentType) {
context.report(node, "header should be a " + commentType + " comment");
} else {
if (commentType === "line") {
if (leadingComments.length < headerLines.length) {
context.report(node, "incorrect header");
context.report(node, errorMessage);
return;
}
for (var i = 0; i < headerLines.length; i++) {
if (!match(leadingComments[i].value, headerLines[i])) {
context.report(node, "incorrect header");
context.report(node, errorMessage);
return;
}
}
} else {
if (!match(leadingComments[0].value, header)) {
context.report(node, "incorrect header");
context.report(node, errorMessage);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/rules/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ ruleTester.run("header", rule, {
errors: [
{message: "incorrect header"}
]
},
{
code: "/* Copyright 20\n Author: [email protected] */",
options: ["block", {pattern: "^\\s*Copyright \\(c\\) \\d{4} ABC Inc. All rights reserved.\\s*$"}, "Copyright information error. Please add below line to your file.\n\tCopyright (c) " + new Date().getFullYear() + " ABC Inc. All rights reserved"],
errors: [
{message: "Copyright information error. Please add below line to your file.\n\tCopyright (c) " + new Date().getFullYear() + " ABC Inc. All rights reserved"}
Copy link
Owner

Choose a reason for hiding this comment

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

These tests will fail in 2018.

Choose a reason for hiding this comment

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

@Stuk, maybe I am missing something, why would this fail in 2018?

The pattern does not match the code, which would cause it to throw an error. And the supplied error message would match the output since new Date().getFullYear() is used in both places.

Copy link

Choose a reason for hiding this comment

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

@Stuk do you mean it will fail in 2100 ?

]
}
]
});