Skip to content

Commit

Permalink
[New] jsx-no-literals: Add allowedStrings option
Browse files Browse the repository at this point in the history
Fixes #2377
  • Loading branch information
bhollander-indeed authored and ljharb committed Aug 12, 2019
1 parent 398b7d7 commit 2c6430d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
10 changes: 8 additions & 2 deletions docs/rules/jsx-no-literals.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ var Hello = <div>{'test'}</div>;

### Options

There is only one option:
There are two options:

* `noStrings` - Enforces no string literals used as children, wrapped or unwrapped.
* `allowedStrings` - an array of unique string values that would otherwise warn, but will be ignored.

To use, you can specify like the following:

```js
"react/jsx-no-literals": [<enabled>, {"noStrings": true}]
"react/jsx-no-literals": [<enabled>, {"noStrings": true, "allowedStrings": ["allowed"]}]
```

In this configuration, the following are considered warnings:
Expand All @@ -53,6 +54,11 @@ var Hello = <div><Text {...message} /></div>
var Hello = <div>{translate('my.translation.key')}</div>
```

```jsx
// an allowed string
var Hello = <div>allowed</div>
```

## When Not To Use It

If you do not want to enforce any style JSX literals, then you can disable this rule.
11 changes: 11 additions & 0 deletions lib/rules/jsx-no-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ module.exports = {
properties: {
noStrings: {
type: 'boolean'
},
allowedStrings: {
type: 'array',
uniqueItems: true,
items: {
type: 'string'
}
}
},
additionalProperties: false
Expand All @@ -34,6 +41,7 @@ module.exports = {

create(context) {
const isNoStrings = context.options[0] ? context.options[0].noStrings : false;
const allowedStrings = context.options[0] ? new Set(context.options[0].allowedStrings) : false;

const message = isNoStrings ?
'Strings not allowed in JSX files' :
Expand All @@ -55,6 +63,9 @@ module.exports = {
}

function getValidation(node) {
if (allowedStrings && allowedStrings.has(node.value)) {
return false;
}
const parent = getParentIgnoringBinaryExpressions(node);
const standard = !/^[\s]+$/.test(node.value) &&
typeof node.value === 'string' &&
Expand Down
43 changes: 42 additions & 1 deletion tests/lib/rules/jsx-no-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,36 @@ ruleTester.run('jsx-no-literals', rule, {
}
`,
options: [{noStrings: true}]
}, {
code: `
class Comp1 extends Component {
render() {
return <div>asdf</div>
}
}
`,
options: [{allowedStrings: ['asdf']}]
},
{
code: `
class Comp1 extends Component {
render() {
return <div>&nbsp;</div>
}
}
`,
options: [{noStrings: true, allowedStrings: ['&nbsp;']}]
},
{
code: `
class Comp1 extends Component {
render() {
return <div>foo: {bar}*</div>
}
}
`,
options: [{noStrings: true, allowedStrings: ['foo: ', '*']}]
}

],

invalid: [
Expand Down Expand Up @@ -385,6 +413,19 @@ ruleTester.run('jsx-no-literals', rule, {
{message: stringsMessage('\'foo\'')},
{message: stringsMessage('`bar`')}
]
}, {
code: `
class Comp1 extends Component {
render() {
return <div bar={'foo'}>asdf</div>
}
}
`,
options: [{noStrings: true, allowedStrings: ['asd']}],
errors: [
{message: stringsMessage('\'foo\'')},
{message: stringsMessage('asdf')}
]
}
]
});

0 comments on commit 2c6430d

Please sign in to comment.