Skip to content

Commit 931d162

Browse files
committed
Merge pull request #6545 from zhengbli/supportJsconfig
Add support for jsconfig.json in language service
2 parents 4ce4367 + 31f5502 commit 931d162

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
lines changed

src/compiler/commandLineParser.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,8 @@ namespace ts {
493493
* @param basePath A root directory to resolve relative path entries in the config
494494
* file to. e.g. outDir
495495
*/
496-
export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions: CompilerOptions = {}): ParsedCommandLine {
497-
const { options: optionsFromJsonConfigFile, errors } = convertCompilerOptionsFromJson(json["compilerOptions"], basePath);
496+
export function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions: CompilerOptions = {}, configFileName?: string): ParsedCommandLine {
497+
const { options: optionsFromJsonConfigFile, errors } = convertCompilerOptionsFromJson(json["compilerOptions"], basePath, configFileName);
498498

499499
const options = extend(existingOptions, optionsFromJsonConfigFile);
500500
return {
@@ -523,7 +523,7 @@ namespace ts {
523523
for (const extension of supportedExtensions) {
524524
const filesInDirWithExtension = host.readDirectory(basePath, extension, exclude);
525525
for (const fileName of filesInDirWithExtension) {
526-
// .ts extension would read the .d.ts extension files too but since .d.ts is lower priority extension,
526+
// .ts extension would read the .d.ts extension files too but since .d.ts is lower priority extension,
527527
// lets pick them when its turn comes up
528528
if (extension === ".ts" && fileExtensionIs(fileName, ".d.ts")) {
529529
continue;
@@ -547,10 +547,15 @@ namespace ts {
547547
}
548548
}
549549

550-
export function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string): { options: CompilerOptions, errors: Diagnostic[] } {
550+
export function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { options: CompilerOptions, errors: Diagnostic[] } {
551551
const options: CompilerOptions = {};
552552
const errors: Diagnostic[] = [];
553553

554+
if (configFileName && getBaseFileName(configFileName) === "jsconfig.json") {
555+
options.module = ModuleKind.CommonJS;
556+
options.allowJs = true;
557+
}
558+
554559
if (!jsonOptions) {
555560
return { options, errors };
556561
}

src/compiler/tsc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ namespace ts {
340340
if (sys.watchDirectory && configFileName) {
341341
const directory = ts.getDirectoryPath(configFileName);
342342
directoryWatcher = sys.watchDirectory(
343-
// When the configFileName is just "tsconfig.json", the watched directory should be
343+
// When the configFileName is just "tsconfig.json", the watched directory should be
344344
// the current direcotry; if there is a given "project" parameter, then the configFileName
345345
// is an absolute file name.
346346
directory == "" ? "." : directory,

src/server/editorServices.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ namespace ts.server {
120120
if (!resolution) {
121121
const existingResolution = currentResolutionsInFile && ts.lookUp(currentResolutionsInFile, moduleName);
122122
if (moduleResolutionIsValid(existingResolution)) {
123-
// ok, it is safe to use existing module resolution results
123+
// ok, it is safe to use existing module resolution results
124124
resolution = existingResolution;
125125
}
126126
else {
@@ -145,8 +145,8 @@ namespace ts.server {
145145
}
146146

147147
if (resolution.resolvedModule) {
148-
// TODO: consider checking failedLookupLocations
149-
// TODO: use lastCheckTime to track expiration for module name resolution
148+
// TODO: consider checking failedLookupLocations
149+
// TODO: use lastCheckTime to track expiration for module name resolution
150150
return true;
151151
}
152152

@@ -483,7 +483,7 @@ namespace ts.server {
483483
openFileRootsConfigured: ScriptInfo[] = [];
484484
// a path to directory watcher map that detects added tsconfig files
485485
directoryWatchersForTsconfig: ts.Map<FileWatcher> = {};
486-
// count of how many projects are using the directory watcher. If the
486+
// count of how many projects are using the directory watcher. If the
487487
// number becomes 0 for a watcher, then we should close it.
488488
directoryWatchersRefCount: ts.Map<number> = {};
489489
hostConfiguration: HostConfiguration;
@@ -564,11 +564,11 @@ namespace ts.server {
564564
// We check if the project file list has changed. If so, we update the project.
565565
if (!arrayIsEqualTo(currentRootFiles && currentRootFiles.sort(), newRootFiles && newRootFiles.sort())) {
566566
// For configured projects, the change is made outside the tsconfig file, and
567-
// it is not likely to affect the project for other files opened by the client. We can
567+
// it is not likely to affect the project for other files opened by the client. We can
568568
// just update the current project.
569569
this.updateConfiguredProject(project);
570570

571-
// Call updateProjectStructure to clean up inferred projects we may have
571+
// Call updateProjectStructure to clean up inferred projects we may have
572572
// created for the new files
573573
this.updateProjectStructure();
574574
}
@@ -792,8 +792,8 @@ namespace ts.server {
792792
* @param info The file that has been closed or newly configured
793793
*/
794794
closeOpenFile(info: ScriptInfo) {
795-
// Closing file should trigger re-reading the file content from disk. This is
796-
// because the user may chose to discard the buffer content before saving
795+
// Closing file should trigger re-reading the file content from disk. This is
796+
// because the user may chose to discard the buffer content before saving
797797
// to the disk, and the server's version of the file can be out of sync.
798798
info.svc.reloadFromFile(info.fileName);
799799

@@ -891,8 +891,8 @@ namespace ts.server {
891891
}
892892

893893
/**
894-
* This function is to update the project structure for every projects.
895-
* It is called on the premise that all the configured projects are
894+
* This function is to update the project structure for every projects.
895+
* It is called on the premise that all the configured projects are
896896
* up to date.
897897
*/
898898
updateProjectStructure() {
@@ -946,7 +946,7 @@ namespace ts.server {
946946

947947
if (rootFile.defaultProject && rootFile.defaultProject.isConfiguredProject()) {
948948
// If the root file has already been added into a configured project,
949-
// meaning the original inferred project is gone already.
949+
// meaning the original inferred project is gone already.
950950
if (!rootedProject.isConfiguredProject()) {
951951
this.removeProject(rootedProject);
952952
}
@@ -1026,10 +1026,16 @@ namespace ts.server {
10261026
// the newly opened file.
10271027
findConfigFile(searchPath: string): string {
10281028
while (true) {
1029-
const fileName = ts.combinePaths(searchPath, "tsconfig.json");
1030-
if (this.host.fileExists(fileName)) {
1031-
return fileName;
1029+
const tsconfigFileName = ts.combinePaths(searchPath, "tsconfig.json");
1030+
if (this.host.fileExists(tsconfigFileName)) {
1031+
return tsconfigFileName;
1032+
}
1033+
1034+
const jsconfigFileName = ts.combinePaths(searchPath, "jsconfig.json");
1035+
if (this.host.fileExists(jsconfigFileName)) {
1036+
return jsconfigFileName;
10321037
}
1038+
10331039
const parentPath = ts.getDirectoryPath(searchPath);
10341040
if (parentPath === searchPath) {
10351041
break;
@@ -1053,9 +1059,9 @@ namespace ts.server {
10531059
}
10541060

10551061
/**
1056-
* This function tries to search for a tsconfig.json for the given file. If we found it,
1062+
* This function tries to search for a tsconfig.json for the given file. If we found it,
10571063
* we first detect if there is already a configured project created for it: if so, we re-read
1058-
* the tsconfig file content and update the project; otherwise we create a new one.
1064+
* the tsconfig file content and update the project; otherwise we create a new one.
10591065
*/
10601066
openOrUpdateConfiguredProjectForFile(fileName: string) {
10611067
const searchPath = ts.normalizePath(getDirectoryPath(fileName));
@@ -1180,7 +1186,7 @@ namespace ts.server {
11801186
return { succeeded: false, error: rawConfig.error };
11811187
}
11821188
else {
1183-
const parsedCommandLine = ts.parseJsonConfigFileContent(rawConfig.config, this.host, dirPath);
1189+
const parsedCommandLine = ts.parseJsonConfigFileContent(rawConfig.config, this.host, dirPath, /*existingOptions*/ {}, configFilename);
11841190
Debug.assert(!!parsedCommandLine.fileNames);
11851191

11861192
if (parsedCommandLine.errors && (parsedCommandLine.errors.length > 0)) {
@@ -1259,7 +1265,7 @@ namespace ts.server {
12591265
info = this.openFile(fileName, /*openedByClient*/ false);
12601266
}
12611267
else {
1262-
// if the root file was opened by client, it would belong to either
1268+
// if the root file was opened by client, it would belong to either
12631269
// openFileRoots or openFileReferenced.
12641270
if (info.isOpen) {
12651271
if (this.openFileRoots.indexOf(info) >= 0) {

src/services/shims.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,8 @@ namespace ts {
946946
};
947947
}
948948

949-
const configFile = parseJsonConfigFileContent(result.config, this.host, getDirectoryPath(normalizeSlashes(fileName)));
949+
const normalizedFileName = normalizeSlashes(fileName);
950+
const configFile = parseJsonConfigFileContent(result.config, this.host, getDirectoryPath(normalizedFileName), /*existingOptions*/ {}, normalizedFileName);
950951

951952
return {
952953
options: configFile.options,

0 commit comments

Comments
 (0)