-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expose Typescript -> ESTree AST conversion logic #350
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
Comments
related to #330 |
I might be misunderstanding, but can't you use |
I tried doing that, but for some reason it's significantly slower than doing it myself when I'm running over many files (maybe because it's not properly reusing the typechecking machinery, not sure). |
I don't know your code, but I would say that your code is faster because you're not setting up the typescript Program. You cannot generate the TS AST -> ESTree AST map without having typescript generate the Program. This is slow because in order for typescript to generate the AST, it has to parse the entire project (because it needs all of the type information to generate the correct information). It's a cost you should only have to pay once per tsconifg.json supplied. |
I am setting up the Program in my code. It's entirely possible that I was just calling typescript-estree wrong in a way that wasn't allowing it to reuse the tsconfig.json properly. |
See #243 for an analysis of the performance of typescript-estree. The program generation performance hit should be proportional to the size of your codebase, not the number of files being parsed. We're still looking for a solution to this. Correct usage of |
Here's how I'm calling for (const file of files) {
const { ast, services } = tsest.parseAndGenerateServices(fs.readFileSync(file, "utf8"), {
project: tsconfigJsonPath,
extraFileExtensions: ["js"],
tsconfigRootDir: inputDir
});
// more stuff
} Note that the tsconfig.json is outside of the project's working directory, since I'm using this to analyze Javascript, so I'm generating a synthetic tsconfig.json file. But the project file and tsconfigRootDir are the same for each file. My faster code (which I don't know if I can post here) does create a program, and is able to successfully invoke |
// should be absolute if possible
const project = path.resolve(__dirname, "../../somepath/to/tsconfig.json");
const tsconfigRootDir = path.dirname(project);
for (const file of files) {
const { ast, services } = tsest.parseAndGenerateServices(fs.readFileSync(file, "utf8"), {
extraFileExtensions: ["js"],
project,
tsconfigRootDir,
// should also pass in the file path
filePath: file,
});
// more stuff
} |
Aha, passing in the file path seems to make it fast as what I was doing manually. |
I no longer need the original thing I was asking for; thank you for the help! Feel free to close it if you want. |
Re-commenting on this because I wound up wanting it again, for... some very silly reasons. I'm doing horrible things to Typescript that go beyond the original use case of 'use it as a linter' (I'm modifying the host to apply some AST transformations before parsing/typechecking), and in general the only things I actually need are convert.ts and the TSESTree type definition. I don't think I can mess around with the created host using the provided API, and I think even if I did, it's likely I'd run into some other thing that I'd need. |
I was under the impression that typescript will give you the AST after it's done its typecheck cycle? |
You can shim the compiler host's But you're right, normally you can't get the AST without typechecking. |
It sounds like you're doing some super weird things. |
Okay, I ran into this issue again. Here's what I'm currently trying to do (which is somewhat different from what I was trying to do earlier): I have an AST explorer that annotates the AST with type information. It runs in the browser, so I can't use the existing typescript-estree infrastructure since that doesn't let me override the compiler host. So right now I'm constructing a host that just emulates a simple filesystem, then constructing a |
Feel free to put up a PR if you want to expose it! |
I'm working on project that wants to parse some code as TypeScript, get the corresponding ESTree AST, and then do some logic that requires both the TypeScript API objects and the converted ESTree output. Right now I'm directly importing
@typescript-eslint/typescript-estree/dist/convert.js
, which makes me feel gross inside. Would it be reasonable to either expose that module in thetypescript-estree
package or split it off into its own package?The text was updated successfully, but these errors were encountered: