mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-07 23:08:20 -06:00
Merge pull request #23915 from Microsoft/jsonCompletions
Suggest json files in completion when resolveJsonModule is set and module resolution is node
This commit is contained in:
commit
8fc4242097
@ -27,7 +27,7 @@ namespace ts.Completions.PathCompletions {
|
||||
const scriptDirectory = getDirectoryPath(scriptPath);
|
||||
|
||||
if (isPathRelativeToScript(literalValue) || isRootedDiskPath(literalValue)) {
|
||||
const extensions = getSupportedExtensions(compilerOptions);
|
||||
const extensions = getSupportedExtensionsForModuleResolution(compilerOptions);
|
||||
if (compilerOptions.rootDirs) {
|
||||
return getCompletionEntriesForDirectoryFragmentWithRootDirs(
|
||||
compilerOptions.rootDirs, literalValue, scriptDirectory, extensions, /*includeExtensions*/ false, compilerOptions, host, scriptPath);
|
||||
@ -42,6 +42,13 @@ namespace ts.Completions.PathCompletions {
|
||||
}
|
||||
}
|
||||
|
||||
function getSupportedExtensionsForModuleResolution(compilerOptions: CompilerOptions) {
|
||||
const extensions = getSupportedExtensions(compilerOptions);
|
||||
return compilerOptions.resolveJsonModule && getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.NodeJs ?
|
||||
extensions.concat(Extension.Json) :
|
||||
extensions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a script path and returns paths for all potential folders that could be merged with its
|
||||
* containing folder via the "rootDirs" compiler option
|
||||
@ -122,7 +129,7 @@ namespace ts.Completions.PathCompletions {
|
||||
continue;
|
||||
}
|
||||
|
||||
const foundFileName = includeExtensions ? getBaseFileName(filePath) : removeFileExtension(getBaseFileName(filePath));
|
||||
const foundFileName = includeExtensions || fileExtensionIs(filePath, Extension.Json) ? getBaseFileName(filePath) : removeFileExtension(getBaseFileName(filePath));
|
||||
|
||||
if (!foundFiles.has(foundFileName)) {
|
||||
foundFiles.set(foundFileName, true);
|
||||
@ -162,7 +169,7 @@ namespace ts.Completions.PathCompletions {
|
||||
|
||||
const result: NameAndKind[] = [];
|
||||
|
||||
const fileExtensions = getSupportedExtensions(compilerOptions);
|
||||
const fileExtensions = getSupportedExtensionsForModuleResolution(compilerOptions);
|
||||
if (baseUrl) {
|
||||
const projectDir = compilerOptions.project || host.getCurrentDirectory();
|
||||
const absolute = isRootedDiskPath(baseUrl) ? baseUrl : combinePaths(projectDir, baseUrl);
|
||||
|
||||
12
tests/cases/fourslash/completionsPathsJsonModule.ts
Normal file
12
tests/cases/fourslash/completionsPathsJsonModule.ts
Normal file
@ -0,0 +1,12 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
|
||||
// @moduleResolution: node
|
||||
// @resolveJsonModule: true
|
||||
|
||||
// @Filename: /project/node_modules/test.json
|
||||
////not read
|
||||
|
||||
// @Filename: /project/index.ts
|
||||
////import { } from "/**/";
|
||||
|
||||
verify.completionsAt("", ["test.json"], { isNewIdentifierLocation: true });
|
||||
12
tests/cases/fourslash/completionsPathsJsonModuleWithAmd.ts
Normal file
12
tests/cases/fourslash/completionsPathsJsonModuleWithAmd.ts
Normal file
@ -0,0 +1,12 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
|
||||
// @module: amd
|
||||
// @resolveJsonModule: true
|
||||
|
||||
// @Filename: /project/test.json
|
||||
////not read
|
||||
|
||||
// @Filename: /project/index.ts
|
||||
////import { } from ".//**/";
|
||||
|
||||
verify.completionsAt("", [], { isNewIdentifierLocation: true });
|
||||
@ -0,0 +1,11 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
|
||||
// @moduleResolution: node
|
||||
|
||||
// @Filename: /project/test.json
|
||||
////not read
|
||||
|
||||
// @Filename: /project/index.ts
|
||||
////import { } from ".//**/";
|
||||
|
||||
verify.completionsAt("", [], { isNewIdentifierLocation: true });
|
||||
12
tests/cases/fourslash/completionsPathsRelativeJsonModule.ts
Normal file
12
tests/cases/fourslash/completionsPathsRelativeJsonModule.ts
Normal file
@ -0,0 +1,12 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
|
||||
// @moduleResolution: node
|
||||
// @resolveJsonModule: true
|
||||
|
||||
// @Filename: /project/test.json
|
||||
////not read
|
||||
|
||||
// @Filename: /project/index.ts
|
||||
////import { } from ".//**/";
|
||||
|
||||
verify.completionsAt("", ["test.json"], { isNewIdentifierLocation: true });
|
||||
Loading…
x
Reference in New Issue
Block a user