├── .eslintignore ├── .gitattributes ├── .gitignore ├── .travis.yml ├── __tests__ ├── hello.po └── lib │ └── rules │ ├── required-positional-markers-for-multiple-variables.js │ ├── has-translation-msgid.js │ └── no-variable-string.js ├── .editorconfig ├── index.js ├── LICENSE ├── package.json ├── lib ├── utils.js └── rules │ ├── required-positional-markers-for-multiple-variables.js │ ├── no-variable-string.js │ └── has-translation-msgid.js └── README.md /.eslintignore: -------------------------------------------------------------------------------- 1 | coverage 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | coverage 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - node 4 | - 10 5 | - 8 6 | after_script: "cat ./coverage/lcov.info | coveralls" 7 | -------------------------------------------------------------------------------- /__tests__/hello.po: -------------------------------------------------------------------------------- 1 | msgid "hello" 2 | msgid_plural "hellos" 3 | msgstr[0] "" 4 | msgstr[1] "" 5 | 6 | 7 | msgctxt "homepage" 8 | msgid "hi" 9 | msgid_plural "his" 10 | msgstr[0] "" 11 | msgstr[1] "" 12 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.json] 11 | indent_size = 2 12 | 13 | [*.md] 14 | trim_trailing_whitespace = false 15 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | rules: { 5 | 'no-variable-string': require('./lib/rules/no-variable-string'), 6 | 'required-positional-markers-for-multiple-variables': require('./lib/rules/required-positional-markers-for-multiple-variables'), 7 | 'has-translation-msgid': require('./lib/rules/has-translation-msgid'), 8 | }, 9 | rulesConfig: { 10 | 'no-variable-string': 0, 11 | }, 12 | }; 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 App Annie 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "eslint-plugin-gettext", 3 | "version": "1.2.0", 4 | "description": "Set of eslint rules for gettext API", 5 | "author": "App Annie Engineering", 6 | "files": [ 7 | "lib", 8 | "index.js" 9 | ], 10 | "engines": { 11 | "node": ">=6.0.0" 12 | }, 13 | "main": "index.js", 14 | "keywords": [ 15 | "gettext", 16 | "gettext-eslint", 17 | "literal", 18 | "eslint", 19 | "eslint-plugin", 20 | "i18n", 21 | "internationalization", 22 | "localization" 23 | ], 24 | "dependencies": { 25 | "gettext-parser": "^4.0.4" 26 | }, 27 | "devDependencies": { 28 | "coveralls": "^3.0.4", 29 | "eslint": "^7.32.0", 30 | "eslint-config-prettier": "^8.3.0", 31 | "eslint-config-xo-space": "^0.29.0", 32 | "eslint-plugin-prettier": "^4.0.0", 33 | "husky": "^7.0.2", 34 | "istanbul": "~0.4.5", 35 | "jest": "^27.2.4", 36 | "lint-staged": "^11.1.2", 37 | "prettier": "^2.4.1" 38 | }, 39 | "scripts": { 40 | "test": "jest --coverage", 41 | "pretest": "eslint .", 42 | "precommit": "lint-staged" 43 | }, 44 | "lint-staged": { 45 | "*.js": [ 46 | "eslint --fix", 47 | "git add" 48 | ] 49 | }, 50 | "husky": { 51 | "hooks": { 52 | "pre-commit": "lint-staged" 53 | } 54 | }, 55 | "repository": "appannie/eslint-plugin-gettext", 56 | "license": "MIT", 57 | "eslintConfig": { 58 | "extends": [ 59 | "xo-space", 60 | "prettier" 61 | ], 62 | "plugins": [ 63 | "prettier" 64 | ], 65 | "env": { 66 | "jest": true, 67 | "node": true 68 | }, 69 | "rules": { 70 | "indent": "off", 71 | "prettier/prettier": [ 72 | "error", 73 | { 74 | "singleQuote": true, 75 | "trailingComma": "es5", 76 | "tabWidth": 4, 77 | "printWidth": 90 78 | } 79 | ] 80 | } 81 | }, 82 | "jest": { 83 | "testEnvironment": "node", 84 | "coverageReporters": [ 85 | "lcov", 86 | "text", 87 | "html" 88 | ], 89 | "coverageThreshold": { 90 | "global": { 91 | "branches": 100, 92 | "functions": 100, 93 | "lines": 100, 94 | "statements": 100 95 | } 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /lib/utils.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const variableMarkerReg = /%[sd]/gm; 4 | const variablePositionMarkerReg = /%\d+\$[sd]/gm; 5 | 6 | const translationFiles = {}; 7 | 8 | function getReportNode(argNode, node) { 9 | return argNode || node.callee; 10 | } 11 | 12 | function isStringLiteral(node) { 13 | return node && node.type === 'Literal' && typeof node.value === 'string'; 14 | } 15 | 16 | const hasMultipleVaribles = (str) => { 17 | if (str.match(variableMarkerReg) && str.match(variablePositionMarkerReg)) { 18 | return true; 19 | } 20 | 21 | return str.split(variableMarkerReg).length >= 3; 22 | }; 23 | 24 | function isI18nAPICall(node, methodName) { 25 | const { callee } = node; 26 | 27 | return ( 28 | (node.type === 'CallExpression' && 29 | callee.type === 'MemberExpression' && 30 | callee.property.type === 'Identifier' && 31 | callee.property.name === methodName) || 32 | (callee.type === 'Identifier' && callee.name === methodName) 33 | ); 34 | } 35 | 36 | function checkRequiredArgument(context, node, argNum) { 37 | const args = node.arguments; 38 | const msg = `Here required ${argNum} arguments, actually get \ 39 | ${args.length} arguments.`; 40 | const nullMsg = 'Here should require valid arguments, not null'; 41 | for (let i = 0; i < argNum; i += 1) { 42 | const argValue = args[i]; 43 | if (argValue === undefined) { 44 | context.report(node.callee, msg); 45 | } 46 | 47 | if (argValue && argValue.value === null) { 48 | context.report(argValue, nullMsg); 49 | } 50 | } 51 | } 52 | 53 | function getTranslations(filename, translationContext) { 54 | if (!translationFiles[filename]) { 55 | const gettextParser = require('gettext-parser'); 56 | 57 | const input = require('fs').readFileSync(filename); 58 | translationFiles[filename] = gettextParser.po.parse(input).translations; 59 | } 60 | 61 | return translationFiles[filename][translationContext || '']; 62 | } 63 | 64 | function isRegisteredTranslatable(context, literal, translationContext) { 65 | const { poFilename } = context.settings; 66 | const translations = getTranslations(poFilename, translationContext); 67 | return Object.keys(translations).findIndex((i) => i === literal) !== -1; 68 | } 69 | 70 | function isRegisteredPlural(context, literal, translationContext, nonPlural) { 71 | const { poFilename } = context.settings; 72 | const translations = getTranslations(poFilename, translationContext); 73 | const entry = translations[nonPlural]; 74 | return entry && entry.msgid_plural === literal; 75 | } 76 | 77 | module.exports = { 78 | checkRequiredArgument, 79 | isStringLiteral, 80 | isI18nAPICall, 81 | getReportNode, 82 | hasMultipleVaribles, 83 | isRegisteredTranslatable, 84 | isRegisteredPlural, 85 | }; 86 | -------------------------------------------------------------------------------- /lib/rules/required-positional-markers-for-multiple-variables.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const utils = require('../utils'); 4 | 5 | const errorMsg = 6 | 'Invalid variables, need adding sprintf positional markers on the variables, like %1$s, %2$d'; 7 | 8 | const i18nMethodMap = { 9 | gettext(context, node) { 10 | const keyTxt = node.arguments[0]; 11 | 12 | if (utils.isStringLiteral(keyTxt)) { 13 | const reportNode = utils.getReportNode(keyTxt, node); 14 | if (utils.hasMultipleVaribles(keyTxt.value)) { 15 | context.report(reportNode, errorMsg); 16 | return true; 17 | } 18 | 19 | return false; 20 | } 21 | 22 | return false; 23 | }, 24 | ngettext(context, node) { 25 | const keyTxt = node.arguments[0]; 26 | const pluralTxt = node.arguments[1]; 27 | 28 | if (utils.isStringLiteral(keyTxt) && utils.hasMultipleVaribles(keyTxt.value)) { 29 | context.report(utils.getReportNode(keyTxt, node), errorMsg); 30 | return true; 31 | } 32 | 33 | if ( 34 | utils.isStringLiteral(pluralTxt) && 35 | utils.hasMultipleVaribles(pluralTxt.value) 36 | ) { 37 | context.report(utils.getReportNode(pluralTxt, node), errorMsg); 38 | return true; 39 | } 40 | 41 | return false; 42 | }, 43 | pgettext(context, node) { 44 | const keyTxt = node.arguments[1]; 45 | 46 | if (utils.isStringLiteral(keyTxt) && utils.hasMultipleVaribles(keyTxt.value)) { 47 | context.report(utils.getReportNode(keyTxt, node), errorMsg); 48 | return true; 49 | } 50 | 51 | return false; 52 | }, 53 | npgettext(context, node) { 54 | const keyTxt = node.arguments[1]; 55 | const pluralTxt = node.arguments[2]; 56 | 57 | if (utils.isStringLiteral(keyTxt) && utils.hasMultipleVaribles(keyTxt.value)) { 58 | context.report(utils.getReportNode(keyTxt, node), errorMsg); 59 | return true; 60 | } 61 | 62 | if ( 63 | utils.isStringLiteral(pluralTxt) && 64 | utils.hasMultipleVaribles(pluralTxt.value) 65 | ) { 66 | context.report(utils.getReportNode(pluralTxt, node), errorMsg); 67 | return true; 68 | } 69 | 70 | return false; 71 | }, 72 | }; 73 | 74 | module.exports = { 75 | meta: { 76 | docs: { 77 | description: 78 | 'Required adding sprintf positional markers to the variables if a string contains more than a single marker ', 79 | recommended: false, 80 | }, 81 | fixable: null, 82 | schema: [], 83 | }, 84 | 85 | create(context) { 86 | return { 87 | CallExpression(node) { 88 | Object.keys(i18nMethodMap).find((apiName) => { 89 | if (utils.isI18nAPICall(node, apiName)) { 90 | return i18nMethodMap[apiName](context, node); 91 | } 92 | 93 | return false; 94 | }); 95 | }, 96 | }; 97 | }, 98 | }; 99 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # eslint-plugin-gettext [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url] 2 | 3 | [Gettext](https://en.wikipedia.org/wiki/Gettext) linting rules for ESLint. 4 | 5 | We're using this rule to lint against misuse of the [jed gettext integration](https://github.com/messageformat/Jed) and the [django JS catalog](https://docs.djangoproject.com/en/1.11/topics/i18n/translation/#internationalization-in-javascript-code); so we're confident it should work against any gettext integration. 6 | 7 | ## Installation 8 | 9 | 1. Install [ESLint](https://www.github.com/eslint/eslint). 10 | 1. Install [eslint-plugin-gettext](https://github.com/appannie/eslint-plugin-gettext) plugin. 11 | 12 | ```sh 13 | npm install eslint --save-dev 14 | npm install eslint-plugin-gettext --save-dev 15 | ``` 16 | 17 | ## Configuration 18 | 19 | 1. Load plugin. 20 | 1. Enable rules. 21 | 22 | ```json 23 | { 24 | "plugins": ["gettext"], 25 | "rules": { 26 | "gettext/no-variable-string": "error", 27 | "gettext/required-positional-markers-for-multiple-variables": "error" 28 | } 29 | } 30 | ``` 31 | 32 | ## Rules 33 | 34 | ### `gettext/no-variable-string` 35 | 36 | Disallow non literal strings inside common `gettext` functions. This is a very common mistake that disallow translation system from statically collecting the translatable strings. 37 | 38 | ```js 39 | // Disallows any non string literals in string reserved fields: 40 | gettext(variable) 41 | gettext(123) 42 | ngettext(varA, varB, 5) 43 | pgettext(varA, varB) 44 | npgettext(varA, varB, varC, 5) 45 | 46 | // Allows: 47 | gettext('hello') 48 | ngettext('cat', '%d cats', 5) 49 | pgettext('homepage', 'hello') 50 | npgettext('homepage', 'cat', '%d cats', 5) 51 | i18n.gettext('hello') // any object can expose the gettext API 52 | this.gettext('hello') 53 | ``` 54 | 55 | ### `gettext/required-positional-markers-for-multiple-variables` 56 | 57 | Require that all strings containing multiple variables also includes positional marker. This allows translator to reorder variables, and prevents `sprintf()` errors if someone change the order of `%s` and `%d`. 58 | 59 | ```js 60 | // Disallows: 61 | gettext('There is %d more event in the %s.') 62 | gettext('There is %d more event in the %1$s.') 63 | 64 | // Allows: 65 | gettext('There is %d more event in the game.') 66 | ngettext('cat %1$s $2$s', '%1$d cats %2$d dogs', count) 67 | ``` 68 | 69 | ## License 70 | 71 | MIT © [App Annie](https://www.appannie.com/en/about/careers/engineering/) 72 | 73 | [npm-image]: https://badge.fury.io/js/eslint-plugin-gettext.svg 74 | [npm-url]: https://npmjs.org/package/eslint-plugin-gettext 75 | [travis-image]: https://travis-ci.org/appannie/eslint-plugin-gettext.svg?branch=master 76 | [travis-url]: https://travis-ci.org/appannie/eslint-plugin-gettext 77 | [daviddm-image]: https://david-dm.org/appannie/eslint-plugin-gettext.svg?theme=shields.io 78 | [daviddm-url]: https://david-dm.org/appannie/eslint-plugin-gettext 79 | [coveralls-image]: https://coveralls.io/repos/appannie/eslint-plugin-gettext/badge.svg 80 | [coveralls-url]: https://coveralls.io/r/appannie/eslint-plugin-gettext 81 | -------------------------------------------------------------------------------- /__tests__/lib/rules/required-positional-markers-for-multiple-variables.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const ruleRequiredPositionalMarkersForMultipleVariables = require('../../../lib/rules/required-positional-markers-for-multiple-variables'); 4 | const { RuleTester } = require('eslint'); 5 | 6 | const ruleTester = new RuleTester(); 7 | 8 | const invalidMessage = 9 | 'Invalid variables, need adding sprintf positional markers on the variables, like %1$s, %2$d'; 10 | 11 | ruleTester.run( 12 | 'required-positional-markers-for-multiple-variables', 13 | ruleRequiredPositionalMarkersForMultipleVariables, 14 | { 15 | valid: [ 16 | 'abc()', 17 | "gettext('There is %d more event in the game.')", 18 | 'gettext(rule)', 19 | "i18n.gettext('There is %d more event in the game.')", 20 | "i18n.gettext('There is %1$d more event in the %2$s.')", 21 | "ngettext('cat %s', '%d cats', 5)", 22 | "ngettext('cat %1$s $2$s', '%1$d cats %2$d dogs', count)", 23 | "pgettext('homepage', 'hello %d')", 24 | 'pgettext(ctx, text)', 25 | "npgettext('homepage', 'cat', '%d cats', 5)", 26 | ], 27 | invalid: [ 28 | { 29 | code: "gettext('There is %d more event in the %s.')", 30 | errors: [ 31 | { 32 | message: invalidMessage, 33 | type: 'Literal', 34 | }, 35 | ], 36 | }, 37 | { 38 | code: "gettext('There is %d more event in the %1$s.')", 39 | errors: [ 40 | { 41 | message: invalidMessage, 42 | type: 'Literal', 43 | }, 44 | ], 45 | }, 46 | { 47 | code: "ngettext('cat', '%d cats %d dogs', count)", 48 | errors: [ 49 | { 50 | message: invalidMessage, 51 | type: 'Literal', 52 | }, 53 | ], 54 | }, 55 | { 56 | code: "ngettext('cat %d and %s', 'cats', count)", 57 | errors: [ 58 | { 59 | message: invalidMessage, 60 | type: 'Literal', 61 | }, 62 | ], 63 | }, 64 | { 65 | code: "pgettext('homepage', 'hello %d my name is %s')", 66 | errors: [ 67 | { 68 | message: invalidMessage, 69 | type: 'Literal', 70 | }, 71 | ], 72 | }, 73 | { 74 | code: "npgettext('homepage', 'cat', 'cat %d and %s dog', 5)", 75 | errors: [ 76 | { 77 | message: invalidMessage, 78 | type: 'Literal', 79 | }, 80 | ], 81 | }, 82 | { 83 | code: "npgettext('homepage', 'cat %s and dog %d', 'cats and dogs', 5)", 84 | errors: [ 85 | { 86 | message: invalidMessage, 87 | type: 'Literal', 88 | }, 89 | ], 90 | }, 91 | ], 92 | } 93 | ); 94 | -------------------------------------------------------------------------------- /lib/rules/no-variable-string.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const utils = require('../utils'); 4 | 5 | const errorMsg = 6 | 'Unexpected argument, gettext function only allows string literals as arguments.'; 7 | 8 | const i18nMethodMap = { 9 | gettext(context, node) { 10 | utils.checkRequiredArgument(context, node, 1); 11 | 12 | const keyTxt = node.arguments[0]; 13 | if (!utils.isStringLiteral(keyTxt)) { 14 | const reportNode = utils.getReportNode(keyTxt, node); 15 | context.report(reportNode, errorMsg); 16 | return true; 17 | } 18 | 19 | return false; 20 | }, 21 | ngettext(context, node) { 22 | utils.checkRequiredArgument(context, node, 3); 23 | 24 | let catchError = false; 25 | const keyTxt = node.arguments[0]; 26 | const pluralTxt = node.arguments[1]; 27 | 28 | if (!utils.isStringLiteral(keyTxt)) { 29 | context.report(utils.getReportNode(keyTxt, node), errorMsg); 30 | catchError = true; 31 | } 32 | 33 | if (!utils.isStringLiteral(pluralTxt)) { 34 | context.report(utils.getReportNode(pluralTxt, node), errorMsg); 35 | catchError = true; 36 | } 37 | 38 | return catchError; 39 | }, 40 | pgettext(context, node) { 41 | utils.checkRequiredArgument(context, node, 2); 42 | 43 | let catchError = false; 44 | const ctxTxt = node.arguments[0]; 45 | const keyTxt = node.arguments[1]; 46 | 47 | if (!utils.isStringLiteral(ctxTxt)) { 48 | context.report(utils.getReportNode(ctxTxt, node), errorMsg); 49 | catchError = true; 50 | } 51 | 52 | if (!utils.isStringLiteral(keyTxt)) { 53 | context.report(utils.getReportNode(keyTxt, node), errorMsg); 54 | catchError = true; 55 | } 56 | 57 | return catchError; 58 | }, 59 | npgettext(context, node) { 60 | utils.checkRequiredArgument(context, node, 4); 61 | 62 | let catchError = false; 63 | const ctxTxt = node.arguments[0]; 64 | const keyTxt = node.arguments[1]; 65 | const pluralTxt = node.arguments[2]; 66 | 67 | if (!utils.isStringLiteral(ctxTxt)) { 68 | context.report(utils.getReportNode(ctxTxt, node), errorMsg); 69 | catchError = true; 70 | } 71 | 72 | if (!utils.isStringLiteral(keyTxt)) { 73 | context.report(utils.getReportNode(keyTxt, node), errorMsg); 74 | catchError = true; 75 | } 76 | 77 | if (!utils.isStringLiteral(pluralTxt)) { 78 | context.report(utils.getReportNode(pluralTxt, node), errorMsg); 79 | catchError = true; 80 | } 81 | 82 | return catchError; 83 | }, 84 | }; 85 | 86 | module.exports = { 87 | meta: { 88 | docs: { 89 | description: 90 | 'Should only translate string literal. Variable/Null/Undefined/Number is not allowed', 91 | recommended: false, 92 | }, 93 | fixable: null, 94 | schema: [], 95 | }, 96 | 97 | create(context) { 98 | return { 99 | CallExpression(node) { 100 | Object.keys(i18nMethodMap).find((apiName) => { 101 | if (utils.isI18nAPICall(node, apiName)) { 102 | return i18nMethodMap[apiName](context, node); 103 | } 104 | 105 | return false; 106 | }); 107 | }, 108 | }; 109 | }, 110 | }; 111 | -------------------------------------------------------------------------------- /__tests__/lib/rules/has-translation-msgid.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const ruleNoMissingMsgId = require('../../../lib/rules/has-translation-msgid'); 4 | const { RuleTester } = require('eslint'); 5 | 6 | const ruleTester = new RuleTester(); 7 | const invalidMessage = 'String literal not in .po file.'; 8 | 9 | ruleTester.run('has-translation-msgid', ruleNoMissingMsgId, { 10 | valid: [ 11 | { 12 | code: "gettext('hello')", 13 | settings: { poFilename: '__tests__/hello.po' }, 14 | }, 15 | { 16 | code: "ngettext('hello', 'hellos', 5)", 17 | settings: { poFilename: '__tests__/hello.po' }, 18 | }, 19 | { 20 | code: "pgettext('homepage', 'hi')", 21 | settings: { poFilename: '__tests__/hello.po' }, 22 | }, 23 | { 24 | code: "npgettext('homepage', 'hi', 'his', 5)", 25 | settings: { poFilename: '__tests__/hello.po' }, 26 | }, 27 | // These are ignored as they are reported by no-variable-string 28 | { 29 | code: 'gettext(1)', 30 | settings: { poFilename: '__tests__/hello.po' }, 31 | }, 32 | { 33 | code: "ngettext(1, 'hellos', 5)", 34 | settings: { poFilename: '__tests__/hello.po' }, 35 | }, 36 | { 37 | code: "ngettext('hello', 1, 5)", 38 | settings: { poFilename: '__tests__/hello.po' }, 39 | }, 40 | { 41 | code: "pgettext(1, 'hi')", 42 | settings: { poFilename: '__tests__/hello.po' }, 43 | }, 44 | { 45 | code: "npgettext(1, 'hi', 'his', 5)", 46 | settings: { poFilename: '__tests__/hello.po' }, 47 | }, 48 | { 49 | code: "npgettext('homepage', 'hi', 1, 5)", 50 | settings: { poFilename: '__tests__/hello.po' }, 51 | }, 52 | ], 53 | invalid: [ 54 | { 55 | code: "gettext('goodbye')", 56 | settings: { poFilename: '__tests__/hello.po' }, 57 | errors: [ 58 | { 59 | message: invalidMessage, 60 | type: 'Literal', 61 | }, 62 | ], 63 | }, 64 | { 65 | code: "ngettext('good', 'bye', 5)", 66 | settings: { poFilename: '__tests__/hello.po' }, 67 | errors: [ 68 | { 69 | message: invalidMessage, 70 | type: 'Literal', 71 | }, 72 | { 73 | message: invalidMessage, 74 | type: 'Literal', 75 | }, 76 | ], 77 | }, 78 | { 79 | code: "ngettext('hello', 'bye', 5)", 80 | settings: { poFilename: '__tests__/hello.po' }, 81 | errors: [ 82 | { 83 | message: invalidMessage, 84 | type: 'Literal', 85 | }, 86 | ], 87 | }, 88 | { 89 | code: "pgettext('homepage', 'goodbye')", 90 | settings: { poFilename: '__tests__/hello.po' }, 91 | errors: [ 92 | { 93 | message: invalidMessage, 94 | type: 'Literal', 95 | }, 96 | ], 97 | }, 98 | { 99 | code: "npgettext('homepage', 'good', 'bye', 5)", 100 | settings: { poFilename: '__tests__/hello.po' }, 101 | errors: [ 102 | { 103 | message: invalidMessage, 104 | type: 'Literal', 105 | }, 106 | { 107 | message: invalidMessage, 108 | type: 'Literal', 109 | }, 110 | ], 111 | }, 112 | ], 113 | }); 114 | -------------------------------------------------------------------------------- /lib/rules/has-translation-msgid.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const utils = require('../utils'); 4 | 5 | const errorMsg = 'String literal not in .po file.'; 6 | 7 | const i18nMethodMap = { 8 | gettext(context, node) { 9 | utils.checkRequiredArgument(context, node, 1); 10 | 11 | const keyTxt = node.arguments[0]; 12 | if (!utils.isStringLiteral(keyTxt)) { 13 | return false; 14 | } 15 | 16 | if (utils.isRegisteredTranslatable(context, keyTxt.value)) { 17 | return false; 18 | } 19 | 20 | const reportNode = utils.getReportNode(keyTxt, node); 21 | context.report(reportNode, errorMsg); 22 | 23 | return true; 24 | }, 25 | ngettext(context, node) { 26 | utils.checkRequiredArgument(context, node, 3); 27 | 28 | let catchError = false; 29 | const keyTxt = node.arguments[0]; 30 | const pluralTxt = node.arguments[1]; 31 | 32 | if (!utils.isStringLiteral(keyTxt)) { 33 | return false; // This is reported by no-variable-string 34 | } 35 | 36 | if (!utils.isRegisteredTranslatable(context, keyTxt.value)) { 37 | context.report(utils.getReportNode(keyTxt, node), errorMsg); 38 | catchError = true; 39 | } 40 | 41 | if (utils.isStringLiteral(pluralTxt)) { 42 | if (!utils.isRegisteredPlural(context, pluralTxt.value, null, keyTxt.value)) { 43 | context.report(utils.getReportNode(pluralTxt, node), errorMsg); 44 | catchError = true; 45 | } 46 | } 47 | 48 | return catchError; 49 | }, 50 | pgettext(context, node) { 51 | utils.checkRequiredArgument(context, node, 2); 52 | 53 | const ctxTxt = node.arguments[0]; 54 | const keyTxt = node.arguments[1]; 55 | 56 | if (!utils.isStringLiteral(ctxTxt) || !utils.isStringLiteral(keyTxt)) { 57 | return false; // This is reported by no-variable-string 58 | } 59 | 60 | if (utils.isRegisteredTranslatable(context, keyTxt.value, ctxTxt.value)) { 61 | return false; 62 | } 63 | 64 | context.report(utils.getReportNode(keyTxt, node), errorMsg); 65 | return true; 66 | }, 67 | npgettext(context, node) { 68 | utils.checkRequiredArgument(context, node, 4); 69 | 70 | let catchError = false; 71 | const ctxTxt = node.arguments[0]; 72 | const keyTxt = node.arguments[1]; 73 | const pluralTxt = node.arguments[2]; 74 | 75 | if (!utils.isStringLiteral(ctxTxt) || !utils.isStringLiteral(keyTxt)) { 76 | return false; // This is reported by no-variable-string 77 | } 78 | 79 | if (!utils.isRegisteredTranslatable(context, keyTxt.value, ctxTxt.value)) { 80 | context.report(utils.getReportNode(keyTxt, node), errorMsg); 81 | catchError = true; 82 | } 83 | 84 | if (utils.isStringLiteral(pluralTxt)) { 85 | if ( 86 | !utils.isRegisteredPlural( 87 | context, 88 | pluralTxt.value, 89 | ctxTxt.value, 90 | keyTxt.value 91 | ) 92 | ) { 93 | context.report(utils.getReportNode(pluralTxt, node), errorMsg); 94 | catchError = true; 95 | } 96 | } 97 | 98 | return catchError; 99 | }, 100 | }; 101 | 102 | module.exports = { 103 | meta: { 104 | docs: { 105 | description: 106 | 'Should only translate string literal. Variable/Null/Undefined/Number is not allowed', 107 | recommended: false, 108 | }, 109 | fixable: null, 110 | schema: [], 111 | }, 112 | 113 | create(context) { 114 | return { 115 | CallExpression(node) { 116 | Object.keys(i18nMethodMap).find((apiName) => { 117 | if (utils.isI18nAPICall(node, apiName)) { 118 | return i18nMethodMap[apiName](context, node); 119 | } 120 | 121 | return false; 122 | }); 123 | }, 124 | }; 125 | }, 126 | }; 127 | -------------------------------------------------------------------------------- /__tests__/lib/rules/no-variable-string.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const ruleNoVariableString = require('../../../lib/rules/no-variable-string'); 4 | const { RuleTester } = require('eslint'); 5 | 6 | const ruleTester = new RuleTester(); 7 | const invalidMessage = 8 | 'Unexpected argument, gettext function only allows string literals as arguments.'; 9 | 10 | ruleTester.run('no-variable-string', ruleNoVariableString, { 11 | valid: [ 12 | "gettext('hello')", 13 | "i18n.gettext('hello')", 14 | "ngettext('cat', '%d cats', 5)", 15 | "ngettext('cat', '%d cats', count)", 16 | "i18n.ngettext('cat', '%d cats', 5)", 17 | "pgettext('homepage', 'hello')", 18 | "i18n.pgettext('homePage', 'hello')", 19 | "npgettext('homepage', 'cat', '%d cats', 5)", 20 | "i18n.npgettext('homepage', 'cat', '%d cats', 5)", 21 | ], 22 | invalid: [ 23 | { 24 | code: 'gettext()', 25 | errors: [ 26 | { 27 | message: 'Here required 1 arguments, actually get 0 arguments.', 28 | type: 'Identifier', 29 | }, 30 | { 31 | message: invalidMessage, 32 | type: 'Identifier', 33 | }, 34 | ], 35 | }, 36 | { 37 | code: 'gettext(undefined)', 38 | errors: [ 39 | { 40 | message: invalidMessage, 41 | type: 'Identifier', 42 | }, 43 | ], 44 | }, 45 | { 46 | code: 'gettext(null)', 47 | errors: [ 48 | { 49 | message: 'Here should require valid arguments, not null', 50 | type: 'Literal', 51 | }, 52 | { 53 | message: 54 | 'Unexpected argument, gettext function only allows string literals as arguments.', 55 | type: 'Literal', 56 | }, 57 | ], 58 | }, 59 | { 60 | code: 'gettext(123)', 61 | errors: [ 62 | { 63 | message: invalidMessage, 64 | type: 'Literal', 65 | }, 66 | ], 67 | }, 68 | { 69 | code: 'gettext(hello)', 70 | errors: [ 71 | { 72 | message: invalidMessage, 73 | type: 'Identifier', 74 | }, 75 | ], 76 | }, 77 | { 78 | code: "gettext('hello' + 'world')", 79 | errors: [ 80 | { 81 | message: invalidMessage, 82 | type: 'BinaryExpression', 83 | }, 84 | ], 85 | }, 86 | { 87 | code: 'i18n.gettext(hello)', 88 | errors: [ 89 | { 90 | message: invalidMessage, 91 | type: 'Identifier', 92 | }, 93 | ], 94 | }, 95 | { 96 | code: 'ngettext()', 97 | errors: [ 98 | { 99 | message: 'Here required 3 arguments, actually get 0 arguments.', 100 | type: 'Identifier', 101 | }, 102 | { 103 | message: 'Here required 3 arguments, actually get 0 arguments.', 104 | type: 'Identifier', 105 | }, 106 | { 107 | message: 'Here required 3 arguments, actually get 0 arguments.', 108 | type: 'Identifier', 109 | }, 110 | { 111 | message: invalidMessage, 112 | type: 'Identifier', 113 | }, 114 | { 115 | message: invalidMessage, 116 | type: 'Identifier', 117 | }, 118 | ], 119 | }, 120 | { 121 | code: "ngettext('cats', 5)", 122 | errors: [ 123 | { 124 | message: 'Here required 3 arguments, actually get 2 arguments.', 125 | type: 'Identifier', 126 | }, 127 | { 128 | message: invalidMessage, 129 | type: 'Literal', 130 | }, 131 | ], 132 | }, 133 | { 134 | code: 'ngettext(undefined, 123, 5)', 135 | errors: [ 136 | { 137 | message: invalidMessage, 138 | type: 'Identifier', 139 | }, 140 | { 141 | message: invalidMessage, 142 | type: 'Literal', 143 | }, 144 | ], 145 | }, 146 | { 147 | code: "ngettext(null, 'cats', 5)", 148 | errors: [ 149 | { 150 | message: 'Here should require valid arguments, not null', 151 | type: 'Literal', 152 | }, 153 | { 154 | message: invalidMessage, 155 | type: 'Literal', 156 | }, 157 | ], 158 | }, 159 | { 160 | code: "ngettext(cat, 'cats', 5)", 161 | errors: [ 162 | { 163 | message: invalidMessage, 164 | type: 'Identifier', 165 | }, 166 | ], 167 | }, 168 | { 169 | code: "ngettext('cat', cats, 5)", 170 | errors: [ 171 | { 172 | message: invalidMessage, 173 | type: 'Identifier', 174 | }, 175 | ], 176 | }, 177 | { 178 | code: "pgettext(homepage, 'hello')", 179 | errors: [ 180 | { 181 | message: invalidMessage, 182 | type: 'Identifier', 183 | }, 184 | ], 185 | }, 186 | { 187 | code: 'pgettext(123, 456)', 188 | errors: [ 189 | { 190 | message: invalidMessage, 191 | type: 'Literal', 192 | }, 193 | { 194 | message: invalidMessage, 195 | type: 'Literal', 196 | }, 197 | ], 198 | }, 199 | { 200 | code: "pgettext('homepage', hello)", 201 | errors: [ 202 | { 203 | message: invalidMessage, 204 | type: 'Identifier', 205 | }, 206 | ], 207 | }, 208 | { 209 | code: "npgettext(null, 'cat', 'cats', 5)", 210 | errors: [ 211 | { 212 | message: 'Here should require valid arguments, not null', 213 | type: 'Literal', 214 | }, 215 | { 216 | message: invalidMessage, 217 | type: 'Literal', 218 | }, 219 | ], 220 | }, 221 | { 222 | code: "npgettext(undefined, 'cat', 'cats', 5)", 223 | errors: [ 224 | { 225 | message: invalidMessage, 226 | type: 'Identifier', 227 | }, 228 | ], 229 | }, 230 | { 231 | code: "npgettext('my', 'cat', cats, 5)", 232 | errors: [ 233 | { 234 | message: invalidMessage, 235 | type: 'Identifier', 236 | }, 237 | ], 238 | }, 239 | { 240 | code: "npgettext(homepage, 'cat', 'cats', 5)", 241 | errors: [ 242 | { 243 | message: invalidMessage, 244 | type: 'Identifier', 245 | }, 246 | ], 247 | }, 248 | { 249 | code: "npgettext('homepage', cat, '%d cats', 5)", 250 | errors: [ 251 | { 252 | message: invalidMessage, 253 | type: 'Identifier', 254 | }, 255 | ], 256 | }, 257 | ], 258 | }); 259 | --------------------------------------------------------------------------------