-
Notifications
You must be signed in to change notification settings - Fork 380
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
refactor(macro): robust whitespace handling according to jsx spec #1882
refactor(macro): robust whitespace handling according to jsx spec #1882
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
packages/macro/src/macroJs.ts
Outdated
@@ -52,7 +52,7 @@ export type MacroJsOpts = { | |||
stripNonEssentialProps: boolean | |||
} | |||
|
|||
export default class MacroJs { | |||
export class MacroJs { |
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.
not related to whitespacing, simply eliminating default import/exports since they are harmful in cjs/esm interops
packages/macro/src/macroJsx.ts
Outdated
|
||
getJsxTagName = (node: JSXElement): string => { | ||
if (this.types.isJSXIdentifier(node.openingElement.name)) { | ||
return node.openingElement.name.name | ||
} | ||
} |
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 not used method
export default function cleanJSXElementLiteralChild(value: string) { | ||
const lines = value.split(/\r\n|\n|\r/) | ||
|
||
let lastNonEmptyLine = 0 |
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.
taken as-is from babel's repo
size-limit report 📦
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## next #1882 +/- ##
==========================================
+ Coverage 76.93% 77.17% +0.24%
==========================================
Files 83 84 +1
Lines 2150 2164 +14
Branches 556 560 +4
==========================================
+ Hits 1654 1670 +16
+ Misses 383 381 -2
Partials 113 113 ☔ View full report in Codecov by Sentry. |
This is a breaking change, users would need to re-extract and re-translate catalogs due to space changes. |
8f51900
to
4904cde
Compare
import { Trans as _Trans } from "@lingui/react" | ||
;<_Trans id={"<stripped>"} message={"Keep multiple\nforced\nnewlines!"} /> |
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 extracted this test cases in the test file cases because it was quite hard to understand is escaping working correctly or not when the code is inside js string
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 like this! But a few questions:
-
does this mean that babel macro would (further) deviate from SWC implementation?
-
Would it maybe make sense to make this configurable?
I know it's extra code and extra config, but then it wouldn't be a breaking change and we'd give users ability to choose this new whitespace handler and make it default with the next major version (and hopefully have swc version by then as well?).
Thank you!
packages/macro/test/jsx-trans.ts
Outdated
@@ -414,35 +414,37 @@ const cases: TestCase[] = [ | |||
`, | |||
}, | |||
{ | |||
name: "Keep forced newlines", | |||
name: "Strip whitespace around tags but keep whitespaces in JSX containers", |
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.
by "JSX container" you mean this? {"Wonderful framework "}
- just clarifying what that term means in this context
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.
Yes, this is JSXExpressionContainer according to AST types
I'm going to update SWC macro as well. And I would like to avoid making it configurable, the previous whitespace cleaning implementation is simply non-safe and lossy. Depending on the project style there might be little to 0 breaking changes, but for other styles there might be a bit bigger effort needed. BTW, I'm also going to reconsider whitespace cleaning in core macro. This white spacing changes would be planned for v5. There is also possible to write an automatic migration which will convert existing catalogs, but honestly i'm not really interested in that (lack of motivation and time for doing that) |
4904cde
to
89eb9c2
Compare
About migration, may be some would want to write it, here is the strategy:
|
@andrii-bodnar could you not a force push a |
89eb9c2
to
2f8d1db
Compare
@thekip my bad, forgot about this active PR for the |
SWC Plugin Port lingui/swc-plugin#83 |
Description
related discussion: #1873
Found much better way to process whitespacing.
Instead of merging all different types of JSX nodes (JSXText, JSXExpressionContainer -> StringLiteral, etc) into one big string and then trying to normalize whitespacing in that string i instead utilize nodes information to understand what whitespacing rules should be applied.
In other words:
Before:
After
So now the processing is reflecting how JSX processes whitespaces
Types of changes
Fixes # (issue)
Checklist