-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(macro): robust whitespace handling according to jsx spec (#1882
- Loading branch information
1 parent
30b5940
commit 2071e0f
Showing
12 changed files
with
112 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/babel-plugin-lingui-macro/src/utils/cleanJSXElementLiteralChild.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// taken from babel repo -> packages/babel-types/src/utils/react/cleanJSXElementLiteralChild.ts | ||
export default function cleanJSXElementLiteralChild(value: string) { | ||
const lines = value.split(/\r\n|\n|\r/) | ||
|
||
let lastNonEmptyLine = 0 | ||
|
||
for (let i = 0; i < lines.length; i++) { | ||
if (lines[i].match(/[^ \t]/)) { | ||
lastNonEmptyLine = i | ||
} | ||
} | ||
|
||
let str = "" | ||
|
||
for (let i = 0; i < lines.length; i++) { | ||
const line = lines[i] | ||
|
||
const isFirstLine = i === 0 | ||
const isLastLine = i === lines.length - 1 | ||
const isLastNonEmptyLine = i === lastNonEmptyLine | ||
|
||
// replace rendered whitespace tabs with spaces | ||
let trimmedLine = line.replace(/\t/g, " ") | ||
|
||
// trim whitespace touching a newline | ||
if (!isFirstLine) { | ||
trimmedLine = trimmedLine.replace(/^[ ]+/, "") | ||
} | ||
|
||
// trim whitespace touching an endline | ||
if (!isLastLine) { | ||
trimmedLine = trimmedLine.replace(/[ ]+$/, "") | ||
} | ||
|
||
if (trimmedLine) { | ||
if (!isLastNonEmptyLine) { | ||
trimmedLine += " " | ||
} | ||
|
||
str += trimmedLine | ||
} | ||
} | ||
|
||
return str | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/babel-plugin-lingui-macro/test/fixtures/jsx-keep-forced-newlines.expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { Trans as _Trans } from "@lingui/react" | ||
;<_Trans id={"<stripped>"} message={"Keep multiple\nforced\nnewlines!"} /> |
6 changes: 6 additions & 0 deletions
6
packages/babel-plugin-lingui-macro/test/fixtures/jsx-keep-forced-newlines.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Trans } from "@lingui/react/macro" | ||
;<Trans> | ||
Keep multiple{"\n"} | ||
forced{"\n"} | ||
newlines! | ||
</Trans> |
Oops, something went wrong.