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.

QUESTION : resolve imports

See original GitHub issue

Hi,

First, let’ say 👍 about this library, very wonderful work !!

I am the maintainer of compodoc, a documentation tool for Angular applications. https://github.com/compodoc/compodoc

I need for some use-case to support dynamic imports of different things.

// module-a.ts
export const VERSION = 5;
// module-b.ts
import { VERSION } from './module-a';
let toto = VERSION;
console.log(toto);

Would be nice if during AST parsing with TypeScript, if i could edit the AST node of toto variable to inject VERSION instead of just having a reference.

Did you know how can i achieve this with your lib or just with TypeScript compiler APIs ? This blog http://blog.scottlogic.com/2017/05/02/typescript-compiler-api-revisited.html, chapter “TRANSFORMING THE AST” helps a little

Thanks

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
vogloblinskycommented, Nov 13, 2017

No, you can leave closed, i manage to get things done. Thanks !

1reaction
dsherretcommented, Sep 27, 2017

I’m glad you like it! There’s still a long way to go… lots of work to do.

Ah, that’s pretty cool. I’ve often thought that generated documentation is a very useful application for using the typescript compiler.

Note on remaining text… I did not re-read what I wrote here… just fired it off. Hopefully it helps as-is…

Using Transformation API

Are you trying to change this when emitting the javascript code? If so, I believe probably the best thing to do to accomplish what you want to do would be to use the transformation api.

Basically:

  1. Create a ts.TransformerFactory to do the transformation (Example)
  2. When emitting, specify to use the transformer factory that you created (Example)

I’m not entirely sure how you would create the TransformerFactory for this scenario. I guess in the TransformerFactory you would need to figure out if VERSION is imported and then keep track of every variable it’s assigned to and then every variable those variables are assigned to and keep repeating until there are no new variables. From there, replace all those variables with the reference to VERSION in the transformation factory (return VERSION instead of the past node similarly to the return ts.createLiteral(nameofString); statement in the example I posted above).

Using this Library (Not supported yet)

This is actually a hard problem and I haven’t included all the details. Probably the easiest thing to do would be to:

  1. Get the node where VERSION is defined

    const versionDeclaration = ast.getSourceFile("module-a.ts")!.getVariableDeclaration("VERSION")!;
    
  2. Find all the references of it…

    const versionReferencedSymbols = versionDeclaration.getNameIdentifier().findReferences();
    
  3. From there, you would want go get all the references that are found in import declarations:

    const importReferences: Identifier[] = [];
    for (const referencedSymbol of versionReferencedSymbols) {
        for (const reference of referencedSymbol.getReferences()) {
            const referenceNode = reference.getNode();
            const importDeclaration = referenceNode.getFirstAncestorByKind(ts.SyntaxKind.ImportDeclaration);
            if (importDeclaration != null)
                importReferences.push(referenceNode);
        }
    }
    
  4. Now… I was going to keep going here, but there’s a lot of scenarios you need to handle. For example, what if someone does import { VERSION as myVersion } from './module-a'; or import * as A from './module-a'; let toto = A.VERSION;. Or what if the imported, then aliased variable is then exported from the current file and used somewhere else? What do you do then? Definitely possible to handle in this library, but just something to think about. Skipping all that, and going to the easy scenario, from these import references you can get the variables:

    // with each import reference....
    const variableDeclarations: VariableDeclaration[] = [];
    for (const referencedSymbol of importReference.findReferences()) {
        for (const reference of referencedSymbol.getReferences()) {
            const parent = reference.getNode().getParentIfKind(ts.SyntaxKind.VariableDeclaration);
            // you would also probably also want a check to make sure VERSION
            // is the only thing assigned to the variable
            if (parent != null)
                variableDeclarations.push(parent);
        }
    }
    
  5. Then with each variable declaration, you could just rename it to be VERSION:

    variableDeclaration.getNameIdentifier().rename("VERSION");
    
  6. Then with each variable declaration, get the variable declaration’s statement and remove it:

    variableDeclaration.getFirstAncestorByKindOrThrow(ts.SyntaxKind.VariableStatement).remove();
    
  7. Finally emit it:

    ast.emit();
    

Anyway, sorry that’s not all supported. I’m working on it though…

Sidenote: I have written down the general idea of “reference paths” so it would basically give you a view of how something (like VERSION) flows through your application. I think it would make solving this problem a lot easier. It’s something to think about in the future though… I need to get the base of this library down first.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Question: How does pylint resolve imports? #3721 - GitHub
pkg.subpkg.example are two different modules sometimes when there are circular imports. It appears as if sometimes this code gets loaded twice, ...
Read more >
How to resolve import errors in python? - Stack Overflow
On windows, Python looks up modules from the Lib folder in the default python path, for example from "C:\Python34\Lib\".
Read more >
Python import: Advanced Techniques and Tips
The problem is that relative imports are resolved differently in scripts than are imported modules.
Read more >
Fix problems importing mail - Gmail Help - Google Support
Quick fixes to try first · On your computer, open Gmail. · In the top right, click Settings Settings and then See all...
Read more >
lightning web components - LWC compiler cannot resolve import
It appears that this problem is caused by the presence of multiple files with the same filename (in different subdirectories). Although I have ......
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