From e8cd6b1111b72c5c9575d7a609d737291c8ee17a Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Fri, 4 May 2018 17:10:48 -0700 Subject: [PATCH 1/2] Suggest json files in completion when resolveJsonModule is set and module resolution is node Fixes #23899 --- src/services/pathCompletions.ts | 13 ++++++++++--- tests/cases/fourslash/completionsPathsJsonModule.ts | 12 ++++++++++++ .../fourslash/completionsPathsJsonModuleWithAmd.ts | 12 ++++++++++++ ...etionsPathsJsonModuleWithoutResolveJsonModule.ts | 11 +++++++++++ .../fourslash/completionsPathsRelativeJsonModule.ts | 12 ++++++++++++ 5 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 tests/cases/fourslash/completionsPathsJsonModule.ts create mode 100644 tests/cases/fourslash/completionsPathsJsonModuleWithAmd.ts create mode 100644 tests/cases/fourslash/completionsPathsJsonModuleWithoutResolveJsonModule.ts create mode 100644 tests/cases/fourslash/completionsPathsRelativeJsonModule.ts diff --git a/src/services/pathCompletions.ts b/src/services/pathCompletions.ts index 7fd3b7cfd0bf6..e2725bd46faec 100644 --- a/src/services/pathCompletions.ts +++ b/src/services/pathCompletions.ts @@ -27,7 +27,7 @@ namespace ts.Completions.PathCompletions { const scriptDirectory = getDirectoryPath(scriptPath); if (isPathRelativeToScript(literalValue) || isRootedDiskPath(literalValue)) { - const extensions = getSupportedExtensions(compilerOptions); + const extensions = getSupportExtensionsForModuleResolution(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 getSupportExtensionsForModuleResolution(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 = getSupportExtensionsForModuleResolution(compilerOptions); if (baseUrl) { const projectDir = compilerOptions.project || host.getCurrentDirectory(); const absolute = isRootedDiskPath(baseUrl) ? baseUrl : combinePaths(projectDir, baseUrl); diff --git a/tests/cases/fourslash/completionsPathsJsonModule.ts b/tests/cases/fourslash/completionsPathsJsonModule.ts new file mode 100644 index 0000000000000..6d1a621d69a65 --- /dev/null +++ b/tests/cases/fourslash/completionsPathsJsonModule.ts @@ -0,0 +1,12 @@ +/// + +// @moduleResolution: node +// @resolveJsonModule: true + +// @Filename: /project/node_modules/test.json +////not read + +// @Filename: /project/index.ts +////import { } from "/**/"; + +verify.completionsAt("", ["test.json"], { isNewIdentifierLocation: true }); \ No newline at end of file diff --git a/tests/cases/fourslash/completionsPathsJsonModuleWithAmd.ts b/tests/cases/fourslash/completionsPathsJsonModuleWithAmd.ts new file mode 100644 index 0000000000000..8a2388655af74 --- /dev/null +++ b/tests/cases/fourslash/completionsPathsJsonModuleWithAmd.ts @@ -0,0 +1,12 @@ +/// + +// @module: amd +// @resolveJsonModule: true + +// @Filename: /project/test.json +////not read + +// @Filename: /project/index.ts +////import { } from ".//**/"; + +verify.completionsAt("", [], { isNewIdentifierLocation: true }); \ No newline at end of file diff --git a/tests/cases/fourslash/completionsPathsJsonModuleWithoutResolveJsonModule.ts b/tests/cases/fourslash/completionsPathsJsonModuleWithoutResolveJsonModule.ts new file mode 100644 index 0000000000000..432fc6e858b6a --- /dev/null +++ b/tests/cases/fourslash/completionsPathsJsonModuleWithoutResolveJsonModule.ts @@ -0,0 +1,11 @@ +/// + +// @moduleResolution: node + +// @Filename: /project/test.json +////not read + +// @Filename: /project/index.ts +////import { } from ".//**/"; + +verify.completionsAt("", [], { isNewIdentifierLocation: true }); diff --git a/tests/cases/fourslash/completionsPathsRelativeJsonModule.ts b/tests/cases/fourslash/completionsPathsRelativeJsonModule.ts new file mode 100644 index 0000000000000..93a6ac6e4e239 --- /dev/null +++ b/tests/cases/fourslash/completionsPathsRelativeJsonModule.ts @@ -0,0 +1,12 @@ +/// + +// @moduleResolution: node +// @resolveJsonModule: true + +// @Filename: /project/test.json +////not read + +// @Filename: /project/index.ts +////import { } from ".//**/"; + +verify.completionsAt("", ["test.json"], { isNewIdentifierLocation: true }); \ No newline at end of file From 92296b34ddb2ecbf7ee200d630a575462b1a4c3e Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Mon, 7 May 2018 12:14:53 -0700 Subject: [PATCH 2/2] Rename --- src/services/pathCompletions.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/services/pathCompletions.ts b/src/services/pathCompletions.ts index e2725bd46faec..f42d70a0c5240 100644 --- a/src/services/pathCompletions.ts +++ b/src/services/pathCompletions.ts @@ -27,7 +27,7 @@ namespace ts.Completions.PathCompletions { const scriptDirectory = getDirectoryPath(scriptPath); if (isPathRelativeToScript(literalValue) || isRootedDiskPath(literalValue)) { - const extensions = getSupportExtensionsForModuleResolution(compilerOptions); + const extensions = getSupportedExtensionsForModuleResolution(compilerOptions); if (compilerOptions.rootDirs) { return getCompletionEntriesForDirectoryFragmentWithRootDirs( compilerOptions.rootDirs, literalValue, scriptDirectory, extensions, /*includeExtensions*/ false, compilerOptions, host, scriptPath); @@ -42,7 +42,7 @@ namespace ts.Completions.PathCompletions { } } - function getSupportExtensionsForModuleResolution(compilerOptions: CompilerOptions) { + function getSupportedExtensionsForModuleResolution(compilerOptions: CompilerOptions) { const extensions = getSupportedExtensions(compilerOptions); return compilerOptions.resolveJsonModule && getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.NodeJs ? extensions.concat(Extension.Json) : @@ -169,7 +169,7 @@ namespace ts.Completions.PathCompletions { const result: NameAndKind[] = []; - const fileExtensions = getSupportExtensionsForModuleResolution(compilerOptions); + const fileExtensions = getSupportedExtensionsForModuleResolution(compilerOptions); if (baseUrl) { const projectDir = compilerOptions.project || host.getCurrentDirectory(); const absolute = isRootedDiskPath(baseUrl) ? baseUrl : combinePaths(projectDir, baseUrl);