From c2be6c8e190680c00f06c8579addc2084c5c1e87 Mon Sep 17 00:00:00 2001 From: Francisco Madeira Date: Fri, 5 Jan 2024 10:36:53 +0000 Subject: [PATCH 1/2] fix: Ignore `null` and empty `array` `php` translations --- src/loader.ts | 8 ++++++++ test/fixtures/lang/en/ignore.php | 6 ++++++ test/loader.test.ts | 7 +++++++ 3 files changed, 21 insertions(+) create mode 100644 test/fixtures/lang/en/ignore.php diff --git a/src/loader.ts b/src/loader.ts index 09e88d9..f957c5a 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -77,6 +77,10 @@ const parseItem = (expr) => { return expr.value } + if (expr.kind === 'nullkeyword') { + return null; + } + if (expr.kind === 'array') { let items = expr.items.map((item) => parseItem(item)) @@ -102,6 +106,10 @@ const convertToDotsSyntax = (list) => { const flatten = (items, context = '') => { const data = {} + if (items === null) { + return data; + } + Object.entries(items).forEach(([key, value]) => { if (typeof value === 'string') { data[context + key] = value diff --git a/test/fixtures/lang/en/ignore.php b/test/fixtures/lang/en/ignore.php new file mode 100644 index 0000000..5eb6b8c --- /dev/null +++ b/test/fixtures/lang/en/ignore.php @@ -0,0 +1,6 @@ + [], + 'null' => null, +]; diff --git a/test/loader.test.ts b/test/loader.test.ts index 59a9eac..55e722b 100644 --- a/test/loader.test.ts +++ b/test/loader.test.ts @@ -98,6 +98,13 @@ it('transforms simple index array to .json', () => { expect(lang['arr.1']).toBe('bar'); }); +it('ignores empty `array` or `null` translations', () => { + const lang = parse(fs.readFileSync(__dirname + '/fixtures/lang/en/ignore.php').toString()); + + expect(lang['empty_array']).toBe(undefined); + expect(lang['null']).toBe(undefined); +}); + it('checks if there is .php translations', () => { expect(hasPhpTranslations(__dirname + '/fixtures/lang/')).toBe(true); expect(hasPhpTranslations(__dirname + '/fixtures/wronglangfolder/')).toBe(false); From 4cef8a23c0c353b4425803a1409ba9784b3849ca Mon Sep 17 00:00:00 2001 From: Francisco Madeira Date: Fri, 5 Jan 2024 10:37:30 +0000 Subject: [PATCH 2/2] style: fixes --- src/loader.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/loader.ts b/src/loader.ts index f957c5a..22332ac 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -78,7 +78,7 @@ const parseItem = (expr) => { } if (expr.kind === 'nullkeyword') { - return null; + return null } if (expr.kind === 'array') { @@ -107,7 +107,7 @@ const convertToDotsSyntax = (list) => { const data = {} if (items === null) { - return data; + return data } Object.entries(items).forEach(([key, value]) => {