Skip to content

Commit 30103de

Browse files
authored
Let AutoImportProviderProject resolve JS when allowJs and maxNodeModulesJsDepth allows (#45524)
* Let AutoImportProviderProject resolve JS when allowJs and maxNodeModulesJsDepth allows * Simplify function
1 parent db0576c commit 30103de

9 files changed

+93
-17
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,9 +1078,8 @@ namespace ts {
10781078
}
10791079

10801080
/* @internal */
1081-
export function tryResolveJSModule(moduleName: string, initialDir: string, host: ModuleResolutionHost): string | undefined {
1082-
const { resolvedModule } = tryResolveJSModuleWorker(moduleName, initialDir, host);
1083-
return resolvedModule && resolvedModule.resolvedFileName;
1081+
export function tryResolveJSModule(moduleName: string, initialDir: string, host: ModuleResolutionHost) {
1082+
return tryResolveJSModuleWorker(moduleName, initialDir, host).resolvedModule;
10841083
}
10851084

10861085
const jsOnlyExtensions = [Extensions.JavaScript];

src/harness/client.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ namespace ts.server {
138138
this.processResponse(request, /*expectEmptyBody*/ true);
139139
}
140140

141+
/*@internal*/
142+
setCompilerOptionsForInferredProjects(options: protocol.CompilerOptions) {
143+
const args: protocol.SetCompilerOptionsForInferredProjectsArgs = { options };
144+
const request = this.processRequest(CommandNames.CompilerOptionsForInferredProjects, args);
145+
this.processResponse(request, /*expectEmptyBody*/ false);
146+
}
147+
141148
openFile(file: string, fileContent?: string, scriptKindName?: "TS" | "JS" | "TSX" | "JSX"): void {
142149
const args: protocol.OpenRequestArgs = { file, fileContent, scriptKindName };
143150
this.processRequest(CommandNames.Open, args);

src/harness/fourslashImpl.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3935,6 +3935,11 @@ namespace FourSlash {
39353935
(this.languageService as ts.server.SessionClient).configurePlugin(pluginName, configuration);
39363936
}
39373937

3938+
public setCompilerOptionsForInferredProjects(options: ts.server.protocol.CompilerOptions) {
3939+
ts.Debug.assert(this.testType === FourSlashTestType.Server);
3940+
(this.languageService as ts.server.SessionClient).setCompilerOptionsForInferredProjects(options);
3941+
}
3942+
39383943
public toggleLineComment(newFileContent: string): void {
39393944
const changes: ts.TextChange[] = [];
39403945
for (const range of this.getRanges()) {
@@ -4077,15 +4082,15 @@ namespace FourSlash {
40774082
try {
40784083
const test = new FourSlashInterface.Test(state);
40794084
const goTo = new FourSlashInterface.GoTo(state);
4080-
const plugins = new FourSlashInterface.Plugins(state);
4085+
const config = new FourSlashInterface.Config(state);
40814086
const verify = new FourSlashInterface.Verify(state);
40824087
const edit = new FourSlashInterface.Edit(state);
40834088
const debug = new FourSlashInterface.Debug(state);
40844089
const format = new FourSlashInterface.Format(state);
40854090
const cancellation = new FourSlashInterface.Cancellation(state);
40864091
// eslint-disable-next-line no-eval
40874092
const f = eval(wrappedCode);
4088-
f(test, goTo, plugins, verify, edit, debug, format, cancellation, FourSlashInterface.classification, FourSlashInterface.Completion, verifyOperationIsCancelled);
4093+
f(test, goTo, config, verify, edit, debug, format, cancellation, FourSlashInterface.classification, FourSlashInterface.Completion, verifyOperationIsCancelled);
40894094
}
40904095
catch (err) {
40914096
// ensure 'source-map-support' is triggered while we still have the handler attached by accessing `error.stack`.

src/harness/fourslashInterfaceImpl.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,17 @@ namespace FourSlashInterface {
4848
}
4949
}
5050

51-
export class Plugins {
51+
export class Config {
5252
constructor(private state: FourSlash.TestState) {
5353
}
5454

5555
public configurePlugin(pluginName: string, configuration: any): void {
5656
this.state.configurePlugin(pluginName, configuration);
5757
}
58+
59+
public setCompilerOptionsForInferredProjects(options: ts.server.protocol.CompilerOptions): void {
60+
this.state.setCompilerOptionsForInferredProjects(options);
61+
}
5862
}
5963

6064
export class GoTo {

src/harness/harnessLanguageService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ namespace Harness.LanguageService {
829829

830830
setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): any {
831831
// eslint-disable-next-line no-restricted-globals
832-
return setTimeout(callback, ms, args);
832+
return setTimeout(callback, ms, ...args);
833833
}
834834

835835
clearTimeout(timeoutId: any): void {

src/server/project.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,16 +1919,25 @@ namespace ts.server {
19191919
}
19201920

19211921
if (dependencyNames) {
1922-
const resolutions = map(arrayFrom(dependencyNames.keys()), name => resolveTypeReferenceDirective(
1923-
name,
1924-
rootFileName,
1925-
compilerOptions,
1926-
moduleResolutionHost));
1922+
const resolutions = mapDefined(arrayFrom(dependencyNames.keys()), name => {
1923+
const types = resolveTypeReferenceDirective(
1924+
name,
1925+
rootFileName,
1926+
compilerOptions,
1927+
moduleResolutionHost);
1928+
1929+
if (types.resolvedTypeReferenceDirective) {
1930+
return types.resolvedTypeReferenceDirective;
1931+
}
1932+
if (compilerOptions.allowJs && compilerOptions.maxNodeModuleJsDepth) {
1933+
return tryResolveJSModule(name, hostProject.currentDirectory, moduleResolutionHost);
1934+
}
1935+
});
19271936

19281937
const symlinkCache = hostProject.getSymlinkCache();
19291938
for (const resolution of resolutions) {
1930-
if (!resolution.resolvedTypeReferenceDirective?.resolvedFileName) continue;
1931-
const { resolvedFileName, originalPath } = resolution.resolvedTypeReferenceDirective;
1939+
if (!resolution.resolvedFileName) continue;
1940+
const { resolvedFileName, originalPath } = resolution;
19321941
if (!program.getSourceFile(resolvedFileName) && (!originalPath || !program.getSourceFile(originalPath))) {
19331942
rootNames = append(rootNames, resolvedFileName);
19341943
// Avoid creating a large project that would significantly slow down time to editor interactivity

tests/cases/fourslash/fourslash.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,20 @@ declare module ts {
111111
exportName: string;
112112
}
113113

114+
interface CompilerOptions {
115+
module?: string;
116+
target?: string;
117+
jsx?: string;
118+
allowJs?: boolean;
119+
maxNodeModulesJsDepth?: number;
120+
strictNullChecks?: boolean;
121+
sourceMap?: boolean;
122+
allowSyntheticDefaultImports?: boolean;
123+
allowNonTsExtensions?: boolean;
124+
resolveJsonModule?: boolean;
125+
[key: string]: string | number | boolean | undefined;
126+
}
127+
114128
function flatMap<T, U>(array: ReadonlyArray<T>, mapfn: (x: T, i: number) => U | ReadonlyArray<U> | undefined): U[];
115129
}
116130

@@ -200,8 +214,9 @@ declare namespace FourSlashInterface {
200214
symbolsInScope(range: Range): any[];
201215
setTypesRegistry(map: { [key: string]: void }): void;
202216
}
203-
class plugins {
217+
class config {
204218
configurePlugin(pluginName: string, configuration: any): void;
219+
setCompilerOptionsForInferredProjects(options: ts.CompilerOptions)
205220
}
206221
class goTo {
207222
marker(name?: string | Marker): void;
@@ -810,7 +825,7 @@ declare namespace FourSlashInterface {
810825
declare function ignoreInterpolations(diagnostic: string | ts.DiagnosticMessage): FourSlashInterface.DiagnosticIgnoredInterpolations;
811826
declare function verifyOperationIsCancelled(f: any): void;
812827
declare var test: FourSlashInterface.test_;
813-
declare var plugins: FourSlashInterface.plugins;
828+
declare var config: FourSlashInterface.config;
814829
declare var goTo: FourSlashInterface.goTo;
815830
declare var verify: FourSlashInterface.verify;
816831
declare var edit: FourSlashInterface.edit;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/// <reference path="../fourslash.ts" />
2+
3+
// @Filename: /packages/a/package.json
4+
//// {
5+
//// "name": "package-a",
6+
//// "dependencies": {
7+
//// "package-b": "*"
8+
//// }
9+
//// }
10+
11+
// @Filename: /packages/a/index.js
12+
//// packageB/**/
13+
14+
// @Filename: /packages/b/package.json
15+
//// { "name": "package-b", "main": "index.js" }
16+
17+
// @Filename: /packages/b/index.js
18+
//// export const packageB = "package-b";
19+
20+
// @link: /packages/b -> /packages/a/node_modules/package-b
21+
22+
config.setCompilerOptionsForInferredProjects({ module: "commonjs", allowJs: true, maxNodeModulesJsDepth: 2 });
23+
goTo.marker("");
24+
verify.completions({
25+
marker: "",
26+
includes: [{
27+
name: "packageB",
28+
source: "package-b",
29+
sourceDisplay: "package-b",
30+
hasAction: true,
31+
sortText: completion.SortText.AutoImportSuggestions,
32+
}],
33+
preferences: {
34+
includeCompletionsForModuleExports: true,
35+
allowIncompleteCompletions: true,
36+
}
37+
});

tests/cases/fourslash/server/configurePlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
// Test that plugin adds an error message which is able to be configured
1919
goTo.marker();
2020
verify.getSemanticDiagnostics([{ message: "configured error", code: 9999, range: { pos: 0, end: 3, fileName: "a.ts" } }]);
21-
plugins.configurePlugin("configurable-diagnostic-adder", { message: "new error" });
21+
config.configurePlugin("configurable-diagnostic-adder", { message: "new error" });
2222
verify.getSemanticDiagnostics([{ message: "new error", code: 9999, range: { pos: 0, end: 3, fileName: "a.ts" } }]);

0 commit comments

Comments
 (0)