question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Doc Request]: Add example how to find reference of a named import in a source file

See original GitHub issue

I tried to look for different parts of the documentation but failed to find the instruction how to find references of Klass in a source file, via following example:

import { Klass } from 'module';

var klass = new Klass();

function config() {
  const klass1 = new Klass();
}

Is there a way to find all 2 references to Klass ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
dsherretcommented, Oct 14, 2018

There is now a const moduleSymbol = project.getAmbientModule("module-name") method (https://dsherret.github.io/ts-simple-ast/navigation/ambient-modules). From that symbol, the source file or source files can be found.

By the way, it should be possible to do the following:

// untested
const importDec = sourceFile.getImportDeclarationOrThrow("module-without-A-export");
const namedImport = importDec.getNamedImports().find(i => i.getName() === "A")!;
const nameNode = namedImport.getNameNode();

const nodes = nameNode.findReferencesAsNodes()
    .filter(n => n.getSourceFile() === sourceFile
        && !TypeGuards.isImportSpecifier(n.getParentOrThrow());
0reactions
bigoponcommented, Sep 25, 2018

@dsherret Another reason to have this is the error resilience, when you specify wrong import name in consumer source file, but all local reference are right. It should work just fine without having to go back to find the class in declaration source file

import { A } from 'module-without-A-export'

a = new A(); // only import { A } is wrong, a = new A is totally valid.
Read more comments on GitHub >

github_iconTop Results From Across the Web

5. The import system — Python 3.11.1 documentation
The import statement combines two operations; it searches for the named module, then it binds the results of that search to a name...
Read more >
import - JavaScript - MDN Web Docs - Mozilla
Syntax · defaultExport. Name that will refer to the default export from the module. · module-name. The module to import from. · name....
Read more >
How to Import References Into Mendeley
Look for "export" or "save" or "citation" in the database's options (the terminology varies by database). Select the "RIS Format" (sometimes ...
Read more >
Documentation - Module Resolution - TypeScript
First, the compiler will try to locate a file that represents the imported module. To do so the compiler follows one of two...
Read more >
Importing files from different folder - python - Stack Overflow
application folder), you are probably fine with sys.path.append('.') then importing the module by using from app2.some_folder.some_file import your_function .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found