Skip to content

Commit

Permalink
Wow a bunch of code was unnecessary
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Mar 14, 2022
1 parent 4e64659 commit 1cb2ba6
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 93 deletions.
92 changes: 0 additions & 92 deletions src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1255,32 +1255,6 @@ namespace ts.server {
pushIfUnique(definitions, jsDefinition, documentSpansEqual);
}
}
else {
const ambientCandidates = definitions.filter(d => d.isAliasTarget && d.isAmbient);
for (const candidate of ambientCandidates) {
const candidateFileName = getEffectiveFileNameOfDefinition(candidate, project.getLanguageService().getProgram()!);
if (candidateFileName) {
const fileNameToSearch = findImplementationFileFromDtsFileName(candidateFileName, file, auxiliaryProject);
const scriptInfo = fileNameToSearch ? auxiliaryProject.getScriptInfo(fileNameToSearch) : undefined;
if (!scriptInfo) {
continue;
}
if (!auxiliaryProject.containsScriptInfo(scriptInfo)) {
auxiliaryProject.addRoot(scriptInfo);
}
const auxiliaryProgram = auxiliaryProject.getLanguageService().getProgram()!;
const fileToSearch = Debug.checkDefined(auxiliaryProgram.getSourceFile(fileNameToSearch!));
const matches = FindAllReferences.Core.getTopMostDeclarationNamesInFile(candidate.name, fileToSearch);
for (const match of matches) {
const symbol = match.symbol || auxiliaryProgram.getTypeChecker().getSymbolAtLocation(match);
const decl = getDeclarationFromName(match);
if (symbol && decl) {
pushIfUnique(definitions, GoToDefinition.createDefinitionInfo(decl, auxiliaryProgram.getTypeChecker(), symbol, match));
}
}
}
}
}
});
}

Expand All @@ -1299,72 +1273,6 @@ namespace ts.server {
textSpan,
};

function getEffectiveFileNameOfDefinition(definition: DefinitionInfo, program: Program) {
const sourceFile = program.getSourceFile(definition.fileName)!;
const checker = program.getTypeChecker();
const symbol = checker.getSymbolAtLocation(getTouchingPropertyName(sourceFile, definition.textSpan.start));
if (symbol) {
let parent = symbol.parent;
while (parent && !isExternalModuleSymbol(parent)) {
parent = parent.parent;
}
if (parent?.declarations && some(parent.declarations, isExternalModuleAugmentation)) {
// Always CommonJS right now, but who knows in the future
const mode = getModeForUsageLocation(sourceFile, find(parent.declarations, isExternalModuleAugmentation)!.name as StringLiteral);
const fileName = sourceFile.resolvedModules?.get(stripQuotes(parent.name), mode)?.resolvedFileName;
if (fileName) {
return fileName;
}
}
const fileName = tryCast(parent?.valueDeclaration, isSourceFile)?.fileName;
if (fileName) {
return fileName;
}
}
}

function findImplementationFileFromDtsFileName(fileName: string, resolveFromFile: string, auxiliaryProject: Project) {
const nodeModulesPathParts = getNodeModulePathParts(fileName);
if (nodeModulesPathParts && fileName.lastIndexOf(nodeModulesPathPart) === nodeModulesPathParts.topLevelNodeModulesIndex) {
// Second check ensures the fileName only contains one `/node_modules/`. If there's more than one I give up.
const packageDirectory = fileName.substring(0, nodeModulesPathParts.packageRootIndex);
const packageJsonCache = project.getModuleResolutionCache()?.getPackageJsonInfoCache();
const compilerOptions = project.getCompilationSettings();
const packageJson = getPackageScopeForPath(project.toPath(packageDirectory + "/package.json"), packageJsonCache, project, compilerOptions);
if (!packageJson) return undefined;
// Use fake options instead of actual compiler options to avoid following export map if the project uses node12 or nodenext -
// Mapping from an export map entry across packages is out of scope for now. Returned entrypoints will only be what can be
// resolved from the package root under --moduleResolution node
const entrypoints = getEntrypointsFromPackageJsonInfo(
packageJson,
{ moduleResolution: ModuleResolutionKind.NodeJs },
project,
project.getModuleResolutionCache());
// This substring is correct only because we checked for a single `/node_modules/` at the top.
const packageNamePathPart = fileName.substring(
nodeModulesPathParts.topLevelPackageNameIndex + 1,
nodeModulesPathParts.packageRootIndex);
const packageName = getPackageNameFromTypesPackageName(unmangleScopedPackageName(packageNamePathPart));
const path = project.toPath(fileName);
if (entrypoints && some(entrypoints, e => project.toPath(e) === path)) {
// This file was the main entrypoint of a package. Try to resolve that same package name with
// the auxiliary project that only resolves to implementation files.
const [implementationResolution] = auxiliaryProject.resolveModuleNames([packageName], resolveFromFile);
return implementationResolution?.resolvedFileName;
}
else {
// It wasn't the main entrypoint but we are in node_modules. Try a subpath into the package.
const pathToFileInPackage = fileName.substring(nodeModulesPathParts.packageRootIndex + 1);
const specifier = `${packageName}/${removeFileExtension(pathToFileInPackage)}`;
const [implementationResolution] = auxiliaryProject.resolveModuleNames([specifier], resolveFromFile);
return implementationResolution?.resolvedFileName;
}
}
// We're not in node_modules, and we only get to this function if non-dts module resolution failed.
// I'm not sure what else I can do here that isn't already covered by that module resolution.
return undefined;
}

function tryRefineDefinition(definition: DefinitionInfo, program: Program, auxiliaryProgram: Program) {
const fileToSearch = auxiliaryProgram.getSourceFile(definition.fileName);
if (!fileToSearch) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@


// @Filename: /index.ts
//// import { /*start*/add } from 'lodash';
//// import { [|/*start*/add|] } from 'lodash';

verify.goToSourceDefinition("start", ["variable", "property"]);

0 comments on commit 1cb2ba6

Please sign in to comment.