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

feat: Preserve formatting #2444

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

kriskowal
Copy link
Member

Closes: #926

Description

This change leverages new Babel support for format preservation.

Security Considerations

None

Scaling Considerations

None

Documentation Considerations

None

Testing Considerations

This change includes a test to perform a narrow spot-check of the verbatim output of a module that should be largely preserved.

Compatibility Considerations

None

Upgrade Considerations

None

  • NEWS.md

inputSourceMap: sourceMap,
retainLines: true,
preserveFormat: true,
compact: true,
Copy link
Member Author

@kriskowal kriskowal Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolo-ribaudo Reviewing https://github.com/babel/babel/pull/16708/files#diff-ca2bda59eec9c35846e26c1c6247759c92c26357b80d4dc881fcdaf12df1d7e6R35-R39, it seems like compact: true here should be causing your code to throw, so I suspect this isn’t exercising the new path in my local testing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found it. Needed to swap out our fork @agoric/babel-generator back to @babel/generator.

@kriskowal kriskowal force-pushed the kriskowal-babel-preserve-format-integration branch from f751a41 to 1fef37d Compare September 3, 2024 23:57
@kriskowal
Copy link
Member Author

@nicolo-ribaudo,

I’ll have a push soon, but this looks correct, actually:

Pre-transform:

function TokenType() {}
const beforeExpr = 0;

export function createBinop(name, binop) {
  return new TokenType(name, {
    beforeExpr,
    binop,
  });
}

Post-transform:

function TokenType() {}
const beforeExpr = 0;

       function createBinop(name, binop) {
  return new TokenType(name, {
    beforeExpr,
    binop,
  });
}

I’ll note that there’s a weird but explicable offset on the function declaration because we have erased export!

This was the previous (undesirable) effect:

function TokenType() { }
const beforeExpr=  0;

function        createBinop(name, binop) {
  return new TokenType(name, {
    beforeExpr,
    binop});

 }
})()

And the test I’ve proposed is not yet passing because the generator doesn’t add a newline at the end of the file. That presumably means that it needs to catch up with the final line and column of the file to preserve trailing whitespace, or just add a single newline to the end to make sure the output is a valid UNIX text file.

@kriskowal kriskowal force-pushed the kriskowal-babel-preserve-format-integration branch 2 times, most recently from 2c8df48 to ff89345 Compare September 4, 2024 00:03
@kriskowal kriskowal force-pushed the kriskowal-babel-preserve-format-integration branch from ff89345 to e8ebd67 Compare September 4, 2024 00:06
@kriskowal
Copy link
Member Author

I’m mistaken above about the newlines issue. I’m updating the test fixture and it looks like the test will pass as-is, no changes needed from Babel.

@@ -0,0 +1,10 @@
({ imports: $h͏_imports, liveVar: $h͏_live, onceVar: $h͏_once, importMeta: $h͏____meta, }) => (function () { 'use strict'; $h͏_imports([]);Object.defineProperty(createBinop, 'name', {value: "createBinop"});$h͏_once.createBinop(createBinop); // deliberately offset

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually surprised by the spaces here. My expectation was that this would have been printed as compact as possible to increase the chances that the next node could be printed in the right place.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see. This code with spaces is generated by you with string concatenation after that Babel runs, and not by Babel itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. I also find the spacing arbitrary, emerging from some unnecessary space in the formatting of the code. I’ve pushed a commit to make that compact as expected, regardless of if not being generated by Babel.

@@ -0,0 +1,10 @@
({ imports: $h͏_imports, liveVar: $h͏_live, onceVar: $h͏_once, importMeta: $h͏____meta, }) => (function () { 'use strict'; $h͏_imports([]);Object.defineProperty(createBinop, 'name', {value: "createBinop"});$h͏_once.createBinop(createBinop); // deliberately offset
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the closing paren for the function expression here?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmm it should be at then end of the file right? It looks like I forgot to flush some tokens somewhere 🤔

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, cloning the PR locally I get this additional line at the end of the file:

})()

Probably @kriskowal accidentally deleted it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed the fix for this. It was present locally when I got the test to pass.

@kriskowal kriskowal force-pushed the kriskowal-babel-preserve-format-integration branch from e8ebd67 to 31db01e Compare September 5, 2024 00:10
@kriskowal kriskowal force-pushed the kriskowal-babel-preserve-format-integration branch from 31db01e to e04e72d Compare September 5, 2024 00:25
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

Successfully merging this pull request may close these issues.

Lots of whitespace differences introduced by endo bundler
3 participants