Skip to content

Commit

Permalink
feat: pass file info to custom rules (#42)
Browse files Browse the repository at this point in the history
* pass file info to custom rules

* add test for file info
  • Loading branch information
WickyNilliams authored Nov 11, 2021
1 parent 5e6c50e commit 8fd0e21
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/lib/linting.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Linting extends EventEmitter {
// execute the rule, potentially waiting for async rules
// also handles catching errors from the rule
Promise.resolve()
.then(() => rule(reporter, cheerioParsed, ast))
.then(() => rule(reporter, cheerioParsed, ast, { filepath: this.path }))
.catch(e => reporter.exception(e))
.then(() => onDone(reporter));
};
Expand Down
8 changes: 5 additions & 3 deletions src/rules/custom.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const logger = require("../lib/logger")("rule:elm");
const logger = require("../lib/logger")("rule:custom");

/** @typedef {import("../lib/reporter.js")} Reporter */
/** @typedef {import("../lib/parse.js").AST} AST */
/** @typedef {import("../lib/parse.js").Node} Node */
/** @typedef {{ filepath: string }} Info */

/**
* @callback CustomRule
Expand All @@ -27,10 +28,11 @@ module.exports = {
* @param {Cheerio} $ A cheerio representation of the document
* @param {AST} ast The underlying AST representation of the document.
* This should be given to Reporter when warning/erroring with a node.
* @param {Info} info Info related to the current file being linted.
*/
return function CustomRule(reporter, $, ast) {
return function CustomRule(reporter, $, ast, info) {
logger.debug("Called", config);
return config(reporter, $, ast);
return config(reporter, $, ast, info);
};
}
};
15 changes: 14 additions & 1 deletion test/custom.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ process.on("unhandledRejection", error => {
* ### `custom`
Specifies a custom rule.
Used as a quick-and-dirty way of adding rules when you don't want to write an
Used as a quick-and-dirty way of adding rules when you don't want to write an
entire NPM package.
```javascript
Expand Down Expand Up @@ -99,6 +99,19 @@ describe("Rule: custom", function(){
return testSucceeds([]);
});

it("should provide file information", function(){
return testSucceeds([
(_reporter, _$, _ast, info) => {
if(!info) {
throw new Error("no info provided");
}
if(!info.hasOwnProperty("filepath")) {
throw new Error("no filepath provided on info");
}
}
]);
});

it("should succeed with a void function", function(){
return testSucceeds([
() => {}
Expand Down

0 comments on commit 8fd0e21

Please sign in to comment.