Skip to content

Let AutoImportProviderProject resolve JS when allowJs and maxNodeModulesJsDepth allows #45524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 25, 2021
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
5 changes: 2 additions & 3 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1078,9 +1078,8 @@ namespace ts {
}

/* @internal */
export function tryResolveJSModule(moduleName: string, initialDir: string, host: ModuleResolutionHost): string | undefined {
const { resolvedModule } = tryResolveJSModuleWorker(moduleName, initialDir, host);
return resolvedModule && resolvedModule.resolvedFileName;
export function tryResolveJSModule(moduleName: string, initialDir: string, host: ModuleResolutionHost) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This function was never used before now 👀

return tryResolveJSModuleWorker(moduleName, initialDir, host).resolvedModule;
}

const jsOnlyExtensions = [Extensions.JavaScript];
Expand Down
7 changes: 7 additions & 0 deletions src/harness/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ namespace ts.server {
this.processResponse(request, /*expectEmptyBody*/ true);
}

/*@internal*/
setCompilerOptionsForInferredProjects(options: protocol.CompilerOptions) {
const args: protocol.SetCompilerOptionsForInferredProjectsArgs = { options };
const request = this.processRequest(CommandNames.CompilerOptionsForInferredProjects, args);
this.processResponse(request, /*expectEmptyBody*/ false);
}

