From 2cb754ddb319fed25edf2124898c01bbf3f3891b Mon Sep 17 00:00:00 2001 From: Omair Vaiyani Date: Sat, 16 Nov 2019 13:27:35 +0000 Subject: [PATCH 1/3] patch: fix experimential parser issue - classProperties --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 74555905..73a18555 100755 --- a/index.js +++ b/index.js @@ -139,7 +139,7 @@ async function analyzeJsFile(content) { // parse the JS file let ast = BabelParser.parse(content, { sourceType: 'module', - plugins: ['decorators-legacy', 'dynamicImport'], + plugins: ['decorators-legacy', 'dynamicImport', 'classProperties'], }); // find translation keys in the syntax tree From 71e48aff53abdbb523d9333badfc3653b410fb45 Mon Sep 17 00:00:00 2001 From: Omair Vaiyani Date: Sat, 16 Nov 2019 13:46:03 +0000 Subject: [PATCH 2/3] feat: check intl config for translation input path --- __snapshots__/test.js.snap | 12 ++++++++ .../app/controllers/application.js | 7 +++++ .../app/templates/application.hbs | 1 + .../alternative-path/config/ember-intl.js | 5 ++++ fixtures/alternative-path/polyglotter/de.json | 4 +++ fixtures/alternative-path/polyglotter/en.json | 4 +++ index.js | 28 +++++++++++++++++-- 7 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 fixtures/alternative-path/app/controllers/application.js create mode 100644 fixtures/alternative-path/app/templates/application.hbs create mode 100644 fixtures/alternative-path/config/ember-intl.js create mode 100644 fixtures/alternative-path/polyglotter/de.json create mode 100644 fixtures/alternative-path/polyglotter/en.json diff --git a/__snapshots__/test.js.snap b/__snapshots__/test.js.snap index 927dc2d4..9cd4f532 100644 --- a/__snapshots__/test.js.snap +++ b/__snapshots__/test.js.snap @@ -1,5 +1,17 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Test Fixtures alternative-path 1`] = ` +"[1/4] 🔍 Finding JS and HBS files... +[2/4] 🔍 Searching for translations keys in JS and HBS files... +[3/4] ⚙️ Checking for unused translations... +[4/4] ⚙️ Checking for missing translations... + + 👏 No unused translations were found! + + 👏 No missing translations were found! +" +`; + exports[`Test Fixtures decorators 1`] = ` "[1/4] 🔍 Finding JS and HBS files... [2/4] 🔍 Searching for translations keys in JS and HBS files... diff --git a/fixtures/alternative-path/app/controllers/application.js b/fixtures/alternative-path/app/controllers/application.js new file mode 100644 index 00000000..e2722ce2 --- /dev/null +++ b/fixtures/alternative-path/app/controllers/application.js @@ -0,0 +1,7 @@ +import Controller from '@ember/controller'; + +export default Controller.extend({ + foo: computed('intl.locale', function() { + return this.intl.t('js-translation'); + }), +}); diff --git a/fixtures/alternative-path/app/templates/application.hbs b/fixtures/alternative-path/app/templates/application.hbs new file mode 100644 index 00000000..8588d949 --- /dev/null +++ b/fixtures/alternative-path/app/templates/application.hbs @@ -0,0 +1 @@ +{{t "hbs-translation"}} diff --git a/fixtures/alternative-path/config/ember-intl.js b/fixtures/alternative-path/config/ember-intl.js new file mode 100644 index 00000000..d89c6281 --- /dev/null +++ b/fixtures/alternative-path/config/ember-intl.js @@ -0,0 +1,5 @@ +module.exports = function() { + return { + inputPath: './polyglotter/' + }; +}; diff --git a/fixtures/alternative-path/polyglotter/de.json b/fixtures/alternative-path/polyglotter/de.json new file mode 100644 index 00000000..5df90742 --- /dev/null +++ b/fixtures/alternative-path/polyglotter/de.json @@ -0,0 +1,4 @@ +{ + "hbs-translation": "Lenkstage", + "js-translation": "Affentheater" +} diff --git a/fixtures/alternative-path/polyglotter/en.json b/fixtures/alternative-path/polyglotter/en.json new file mode 100644 index 00000000..70c4c749 --- /dev/null +++ b/fixtures/alternative-path/polyglotter/en.json @@ -0,0 +1,4 @@ +{ + "hbs-translation": "HBS!", + "js-translation": "JS!" +} diff --git a/index.js b/index.js index 73a18555..28128a95 100755 --- a/index.js +++ b/index.js @@ -24,6 +24,7 @@ async function run(rootDir, options = {}) { const step = num => chalk.dim(`[${num}/${NUM_STEPS}]`); let config = readConfig(rootDir); + let intlConfig = readIntlConfig(rootDir); log(`${step(1)} 🔍 Finding JS and HBS files...`); let files = await findAppFiles(rootDir); @@ -33,7 +34,7 @@ async function run(rootDir, options = {}) { log(`${step(3)} ⚙️ Checking for unused translations...`); - let translationFiles = await findTranslationFiles(rootDir); + let translationFiles = await findTranslationFiles(rootDir, intlConfig); let existingTranslationKeys = await analyzeTranslationFiles(rootDir, translationFiles); let whitelist = config.whitelist || []; @@ -89,12 +90,33 @@ function readConfig(cwd) { return config; } +function readIntlConfig(cwd) { + let configPath = `${cwd}/config/ember-intl.js`; + + let config; + if (fs.existsSync(configPath)) { + let requireESM = require('esm')(module); + config = requireESM(configPath); + if (config) { + if (typeof config === 'function') { + config = config(); + } else if (config.hasOwnProperty('default')) { + config = config.default; + } + } + } + + return config || {}; +} + async function findAppFiles(cwd) { return globby(['app/**/*.js', 'app/**/*.hbs', 'app/**/*.emblem'], { cwd }); } -async function findTranslationFiles(cwd) { - return globby(['translations/**/*.json', 'translations/**/*.yaml', 'translations/**/*.yml'], { +async function findTranslationFiles(cwd, intlConfig) { + let { inputPath = 'translations/' } = intlConfig; + + return globby([`${inputPath}**/*.json`, `${inputPath}**/*.yaml`, `${inputPath}**/*.yml`], { cwd, }); } From e34b22d12ad50ec06463d59b21669858e4eeb205 Mon Sep 17 00:00:00 2001 From: Omair Vaiyani Date: Sat, 16 Nov 2019 13:59:02 +0000 Subject: [PATCH 3/3] fix lint error --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 28128a95..e4e38d03 100755 --- a/index.js +++ b/index.js @@ -100,7 +100,7 @@ function readIntlConfig(cwd) { if (config) { if (typeof config === 'function') { config = config(); - } else if (config.hasOwnProperty('default')) { + } else if (Object.prototype.hasOwnProperty.call(config, 'default')) { config = config.default; } }