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
OpenBraceToken was not found #665
Comments
Example of react component suffering from the same problem:
|
Does it work when the file has a .tsx extension? Could you also follow the issue template and post the error message? Thanks, will look into this later. |
In my case, the file has a tsx extension, yes. The error message I get is:
|
By the way, change this: import { Project } from 'ts-morph';
import * as ts from 'typescript'; To this to ensure the same ts version is used that ts-morph uses: import { Project, ts } from 'ts-morph'; @martaver thanks! Will look into it. Looks like single statement arrow functions are not handled properly in the comment parser. |
Thanks for your fast reply! |
@dsherret thanks!! that does seem to work, however, I can't seem to figure out how to name those |
It shouldn't mention anything if the file doesn't exist. Is it really saying it doesn't exist? Please open an issue if so. By default, the Project will work with the actual file system and so if you create a file at a location that exists then it will throw (that message should direct you to provide an The TypeScript compiler api requires a source file name for every file and will use that information to figure out how to parse the file (ex. if it is a |
I'm only able to reproduce these scenarios in non-jsx/tsx files. For some reason when doing this, the compiler will parse block nodes with zero length and this causes the issue in ts-morph (since it expects a block to have an open brace token): Probably a bug in the compiler, but this is too obscure to bother reporting. Anyway, I've fixed this and will do a release later, but recommend ensuring that the source files are created as |
And published in 3.1.3. Please let me know if there are any other issues! |
@dsherret thanks for that.
I added this option, however, it didn't solve anything. Passing an absolute path to a file that exists in the real fs, throws an error The only thing that works is Why wouldn't it throw an error on UPD.
My module can be anything - js, jsx, ts, tsx But I have to use |
@nchanged yeah, if you use a virtual file system (in memory file system) then it will be in memory and it will not access anything on the actual file system. That doesn't seem like what you want for that scenario.
Could you be more specific with the question? Perhaps open a new issue so it's not so off-topic with this one. If I'm understanding this correctly, if the file system looks like this:
...and someone does... project.createSourceFile("/MyClass.tsx", "class MyClass {}");
project.createSourceFile("/MyClass2.tsx", "class MyClass {}"); ... you are wondering why it throws the error "A source file already exists at the provided file path: /MyClass.tsx" only for the first statement? It's because the file already exists at /MyClass.tsx, but not at /MyClass2.tsx. Why would you expect it to throw on the second statement? If someone is creating a file and it doesn't exist then there's no reason to throw an error. If you don't want this error message for the first statement, then you have to be explicit and provide the overwrite option: project.createSourceFile("/MyClass.tsx", "class MyClass {}", { overwrite: true }); I've opened #666 to include details about the overwrite option in the error message. I thought it was already doing that, but I must have missed it for this scenario.
You can make the filename anything you like. You don't need to make it MyClass.tsx on all files. The compiler api expects a source file name though: const sourceFile = ts.createSourceFile(fileName, fileText); Also, you can't make it // assertion, but parses as JsxElement
const t = <string>myNumber; That file would need a
There are more scenarios that work than the text MyClass.tsx and you are depending on the file system in order to load the files. You are making changes to the project in memory though and it needs to know the location of the file in order to resolve modules. For example, if your MyClass.tsx looks like this: import { OtherClass } from "./OtherClass.tsx"; How would it know how to resolve the module specifier Anyway, it's for resolving modules that a source file needs a file path and even if it has no modules it still needs to figure out how to parse the file based on the file extension (caveat: there is also a |
@dsherret the problem is that if I give it a real absolute path that exists, it throws this error
That's my main issue ATM. /Users/.../node_modules/react/index.js exists 100%.
I handle all references manually (I have my own dependency tree) So It's all resolved. |
@nchanged that is a different issue not related to Could you open an issue with some reproduction steps? Most likely it's not resolving react properly and the compiler can't find the file in the ts.Program. Check if there are any diagnostics in |
Hi
There is a problem with this snippet
Basically, removing
<div></div>
helps, but otherwise it breaks on this JSX file. And theJSX
is valid. Also removing an argument fromgetSourceStyle()
works just fine.The text was updated successfully, but these errors were encountered: