Skip to content
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

API about Identifier's scope #28026

Open
xiaoxiangmoe opened this issue Oct 21, 2018 · 2 comments
Open

API about Identifier's scope #28026

xiaoxiangmoe opened this issue Oct 21, 2018 · 2 comments
Labels
API Relates to the public API for TypeScript Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@xiaoxiangmoe
Copy link
Contributor

xiaoxiangmoe commented Oct 21, 2018

I want to write a CustomTransformers for my webpack loader config.

Babel's visitors have API about scope
If I get a path of ImportDeclaration import * as React from "react", I can using path.scope.getBinding('React').referencePaths to get all reference paths using React.

Is there any API in TypeScript to get identifier's scope or referenced nodes/identifiers?

Also, Is there any API in TypeScript to get ImportDeclaration's moduleSpecifier's resolved source file? for example import * as React from "react"'s resolved source file is /home/me/my-ts-project/node_modules/react/cjs/react.development.js

@ajafff
Copy link
Contributor

ajafff commented Oct 21, 2018

I have written a utility function to get all references to a given declaration.

import {collectVariableUsage} from 'tsutils';

/** 
  *`identifier` needs to be the name of the declaration and be located within `sourceFile`.
  * Note that this only works on one source file at a time and can therefore not detect references to global variables in other files.
  */
function getUses(sourceFile: ts.SourceFile, identifier: ts.Identifier) {
  // you might want to cache the result of `collectVariableUsage`
  return collectVariableUsage(sourceFile).get(identifier)!.uses;
}

Currently you can only look up the uses of a declaration. A reverse lookup is not possible. It also doesn't tell if an identifier is in scope at a given location.
Tough I'm planning on improving the API to make it more powerful and easier to use: ajafff/tsutils#38

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Mar 22, 2024

resolveName is now an API on TypeChecker:

resolveName(name: string, location: Node | undefined, meaning: SymbolFlags, excludeGlobals: boolean): Symbol | undefined;

Program has resolveModuleNames:

resolveModuleNames?(moduleNames: string[], containingFile: string, reusedNames: string[] | undefined, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions, containingSourceFile?: SourceFile): (ResolvedModule | undefined)[];

I think these are close enough to what you might be asking for - but not 100% certain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Relates to the public API for TypeScript Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants