Skip to content

Commit a3cd7d8

Browse files
author
Zhengbo Li
authored
Avoid returning type symbols for js configured projects (microsoft#10654)
* Exclude all things from .d.ts files for projects containing only .js and .d.ts files
1 parent 95378aa commit a3cd7d8

6 files changed

Lines changed: 61 additions & 15 deletions

File tree

src/harness/unittests/compileOnSave.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ namespace ts.projectSystem {
3737
// A compile on save affected file request using file1
3838
let moduleFile1FileListRequest: server.protocol.Request;
3939

40-
function createSession(host: server.ServerHost) {
41-
const typingsInstaller = new TestTypingsInstaller("/a/data/", host);
42-
return new server.Session(host, nullCancellationToken, /*useSingleInferredProject*/ false, typingsInstaller, Utils.byteLength, process.hrtime, nullLogger, /*canUseEvents*/ false);
43-
}
44-
4540
beforeEach(() => {
4641
moduleFile1 = {
4742
path: "/a/b/moduleFile1.ts",

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ namespace ts.projectSystem {
113113
fileOrFolderList);
114114
}
115115

116+
export function createSession(host: server.ServerHost, typingsInstaller?: server.ITypingsInstaller) {
117+
if (typingsInstaller === undefined) {
118+
typingsInstaller = new TestTypingsInstaller("/a/data/", host);
119+
}
120+
return new server.Session(host, nullCancellationToken, /*useSingleInferredProject*/ false, typingsInstaller, Utils.byteLength, process.hrtime, nullLogger, /*canUseEvents*/ false);
121+
}
122+
116123
export interface CreateProjectServiceParameters {
117124
cancellationToken?: HostCancellationToken;
118125
logger?: server.Logger;
@@ -1699,7 +1706,36 @@ namespace ts.projectSystem {
16991706
"File '/a/cache/node_modules/@types/lib/index.tsx' does not exist.",
17001707
"File '/a/cache/node_modules/@types/lib/index.d.ts' exist - use it as a name resolution result.",
17011708
]);
1702-
checkProjectActualFiles(proj, [ file1.path, lib.path ]);
1709+
checkProjectActualFiles(proj, [file1.path, lib.path]);
1710+
});
1711+
});
1712+
1713+
describe("navigate-to for javascript project", () => {
1714+
function containsNavToItem(items: server.protocol.NavtoItem[], itemName: string, itemKind: string) {
1715+
return find(items, item => item.name === itemName && item.kind === itemKind) !== undefined;
1716+
}
1717+
1718+
it("should not include type symbols", () => {
1719+
const file1: FileOrFolder = {
1720+
path: "/a/b/file1.js",
1721+
content: "function foo() {}"
1722+
};
1723+
const configFile: FileOrFolder = {
1724+
path: "/a/b/jsconfig.json",
1725+
content: "{}"
1726+
};
1727+
const host = createServerHost([file1, configFile, libFile]);
1728+
const session = createSession(host);
1729+
openFilesForSession([file1], session);
1730+
1731+
// Try to find some interface type defined in lib.d.ts
1732+
const libTypeNavToRequest = makeSessionRequest<server.protocol.NavtoRequestArgs>(server.CommandNames.Navto, { searchValue: "Document", file: file1.path, projectFileName: configFile.path });
1733+
const items: server.protocol.NavtoItem[] = session.executeCommand(libTypeNavToRequest).response;
1734+
assert.isFalse(containsNavToItem(items, "Document", "interface"), `Found lib.d.ts symbol in JavaScript project nav to request result.`);
1735+
1736+
const localFunctionNavToRequst = makeSessionRequest<server.protocol.NavtoRequestArgs>(server.CommandNames.Navto, { searchValue: "foo", file: file1.path, projectFileName: configFile.path });
1737+
const items2: server.protocol.NavtoItem[] = session.executeCommand(localFunctionNavToRequst).response;
1738+
assert.isTrue(containsNavToItem(items2, "foo", "function"), `Cannot find function symbol "foo".`);
17031739
});
17041740
});
17051741
}

src/server/project.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ namespace ts.server {
2020
}
2121
}
2222

23+
function isJsOrDtsFile(info: ScriptInfo) {
24+
return info.scriptKind === ScriptKind.JS || info.scriptKind == ScriptKind.JSX || fileExtensionIs(info.fileName, ".d.ts");
25+
}
26+
2327
export function allRootFilesAreJsOrDts(project: Project): boolean {
24-
return project.getRootScriptInfos().every(f => {
25-
return f.scriptKind === ScriptKind.JS || f.scriptKind == ScriptKind.JSX || fileExtensionIs(f.fileName, ".d.ts");
26-
});
28+
return project.getRootScriptInfos().every(isJsOrDtsFile);
29+
}
30+
31+
export function allFilesAreJsOrDts(project: Project): boolean {
32+
return project.getScriptInfos().every(isJsOrDtsFile);
2733
}
2834

2935
export interface ProjectFilesWithTSDiagnostics extends protocol.ProjectFiles {
@@ -63,6 +69,11 @@ namespace ts.server {
6369

6470
protected projectErrors: Diagnostic[];
6571

72+
public isJsOnlyProject() {
73+
this.updateGraph();
74+
return allFilesAreJsOrDts(this);
75+
}
76+
6677
constructor(
6778
readonly projectKind: ProjectKind,
6879
readonly projectService: ProjectService,
@@ -550,6 +561,7 @@ namespace ts.server {
550561
private projectFileWatcher: FileWatcher;
551562
private directoryWatcher: FileWatcher;
552563
private directoriesWatchedForWildcards: Map<FileWatcher>;
564+
553565
/** Used for configured projects which may have multiple open roots */
554566
openRefCount = 0;
555567

src/server/session.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ namespace ts.server {
10991099
return combineProjectOutput(
11001100
projects,
11011101
project => {
1102-
const navItems = project.getLanguageService().getNavigateToItems(args.searchValue, args.maxResultCount);
1102+
const navItems = project.getLanguageService().getNavigateToItems(args.searchValue, args.maxResultCount, /*excludeDts*/ project.isJsOnlyProject());
11031103
if (!navItems) {
11041104
return [];
11051105
}
@@ -1137,7 +1137,7 @@ namespace ts.server {
11371137
else {
11381138
return combineProjectOutput(
11391139
projects,
1140-
project => project.getLanguageService().getNavigateToItems(args.searchValue, args.maxResultCount),
1140+
project => project.getLanguageService().getNavigateToItems(args.searchValue, args.maxResultCount, /*excludeDts*/ project.isJsOnlyProject()),
11411141
/*comparer*/ undefined,
11421142
navigateToItemIsEqualTo);
11431143
}

src/services/navigateTo.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace ts.NavigateTo {
33
type RawNavigateToItem = { name: string; fileName: string; matchKind: PatternMatchKind; isCaseSensitive: boolean; declaration: Declaration };
44

5-
export function getNavigateToItems(program: Program, checker: TypeChecker, cancellationToken: CancellationToken, searchValue: string, maxResultCount: number): NavigateToItem[] {
5+
export function getNavigateToItems(program: Program, checker: TypeChecker, cancellationToken: CancellationToken, searchValue: string, maxResultCount: number, excludeDts: boolean): NavigateToItem[] {
66
const patternMatcher = createPatternMatcher(searchValue);
77
let rawItems: RawNavigateToItem[] = [];
88

@@ -43,6 +43,9 @@ namespace ts.NavigateTo {
4343

4444
const fileName = sourceFile.fileName;
4545
const matchKind = bestMatchKind(matches);
46+
if (excludeDts && fileExtensionIs(declaration.getSourceFile().fileName, ".d.ts")) {
47+
continue;
48+
}
4649
rawItems.push({ name, fileName, matchKind, isCaseSensitive: allMatchesAreCaseSensitive(matches), declaration });
4750
}
4851
}

src/services/services.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ namespace ts {
12401240
/** @deprecated */
12411241
getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[];
12421242

1243-
getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[];
1243+
getNavigateToItems(searchValue: string, maxResultCount?: number, excludeDts?: boolean): NavigateToItem[];
12441244
getNavigationBarItems(fileName: string): NavigationBarItem[];
12451245

12461246
getOutliningSpans(fileName: string): OutliningSpan[];
@@ -7066,10 +7066,10 @@ namespace ts {
70667066
}
70677067

70687068
/// NavigateTo
7069-
function getNavigateToItems(searchValue: string, maxResultCount?: number): NavigateToItem[] {
7069+
function getNavigateToItems(searchValue: string, maxResultCount?: number, excludeDts?: boolean): NavigateToItem[] {
70707070
synchronizeHostData();
70717071
const checker = getProgram().getTypeChecker();
7072-
return ts.NavigateTo.getNavigateToItems(program, checker, cancellationToken, searchValue, maxResultCount);
7072+
return ts.NavigateTo.getNavigateToItems(program, checker, cancellationToken, searchValue, maxResultCount, excludeDts);
70737073
}
70747074

70757075
function getEmitOutput(fileName: string, emitDeclarationsOnly?: boolean): EmitOutput {

0 commit comments

Comments
 (0)