Skip to content

Commit

Permalink
feat: allow ignoring lists of components
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Strassel committed Jun 7, 2020
1 parent 0494d81 commit eebb338
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,14 @@ Examples of correct code for the `{ "ignoreProperty": ["foo"] }` option:
/*eslint i18next/no-literal-string: ["error", { "ignoreProperty": ["foo"] }]*/
const a = { foo: 'bar' };
```

#### ignoreComponent

The `ignoreComponent` option specifies exceptions not to check for string literals inside a list of named components. It includes `<Trans>` per default.

Examples of correct code for the `{ "ignoreComponent": ["Icon"] }` option:

```jsx
/*eslint i18next/no-literal-string: ["error", { "ignoreComponent": ["Icon"] }]*/
<Icon>arrow<Icon/>
```
17 changes: 16 additions & 1 deletion lib/rules/no-literal-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ module.exports = {
type: 'string'
}
},
ignoreComponent: {
type: 'array',
items: {
type: 'string'
}
},
markupOnly: {
type: 'boolean'
}
Expand Down Expand Up @@ -114,6 +120,11 @@ module.exports = {
}

// Ignore the Trans component for react-i18next compatibility
let ignoredComponents = ['Trans'];
if (option && option.ignoreComponent)
ignoredComponents = ignoredComponents.concat(option.ignoreComponent);
console.error(ignoredComponents);

function hasValidElementName(node) {
let currentNode = node;
while (
Expand All @@ -122,7 +133,11 @@ module.exports = {
currentNode.parent.openingElement &&
currentNode.parent.openingElement.name
) {
if (currentNode.parent.openingElement.name.name === 'Trans') {
if (
ignoredComponents.includes(
currentNode.parent.openingElement.name.name
)
) {
return true;
}
currentNode = currentNode.parent;
Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-literal-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ ruleTester.run('no-literal-string', rule, {
{ code: '<DIV foo="bar" />', options: [{ ignoreAttribute: ['foo'] }] },
{ code: '<Trans>foo</Trans>' },
{ code: '<Trans><span>bar</span></Trans>' },
{ code: '<Icon>arrow</Icon>', options: [{ ignoreComponent: ['Icon'] }] },
{ code: 'a + "b"', options: [{ markupOnly: true }] },
{ code: '<div>{import("abc")}</div>', options: [{ markupOnly: true }] },
{
Expand Down

0 comments on commit eebb338

Please sign in to comment.