From b5ed7f3edaccf373adc07dd4018cad85b819cc43 Mon Sep 17 00:00:00 2001 From: zhengbli Date: Tue, 19 Jan 2016 16:30:52 -0800 Subject: [PATCH] Add support for jsconfig.json in language service --- src/compiler/commandLineParser.ts | 11 ++++++++--- src/compiler/tsc.ts | 2 +- src/server/editorServices.ts | 16 ++++++++++------ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 247a204e9bd..af795377bc0 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -493,8 +493,9 @@ namespace ts { * @param basePath A root directory to resolve relative path entries in the config * file to. e.g. outDir */ - export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions: CompilerOptions = {}): ParsedCommandLine { - const { options: optionsFromJsonConfigFile, errors } = convertCompilerOptionsFromJson(json["compilerOptions"], basePath); + export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, configFileName: string, existingOptions: CompilerOptions = {}): ParsedCommandLine { + const basePath = getDirectoryPath(configFileName); + const { options: optionsFromJsonConfigFile, errors } = convertCompilerOptionsFromJson(json["compilerOptions"], basePath, configFileName); const options = extend(existingOptions, optionsFromJsonConfigFile); return { @@ -547,10 +548,14 @@ namespace ts { } } - export function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string): { options: CompilerOptions, errors: Diagnostic[] } { + export function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { options: CompilerOptions, errors: Diagnostic[] } { const options: CompilerOptions = {}; const errors: Diagnostic[] = []; + if (configFileName && getBaseFileName(configFileName) === "jsconfig.json") { + options.allowJs = true; + } + if (!jsonOptions) { return { options, errors }; } diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index 808ee6da804..94dc8b18847 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -376,7 +376,7 @@ namespace ts { sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped); return; } - const configParseResult = parseJsonConfigFileContent(configObject, sys, getDirectoryPath(configFileName), commandLine.options); + const configParseResult = parseJsonConfigFileContent(configObject, sys, configFileName, commandLine.options); if (configParseResult.errors.length > 0) { reportDiagnostics(configParseResult.errors, /* compilerHost */ undefined); sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped); diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 7cc3a6c96a6..a43252868d8 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1026,10 +1026,16 @@ namespace ts.server { // the newly opened file. findConfigFile(searchPath: string): string { while (true) { - const fileName = ts.combinePaths(searchPath, "tsconfig.json"); - if (this.host.fileExists(fileName)) { - return fileName; + const tsconfigFileName = ts.combinePaths(searchPath, "tsconfig.json"); + if (this.host.fileExists(tsconfigFileName)) { + return tsconfigFileName; } + + const jsconfigFileName = ts.combinePaths(searchPath, "jsconfig.json"); + if (this.host.fileExists(jsconfigFileName)) { + return jsconfigFileName; + } + const parentPath = ts.getDirectoryPath(searchPath); if (parentPath === searchPath) { break; @@ -1172,15 +1178,13 @@ namespace ts.server { configFileToProjectOptions(configFilename: string): { succeeded: boolean, projectOptions?: ProjectOptions, error?: ProjectOpenResult } { configFilename = ts.normalizePath(configFilename); - // file references will be relative to dirPath (or absolute) - const dirPath = ts.getDirectoryPath(configFilename); const contents = this.host.readFile(configFilename); const rawConfig: { config?: ProjectOptions; error?: Diagnostic; } = ts.parseConfigFileTextToJson(configFilename, contents); if (rawConfig.error) { return { succeeded: false, error: rawConfig.error }; } else { - const parsedCommandLine = ts.parseJsonConfigFileContent(rawConfig.config, this.host, dirPath); + const parsedCommandLine = ts.parseJsonConfigFileContent(rawConfig.config, this.host, configFilename); Debug.assert(!!parsedCommandLine.fileNames); if (parsedCommandLine.errors && (parsedCommandLine.errors.length > 0)) {