MCPcopy
hub / github.com/xojs/xo / preProcessXoConfig

Function preProcessXoConfig

lib/utils.ts:109–189  ·  view source on GitHub ↗
(xoConfig: XoConfigItem[])

Source from the content-addressed store, hash-verified

107*/
108// eslint-disable-next-line complexity
109export const preProcessXoConfig = (xoConfig: XoConfigItem[]): {config: XoConfigItem[]; tsFilesGlob: string[]; tsFilesIgnoresGlob: string[]} => {
110 validateXoConfig(xoConfig);
111
112 const tsFilesGlob: string[] = [];
113 const tsFilesIgnoresGlob: string[] = [];
114
115 // The first config item is the internal base config; push it through unmodified.
116 const processedConfig: XoConfigItem[] = xoConfig[0] ? [{...xoConfig[0]}] : [];
117
118 for (const {...config} of xoConfig.values().drop(1)) {
119 const {languageOptions} = config;
120 const parserOptions = languageOptions?.['parserOptions'] as TypeScriptParserOptions | undefined;
121
122 // Use TS parser/plugin for JS files if the config contains TypeScript rules which are applied to JS files.
123 // typescript-eslint rules set to "off" are ignored and not applied to JS files.
124 if (
125 config.rules
126
127 && languageOptions?.['parser'] === undefined
128 && parserOptions?.project === undefined
129 && parserOptions?.programs === undefined
130 && !config.plugins?.['@typescript-eslint']
131 ) {
132 const hasTsRules = Object.entries(config.rules).some(rulePair => {
133 // If its not a @typescript-eslint rule, we don't care
134 if (!rulePair[0].startsWith('@typescript-eslint/')) {
135 return false;
136 }
137
138 if (Array.isArray(rulePair[1])) {
139 return rulePair[1]?.[0] !== 'off' && rulePair[1]?.[0] !== 0;
140 }
141
142 return rulePair[1] !== 'off'
143 && rulePair[1] !== 0;
144 });
145
146 if (hasTsRules) {
147 let isAppliedToJsFiles = false;
148
149 if (config.files !== undefined) {
150 const normalizedFiles = arrify(config.files).flat().map(file => path.normalize(file));
151 // Strip the basename off any globs
152 const globs = normalizedFiles.map(file => micromatch.scan(file, {dot: true}).glob).filter(Boolean);
153 // Check if the files globs match a test file with a js extension
154 // If not, check that the file paths match a js extension
155 isAppliedToJsFiles = micromatch.some(jsExtensions.map(ext => `test.${ext}`), globs, {dot: true})
156 || micromatch.some(normalizedFiles, jsFilesGlob, {dot: true});
157 } else if (config.files === undefined) {
158 isAppliedToJsFiles = true;
159 }
160
161 if (isAppliedToJsFiles) {
162 const updatedLanguageOptions: Linter.LanguageOptions = languageOptions
163 ? {...languageOptions, parser: typescriptParser}
164 : {parser: typescriptParser};
165 config.languageOptions = updatedLanguageOptions;
166 tsFilesGlob.push(...arrify(config.files ?? allFilesGlob).flat());

Callers 3

setXoConfigMethod · 0.85

Calls 1

validateXoConfigFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…