Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/services/pathCompletions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions tests/cases/fourslash/completionsPathsJsonModule.ts
Original file line number Diff line number Diff line change
@@ -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 });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: It would more realistically be node_modules/package-name/index.json.

12 changes: 12 additions & 0 deletions tests/cases/fourslash/completionsPathsJsonModuleWithAmd.ts
Original file line number Diff line number Diff line change
@@ -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 });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doesn't this have a completion?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because module resolution strategy isnt "node" and hence json files wont be resolved.

Original file line number Diff line number Diff line change
@@ -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 changes: 12 additions & 0 deletions tests/cases/fourslash/completionsPathsRelativeJsonModule.ts
Original file line number Diff line number Diff line change
@@ -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 });