Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')}
]
}
]
});