Skip to content

Commit a19072f

Browse files
authored
fix: add logic to handle fixTypes in the lintText() method (#18900)
1 parent 04c7188 commit a19072f

2 files changed

Lines changed: 141 additions & 61 deletions

File tree

lib/eslint/flat-eslint.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,23 @@ function createExtraneousResultsError() {
541541
return new TypeError("Results object was not created from this ESLint instance.");
542542
}
543543

544+
/**
545+
* Creates a fixer function based on the provided fix, fixTypesSet, and config.
546+
* @param {Function|boolean} fix The original fix option.
547+
* @param {Set<string>} fixTypesSet A set of fix types to filter messages for fixing.
548+
* @param {FlatConfig} config The config for the file that generated the message.
549+
* @returns {Function|boolean} The fixer function or the original fix value.
550+
*/
551+
function getFixerForFixTypes(fix, fixTypesSet, config) {
552+
if (!fix || !fixTypesSet) {
553+
return fix;
554+
}
555+
556+
const originalFix = (typeof fix === "function") ? fix : () => true;
557+
558+
return message => shouldMessageBeFixed(message, config, fixTypesSet) && originalFix(message);
559+
}
560+
544561
//-----------------------------------------------------------------------------
545562
// Main API
546563
//-----------------------------------------------------------------------------
@@ -827,16 +844,7 @@ class FlatESLint {
827844

828845

829846
// set up fixer for fixTypes if necessary
830-
let fixer = fix;
831-
832-
if (fix && fixTypesSet) {
833-
834-
// save original value of options.fix in case it's a function
835-
const originalFix = (typeof fix === "function")
836-
? fix : () => true;
837-
838-
fixer = message => shouldMessageBeFixed(message, config, fixTypesSet) && originalFix(message);
839-
}
847+
const fixer = getFixerForFixTypes(fix, fixTypesSet, config);
840848

841849
return fs.readFile(filePath, "utf8")
842850
.then(text => {
@@ -933,11 +941,16 @@ class FlatESLint {
933941
allowInlineConfig,
934942
cwd,
935943
fix,
944+
fixTypes,
936945
warnIgnored: constructorWarnIgnored
937946
} = eslintOptions;
938947
const results = [];
939948
const startTime = Date.now();
949+
const fixTypesSet = fixTypes ? new Set(fixTypes) : null;
940950
const resolvedFilename = path.resolve(cwd, filePath || "__placeholder__.js");
951+
const config = configs.getConfig(resolvedFilename);
952+
953+
const fixer = getFixerForFixTypes(fix, fixTypesSet, config);
941954

942955
// Clear the last used config arrays.
943956
if (resolvedFilename && await this.isPathIgnored(resolvedFilename)) {
@@ -954,7 +967,7 @@ class FlatESLint {
954967
filePath: resolvedFilename.endsWith("__placeholder__.js") ? "<text>" : resolvedFilename,
955968
configs,
956969
cwd,
957-
fix,
970+
fix: fixer,
958971
allowInlineConfig,
959972
linter
960973
}));

tests/lib/eslint/flat-eslint.js

Lines changed: 117 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4664,75 +4664,142 @@ describe("FlatESLint", () => {
46644664

46654665
let eslint;
46664666

4667-
it("should throw an error when an invalid fix type is specified", () => {
4668-
assert.throws(() => {
4667+
describe("fixTypes values validation", () => {
4668+
it("should throw an error when an invalid fix type is specified", () => {
4669+
assert.throws(() => {
4670+
eslint = new FlatESLint({
4671+
cwd: path.join(fixtureDir, ".."),
4672+
overrideConfigFile: true,
4673+
fix: true,
4674+
fixTypes: ["layou"]
4675+
});
4676+
}, /'fixTypes' must be an array of any of "directive", "problem", "suggestion", and "layout"\./iu);
4677+
});
4678+
});
4679+
4680+
describe("with lintFiles", () => {
4681+
it("should not fix any rules when fixTypes is used without fix", async () => {
4682+
eslint = new FlatESLint({
4683+
cwd: path.join(fixtureDir, ".."),
4684+
overrideConfigFile: true,
4685+
fix: false,
4686+
fixTypes: ["layout"]
4687+
});
4688+
const inputPath = getFixturePath("fix-types/fix-only-semi.js");
4689+
const results = await eslint.lintFiles([inputPath]);
4690+
4691+
assert.strictEqual(results[0].output, void 0);
4692+
});
4693+
4694+
it("should not fix non-style rules when fixTypes has only 'layout'", async () => {
46694695
eslint = new FlatESLint({
46704696
cwd: path.join(fixtureDir, ".."),
46714697
overrideConfigFile: true,
46724698
fix: true,
4673-
fixTypes: ["layou"]
4699+
fixTypes: ["layout"]
46744700
});
4675-
}, /'fixTypes' must be an array of any of "directive", "problem", "suggestion", and "layout"\./iu);
4676-
});
4701+
const inputPath = getFixturePath("fix-types/fix-only-semi.js");
4702+
const outputPath = getFixturePath("fix-types/fix-only-semi.expected.js");
4703+
const results = await eslint.lintFiles([inputPath]);
4704+
const expectedOutput = fs.readFileSync(outputPath, "utf8");
46774705

4678-
it("should not fix any rules when fixTypes is used without fix", async () => {
4679-
eslint = new FlatESLint({
4680-
cwd: path.join(fixtureDir, ".."),
4681-
overrideConfigFile: true,
4682-
fix: false,
4683-
fixTypes: ["layout"]
4706+
assert.strictEqual(results[0].output, expectedOutput);
46844707
});
4685-
const inputPath = getFixturePath("fix-types/fix-only-semi.js");
4686-
const results = await eslint.lintFiles([inputPath]);
46874708

4688-
assert.strictEqual(results[0].output, void 0);
4689-
});
4709+
it("should not fix style or problem rules when fixTypes has only 'suggestion'", async () => {
4710+
eslint = new FlatESLint({
4711+
cwd: path.join(fixtureDir, ".."),
4712+
overrideConfigFile: true,
4713+
fix: true,
4714+
fixTypes: ["suggestion"]
4715+
});
4716+
const inputPath = getFixturePath("fix-types/fix-only-prefer-arrow-callback.js");
4717+
const outputPath = getFixturePath("fix-types/fix-only-prefer-arrow-callback.expected.js");
4718+
const results = await eslint.lintFiles([inputPath]);
4719+
const expectedOutput = fs.readFileSync(outputPath, "utf8");
46904720

4691-
it("should not fix non-style rules when fixTypes has only 'layout'", async () => {
4692-
eslint = new FlatESLint({
4693-
cwd: path.join(fixtureDir, ".."),
4694-
overrideConfigFile: true,
4695-
fix: true,
4696-
fixTypes: ["layout"]
4721+
assert.strictEqual(results[0].output, expectedOutput);
46974722
});
4698-
const inputPath = getFixturePath("fix-types/fix-only-semi.js");
4699-
const outputPath = getFixturePath("fix-types/fix-only-semi.expected.js");
4700-
const results = await eslint.lintFiles([inputPath]);
4701-
const expectedOutput = fs.readFileSync(outputPath, "utf8");
47024723

4703-
assert.strictEqual(results[0].output, expectedOutput);
4724+
it("should fix both style and problem rules when fixTypes has 'suggestion' and 'layout'", async () => {
4725+
eslint = new FlatESLint({
4726+
cwd: path.join(fixtureDir, ".."),
4727+
overrideConfigFile: true,
4728+
fix: true,
4729+
fixTypes: ["suggestion", "layout"]
4730+
});
4731+
const inputPath = getFixturePath("fix-types/fix-both-semi-and-prefer-arrow-callback.js");
4732+
const outputPath = getFixturePath("fix-types/fix-both-semi-and-prefer-arrow-callback.expected.js");
4733+
const results = await eslint.lintFiles([inputPath]);
4734+
const expectedOutput = fs.readFileSync(outputPath, "utf8");
4735+
4736+
assert.strictEqual(results[0].output, expectedOutput);
4737+
});
47044738
});
47054739

4706-
it("should not fix style or problem rules when fixTypes has only 'suggestion'", async () => {
4707-
eslint = new FlatESLint({
4708-
cwd: path.join(fixtureDir, ".."),
4709-
overrideConfigFile: true,
4710-
fix: true,
4711-
fixTypes: ["suggestion"]
4740+
describe("with lintText", () => {
4741+
it("should not fix any rules when fixTypes is used without fix", async () => {
4742+
eslint = new FlatESLint({
4743+
cwd: path.join(fixtureDir, ".."),
4744+
overrideConfigFile: true,
4745+
fix: false,
4746+
fixTypes: ["layout"]
4747+
});
4748+
const inputPath = getFixturePath("fix-types/fix-only-semi.js");
4749+
const content = fs.readFileSync(inputPath, "utf8");
4750+
const results = await eslint.lintText(content, { filePath: inputPath });
4751+
4752+
assert.strictEqual(results[0].output, void 0);
47124753
});
4713-
const inputPath = getFixturePath("fix-types/fix-only-prefer-arrow-callback.js");
4714-
const outputPath = getFixturePath("fix-types/fix-only-prefer-arrow-callback.expected.js");
4715-
const results = await eslint.lintFiles([inputPath]);
4716-
const expectedOutput = fs.readFileSync(outputPath, "utf8");
47174754

4718-
assert.strictEqual(results[0].output, expectedOutput);
4719-
});
4755+
it("should not fix non-style rules when fixTypes has only 'layout'", async () => {
4756+
eslint = new FlatESLint({
4757+
cwd: path.join(fixtureDir, ".."),
4758+
overrideConfigFile: true,
4759+
fix: true,
4760+
fixTypes: ["layout"]
4761+
});
4762+
const inputPath = getFixturePath("fix-types/fix-only-semi.js");
4763+
const outputPath = getFixturePath("fix-types/fix-only-semi.expected.js");
4764+
const content = fs.readFileSync(inputPath, "utf8");
4765+
const results = await eslint.lintText(content, { filePath: inputPath });
4766+
const expectedOutput = fs.readFileSync(outputPath, "utf8");
47204767

4721-
it("should fix both style and problem rules when fixTypes has 'suggestion' and 'layout'", async () => {
4722-
eslint = new FlatESLint({
4723-
cwd: path.join(fixtureDir, ".."),
4724-
overrideConfigFile: true,
4725-
fix: true,
4726-
fixTypes: ["suggestion", "layout"]
4768+
assert.strictEqual(results[0].output, expectedOutput);
47274769
});
4728-
const inputPath = getFixturePath("fix-types/fix-both-semi-and-prefer-arrow-callback.js");
4729-
const outputPath = getFixturePath("fix-types/fix-both-semi-and-prefer-arrow-callback.expected.js");
4730-
const results = await eslint.lintFiles([inputPath]);
4731-
const expectedOutput = fs.readFileSync(outputPath, "utf8");
47324770

4733-
assert.strictEqual(results[0].output, expectedOutput);
4734-
});
4771+
it("should not fix style or problem rules when fixTypes has only 'suggestion'", async () => {
4772+
eslint = new FlatESLint({
4773+
cwd: path.join(fixtureDir, ".."),
4774+
overrideConfigFile: true,
4775+
fix: true,
4776+
fixTypes: ["suggestion"]
4777+
});
4778+
const inputPath = getFixturePath("fix-types/fix-only-prefer-arrow-callback.js");
4779+
const outputPath = getFixturePath("fix-types/fix-only-prefer-arrow-callback.expected.js");
4780+
const content = fs.readFileSync(inputPath, "utf8");
4781+
const results = await eslint.lintText(content, { filePath: inputPath });
4782+
const expectedOutput = fs.readFileSync(outputPath, "utf8");
4783+
4784+
assert.strictEqual(results[0].output, expectedOutput);
4785+
});
47354786

4787+
it("should fix both style and problem rules when fixTypes has 'suggestion' and 'layout'", async () => {
4788+
eslint = new FlatESLint({
4789+
cwd: path.join(fixtureDir, ".."),
4790+
overrideConfigFile: true,
4791+
fix: true,
4792+
fixTypes: ["suggestion", "layout"]
4793+
});
4794+
const inputPath = getFixturePath("fix-types/fix-both-semi-and-prefer-arrow-callback.js");
4795+
const outputPath = getFixturePath("fix-types/fix-both-semi-and-prefer-arrow-callback.expected.js");
4796+
const content = fs.readFileSync(inputPath, "utf8");
4797+
const results = await eslint.lintText(content, { filePath: inputPath });
4798+
const expectedOutput = fs.readFileSync(outputPath, "utf8");
4799+
4800+
assert.strictEqual(results[0].output, expectedOutput);
4801+
});
4802+
});
47364803
});
47374804

47384805
describe("isPathIgnored", () => {

0 commit comments

Comments
 (0)