11import type { Linter } from "eslint" ;
22import { existsSync , statSync } from "fs" ;
33import { dirname , extname , resolve } from "path" ;
4- import type { RuleModule } from "../types" ;
4+ import type { RuleModule } from "../../types" ;
5+ import { shouldUseFlatConfig } from "./should-use-flat-config" ;
6+ import { calculateConfigForFile } from "./calculate-config-for-file" ;
57
6- let configResolver : ( filePath : string ) => Linter . Config , ruleNames : Set < string > ;
8+ const configResolvers : Record <
9+ string ,
10+ undefined | ( ( filePath : string ) => Pick < Linter . Config , "rules" > )
11+ > = { } ;
12+ let ruleNames : Set < string > ;
713
814/**
915 * Get config resolver
1016 */
11- function getConfigResolver ( ) : ( filePath : string ) => Linter . Config {
17+ function getConfigResolver (
18+ cwd : string ,
19+ ) : ( filePath : string ) => Pick < Linter . Config , "rules" > {
20+ const configResolver = configResolvers [ cwd ] ;
1221 if ( configResolver ) {
1322 return configResolver ;
1423 }
1524
25+ if ( shouldUseFlatConfig ( cwd ) ) {
26+ return ( configResolvers [ cwd ] = ( filePath : string ) =>
27+ calculateConfigForFile ( cwd , filePath ) ) ;
28+ }
29+
1630 // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires -- special
17- const plugin = require ( ".." ) ;
31+ const plugin = require ( "../.. " ) ;
1832 try {
1933 // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires -- special
2034 const eslintrc = require ( "@eslint/eslintrc" ) ;
2135 const configArrayFactory = new eslintrc . Legacy . CascadingConfigArrayFactory ( {
2236 additionalPluginPool : new Map ( [ [ "eslint-plugin-jsonc" , plugin ] ] ) ,
2337 getEslintRecommendedConfig ( ) {
2438 // eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore
25- return require ( "../../conf/eslint-recommended.js" ) ;
39+ return require ( "../../../ conf/eslint-recommended.js" ) ;
2640 } ,
2741 getEslintAllConfig ( ) {
2842 // eslint-disable-next-line @typescript-eslint/no-require-imports -- ignore
29- return require ( "../../conf/eslint-all.js" ) ;
43+ return require ( "../../../ conf/eslint-all.js" ) ;
3044 } ,
3145 // for v1.1.0
3246 eslintRecommendedPath : require . resolve (
33- "../../conf/eslint-recommended.js" ,
47+ "../../../ conf/eslint-recommended.js" ,
3448 ) ,
35- eslintAllPath : require . resolve ( "../../conf/eslint-all.js" ) ,
49+ eslintAllPath : require . resolve ( "../../../ conf/eslint-all.js" ) ,
3650 } ) ;
37- return ( configResolver = ( filePath : string ) => {
38- const absolutePath = resolve ( process . cwd ( ) , filePath ) ;
51+ return ( configResolvers [ cwd ] = ( filePath : string ) => {
52+ const absolutePath = resolve ( cwd , filePath ) ;
3953 return configArrayFactory
4054 . getConfigArrayForFile ( absolutePath )
4155 . extractConfig ( absolutePath )
4256 . toCompatibleObjectAsConfigFileContent ( ) ;
4357 } ) ;
44- } catch {
58+ } catch ( _e ) {
4559 // ignore
60+ // console.log(_e);
4661 }
4762 try {
4863 // For ESLint v6
4964
5065 // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires -- special
5166 const eslint = require ( "eslint" ) ;
52- const engine = new eslint . CLIEngine ( { } ) ;
67+ const engine = new eslint . CLIEngine ( { cwd } ) ;
5368 engine . addPlugin ( "eslint-plugin-jsonc" , plugin ) ;
54- return ( configResolver = ( filePath ) => {
69+ return ( configResolvers [ cwd ] = ( filePath ) => {
5570 // Adjust the file name to avoid a crash.
5671 // https://github.com/ota-meshi/eslint-plugin-jsonc/issues/28
5772 let targetFilePath = filePath ;
@@ -95,8 +110,11 @@ function isValidFilename(filename: string) {
95110 * Get config for the given filename
96111 * @param filename
97112 */
98- function getConfig ( filename : string ) : Linter . Config {
99- return getConfigResolver ( ) ( filename ) ;
113+ function getConfig (
114+ cwd : string ,
115+ filename : string ,
116+ ) : Pick < Linter . Config , "rules" > {
117+ return getConfigResolver ( cwd ) ( filename ) ;
100118}
101119
102120/**
@@ -108,24 +126,31 @@ function getJsoncRule(rule: string) {
108126 ruleNames ||
109127 new Set (
110128 // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires -- special
111- ( require ( "./rules" ) . rules as RuleModule [ ] ) . map (
129+ ( require ( ".. /rules" ) . rules as RuleModule [ ] ) . map (
112130 ( r ) => r . meta . docs . ruleName ,
113131 ) ,
114132 ) ;
115133
116- return ruleNames . has ( rule ) ? `jsonc/${ rule } ` : null ;
134+ const ruleName = rule . startsWith ( "@stylistic/" )
135+ ? rule . split ( "/" ) . pop ( ) ?? rule
136+ : rule ;
137+
138+ return ruleNames . has ( ruleName ) ? `jsonc/${ ruleName } ` : null ;
117139}
118140
119141/**
120142 * Get additional jsonc rules config from fileName
121143 * @param filename
122144 */
123- export function getAutoConfig ( filename : string ) : {
145+ export function getAutoConfig (
146+ cwd : string ,
147+ filename : string ,
148+ ) : {
124149 [ name : string ] : Linter . RuleEntry ;
125150} {
126151 const autoConfig : { [ name : string ] : Linter . RuleEntry } = { } ;
127152
128- const config = getConfig ( filename ) ;
153+ const config = getConfig ( cwd , filename ) ;
129154 if ( config . rules ) {
130155 for ( const ruleName of Object . keys ( config . rules ) ) {
131156 const jsoncName = getJsoncRule ( ruleName ) ;
0 commit comments