fix: simplify directive scanner + handle JSX quotes#240
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #240 +/- ##
==========================================
- Coverage 94.95% 94.55% -0.40%
==========================================
Files 10 9 -1
Lines 2597 2371 -226
==========================================
- Hits 2466 2242 -224
+ Misses 131 129 -2
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
Need to test how much this regex is making a transform slower. I'm using benchmark from this branch lingui/js-lingui#2560 And manually adding an additional scenario into bench.add("SWC - customized", async () => {
await Promise.all(
sourceFiles.map(async ({ filename, code }) => {
const isTsx = filename.endsWith(".tsx")
await swc.transform(code, {
filename,
jsc: {
target: "esnext",
transform: {
react: {
runtime: "preserve",
},
},
parser: isTsx
? { syntax: "typescript", tsx: true }
: { syntax: "typescript", tsx: false },
experimental: {
plugins: [
[
"/Users/timofei.Iatsenko/Projects/swc-plugin/target/wasm32-wasip1/release/lingui_macro_plugin.wasm",
{ descriptorFields: "all" } as Record<string, unknown>,
],
],
},
},
})
}),
)
})Could you test this version in comparison to what is latest published in npm? (the branch may have not the latest) |
|
I asked Claude to reproduce the The regex introduces a likely correctness regression and a ~12% transform slowdown. Claude suggests to keep the custom scanner implementation:
SetupThree plugin
Building 🔴 Correctness regression (surfaced by the benchmark fixtures)The branch fails to compile block-comment directives inside a JSX expression container —
🟡 Performance — 1000 files × 10 msgs, median of 30 interleaved rounds
The cost is per-file fixed overhead, not proportional to source size: re-running with 100 files × 100 msgs (same total content, 1/10th the files) drops the regex penalty to +4.5%. Two per-file contributors:
Real projects resemble the "medium" preset (many small/medium files), so ~12% is the representative figure; it only shrinks for unusually few, huge files. |
|
What about doing this without regex, by just using a manual scanner?
Should be much simplier and without using regexp. The current direction seems very complex as we tried to literally rewrite js parser by ourselves. |
|
closed in favor of #242 |
Alternative solution to #238 which sacrifices correctness for simplicity by using a regex scanning approach. The caveat is that
lingui-setandlingui-resetdirectives within strings will be seen as valid directives and thus be applied. @timofei-iatsenko considered this to be an acceptable compromise (comment).