@@ -4799,4 +4799,192 @@ describe("FlatESLint", () => {
47994799 } ) ;
48004800 } ) ;
48014801
4802+ describe ( "baseConfig" , ( ) => {
4803+ it ( "can be an object" , async ( ) => {
4804+ const eslint = new FlatESLint ( {
4805+ overrideConfigFile : true ,
4806+ baseConfig : {
4807+ rules : {
4808+ semi : 2
4809+ }
4810+ }
4811+ } ) ;
4812+
4813+ const [ { messages } ] = await eslint . lintText ( "foo" ) ;
4814+
4815+ assert . strictEqual ( messages . length , 1 ) ;
4816+ assert . strictEqual ( messages [ 0 ] . ruleId , "semi" ) ;
4817+ } ) ;
4818+
4819+ it ( "can be an array" , async ( ) => {
4820+ const eslint = new FlatESLint ( {
4821+ overrideConfigFile : true ,
4822+ baseConfig : [
4823+ {
4824+ rules : {
4825+ "no-var" : 2
4826+ }
4827+ } ,
4828+ {
4829+ rules : {
4830+ semi : 2
4831+ }
4832+ }
4833+ ]
4834+ } ) ;
4835+
4836+ const [ { messages } ] = await eslint . lintText ( "var foo" ) ;
4837+
4838+ assert . strictEqual ( messages . length , 2 ) ;
4839+ assert . strictEqual ( messages [ 0 ] . ruleId , "no-var" ) ;
4840+ assert . strictEqual ( messages [ 1 ] . ruleId , "semi" ) ;
4841+ } ) ;
4842+
4843+ it ( "should be inserted after default configs" , async ( ) => {
4844+ const eslint = new FlatESLint ( {
4845+ overrideConfigFile : true ,
4846+ baseConfig : {
4847+ languageOptions : {
4848+ ecmaVersion : 5 ,
4849+ sourceType : "script"
4850+ }
4851+ }
4852+ } ) ;
4853+
4854+ const [ { messages } ] = await eslint . lintText ( "let x" ) ;
4855+
4856+ /*
4857+ * if baseConfig was inserted before default configs,
4858+ * `ecmaVersion: "latest"` from default configs would overwrite
4859+ * `ecmaVersion: 5` from baseConfig, so this wouldn't be a parsing error.
4860+ */
4861+
4862+ assert . strictEqual ( messages . length , 1 ) ;
4863+ assert ( messages [ 0 ] . fatal , "Fatal error expected." ) ;
4864+ } ) ;
4865+
4866+ it ( "should be inserted before configs from the config file" , async ( ) => {
4867+ const eslint = new FlatESLint ( {
4868+ cwd : getFixturePath ( ) ,
4869+ baseConfig : {
4870+ rules : {
4871+ strict : [ "error" , "global" ]
4872+ } ,
4873+ languageOptions : {
4874+ sourceType : "script"
4875+ }
4876+ }
4877+ } ) ;
4878+
4879+ const [ { messages } ] = await eslint . lintText ( "foo" ) ;
4880+
4881+ /*
4882+ * if baseConfig was inserted after configs from the config file,
4883+ * `strict: 0` from eslint.config.js wouldn't overwrite `strict: ["error", "global"]`
4884+ * from baseConfig, so there would be an error message from the `strict` rule.
4885+ */
4886+
4887+ assert . strictEqual ( messages . length , 0 ) ;
4888+ } ) ;
4889+
4890+ it ( "should be inserted before overrideConfig" , async ( ) => {
4891+ const eslint = new FlatESLint ( {
4892+ overrideConfigFile : true ,
4893+ baseConfig : {
4894+ rules : {
4895+ semi : 2
4896+ }
4897+ } ,
4898+ overrideConfig : {
4899+ rules : {
4900+ semi : 1
4901+ }
4902+ }
4903+ } ) ;
4904+
4905+ const [ { messages } ] = await eslint . lintText ( "foo" ) ;
4906+
4907+ assert . strictEqual ( messages . length , 1 ) ;
4908+ assert . strictEqual ( messages [ 0 ] . ruleId , "semi" ) ;
4909+ assert . strictEqual ( messages [ 0 ] . severity , 1 ) ;
4910+ } ) ;
4911+
4912+ it ( "should be inserted before configs from the config file and overrideConfig" , async ( ) => {
4913+ const eslint = new FlatESLint ( {
4914+ overrideConfigFile : getFixturePath ( "eslint.config_with_rules.js" ) ,
4915+ baseConfig : {
4916+ rules : {
4917+ quotes : [ "error" , "double" ] ,
4918+ semi : "error"
4919+ }
4920+ } ,
4921+ overrideConfig : {
4922+ rules : {
4923+ quotes : "warn"
4924+ }
4925+ }
4926+ } ) ;
4927+
4928+ const [ { messages } ] = await eslint . lintText ( 'const foo = "bar"' ) ;
4929+
4930+ /*
4931+ * baseConfig: { quotes: ["error", "double"], semi: "error" }
4932+ * eslint.config_with_rules.js: { quotes: ["error", "single"] }
4933+ * overrideConfig: { quotes: "warn" }
4934+ *
4935+ * Merged config: { quotes: ["warn", "single"], semi: "error" }
4936+ */
4937+
4938+ assert . strictEqual ( messages . length , 2 ) ;
4939+ assert . strictEqual ( messages [ 0 ] . ruleId , "quotes" ) ;
4940+ assert . strictEqual ( messages [ 0 ] . severity , 1 ) ;
4941+ assert . strictEqual ( messages [ 1 ] . ruleId , "semi" ) ;
4942+ assert . strictEqual ( messages [ 1 ] . severity , 2 ) ;
4943+ } ) ;
4944+
4945+ it ( "when it has 'files' they should be intepreted as relative to the config file" , async ( ) => {
4946+
4947+ /*
4948+ * `fixtures/plugins` directory does not have a config file.
4949+ * It's parent directory `fixtures` does have a config file, so
4950+ * the base path will be `fixtures`, cwd will be `fixtures/plugins`
4951+ */
4952+ const eslint = new FlatESLint ( {
4953+ cwd : getFixturePath ( "plugins" ) ,
4954+ baseConfig : {
4955+ files : [ "plugins/a.js" ] ,
4956+ rules : {
4957+ semi : 2
4958+ }
4959+ }
4960+ } ) ;
4961+
4962+ const [ { messages } ] = await eslint . lintText ( "foo" , { filePath : getFixturePath ( "plugins/a.js" ) } ) ;
4963+
4964+ assert . strictEqual ( messages . length , 1 ) ;
4965+ assert . strictEqual ( messages [ 0 ] . ruleId , "semi" ) ;
4966+ } ) ;
4967+
4968+ it ( "when it has 'ignores' they should be intepreted as relative to the config file" , async ( ) => {
4969+
4970+ /*
4971+ * `fixtures/plugins` directory does not have a config file.
4972+ * It's parent directory `fixtures` does have a config file, so
4973+ * the base path will be `fixtures`, cwd will be `fixtures/plugins`
4974+ */
4975+ const eslint = new FlatESLint ( {
4976+ cwd : getFixturePath ( "plugins" ) ,
4977+ baseConfig : {
4978+ ignores : [ "plugins/a.js" ]
4979+ }
4980+ } ) ;
4981+
4982+ const [ { messages } ] = await eslint . lintText ( "foo" , { filePath : getFixturePath ( "plugins/a.js" ) , warnIgnored : true } ) ;
4983+
4984+ assert . strictEqual ( messages . length , 1 ) ;
4985+ assert . strictEqual ( messages [ 0 ] . severity , 1 ) ;
4986+ assert . match ( messages [ 0 ] . message , / i g n o r e d / u) ;
4987+ } ) ;
4988+ } ) ;
4989+
48024990} ) ;
0 commit comments