Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita19072f

Browse files
authored
fix: add logic to handle fixTypes in the lintText() method (#18900)
1 parent04c7188 commita19072f

File tree

2 files changed

+141
-61
lines changed

2 files changed

+141
-61
lines changed

‎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
returnnewTypeError("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+
functiongetFixerForFixTypes(fix,fixTypesSet,config){
552+
if(!fix||!fixTypesSet){
553+
returnfix;
554+
}
555+
556+
constoriginalFix=(typeoffix==="function") ?fix :()=>true;
557+
558+
returnmessage=>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-
letfixer=fix;
831-
832-
if(fix&&fixTypesSet){
833-
834-
// save original value of options.fix in case it's a function
835-
constoriginalFix=(typeoffix==="function")
836-
?fix :()=>true;
837-
838-
fixer=message=>shouldMessageBeFixed(message,config,fixTypesSet)&&originalFix(message);
839-
}
847+
constfixer=getFixerForFixTypes(fix,fixTypesSet,config);
840848

841849
returnfs.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
constresults=[];
939948
conststartTime=Date.now();
949+
constfixTypesSet=fixTypes ?newSet(fixTypes) :null;
940950
constresolvedFilename=path.resolve(cwd,filePath||"__placeholder__.js");
951+
constconfig=configs.getConfig(resolvedFilename);
952+
953+
constfixer=getFixerForFixTypes(fix,fixTypesSet,config);
941954

942955
// Clear the last used config arrays.
943956
if(resolvedFilename&&awaitthis.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
leteslint;
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=newFlatESLint({
4671+
cwd:path.join(fixtureDir,".."),
4672+
overrideConfigFile:true,
4673+
fix:true,
4674+
fixTypes:["layou"]
4675+
});
4676+
},/'fixTypes'mustbeanarrayofanyof"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=newFlatESLint({
4683+
cwd:path.join(fixtureDir,".."),
4684+
overrideConfigFile:true,
4685+
fix:false,
4686+
fixTypes:["layout"]
4687+
});
4688+
constinputPath=getFixturePath("fix-types/fix-only-semi.js");
4689+
constresults=awaiteslint.lintFiles([inputPath]);
4690+
4691+
assert.strictEqual(results[0].output,void0);
4692+
});
4693+
4694+
it("should not fix non-style rules when fixTypes has only 'layout'",async()=>{
46694695
eslint=newFlatESLint({
46704696
cwd:path.join(fixtureDir,".."),
46714697
overrideConfigFile:true,
46724698
fix:true,
4673-
fixTypes:["layou"]
4699+
fixTypes:["layout"]
46744700
});
4675-
},/'fixTypes'mustbeanarrayofanyof"directive","problem","suggestion",and"layout"\./iu);
4676-
});
4701+
constinputPath=getFixturePath("fix-types/fix-only-semi.js");
4702+
constoutputPath=getFixturePath("fix-types/fix-only-semi.expected.js");
4703+
constresults=awaiteslint.lintFiles([inputPath]);
4704+
constexpectedOutput=fs.readFileSync(outputPath,"utf8");
46774705

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

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

4691-
it("should not fix non-style rules when fixTypes has only 'layout'",async()=>{
4692-
eslint=newFlatESLint({
4693-
cwd:path.join(fixtureDir,".."),
4694-
overrideConfigFile:true,
4695-
fix:true,
4696-
fixTypes:["layout"]
4721+
assert.strictEqual(results[0].output,expectedOutput);
46974722
});
4698-
constinputPath=getFixturePath("fix-types/fix-only-semi.js");
4699-
constoutputPath=getFixturePath("fix-types/fix-only-semi.expected.js");
4700-
constresults=awaiteslint.lintFiles([inputPath]);
4701-
constexpectedOutput=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=newFlatESLint({
4726+
cwd:path.join(fixtureDir,".."),
4727+
overrideConfigFile:true,
4728+
fix:true,
4729+
fixTypes:["suggestion","layout"]
4730+
});
4731+
constinputPath=getFixturePath("fix-types/fix-both-semi-and-prefer-arrow-callback.js");
4732+
constoutputPath=getFixturePath("fix-types/fix-both-semi-and-prefer-arrow-callback.expected.js");
4733+
constresults=awaiteslint.lintFiles([inputPath]);
4734+
constexpectedOutput=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=newFlatESLint({
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=newFlatESLint({
4743+
cwd:path.join(fixtureDir,".."),
4744+
overrideConfigFile:true,
4745+
fix:false,
4746+
fixTypes:["layout"]
4747+
});
4748+
constinputPath=getFixturePath("fix-types/fix-only-semi.js");
4749+
constcontent=fs.readFileSync(inputPath,"utf8");
4750+
constresults=awaiteslint.lintText(content,{filePath:inputPath});
4751+
4752+
assert.strictEqual(results[0].output,void0);
47124753
});
4713-
constinputPath=getFixturePath("fix-types/fix-only-prefer-arrow-callback.js");
4714-
constoutputPath=getFixturePath("fix-types/fix-only-prefer-arrow-callback.expected.js");
4715-
constresults=awaiteslint.lintFiles([inputPath]);
4716-
constexpectedOutput=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=newFlatESLint({
4757+
cwd:path.join(fixtureDir,".."),
4758+
overrideConfigFile:true,
4759+
fix:true,
4760+
fixTypes:["layout"]
4761+
});
4762+
constinputPath=getFixturePath("fix-types/fix-only-semi.js");
4763+
constoutputPath=getFixturePath("fix-types/fix-only-semi.expected.js");
4764+
constcontent=fs.readFileSync(inputPath,"utf8");
4765+
constresults=awaiteslint.lintText(content,{filePath:inputPath});
4766+
constexpectedOutput=fs.readFileSync(outputPath,"utf8");
47204767

4721-
it("should fix both style and problem rules when fixTypes has 'suggestion' and 'layout'",async()=>{
4722-
eslint=newFlatESLint({
4723-
cwd:path.join(fixtureDir,".."),
4724-
overrideConfigFile:true,
4725-
fix:true,
4726-
fixTypes:["suggestion","layout"]
4768+
assert.strictEqual(results[0].output,expectedOutput);
47274769
});
4728-
constinputPath=getFixturePath("fix-types/fix-both-semi-and-prefer-arrow-callback.js");
4729-
constoutputPath=getFixturePath("fix-types/fix-both-semi-and-prefer-arrow-callback.expected.js");
4730-
constresults=awaiteslint.lintFiles([inputPath]);
4731-
constexpectedOutput=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=newFlatESLint({
4773+
cwd:path.join(fixtureDir,".."),
4774+
overrideConfigFile:true,
4775+
fix:true,
4776+
fixTypes:["suggestion"]
4777+
});
4778+
constinputPath=getFixturePath("fix-types/fix-only-prefer-arrow-callback.js");
4779+
constoutputPath=getFixturePath("fix-types/fix-only-prefer-arrow-callback.expected.js");
4780+
constcontent=fs.readFileSync(inputPath,"utf8");
4781+
constresults=awaiteslint.lintText(content,{filePath:inputPath});
4782+
constexpectedOutput=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=newFlatESLint({
4789+
cwd:path.join(fixtureDir,".."),
4790+
overrideConfigFile:true,
4791+
fix:true,
4792+
fixTypes:["suggestion","layout"]
4793+
});
4794+
constinputPath=getFixturePath("fix-types/fix-both-semi-and-prefer-arrow-callback.js");
4795+
constoutputPath=getFixturePath("fix-types/fix-both-semi-and-prefer-arrow-callback.expected.js");
4796+
constcontent=fs.readFileSync(inputPath,"utf8");
4797+
constresults=awaiteslint.lintText(content,{filePath:inputPath});
4798+
constexpectedOutput=fs.readFileSync(outputPath,"utf8");
4799+
4800+
assert.strictEqual(results[0].output,expectedOutput);
4801+
});
4802+
});
47364803
});
47374804

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp