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

refactor(macro): robust whitespace handling according to jsx spec #1882

Merged
merged 1 commit into from
Mar 26, 2024

Conversation

timofei-iatsenko
Copy link
Collaborator

@timofei-iatsenko timofei-iatsenko commented Mar 8, 2024

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:

<Trans>Hello◦{"◦"}◦world</Trans> 
// -> normalizeWhitespace("Hello◦◦◦world")  we lost here information that middle space came from explicit `{"◦"}` 

After

<Trans>Hello◦{"◦"}◦world</Trans>

// there are strict rules how whitespaces processed in JSXText, 
// use `cleanJSXElementLiteralChild` function which follows this rules (taken from babel's sources)
cleanJSXElementLiteralChild("processHello◦") // JSXText
{"◦"} // JSXExpressionContainer - arbitary content, left as is
cleanJSXElementLiteralChild("◦world") // JSXText

So now the processing is reflecting how JSX processes whitespaces

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Examples update

Fixes # (issue)

Checklist

  • I have read the CONTRIBUTING and CODE_OF_CONDUCT docs
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation (if appropriate)

Copy link

vercel bot commented Mar 8, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
js-lingui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 19, 2024 9:43am

@@ -52,7 +52,7 @@ export type MacroJsOpts = {
stripNonEssentialProps: boolean
}

export default class MacroJs {
export class MacroJs {
Copy link
Collaborator Author

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

Comment on lines 532 to 537

getJsxTagName = (node: JSXElement): string => {
if (this.types.isJSXIdentifier(node.openingElement.name)) {
return node.openingElement.name.name
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

found not used method

Comment on lines 2 to 5
export default function cleanJSXElementLiteralChild(value: string) {
const lines = value.split(/\r\n|\n|\r/)

let lastNonEmptyLine = 0
Copy link
Collaborator Author

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

Copy link

github-actions bot commented Mar 8, 2024

size-limit report 📦

Path Size
./packages/core/dist/index.mjs 2.86 KB (0%)
./packages/detect-locale/dist/index.mjs 723 B (0%)
./packages/react/dist/index.mjs 1.67 KB (0%)
./packages/remote-loader/dist/index.mjs 7.26 KB (0%)

Copy link

codecov bot commented Mar 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.17%. Comparing base (30b5940) to head (2f8d1db).

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.
📢 Have feedback on the report? Share it here.

@timofei-iatsenko
Copy link
Collaborator Author

timofei-iatsenko commented Mar 8, 2024

This is a breaking change, users would need to re-extract and re-translate catalogs due to space changes.

Comment on lines 1 to 2
import { Trans as _Trans } from "@lingui/react"
;<_Trans id={"<stripped>"} message={"Keep multiple\nforced\nnewlines!"} />
Copy link
Collaborator Author

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

Copy link
Collaborator

@vonovak vonovak left a 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!

@@ -414,35 +414,37 @@ const cases: TestCase[] = [
`,
},
{
name: "Keep forced newlines",
name: "Strip whitespace around tags but keep whitespaces in JSX containers",
Copy link
Collaborator

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

Copy link
Collaborator Author

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

@timofei-iatsenko
Copy link
Collaborator Author

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)

@timofei-iatsenko
Copy link
Collaborator Author

timofei-iatsenko commented Mar 18, 2024

About migration, may be some would want to write it, here is the strategy:

  1. Extract messages with old-style whitespace management (fix the specific version of lingui/macro)
  2. Give an uniq id for every message based on filename / line / column (there are such information on the level of catalog)
  3. Extract messages with new version, assign ids, copy translations for ids.
  4. write updated catalogs.

@timofei-iatsenko
Copy link
Collaborator Author

timofei-iatsenko commented Mar 19, 2024

@andrii-bodnar could you not a force push a next branch while there are opened PR's. Now it's extremely hard to fix this branch.

@andrii-bodnar
Copy link
Contributor

@thekip my bad, forgot about this active PR for the next branch 😬

@timofei-iatsenko
Copy link
Collaborator Author

SWC Plugin Port lingui/swc-plugin#83

@andrii-bodnar andrii-bodnar merged commit 2071e0f into lingui:next Mar 26, 2024
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants