Skip to content

Commit

Permalink
Merge pull request #7 from 7ixi0/master
Browse files Browse the repository at this point in the history
feat: add ignoreAttribute option
  • Loading branch information
edvardchen authored Sep 25, 2019
2 parents 2fa1aa6 + 9bc25df commit 3550d44
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,14 @@ Examples of correct code for the `{ "ignoreCallee": ["foo"] }` option:
/*eslint i18next/no-literal-string: ["error", { "ignoreCallee": ["foo"] }]*/
const bar = foo('bar');
```

#### ignoreAttribute

The `ignoreAttribute` option specifies exceptions not to check for JSX attributes that match one of ignored attributes.

Examples of correct code for the `{ "ignoreAttribute": ["foo"] }` option:

```jsx
/*eslint i18next/no-literal-string: ["error", { "ignoreAttribute": ["foo"] }]*/
const element = <div foo="bar" />;
```
11 changes: 10 additions & 1 deletion lib/rules/no-literal-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ module.exports = {
items: {
type: 'string'
}
},
ignoreAttribute: {
type: 'array',
items: {
type: 'string'
}
}
},
additionalProperties: false
Expand Down Expand Up @@ -76,10 +82,13 @@ module.exports = {
return calleeWhitelists.simple.indexOf(calleeName) !== -1;
}

const ignoredAttributes = (option && option.ignoreAttribute) || [];
const atts = [
'className', 'style', 'styleName', 'src', 'type', 'id',
// SVG related attributes.
'viewBox', 'cx', 'cy', 'r', 'rx', 'ry', 'd', 'x1', 'y1', 'x2', 'y2', 'fill', 'stroke', 'strokeWidth', 'points'
'viewBox', 'cx', 'cy', 'r', 'rx', 'ry', 'd', 'x1', 'y1', 'x2', 'y2', 'fill', 'stroke', 'strokeWidth', 'points',

...ignoredAttributes
];
function isValidAttrName(name) {
return atts.includes(name);
Expand Down
6 changes: 4 additions & 2 deletions tests/lib/rules/no-literal-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ ruleTester.run('no-literal-string', rule, {
{ code: '<svg viewBox="0 0 20 40"></svg>' },
{ code: '<line x1="0" y1="0" x2="10" y2="20" />' },
{ code: '<path d="M10 10" />' },
{ code: '<circle cx="10" cy="10" r="2" fill="red" />' }
{ code: '<circle cx="10" cy="10" r="2" fill="red" />' },
{ code: '<div foo="bar" />', options: [{ ignoreAttribute: ['foo'] }] }
],

invalid: [
Expand All @@ -78,7 +79,8 @@ ruleTester.run('no-literal-string', rule, {
{ code: 'const a = "afoo";', options: [{ ignore: ['^foo'] }], errors },
// JSX
{ code: '<div>foo</div>', errors },
{ code: '<div>FOO</div>', errors }
{ code: '<div>FOO</div>', errors },
{ code: '<div foo="bar" />', errors }
]
});

Expand Down

0 comments on commit 3550d44

Please sign in to comment.