From a2890d00c9ba11fa1099f42fba37fc7db6011c07 Mon Sep 17 00:00:00 2001 From: Ignace Maes Date: Fri, 19 Apr 2024 10:45:21 +0200 Subject: [PATCH 1/9] feat: discover translationKey tagged template --- index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/index.js b/index.js index 5003f9e..187e36b 100755 --- a/index.js +++ b/index.js @@ -334,6 +334,13 @@ async function analyzeJsFile(content, userPlugins) { } } }, + TaggedTemplateExpression({ node }) { + if (node.tag.name !== 'translationKey') return; + if (node.quasi.quasis.length === 0) return; + + // handle translationKey`foo.bar` case + translationKeys.add(node.quasi.quasis[0].value.raw); + }, }); return translationKeys; From 0f2e6d62ed85d4b79681bde0ed2cdec190b89028 Mon Sep 17 00:00:00 2001 From: Ignace Maes Date: Fri, 19 Apr 2024 10:46:05 +0200 Subject: [PATCH 2/9] feat: export translationKey tagged template util --- index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 187e36b..6b6ad7e 100755 --- a/index.js +++ b/index.js @@ -571,4 +571,8 @@ function mergeMaps(mapA, mapB) { return resultMap; } -module.exports = { run, generateFileList }; +function translationKey(strings) { + return strings[0]; +} + +module.exports = { run, generateFileList, translationKey }; From 31fa8c899fe0e4624bf8a09bd4d0ff99645a45d5 Mon Sep 17 00:00:00 2001 From: Ignace Maes Date: Fri, 19 Apr 2024 10:48:43 +0200 Subject: [PATCH 3/9] feat: add TypeScript type --- index.d.ts | 5 +++++ package.json | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..7c40bad --- /dev/null +++ b/index.d.ts @@ -0,0 +1,5 @@ +/** + * Tag stand-alone translation keys with this tagged template literal to have them discovered by `ember-intl-analyzer`. + * @param strings : The translation key to tag. + */ +export function translationKey(strings: TemplateStringsArray): string; diff --git a/package.json b/package.json index b2f1165..67bbe6f 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "author": "Tobias Bieniek ", "files": [ "bin", - "index.js" + "index.js", + "index.d.ts" ], "bin": { "ember-intl-analyzer": "bin/cli.js" From 2bf6e81c00bd63c249a62efd89f8f827d4b39725 Mon Sep 17 00:00:00 2001 From: Ignace Maes Date: Mon, 22 Apr 2024 10:01:02 +0200 Subject: [PATCH 4/9] test: add coverage for standalone translation --- __snapshots__/test.js.snap | 14 ++++++++++++++ .../app/controllers/application.js | 10 ++++++++++ .../app/templates/application.hbs | 2 ++ .../standalone-translation-key/app/utils/consts.js | 3 +++ .../translations/de.json | 4 ++++ .../translations/en.json | 4 ++++ 6 files changed, 37 insertions(+) create mode 100644 fixtures/standalone-translation-key/app/controllers/application.js create mode 100644 fixtures/standalone-translation-key/app/templates/application.hbs create mode 100644 fixtures/standalone-translation-key/app/utils/consts.js create mode 100644 fixtures/standalone-translation-key/translations/de.json create mode 100644 fixtures/standalone-translation-key/translations/en.json diff --git a/__snapshots__/test.js.snap b/__snapshots__/test.js.snap index 14fca07..5f467e4 100644 --- a/__snapshots__/test.js.snap +++ b/__snapshots__/test.js.snap @@ -269,6 +269,20 @@ Map { } `; +exports[`Test Fixtures standalone-translation-key 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 standalone-translation-key 2`] = `Map {}`; + exports[`Test Fixtures unused-translations 1`] = ` "[1/4] 🔍 Finding JS and HBS files... [2/4] 🔍 Searching for translations keys in JS and HBS files... diff --git a/fixtures/standalone-translation-key/app/controllers/application.js b/fixtures/standalone-translation-key/app/controllers/application.js new file mode 100644 index 0000000..bc13210 --- /dev/null +++ b/fixtures/standalone-translation-key/app/controllers/application.js @@ -0,0 +1,10 @@ +import Controller from '@ember/controller'; +import { tracked } from '@glimmer/tracking'; +import { bar } from '../utils/consts'; + +export default class ApplicationController extends Controller { + @tracked foo; + foo() { + return this.intl.t(bar); + } +} diff --git a/fixtures/standalone-translation-key/app/templates/application.hbs b/fixtures/standalone-translation-key/app/templates/application.hbs new file mode 100644 index 0000000..850dcc0 --- /dev/null +++ b/fixtures/standalone-translation-key/app/templates/application.hbs @@ -0,0 +1,2 @@ +{{t "hbs-translation"}} +{{t (concat "concat-" "expression.is-skipped")}} diff --git a/fixtures/standalone-translation-key/app/utils/consts.js b/fixtures/standalone-translation-key/app/utils/consts.js new file mode 100644 index 0000000..c888340 --- /dev/null +++ b/fixtures/standalone-translation-key/app/utils/consts.js @@ -0,0 +1,3 @@ +import { translationKey } from 'ember-intl-analyzer'; + +export const bar = translationKey`js-standalone-translation`; diff --git a/fixtures/standalone-translation-key/translations/de.json b/fixtures/standalone-translation-key/translations/de.json new file mode 100644 index 0000000..2012985 --- /dev/null +++ b/fixtures/standalone-translation-key/translations/de.json @@ -0,0 +1,4 @@ +{ + "hbs-translation": "Lenkstage", + "js-standalone-translation": "Affentheater (standalone)" +} diff --git a/fixtures/standalone-translation-key/translations/en.json b/fixtures/standalone-translation-key/translations/en.json new file mode 100644 index 0000000..ce60f42 --- /dev/null +++ b/fixtures/standalone-translation-key/translations/en.json @@ -0,0 +1,4 @@ +{ + "hbs-translation": "HBS!", + "js-standalone-translation": "JS Standalone!" +} From 099b49ff840281ec58df1fb3287a3256a4526fc0 Mon Sep 17 00:00:00 2001 From: Ignace Maes Date: Mon, 22 Apr 2024 10:20:56 +0200 Subject: [PATCH 5/9] docs: add section for translationKey --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index f9aeebf..8b91fb3 100644 --- a/README.md +++ b/README.md @@ -171,6 +171,20 @@ export default { }; ``` +Discover stand-alone translation keys +------------------------------------------------------------------------------ + +By default, only translation keys which are immediately translated are picked up by the analyzer. If you have translation keys which are not immediately translated, but are used in your application, you can mark them using the `translationKey` tagged template util. + +```diff ++ import { translationKey } from 'ember-intl-analyzer'; + +- const thisIsAKey = 'this.is.a.key'; ++ const thisIsAKey = translationKey`this.is.a.key`; +``` + +This will mark the key `this.is.a.key` as used in your application, and it will not be flagged as unused by the analyzer. This can be a useful tool to mark keys which are used in your application, so they don't have to be added to the whitelist. + Caveats ------------------------------------------------------------------------------ From 872dafe0705635a19d0606bb9fca1e22ae5ef564 Mon Sep 17 00:00:00 2001 From: Ignace Maes Date: Mon, 22 Apr 2024 10:24:09 +0200 Subject: [PATCH 6/9] refactor: move translationKey to separate file --- README.md | 2 +- fixtures/standalone-translation-key/app/utils/consts.js | 2 +- index.js | 6 +----- index.d.ts => translation-key.d.ts | 0 translation-key.js | 3 +++ 5 files changed, 6 insertions(+), 7 deletions(-) rename index.d.ts => translation-key.d.ts (100%) create mode 100755 translation-key.js diff --git a/README.md b/README.md index 8b91fb3..d667e3e 100644 --- a/README.md +++ b/README.md @@ -177,7 +177,7 @@ Discover stand-alone translation keys By default, only translation keys which are immediately translated are picked up by the analyzer. If you have translation keys which are not immediately translated, but are used in your application, you can mark them using the `translationKey` tagged template util. ```diff -+ import { translationKey } from 'ember-intl-analyzer'; ++ import { translationKey } from 'ember-intl-analyzer/translation-key'; - const thisIsAKey = 'this.is.a.key'; + const thisIsAKey = translationKey`this.is.a.key`; diff --git a/fixtures/standalone-translation-key/app/utils/consts.js b/fixtures/standalone-translation-key/app/utils/consts.js index c888340..259dea0 100644 --- a/fixtures/standalone-translation-key/app/utils/consts.js +++ b/fixtures/standalone-translation-key/app/utils/consts.js @@ -1,3 +1,3 @@ -import { translationKey } from 'ember-intl-analyzer'; +import { translationKey } from 'ember-intl-analyzer/translation-key'; export const bar = translationKey`js-standalone-translation`; diff --git a/index.js b/index.js index 6b6ad7e..187e36b 100755 --- a/index.js +++ b/index.js @@ -571,8 +571,4 @@ function mergeMaps(mapA, mapB) { return resultMap; } -function translationKey(strings) { - return strings[0]; -} - -module.exports = { run, generateFileList, translationKey }; +module.exports = { run, generateFileList }; diff --git a/index.d.ts b/translation-key.d.ts similarity index 100% rename from index.d.ts rename to translation-key.d.ts diff --git a/translation-key.js b/translation-key.js new file mode 100755 index 0000000..d68cf09 --- /dev/null +++ b/translation-key.js @@ -0,0 +1,3 @@ +export function translationKey(strings) { + return strings[0]; +} From f7b4b559176596711455b29031a5d6a341a7cefb Mon Sep 17 00:00:00 2001 From: Ignace Maes Date: Mon, 22 Apr 2024 10:37:00 +0200 Subject: [PATCH 7/9] feat: include new files in pkg output --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 67bbe6f..060184a 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "files": [ "bin", "index.js", - "index.d.ts" + "translation-key.js", + "translation-key.d.ts" ], "bin": { "ember-intl-analyzer": "bin/cli.js" From 0c07d9aade157d48707a33f3a9e0eb6cf060b5ec Mon Sep 17 00:00:00 2001 From: Ignace Maes Date: Mon, 22 Apr 2024 10:42:51 +0200 Subject: [PATCH 8/9] deps: setup ESM import in jest --- package.json | 10 ++++++++++ yarn.lock | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/package.json b/package.json index 060184a..f0a146c 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "yaml": "^1.10.2" }, "devDependencies": { + "@babel/plugin-transform-modules-commonjs": "^7.24.1", "eslint": "8.56.0", "eslint-config-prettier": "9.1.0", "eslint-plugin-node": "11.1.0", @@ -46,5 +47,14 @@ }, "engines": { "node": "^12.22.0 || ^14.17.0 || >=16.0.0" + }, + "babel" :{ + "env": { + "test": { + "plugins": [ + "@babel/plugin-transform-modules-commonjs" + ] + } + } } } diff --git a/yarn.lock b/yarn.lock index 126afb5..a5fb0b8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -112,6 +112,13 @@ dependencies: "@babel/types" "^7.22.5" +"@babel/helper-module-imports@^7.22.15": + version "7.24.3" + resolved "https://registry.yarnpkg.com/@babel/helper-module-imports/-/helper-module-imports-7.24.3.tgz#6ac476e6d168c7c23ff3ba3cf4f7841d46ac8128" + integrity sha512-viKb0F9f2s0BCS22QSF308z/+1YWKV/76mwt61NBzS5izMzDPwdq1pTrzf+Li3npBWX9KdQbkeCt1jSAM7lZqg== + dependencies: + "@babel/types" "^7.24.0" + "@babel/helper-module-imports@^7.22.5": version "7.22.5" resolved "https://registry.yarnpkg.com/@babel/helper-module-imports/-/helper-module-imports-7.22.5.tgz#1a8f4c9f4027d23f520bd76b364d44434a72660c" @@ -133,11 +140,27 @@ "@babel/traverse" "^7.22.5" "@babel/types" "^7.22.5" +"@babel/helper-module-transforms@^7.23.3": + version "7.23.3" + resolved "https://registry.yarnpkg.com/@babel/helper-module-transforms/-/helper-module-transforms-7.23.3.tgz#d7d12c3c5d30af5b3c0fcab2a6d5217773e2d0f1" + integrity sha512-7bBs4ED9OmswdfDzpz4MpWgSrV7FXlc3zIagvLFjS5H+Mk7Snr21vQ6QwrsoCGMfNC4e4LQPdoULEt4ykz0SRQ== + dependencies: + "@babel/helper-environment-visitor" "^7.22.20" + "@babel/helper-module-imports" "^7.22.15" + "@babel/helper-simple-access" "^7.22.5" + "@babel/helper-split-export-declaration" "^7.22.6" + "@babel/helper-validator-identifier" "^7.22.20" + "@babel/helper-plugin-utils@^7.0.0", "@babel/helper-plugin-utils@^7.10.4", "@babel/helper-plugin-utils@^7.12.13", "@babel/helper-plugin-utils@^7.14.5", "@babel/helper-plugin-utils@^7.22.5", "@babel/helper-plugin-utils@^7.8.0": version "7.22.5" resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.22.5.tgz#dd7ee3735e8a313b9f7b05a773d892e88e6d7295" integrity sha512-uLls06UVKgFG9QD4OeFYLEGteMIAa5kpTPcFL28yuCIIzsf6ZyKZMllKVOCZFhiZ5ptnwX4mtKdWCBE/uT4amg== +"@babel/helper-plugin-utils@^7.24.0": + version "7.24.0" + resolved "https://registry.yarnpkg.com/@babel/helper-plugin-utils/-/helper-plugin-utils-7.24.0.tgz#945681931a52f15ce879fd5b86ce2dae6d3d7f2a" + integrity sha512-9cUznXMG0+FxRuJfvL82QlTqIzhVW9sL0KjMPHhAOOvpQGL8QtdxnBKILjBqxlHyliz0yCa1G903ZXI/FuHy2w== + "@babel/helper-simple-access@^7.22.5": version "7.22.5" resolved "https://registry.yarnpkg.com/@babel/helper-simple-access/-/helper-simple-access-7.22.5.tgz#4938357dc7d782b80ed6dbb03a0fba3d22b1d5de" @@ -157,6 +180,11 @@ resolved "https://registry.yarnpkg.com/@babel/helper-string-parser/-/helper-string-parser-7.22.5.tgz#533f36457a25814cf1df6488523ad547d784a99f" integrity sha512-mM4COjgZox8U+JcXQwPijIZLElkgEpO5rsERVDJTc2qfCDfERyob6k5WegS14SX18IIjv+XD+GrqNumY5JRCDw== +"@babel/helper-string-parser@^7.23.4": + version "7.24.1" + resolved "https://registry.yarnpkg.com/@babel/helper-string-parser/-/helper-string-parser-7.24.1.tgz#f99c36d3593db9540705d0739a1f10b5e20c696e" + integrity sha512-2ofRCjnnA9y+wk8b9IAREroeUP02KHp431N2mhKniy2yKIDKpbrHv9eXwm8cBeWQYcJmzv5qKCu65P47eCF7CQ== + "@babel/helper-validator-identifier@^7.22.20", "@babel/helper-validator-identifier@^7.22.5": version "7.22.20" resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.22.20.tgz#c4ae002c61d2879e724581d96665583dbc1dc0e0" @@ -303,6 +331,15 @@ "@babel/helper-module-transforms" "^7.22.5" "@babel/helper-plugin-utils" "^7.22.5" +"@babel/plugin-transform-modules-commonjs@^7.24.1": + version "7.24.1" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-modules-commonjs/-/plugin-transform-modules-commonjs-7.24.1.tgz#e71ba1d0d69e049a22bf90b3867e263823d3f1b9" + integrity sha512-szog8fFTUxBfw0b98gEWPaEqF42ZUD/T3bkynW/wtgx2p/XCP55WEsb+VosKceRSd6njipdZvNogqdtI4Q0chw== + dependencies: + "@babel/helper-module-transforms" "^7.23.3" + "@babel/helper-plugin-utils" "^7.24.0" + "@babel/helper-simple-access" "^7.22.5" + "@babel/polyfill@^7.11.5": version "7.12.1" resolved "https://registry.yarnpkg.com/@babel/polyfill/-/polyfill-7.12.1.tgz#1f2d6371d1261bbd961f3c5d5909150e12d0bd96" @@ -363,6 +400,15 @@ "@babel/helper-validator-identifier" "^7.22.20" to-fast-properties "^2.0.0" +"@babel/types@^7.24.0": + version "7.24.0" + resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.24.0.tgz#3b951f435a92e7333eba05b7566fd297960ea1bf" + integrity sha512-+j7a5c253RfKh8iABBhywc8NSfP5LURe7Uh4qpsh6jc+aLJguvmIUBdjSdEMQv2bENrCR5MfRdjGo7vzS/ob7w== + dependencies: + "@babel/helper-string-parser" "^7.23.4" + "@babel/helper-validator-identifier" "^7.22.20" + to-fast-properties "^2.0.0" + "@bcoe/v8-coverage@^0.2.3": version "0.2.3" resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39" From a9a5165a9a360598d097dfc07812aee1f15cc441 Mon Sep 17 00:00:00 2001 From: Ignace Maes Date: Mon, 22 Apr 2024 10:43:00 +0200 Subject: [PATCH 9/9] test: add coverage for translationKey util --- test.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test.js b/test.js index 6a3d6aa..005936d 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,6 @@ const fs = require('fs'); const { run, generateFileList } = require('./index'); +const { translationKey } = require('./translation-key'); let fixtures = fs.readdirSync(`${__dirname}/fixtures/`); @@ -97,3 +98,9 @@ describe('generateFileList', () => { expect(() => generateFileList([])).toThrow('Unexpected empty file list'); }); }); + +describe('translationKey', () => { + test('util returns the exact same string', () => { + expect(translationKey`some.translation.key`).toBe('some.translation.key'); + }); +});