Skip to content

Commit 8fc4242

Browse files
authored
Merge pull request microsoft#23915 from Microsoft/jsonCompletions
Suggest json files in completion when resolveJsonModule is set and module resolution is node
2 parents 004a558 + 92296b3 commit 8fc4242

5 files changed

+57
-3
lines changed

src/services/pathCompletions.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace ts.Completions.PathCompletions {
2727
const scriptDirectory = getDirectoryPath(scriptPath);
2828

2929
if (isPathRelativeToScript(literalValue) || isRootedDiskPath(literalValue)) {
30-
const extensions = getSupportedExtensions(compilerOptions);
30+
const extensions = getSupportedExtensionsForModuleResolution(compilerOptions);
3131
if (compilerOptions.rootDirs) {
3232
return getCompletionEntriesForDirectoryFragmentWithRootDirs(
3333
compilerOptions.rootDirs, literalValue, scriptDirectory, extensions, /*includeExtensions*/ false, compilerOptions, host, scriptPath);
@@ -42,6 +42,13 @@ namespace ts.Completions.PathCompletions {
4242
}
4343
}
4444

45+
function getSupportedExtensionsForModuleResolution(compilerOptions: CompilerOptions) {
46+
const extensions = getSupportedExtensions(compilerOptions);
47+
return compilerOptions.resolveJsonModule && getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.NodeJs ?
48+
extensions.concat(Extension.Json) :
49+
extensions;
50+
}
51+
4552
/**
4653
* Takes a script path and returns paths for all potential folders that could be merged with its
4754
* containing folder via the "rootDirs" compiler option
@@ -122,7 +129,7 @@ namespace ts.Completions.PathCompletions {
122129
continue;
123130
}
124131

125-
const foundFileName = includeExtensions ? getBaseFileName(filePath) : removeFileExtension(getBaseFileName(filePath));
132+
const foundFileName = includeExtensions || fileExtensionIs(filePath, Extension.Json) ? getBaseFileName(filePath) : removeFileExtension(getBaseFileName(filePath));
126133

127134
if (!foundFiles.has(foundFileName)) {
128135
foundFiles.set(foundFileName, true);
@@ -162,7 +169,7 @@ namespace ts.Completions.PathCompletions {
162169

163170
const result: NameAndKind[] = [];
164171

165-
const fileExtensions = getSupportedExtensions(compilerOptions);
172+
const fileExtensions = getSupportedExtensionsForModuleResolution(compilerOptions);
166173
if (baseUrl) {
167174
const projectDir = compilerOptions.project || host.getCurrentDirectory();
168175
const absolute = isRootedDiskPath(baseUrl) ? baseUrl : combinePaths(projectDir, baseUrl);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @moduleResolution: node
4+
// @resolveJsonModule: true
5+
6+
// @Filename: /project/node_modules/test.json
7+
////not read
8+
9+
// @Filename: /project/index.ts
10+
////import { } from "/**/";
11+
12+
verify.completionsAt("", ["test.json"], { isNewIdentifierLocation: true });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @module: amd
4+
// @resolveJsonModule: true
5+
6+
// @Filename: /project/test.json
7+
////not read
8+
9+
// @Filename: /project/index.ts
10+
////import { } from ".//**/";
11+
12+
verify.completionsAt("", [], { isNewIdentifierLocation: true });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @moduleResolution: node
4+
5+
// @Filename: /project/test.json
6+
////not read
7+
8+
// @Filename: /project/index.ts
9+
////import { } from ".//**/";
10+
11+
verify.completionsAt("", [], { isNewIdentifierLocation: true });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @moduleResolution: node
4+
// @resolveJsonModule: true
5+
6+
// @Filename: /project/test.json
7+
////not read
8+
9+
// @Filename: /project/index.ts
10+
////import { } from ".//**/";
11+
12+
verify.completionsAt("", ["test.json"], { isNewIdentifierLocation: true });

0 commit comments

Comments
 (0)