QUESTION : resolve imports
See original GitHub issueHi,
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:
- Created 6 years ago
- Comments:7 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
No, you can leave closed, i manage to get things done. Thanks !
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:
ts.TransformerFactory
to do the transformation (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 ifVERSION
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 toVERSION
in the transformation factory (returnVERSION
instead of the past node similarly to thereturn 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:
Get the node where
VERSION
is definedFind all the references of it…
From there, you would want go get all the references that are found in import declarations:
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';
orimport * 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:Then with each variable declaration, you could just rename it to be
VERSION
:Then with each variable declaration, get the variable declaration’s statement and remove it:
Finally emit it:
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.