Skip to content

Commit

Permalink
Update: pass cwd to formatters (refs eslint/rfcs#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea authored and btmills committed Nov 21, 2021
1 parent e6bfe4d commit 3621ccc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/eslint/eslint.js
Expand Up @@ -617,7 +617,7 @@ class ESLint {
throw new Error("'name' must be a string");
}

const { cliEngine } = privateMembersMap.get(this);
const { cliEngine, options } = privateMembersMap.get(this);
const formatter = cliEngine.getFormatter(name);

if (typeof formatter !== "function") {
Expand All @@ -637,6 +637,9 @@ class ESLint {
results.sort(compareResultsByFilePath);

return formatter(results, {
get cwd() {
return options.cwd;
},
get rulesMeta() {
if (!rulesMeta) {
rulesMeta = createRulesMeta(cliEngine.getRules());
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/formatters/cwd.js
@@ -0,0 +1,4 @@
/*global module*/
module.exports = function(results, context) {
return context.cwd;
};
11 changes: 7 additions & 4 deletions tests/lib/cli.js
Expand Up @@ -249,10 +249,13 @@ describe("cli", () => {

// Check metadata.
const { metadata } = JSON.parse(log.info.args[0][0]);
const expectedMetadata = Array.from(BuiltinRules).reduce((obj, [ruleId, rule]) => {
obj.rulesMeta[ruleId] = rule.meta;
return obj;
}, { rulesMeta: {} });
const expectedMetadata = {
cwd: process.cwd(),
rulesMeta: Array.from(BuiltinRules).reduce((obj, [ruleId, rule]) => {
obj[ruleId] = rule.meta;
return obj;
}, {})
};

assert.deepStrictEqual(metadata, expectedMetadata);
});
Expand Down
9 changes: 9 additions & 0 deletions tests/lib/eslint/eslint.js
Expand Up @@ -4731,6 +4731,15 @@ describe("ESLint", () => {
await engine.loadFormatter(5);
}, /'name' must be a string/u);
});

it("should pass cwd to the `cwd` property of the second argument.", async () => {
const cwd = getFixturePath();
const engine = new ESLint({ cwd });
const formatterPath = getFixturePath("formatters", "cwd.js");
const formatter = await engine.loadFormatter(formatterPath);

assert.strictEqual(formatter.format([]), cwd);
});
});

describe("getErrorResults()", () => {
Expand Down

0 comments on commit 3621ccc

Please sign in to comment.