diff --git a/README.md b/README.md index 0d20af6..0423676 100644 --- a/README.md +++ b/README.md @@ -236,3 +236,19 @@ Examples of correct code for the `{ "ignoreAttribute": ["foo"] }` option: /*eslint i18next/no-literal-string: ["error", { "ignoreAttribute": ["foo"] }]*/ const element =
; ``` + +#### ignoreElement + +The `ignoreElement` option specifies exceptions not to check for JSX elements that match one of ignored elements. This includes strings within children of these elements. + +Examples of correct code for the `{ "ignoreElement": ["foo"] }` option: + +```jsx +/*eslint i18next/no-literal-string: ["error", { "ignoreElement": ["foo"] }]*/ +const element = a; +const element2 = ( + + b + +); +``` diff --git a/lib/rules/no-literal-string.js b/lib/rules/no-literal-string.js index d57b9f4..bb85d77 100644 --- a/lib/rules/no-literal-string.js +++ b/lib/rules/no-literal-string.js @@ -28,13 +28,19 @@ module.exports = { type: 'string' } }, + ignoreAttribute: { + type: 'array', + items: { + type: 'string' + } + }, ignoreCallee: { type: 'array', items: { type: 'string' } }, - ignoreAttribute: { + ignoreElement: { type: 'array', items: { type: 'string' @@ -100,6 +106,25 @@ module.exports = { return userJSXAttrs.includes(name); } + const ignoredElements = (option && option.ignoreElement) || []; + const userElements = ['Trans', ...ignoredElements]; + function hasValidElementName(node) { + let currentNode = node; + while ( + currentNode.parent && + currentNode.parent.openingElement && + currentNode.parent.openingElement.name + ) { + if ( + userElements.includes(currentNode.parent.openingElement.name.name) + ) { + return true; + } + currentNode = currentNode.parent; + } + return false; + } + //---------------------------------------------------------------------- // Public //---------------------------------------------------------------------- @@ -176,6 +201,10 @@ module.exports = { const trimed = node.value.trim(); visited.add(node); + if (hasValidElementName(node)) { + return; + } + if (!trimed || match(trimed)) { return; } diff --git a/tests/lib/rules/no-literal-string.js b/tests/lib/rules/no-literal-string.js index a986e78..205791b 100644 --- a/tests/lib/rules/no-literal-string.js +++ b/tests/lib/rules/no-literal-string.js @@ -90,7 +90,12 @@ ruleTester.run('no-literal-string', rule, { { code: '
' }, { code: '' }, { code: '