Skip to content

Commit 193dd83

Browse files
committed
Fixed bug in extraction if string is followed by comma
1 parent b0e519b commit 193dd83

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/extract/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function parseFunctionCall(mapping: KeywordMapping, tokens: Token[]): Msg
4444

4545
// parse strings arguments
4646
while (true) {
47-
if (t.kind !== TokenKind.String) {
47+
if (!t || t.kind !== TokenKind.String) {
4848
break;
4949
}
5050
assertIsDefined(t.value);

tests/extract.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { mkdir, mkdtemp, readFile, rm, symlink, writeFile } from "fs/promises";
2+
import { join } from "path";
3+
import { tmpdir } from "os";
4+
import { cwd } from "process";
5+
import { execSync } from "child_process";
6+
import { describe, it, expect } from "vitest";
7+
8+
describe("Extractor Script Tests", () => {
9+
type WithTempDirTest = (tmpDir: string) => Promise<any>;
10+
11+
// This helper function is copied directly from config.test.ts
12+
const withTempDir = async (testFunc: WithTempDirTest) => {
13+
let tmpDir;
14+
try {
15+
tmpDir = await mkdtemp(join(tmpdir(), "vue3-gettext-extract-"));
16+
await testFunc(tmpDir);
17+
} finally {
18+
if (tmpDir) {
19+
await rm(tmpDir, { recursive: true, force: true });
20+
}
21+
}
22+
};
23+
24+
it("should correctly extract a message from a $gettext call with a trailing comma", async () => {
25+
await withTempDir(async (tmpDir) => {
26+
// 1. Setup the project structure inside the temp directory
27+
for (const d of ["src", "scripts", "node_modules"]) {
28+
await symlink(join(cwd(), d), join(tmpDir, d));
29+
}
30+
await writeFile(join(tmpDir, "package.json"), JSON.stringify({ name: "test", type: "commonjs" }));
31+
32+
await writeFile(
33+
join(tmpDir, "gettext.config.js"),
34+
`module.exports = { input: { path: './srctest' }, output: { path: './srctest/lang' } };`,
35+
);
36+
37+
await mkdir(join(tmpDir, "srctest", "lang"), { recursive: true });
38+
await writeFile(
39+
join(tmpDir, "srctest", "MultiLineLiteralWithComma.js"),
40+
`
41+
const myText = $gettext(
42+
\`This is a multiline template literal,
43+
That previously caused a crash in extraction.\`,
44+
);
45+
`,
46+
);
47+
48+
execSync(`sh -c 'cd ${tmpDir}; tsx ./scripts/gettext_extract.ts'`);
49+
50+
// Verify that the output .pot file is correct.
51+
const potContent = (await readFile(join(tmpDir, "srctest", "lang", "messages.pot"))).toString();
52+
console.debug(potContent);
53+
expect(potContent).toContain(
54+
'msgid ""\n' +
55+
'"This is a multiline template literal,\\n"\n' +
56+
'" That previously caused a crash in extraction."\n',
57+
);
58+
expect(potContent).toContain("#: srctest/MultiLineLiteralWithComma.js:2");
59+
});
60+
});
61+
});

0 commit comments

Comments
 (0)