Skip to content

Commit

Permalink
fix: normalize cwd passed to ESLint/FlatESLint constructor (#17277
Browse files Browse the repository at this point in the history
)

Fixes #17042
  • Loading branch information
mdjermanovic committed Jun 14, 2023
1 parent 9e3d77c commit 5338b56
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/eslint/eslint-helpers.js
Expand Up @@ -795,7 +795,7 @@ function processOptions({
// when overrideConfigFile is true that means don't do config file lookup
configFile: overrideConfigFile === true ? false : overrideConfigFile,
overrideConfig,
cwd,
cwd: path.normalize(cwd),
errorOnUnmatchedPattern,
fix,
fixTypes,
Expand Down
2 changes: 1 addition & 1 deletion lib/eslint/eslint.js
Expand Up @@ -289,7 +289,7 @@ function processOptions({
cacheLocation,
cacheStrategy,
configFile: overrideConfigFile,
cwd,
cwd: path.normalize(cwd),
errorOnUnmatchedPattern,
extensions,
fix,
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/example-app3/.eslintignore
@@ -0,0 +1 @@
src/dist
9 changes: 9 additions & 0 deletions tests/fixtures/example-app3/.eslintrc.js
@@ -0,0 +1,9 @@
module.exports = {
parserOptions: {
ecmaVersion: 2020,
},
'root': true,
'rules': {
'semi': 'error',
},
};
10 changes: 10 additions & 0 deletions tests/fixtures/example-app3/eslint.config.js
@@ -0,0 +1,10 @@
module.exports = [
{
ignores: ["src/dist"]
},
{
rules: {
semi: "error"
}
}
];

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/fixtures/example-app3/src/1.js
@@ -0,0 +1 @@
console.log(1)
1 change: 1 addition & 0 deletions tests/fixtures/example-app3/src/dist/2.js
@@ -0,0 +1 @@
console.log(2)
68 changes: 68 additions & 0 deletions tests/lib/eslint/eslint.js
Expand Up @@ -126,6 +126,28 @@ describe("ESLint", () => {
}
});

it("should normalize 'options.cwd'.", async () => {
const cwd = getFixturePath("example-app3");
const engine = new ESLint({
cwd: `${cwd}${path.sep}foo${path.sep}..`, // `<cwd>/foo/..` should be normalized to `<cwd>`
useEslintrc: false,
overrideConfig: {
plugins: ["test"],
rules: {
"test/report-cwd": "error"
}
}
});
const results = await engine.lintText("");

assert.strictEqual(results[0].messages[0].ruleId, "test/report-cwd");
assert.strictEqual(results[0].messages[0].message, cwd);

const formatter = await engine.loadFormatter("cwd");

assert.strictEqual(formatter.format(results), cwd);
});

it("should report one fatal message when given a path by --ignore-path that is not a file when ignore is true.", () => {
assert.throws(() => {
// eslint-disable-next-line no-new -- Check for throwing
Expand Down Expand Up @@ -6945,4 +6967,50 @@ describe("ESLint", () => {
});
});
});

// only works on a Windows machine
if (os.platform() === "win32") {

// https://github.com/eslint/eslint/issues/17042
describe("with cwd that is using forward slash on Windows", () => {
const cwd = getFixturePath("example-app3");
const cwdForwardSlash = cwd.replace(/\\/gu, "/");

it("should correctly handle ignore patterns", async () => {
const engine = new ESLint({ cwd: cwdForwardSlash });
const results = await engine.lintFiles(["./src"]);

// src/dist/2.js should be ignored
assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].filePath, path.join(cwd, "src\\1.js"));
});

it("should pass cwd with backslashes to rules", async () => {
const engine = new ESLint({
cwd: cwdForwardSlash,
useEslintrc: false,
overrideConfig: {
plugins: ["test"],
rules: {
"test/report-cwd": "error"
}
}
});
const results = await engine.lintText("");

assert.strictEqual(results[0].messages[0].ruleId, "test/report-cwd");
assert.strictEqual(results[0].messages[0].message, cwd);
});

it("should pass cwd with backslashes to formatters", async () => {
const engine = new ESLint({
cwd: cwdForwardSlash
});
const results = await engine.lintText("");
const formatter = await engine.loadFormatter("cwd");

assert.strictEqual(formatter.format(results), cwd);
});
});
}
});
72 changes: 72 additions & 0 deletions tests/lib/eslint/flat-eslint.js
Expand Up @@ -140,6 +140,30 @@ describe("FlatESLint", () => {
}
});

it("should normalize 'options.cwd'.", async () => {
const cwd = getFixturePath("example-app3");
const engine = new FlatESLint({
cwd: `${cwd}${path.sep}foo${path.sep}..`, // `<cwd>/foo/..` should be normalized to `<cwd>`
overrideConfigFile: true,
overrideConfig: {
plugins: {
test: require(path.join(cwd, "node_modules", "eslint-plugin-test"))
},
rules: {
"test/report-cwd": "error"
}
}
});
const results = await engine.lintText("");

assert.strictEqual(results[0].messages[0].ruleId, "test/report-cwd");
assert.strictEqual(results[0].messages[0].message, cwd);

const formatter = await engine.loadFormatter("cwd");

assert.strictEqual(formatter.format(results), cwd);
});

// https://github.com/eslint/eslint/issues/2380
it("should not modify baseConfig when format is specified", () => {
const customBaseConfig = { root: true };
Expand Down Expand Up @@ -5701,6 +5725,54 @@ describe("FlatESLint", () => {
});
});

// only works on a Windows machine
if (os.platform() === "win32") {

// https://github.com/eslint/eslint/issues/17042
describe("with cwd that is using forward slash on Windows", () => {
const cwd = getFixturePath("example-app3");
const cwdForwardSlash = cwd.replace(/\\/gu, "/");

it("should correctly handle ignore patterns", async () => {
const engine = new FlatESLint({ cwd: cwdForwardSlash });
const results = await engine.lintFiles(["./src"]);

// src/dist/2.js should be ignored
assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].filePath, path.join(cwd, "src\\1.js"));
});

it("should pass cwd with backslashes to rules", async () => {
const engine = new FlatESLint({
cwd: cwdForwardSlash,
overrideConfigFile: true,
overrideConfig: {
plugins: {
test: require(path.join(cwd, "node_modules", "eslint-plugin-test"))
},
rules: {
"test/report-cwd": "error"
}
}
});
const results = await engine.lintText("");

assert.strictEqual(results[0].messages[0].ruleId, "test/report-cwd");
assert.strictEqual(results[0].messages[0].message, cwd);
});

it("should pass cwd with backslashes to formatters", async () => {
const engine = new FlatESLint({
cwd: cwdForwardSlash
});
const results = await engine.lintText("");
const formatter = await engine.loadFormatter("cwd");

assert.strictEqual(formatter.format(results), cwd);
});
});
}

});

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

0 comments on commit 5338b56

Please sign in to comment.