Repository: xiCO2k/laravel-vue-i18n (ts)

Issue:
Problem with php langs generation
Hi,

I am facing an issue in our project with generation of langs from php files. Our project started out i  think on laravel 7 and as the time passed we updated it to 10 as of now.

However we still have langs in 'resources/lang' directory.

When I tried to specify this path as mentioned in docs in mix.i18n("resources/lang") my build crashes with following error:
```

/var/www/html/node_modules/laravel-vue-i18n/dist/loader.cjs:69
    if (expr.kind === 'string') {
             ^

TypeError: Cannot read properties of undefined (reading 'kind')
    at parseItem (/var/www/html/node_modules/laravel-vue-i18n/dist/loader.cjs:69:14)
    at parseItem (/var/www/html/node_modules/laravel-vue-i18n/dist/loader.cjs:85:12)
    at parseItem (/var/www/html/node_modules/laravel-vue-i18n/dist/loader.cjs:83:36)
    at /var/www/html/node_modules/laravel-vue-i18n/dist/loader.cjs:73:46
    at Array.map (<anonymous>)
    at parseItem (/var/www/html/node_modules/laravel-vue-i18n/dist/loader.cjs:73:32)
    at parseItem (/var/www/html/node_modules/laravel-vue-i18n/dist/loader.cjs:83:36)
    at /var/www/html/node_modules/laravel-vue-i18n/dist/loader.cjs:73:46
    at Array.map (<anonymous>)
    at parseItem (/var/www/html/node_modules/laravel-vue-i18n/dist/loader.cjs:73:32)
    at parseItem (/var/www/html/node_modules/laravel-vue-i18n/dist/loader.cjs:83:36)
    at /var/www/html/node_modules/laravel-vue-i18n/dist/loader.cjs:73:46
    at Array.map (<anonymous>)
    at parseItem (/var/www/html/node_modules/laravel-vue-i18n/dist/loader.cjs:73:32)
    at parse (/var/www/html/node_modules/laravel-vue-i18n/dist/loader.cjs:65:32)
    at /var/www/html/node_modules/laravel-vue-i18n/dist/loader.cjs:121:66
    at Array.forEach (<anonymous>)
    at readThroughDir (/var/www/html/node_modules/laravel-vue-i18n/dist/loader.cjs:114:35)
    at parseAll (/var/www/html/node_modules/laravel-vue-i18n/dist/loader.cjs:41:49)
    at /var/www/html/node_modules/laravel-vue-i18n/dist/mix.cjs:39:141
    at Hook.eval [as call] (eval at create (/var/www/html/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:16:1)
    at Hook.CALL_DELEGATE [as _call] (/var/www/html/node_modules/tapable/lib/Hook.js:14:14)
    at /var/www/html/node_modules/webpack/lib/Compiler.js:1168:23
    at Hook.eval [as callAsync] (eval at create (/var/www/html/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:22:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/var/www/html/node_modules/tapable/lib/Hook.js:18:14)
    at Compiler.compile (/var/www/html/node_modules/webpack/lib/Compiler.js:1165:28)
    at /var/www/html/node_modules/webpack/lib/Watching.js:218:19
    at Hook.eval [as callAsync] (eval at create (/var/www/html/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:22:1)
    at Hook.CALL_ASYNC_DELEGATE [as _callAsync] (/var/www/html/node_modules/tapable/lib/Hook.js:18:14)
    at run (/var/www/html/node_modules/webpack/lib/Watching.js:172:33)
    at /var/www/html/node_modules/webpack/lib/Watching.js:167:6
    at Compiler.readRecords (/var/www/html/node_modules/webpack/lib/Compiler.js:989:5)
    at run (/var/www/html/node_modules/webpack/lib/Watching.js:163:26)
    at Watching._go (/var/www/html/node_modules/webpack/lib/Watching.js:222:3)
    at Watching._invalidate (/var/www/html/node_modules/webpack/lib/Watching.js:423:9)
    at /var/www/html/node_modules/webpack/lib/Watching.js:69:28
    at processTicksAndRejections (node:internal/process/task_queues:78:11)
```

Got any advice please? If I dont specify the path I see the lang folder beign created during the build / watcher process but as soon as it hits 100% it gets deleted.

Thank you.

Pull request description:
fix: Ignore `null` and empty `array` `php` translations
Fixes #158

Patch:
diff --git a/src/loader.ts b/src/loader.ts
index 09e88d9..22332ac 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


Test patch:
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 @@
+<?php
+
+return [
+    'empty_array' => [],
+    '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);