openFile(file: string, fileContent?: string, scriptKindName?: "TS" | "JS" | "TSX" | "JSX"): void {
const args: protocol.OpenRequestArgs = { file, fileContent, scriptKindName };
this.processRequest(CommandNames.Open, args);
Expand Down
9 changes: 7 additions & 2 deletions src/harness/fourslashImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3935,6 +3935,11 @@ namespace FourSlash {
(this.languageService as ts.server.SessionClient).configurePlugin(pluginName, configuration);
}

public setCompilerOptionsForInferredProjects(options: ts.server.protocol.CompilerOptions) {
ts.Debug.assert(this.testType === FourSlashTestType.Server);
(this.languageService as ts.server.SessionClient).setCompilerOptionsForInferredProjects(options);
}

public toggleLineComment(newFileContent: string): void {
const changes: ts.TextChange[] = [];
for (const range of this.getRanges()) {
Expand Down Expand Up @@ -4077,15 +4082,15 @@ namespace FourSlash {
try {
const test = new FourSlashInterface.Test(state);
const goTo = new FourSlashInterface.GoTo(state);
const plugins = new FourSlashInterface.Plugins(state);
const config = new FourSlashInterface.Config(state);
const verify = new FourSlashInterface.Verify(state);
const edit = new FourSlashInterface.Edit(state);
const debug = new FourSlashInterface.Debug(state);
const format = new FourSlashInterface.Format(state);
const cancellation = new FourSlashInterface.Cancellation(state);
// eslint-disable-next-line no-eval
const f = eval(wrappedCode);
f(test, goTo, plugins, verify, edit, debug, format, cancellation, FourSlashInterface.classification, FourSlashInterface.Completion, verifyOperationIsCancelled);
f(test, goTo, config, verify, edit, debug, format, cancellation, FourSlashInterface.classification, FourSlashInterface.Completion, verifyOperationIsCancelled);
}
catch (err) {
// ensure 'source-map-support' is triggered while we still have the handler attached by accessing `error.stack`.
Expand Down
6 changes: 5 additions & 1 deletion src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ namespace FourSlashInterface {
}
}

export class Plugins {
export class Config {
constructor(private state: FourSlash.TestState) {
}

public configurePlugin(pluginName: string, configuration: any): void {
this.state.configurePlugin(pluginName, configuration);
}

public setCompilerOptionsForInferredProjects(options: ts.server.protocol.CompilerOptions): void {
this.state.setCompilerOptionsForInferredProjects(options);
}
}

export class GoTo {
Expand Down
2 changes: 1 addition & 1 deletion src/harness/harnessLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ namespace Harness.LanguageService {

setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): any {
// eslint-disable-next-line no-restricted-globals
return setTimeout(callback, ms, args);
return setTimeout(callback, ms, ...args);
Copy link
Member Author

Choose a reason for hiding this comment

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

At least a 5-year-old bug right here

}

clearTimeout(timeoutId: any): void {
Expand Down
23 changes: 16 additions & 7 deletions src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1919,16 +1919,25 @@ namespace ts.server {
}

if (dependencyNames) {
const resolutions = map(arrayFrom(dependencyNames.keys()), name => resolveTypeReferenceDirective(
name,
rootFileName,
compilerOptions,
moduleResolutionHost));
const resolutions = mapDefined(arrayFrom(dependencyNames.keys()), name => {
const types = resolveTypeReferenceDirective(
name,
rootFileName,
compilerOptions,
moduleResolutionHost);

if (types.resolvedTypeReferenceDirective) {
return types.resolvedTypeReferenceDirective;
}
if (compilerOptions.allowJs && compilerOptions.maxNodeModuleJsDepth) {
return tryResolveJSModule(name, hostProject.currentDirectory, moduleResolutionHost);
}
});

const symlinkCache = hostProject.getSymlinkCache();
for (const resolution of resolutions) {
if (!resolution.resolvedTypeReferenceDirective?.resolvedFileName) continue;
const { resolvedFileName, originalPath } = resolution.resolvedTypeReferenceDirective;
if (!resolution.resolvedFileName) continue;
const { resolvedFileName, originalPath } = resolution;
if (!program.getSourceFile(resolvedFileName) && (!originalPath || !program.getSourceFile(originalPath))) {
rootNames = append(rootNames, resolvedFileName);
// Avoid creating a large project that would significantly slow down time to editor interactivity
Expand Down
19 changes: 17 additions & 2 deletions tests/cases/fourslash/fourslash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ declare module ts {
exportName: string;
}

interface CompilerOptions {
module?: string;
target?: string;
jsx?: string;
allowJs?: boolean;
maxNodeModulesJsDepth?: number;
strictNullChecks?: boolean;
sourceMap?: boolean;
allowSyntheticDefaultImports?: boolean;
allowNonTsExtensions?: boolean;
resolveJsonModule?: boolean;
[key: string]: string | number | boolean | undefined;
}

function flatMap<T, U>(array: ReadonlyArray<T>, mapfn: (x: T, i: number) => U | ReadonlyArray<U> | undefined): U[];
}

Expand Down Expand Up @@ -200,8 +214,9 @@ declare namespace FourSlashInterface {
symbolsInScope(range: Range): any[];
setTypesRegistry(map: { [key: string]: void }): void;
}
class plugins {
class config {
configurePlugin(pluginName: string, configuration: any): void;
setCompilerOptionsForInferredProjects(options: ts.CompilerOptions)
}
class goTo {
marker(name?: string | Marker): void;
Expand Down Expand Up @@ -810,7 +825,7 @@ declare namespace FourSlashInterface {
declare function ignoreInterpolations(diagnostic: string | ts.DiagnosticMessage): FourSlashInterface.DiagnosticIgnoredInterpolations;
declare function verifyOperationIsCancelled(f: any): void;
declare var test: FourSlashInterface.test_;
declare var plugins: FourSlashInterface.plugins;
declare var config: FourSlashInterface.config;
declare var goTo: FourSlashInterface.goTo;
declare var verify: FourSlashInterface.verify;
declare var edit: FourSlashInterface.edit;
Expand Down
37 changes: 37 additions & 0 deletions tests/cases/fourslash/server/autoImportSymlinkedJsPackages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// <reference path="../fourslash.ts" />

// @Filename: /packages/a/package.json
//// {
//// "name": "package-a",
//// "dependencies": {
//// "package-b": "*"
//// }
//// }

// @Filename: /packages/a/index.js
//// packageB/**/

// @Filename: /packages/b/package.json
//// { "name": "package-b", "main": "index.js" }

// @Filename: /packages/b/index.js
//// export const packageB = "package-b";

// @link: /packages/b -> /packages/a/node_modules/package-b

config.setCompilerOptionsForInferredProjects({ module: "commonjs", allowJs: true, maxNodeModulesJsDepth: 2 });
goTo.marker("");
verify.completions({
marker: "",
includes: [{
name: "packageB",
source: "package-b",
sourceDisplay: "package-b",
hasAction: true,
sortText: completion.SortText.AutoImportSuggestions,
}],
preferences: {
includeCompletionsForModuleExports: true,
allowIncompleteCompletions: true,
}
});
2 changes: 1 addition & 1 deletion tests/cases/fourslash/server/configurePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
// Test that plugin adds an error message which is able to be configured
goTo.marker();
verify.getSemanticDiagnostics([{ message: "configured error", code: 9999, range: { pos: 0, end: 3, fileName: "a.ts" } }]);
plugins.configurePlugin("configurable-diagnostic-adder", { message: "new error" });
config.configurePlugin("configurable-diagnostic-adder", { message: "new error" });
verify.getSemanticDiagnostics([{ message: "new error", code: 9999, range: { pos: 0, end: 3, fileName: "a.ts" } }]);