Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add allowed strings, jsx-no-literals #2380

Merged
merged 1 commit into from Aug 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions docs/rules/jsx-no-literals.md
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
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
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')}
]
}
]
});