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 committed Jun 5, 2020
1 parent dd949ae commit 43a7e20
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/eslint/eslint.js
Expand Up @@ -575,7 +575,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 @@ -595,6 +595,9 @@ class ESLint {
results.sort(compareResultsByFilePath);

return formatter(results, {
get cwd() {
return options.cwd;
},
get rulesMeta() {
if (!rulesMeta) {
rulesMeta = createRulesMeta(cliEngine.getRules());
Expand Down
5 changes: 5 additions & 0 deletions tests/_utils/in-memory-fs.js
Expand Up @@ -392,6 +392,7 @@ function defineCascadingConfigArrayFactoryWithInMemoryFileSystem({
// Override the default cwd.
return {
fs,
stubs,
RelativeModuleResolver,
ConfigArrayFactory,
CascadingConfigArrayFactory: cwd === process.cwd
Expand All @@ -417,6 +418,7 @@ function defineFileEnumeratorWithInMemoryFileSystem({
} = {}) {
const {
fs,
stubs,
RelativeModuleResolver,
ConfigArrayFactory,
CascadingConfigArrayFactory
Expand All @@ -430,6 +432,7 @@ function defineFileEnumeratorWithInMemoryFileSystem({
// Override the default cwd.
return {
fs,
stubs,
RelativeModuleResolver,
ConfigArrayFactory,
CascadingConfigArrayFactory,
Expand All @@ -456,13 +459,15 @@ function defineCLIEngineWithInMemoryFileSystem({
} = {}) {
const {
fs,
stubs,
RelativeModuleResolver,
ConfigArrayFactory,
CascadingConfigArrayFactory,
FileEnumerator
} =
defineFileEnumeratorWithInMemoryFileSystem({ cwd, files });
const { CLIEngine, getCLIEngineInternalSlots } = proxyquire(CLIEnginePath, {
...stubs,
fs,
"./cascading-config-array-factory": { CascadingConfigArrayFactory },
"./file-enumerator": { FileEnumerator },
Expand Down
11 changes: 7 additions & 4 deletions tests/lib/cli.js
Expand Up @@ -241,10 +241,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
17 changes: 17 additions & 0 deletions tests/lib/eslint/eslint.js
Expand Up @@ -4318,6 +4318,23 @@ 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 ESLintWithInMemoryFs = defineESLintWithInMemoryFileSystem({
cwd: () => __dirname,
files: {
"node_modules/eslint-formatter-return-cwd/index.js": "module.exports = (results, context) => context.cwd;",
"test.js": "",
".eslintrc.json": "{}"
}
}).ESLint;
const cwd = path.join(__dirname, "foo/bar");
const eslint = new ESLintWithInMemoryFs({ cwd });
const formatter = await eslint.loadFormatter("return-cwd");

// This formatter just returns cwd.
assert.strictEqual(formatter.format([]), cwd);
});
});

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

0 comments on commit 43a7e20

Please sign in to comment.