Closed taymonbeal closed 3 years ago
@taymonbeal thanks for the repro instructions!
So I need to apologize in advance that it's going to take awhile to fix this, I'm having growing pains with this project and it's been hard to clear some hurdles.
I've got WIP on a big refactor that allows using Babel toolchain (instead of jscodeshift
/recast
) to do the transforms, and I won't be able to completely fix this issue until I get that released (recast
output is too buggy, and I'm getting tired of it. Babel code generation will be much more reliable).
And before that, I need to release a major branch I've been working on with a bunch of improvements. I partially fixed this issue on that branch, but as I mentioned, the recast
output is buggy for TS, and fails to preserve the type parameters in your replacement:
find
with parser: babylon
✓ bugs_unexpectedObjectArray
with parser: flow
✓ bugs_unexpectedObjectArray (45ms)
with parser: tsx
1) bugs_unexpectedObjectArray
2 passing (89ms)
1 failing
1) find
with parser: tsx
bugs_unexpectedObjectArray:
AssertionError: expected 'const fakeServerHandler = jasmine.createSpy("fakeServerHandler");\n\nfakeServerHandler\n .withArgs(\n jasmine.objectContaining({\n url: new URL(biddingLogicUrl1),\n })\n )\n .and.resolveTo({\n headers: javaScriptHeaders,\n\n body: [\n "function generateBid() {",\n " return { bid: 0.03, render: \'about:blank#1\' };",\n "}",\n ].join("\\n"),\n });\n' to deeply equal 'const fakeServerHandler = jasmine.createSpy<FakeServerHandler>(\n "fakeServerHandler"\n);\n\nfakeServerHandler\n .withArgs(\n jasmine.objectContaining<FakeRequest>({\n url: new URL(biddingLogicUrl1),\n })\n )\n .and.resolveTo({\n headers: javaScriptHeaders,\n\n body: [\n "function generateBid() {",\n " return { bid: 0.03, render: \'about:blank#1\' };",\n "}",\n ].join("\\n"),\n });\n'
+ expected - actual
-const fakeServerHandler = jasmine.createSpy("fakeServerHandler");
+const fakeServerHandler = jasmine.createSpy<FakeServerHandler>(
+ "fakeServerHandler"
+);
fakeServerHandler
.withArgs(
- jasmine.objectContaining({
+ jasmine.objectContaining<FakeRequest>({
url: new URL(biddingLogicUrl1),
})
)
.and.resolveTo({
at Context.<anonymous> (test/findReplace/jscodeshift.test.ts:118:46)
at processImmediate (internal/timers.js:456:21)
I've kind of given up on recast
. I've made PRs to fix issues like this and they've languished. I'll double check if I've made a PR to fix TSTypeParameter output, but no guarantees recast
will merge it anytime soon 😕
So please bear with me until I get the Babel toolchain support released.
Some heads up about that:
$$args
instead of $_args
)recast
, the Babel toolchain won't preserve formatting (unless you have a prettier config in your project, then it runs prettier before showing you the diff)@taymonbeal okay I was able to figure out a solution and released a beta for version 2. Can you try installing astx@beta
? and use the --babel-generator
option?
Unfortunately this may not preserve formatting (esp if you aren't using prettier
in your project) but it's the only thing that works for your example at the moment. Without it the <FakeServerHandler>
etc disappears.
Also be sure to check the breaking changes for version 2
Oh actually, looks like it works in the beta even without the --babel-generator
option! But definitely give that option a try if you run into trouble.
@taymonbeal did you ever get a chance to try out my fixes?
gonna close this since it should be fixed on beta, but let me know if you have any more issues
Repro instructions:
Full output: