Skip to content
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

babel parser cannot preserve formatting for parts in some case but Esprima does #1296

Open
zoeyzhao19 opened this issue May 17, 2023 · 0 comments

Comments

@zoeyzhao19
Copy link

zoeyzhao19 commented May 17, 2023

codesandbox

I am doing the same test between babel and esprima , but it confused me that a semicolon is append to a import statement with babel parser after I do some modification and print, but esprima does not
image

Expected result (as esprima does):

import { defineConfig } from "foo"
    export default defineConfig({})

I found the doc said

Whenever Recast cannot reprint a modified node using the original source code, it falls back to using a generic pretty printer. So the worst that can happen is that your changes trigger some harmless reformatting of your code.

But also

When you call recast.parse, it makes a shadow copy of the AST before returning it to you, giving every copied AST node a reference back to the original through a special .original property. This information is what enables recast.print to detect where the AST has been modified, so that it can preserve formatting for parts of the AST that were not modified

So in my side, I might think different parser should not have caused different formatting through recast.parse

This is also related to a magicast issue

Is there any better explaination or it's expected?
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant