Skip to content

Commit

Permalink
feat: emit warning when .eslintignore file is detected (#17952)
Browse files Browse the repository at this point in the history
* feat: emit warning when `.eslintignore` file is detected

* feat: emit warning when `.eslintignore` file is detected

* Update lib/eslint/eslint.js

Co-authored-by: Bryan Mishkin <698306+bmish@users.noreply.github.com>

* refactor: remove .eslintignore file

* chore: fix tests

* test: supress warnings

* refactor: add .eslintignore

* refactor: fix lint

* chore: refactor tests

---------

Co-authored-by: Bryan Mishkin <698306+bmish@users.noreply.github.com>
  • Loading branch information
snitin315 and bmish committed Jan 19, 2024
1 parent 38b9b06 commit e3051be
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
7 changes: 7 additions & 0 deletions lib/eslint/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,13 @@ class ESLint {
});
}

// Check for the .eslintignore file, and warn if it's present.
if (existsSync(path.resolve(processedOptions.cwd, ".eslintignore"))) {
process.emitWarning(
"The \".eslintignore\" file is no longer supported. Switch to using the \"ignores\" property in \"eslint.config.js\": https://eslint.org/docs/latest/use/configure/migration-guide#ignoring-files",
"ESLintIgnoreWarning"
);
}
}

/**
Expand Down
25 changes: 16 additions & 9 deletions tests/bin/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,22 @@ describe("bin/eslint.js", () => {
});

it("has exit code 0 if no linting errors are reported", () => {
const child = runESLint([
"--stdin",
"--no-config-lookup",
"--rule",
"{'no-extra-semi': 2}",
"--fix-dry-run",
"--format",
"json"
]);
const child = runESLint(
[
"--stdin",
"--no-config-lookup",
"--rule",
"{'no-extra-semi': 2}",
"--fix-dry-run",
"--format",
"json"
],
{

// Use the tests directory as the CWD to supress the ESLintIgnoreWarning
cwd: path.resolve(__dirname, "../")
}
);

const expectedOutput = JSON.stringify([
{
Expand Down
8 changes: 3 additions & 5 deletions tests/lib/cli-engine/file-enumerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,13 +457,11 @@ describe("FileEnumerator", () => {
});

it("should throw an error if no files match a glob", () => {

// Relying here on the .eslintignore from the repo root
const patterns = ["tests/fixtures/glob-util/ignored/**/*.js"];
const patterns = ["dir-does-not-exist/**/*.js"];

assert.throws(() => {
listFiles(patterns);
}, `All files matched by '${patterns[0]}' are ignored.`);
listFiles(patterns, { cwd: getFixturePath("ignored-paths") });
}, `No files matching '${patterns[0]}' were found.`);
});

it("should return an ignored file, if ignore option is turned off", () => {
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,13 @@ describe("cli", () => {
sh.cp("-r", "./tests/fixtures/.", fixtureDir);
});

beforeEach(() => {
sinon.stub(process, "emitWarning").withArgs(sinon.match.any, "ESLintIgnoreWarning").returns();
process.emitWarning.callThrough();
});

afterEach(() => {
sinon.restore();
log.info.resetHistory();
log.error.resetHistory();
});
Expand Down Expand Up @@ -148,6 +154,7 @@ describe("cli", () => {
let processStub;

beforeEach(() => {
sinon.restore();
processStub = sinon.stub(process, "emitWarning");
process.env = { ...originalEnv };
});
Expand Down
23 changes: 22 additions & 1 deletion tests/lib/eslint/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ describe("ESLint", () => {

beforeEach(() => {
({ ESLint } = require("../../../lib/eslint/eslint"));
sinon.stub(process, "emitWarning").withArgs(sinon.match.any, "ESLintIgnoreWarning").returns();
process.emitWarning.callThrough();
});

afterEach(() => {
sinon.restore();
});

after(() => {
Expand Down Expand Up @@ -280,6 +286,22 @@ describe("ESLint", () => {
].join("\n")), "u")
);
});

it("should warn if .eslintignore file is present", async () => {
const cwd = getFixturePath("ignored-paths");

sinon.restore();
const processStub = sinon.stub(process, "emitWarning");

// eslint-disable-next-line no-new -- for testing purpose only
new ESLint({ cwd });

assert.strictEqual(processStub.callCount, 1, "calls `process.emitWarning()` once");
assert.strictEqual(processStub.getCall(0).args[0], "The \".eslintignore\" file is no longer supported. Switch to using the \"ignores\" property in \"eslint.config.js\": https://eslint.org/docs/latest/use/configure/migration-guide#ignoring-files");
assert.strictEqual(processStub.getCall(0).args[1], "ESLintIgnoreWarning");

processStub.restore();
});
});

describe("lintText()", () => {
Expand Down Expand Up @@ -4379,7 +4401,6 @@ describe("ESLint", () => {

assert.strictEqual(results[0].output, expectedOutput);
});

});

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

0 comments on commit e3051be

Please sign in to comment.