-
Notifications
You must be signed in to change notification settings - Fork 393
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(cli): Extract - Flatten ICU messages #1431
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
size-limit report 📦
|
Codecov ReportBase: 69.24% // Head: 70.01% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## next #1431 +/- ##
==========================================
+ Coverage 69.24% 70.01% +0.77%
==========================================
Files 71 73 +2
Lines 2019 2081 +62
Branches 542 561 +19
==========================================
+ Hits 1398 1457 +59
- Misses 499 501 +2
- Partials 122 123 +1
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
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.
How this supposed to work in general? When the message would be flattened, it would be a different string. And it would not match to id string from runtime.
// Redirect imports to the compiled bundles | ||
moduleNameMapper: {}, |
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.
Without this, integration test become absolutely the same to the "regular" tests. The idea was to check that code works from ./build instead of ./src
What was wrong with that?
@@ -20,6 +20,7 @@ import extract, { ExtractedMessage } from "./extractors" | |||
import { CliExtractOptions } from "../lingui-extract" | |||
import { CliExtractTemplateOptions } from "../lingui-extract-template" | |||
import { CompiledCatalogNamespace } from "./compile" | |||
import { flattenMessage } from "@lingui/core/flatten" |
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.
Why it's in @lingui/core
? Is it supposed to be used on runtime?
function cloneDeep<T>(obj: T): T { | ||
if (Array.isArray(obj)) { | ||
// @ts-expect-error | ||
return [...obj.map(cloneDeep)] | ||
} | ||
if (obj !== null && typeof obj === "object") { | ||
// @ts-expect-error | ||
return Object.keys(obj).reduce((cloned, k) => { | ||
cloned[k] = cloneDeep(obj[k]) | ||
return cloned | ||
}, {}) | ||
} | ||
return obj |
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.
Please, avoid @ts-expect-error
For clone deep you can use simpler JSON.parse(JSON.stringify())
but, better is avoiding mutating data in flattenTokens
for (let i = 0; i < tokens.length; i++) { | ||
const token = tokens[i] | ||
if (isPluralOrSelect(token)) { | ||
const cloned = cloneDeep(token) | ||
const { cases } = cloned | ||
cloned.cases = cases.reduce((all, k, index) => { | ||
const newValue = flattenTokens([ | ||
...tokens.slice(0, i), | ||
...cases[index].tokens, | ||
...tokens.slice(i + 1), | ||
]) | ||
all[index] = { ...k, tokens: newValue } | ||
return all | ||
}, []) | ||
|
||
return [cloned] | ||
} | ||
} | ||
return tokens |
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.
Why not a usual forEach()
?
tokens.forEach((token, index) => { /* … */ })
const cloned = cloneDeep(token) | ||
const { cases } = cloned | ||
cloned.cases = cases.reduce((all, k, index) => { | ||
const newValue = flattenTokens([ | ||
...tokens.slice(0, i), | ||
...cases[index].tokens, | ||
...tokens.slice(i + 1), | ||
]) | ||
all[index] = { ...k, tokens: newValue } | ||
return all | ||
}, []) | ||
|
||
return [cloned] |
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.
Without reduce
, mutating data and cloneDeep
:
return [{
...token,
cases: token.cases.map((k, index) => {
const newValue = flattenTokens([
...tokens.slice(0, i),
...cases[index].tokens,
...tokens.slice(i + 1)
])
return {...k, tokens: newValue}
})
}]
I see here a couple ways how this could be done:
|
@thekip I have been thinking about this and I believe that the second option would be the best way to go. |
Yes, this is exactly how i'm see implementation of hash-based ids with macro. Two cases:
|
1b9469e
to
4aa80de
Compare
Description
Fixes #1186
Based on suggestion from #1186, this PR adds flattening of ICU messages for the
extract
andextract-template
CLI commands.Types of changes
Checklist