-
Notifications
You must be signed in to change notification settings - Fork 71
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
base: master
Are you sure you want to change the base?
Conversation
inputSourceMap: sourceMap, | ||
retainLines: true, | ||
preserveFormat: true, | ||
compact: true, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
f751a41
to
1fef37d
Compare
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 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. |
2c8df48
to
ff89345
Compare
ff89345
to
e8ebd67
Compare
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
e8ebd67
to
31db01e
Compare
31db01e
to
e04e72d
Compare
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