diff --git a/packages/language-core/lib/compilerOptions.ts b/packages/language-core/lib/compilerOptions.ts index 7ae177aedb..622b3b196b 100644 --- a/packages/language-core/lib/compilerOptions.ts +++ b/packages/language-core/lib/compilerOptions.ts @@ -5,104 +5,89 @@ import { generateGlobalTypes, getGlobalTypesFileName } from './codegen/globalTyp import type { RawVueCompilerOptions, VueCompilerOptions, VueLanguagePlugin } from './types'; import { hyphenateTag } from './utils/shared'; -interface ParseConfigHost extends Omit {} - export interface ParsedCommandLine extends Omit { vueOptions: VueCompilerOptions; } -export function createParsedCommandLineByJson( - ts: typeof import('typescript'), - host: ParseConfigHost, - rootDir: string, - json: any, - configFileName?: string, -): ParsedCommandLine { - const extendedPaths = new Set(); - const proxyHost = { - ...host, - readFile(fileName: string) { - if (!fileName.endsWith('/package.json')) { - extendedPaths.add(fileName); - } - return host.readFile(fileName); - }, - readDirectory() { - return []; - }, - }; - const parsed = ts.parseJsonConfigFileContent(json, proxyHost, rootDir, {}, configFileName); - const resolver = new CompilerOptionsResolver(host.fileExists); - - for (const extendPath of [...extendedPaths].reverse()) { - try { - const configFile = ts.readJsonConfigFile(extendPath, host.readFile); - const obj = ts.convertToObject(configFile, []); - const rawOptions: RawVueCompilerOptions = obj?.vueCompilerOptions ?? {}; - resolver.addConfig(rawOptions, path.dirname(configFile.fileName)); - } - catch {} - } - - // ensure the rootDir is added to the config roots - resolver.addConfig({}, rootDir); - - return { - ...parsed, - vueOptions: resolver.build(), - }; -} +export const createParsedCommandLineByJson = create( + (ts, host, rootDir: string, json: any, configFileName?: string) => { + // `parseJsonConfigFileContent` is missing in tsc + return 'parseJsonConfigFileContent' in (ts as any) + ? ts.parseJsonConfigFileContent(json, host, rootDir, {}, configFileName) + : ts.parseJsonSourceFileConfigFileContent( + ts.parseJsonText(configFileName ?? 'tsconfig.json', JSON.stringify(json)), + host, + rootDir, + {}, + configFileName, + ); + }, +); -export function createParsedCommandLine( - ts: typeof import('typescript'), - host: ParseConfigHost, - configFileName: string, -): ParsedCommandLine { - try { - const extendedPaths = new Set(); - const proxyHost = { - ...host, - readFile(fileName: string) { - if (!fileName.endsWith('/package.json')) { - extendedPaths.add(fileName); - } - return host.readFile(fileName); - }, - readDirectory() { - return []; - }, - }; - const config = ts.readJsonConfigFile(configFileName, proxyHost.readFile); - const parsed = ts.parseJsonSourceFileConfigFileContent( +export const createParsedCommandLine = create( + (ts, host, configFileName: string) => { + const config = ts.readJsonConfigFile(configFileName, host.readFile); + return ts.parseJsonSourceFileConfigFileContent( config, - proxyHost, + host, path.dirname(configFileName), {}, configFileName, ); - const resolver = new CompilerOptionsResolver(host.fileExists); + }, +); - for (const extendPath of [...extendedPaths].reverse()) { - try { - const configFile = ts.readJsonConfigFile(extendPath, host.readFile); - const obj = ts.convertToObject(configFile, []); - const rawOptions: RawVueCompilerOptions = obj?.vueCompilerOptions ?? {}; - resolver.addConfig(rawOptions, path.dirname(configFile.fileName)); +function create( + getParsedCommandLine: ( + ts: typeof import('typescript'), + host: ts.ParseConfigHost, + ...args: T + ) => ts.ParsedCommandLine, +) { + return ( + ts: typeof import('typescript'), + host: Omit, + ...args: T + ): ParsedCommandLine => { + try { + const extendedPaths = new Set(); + const proxyHost = { + ...host, + readFile(fileName: string) { + if (!fileName.endsWith('/package.json')) { + extendedPaths.add(fileName); + } + return host.readFile(fileName); + }, + readDirectory() { + return []; + }, + }; + const parsed = getParsedCommandLine(ts, proxyHost, ...args); + + const resolver = new CompilerOptionsResolver(host.fileExists); + for (const extendPath of [...extendedPaths].reverse()) { + try { + const configFile = ts.readJsonConfigFile(extendPath, host.readFile); + const obj = ts.convertToObject(configFile, []); + const rawOptions: RawVueCompilerOptions = obj?.vueCompilerOptions ?? {}; + resolver.addConfig(rawOptions, path.dirname(configFile.fileName)); + } + catch {} } - catch {} + + return { + ...parsed, + vueOptions: resolver.build(), + }; } + catch {} return { - ...parsed, - vueOptions: resolver.build(), + options: {}, + errors: [], + vueOptions: getDefaultCompilerOptions(), }; - } - catch {} - - return { - options: {}, - errors: [], - vueOptions: getDefaultCompilerOptions(), }; }