Skip to content

Commit 5f78866

Browse files
Add allowed strings, jsx-no-literals
1 parent 1aab93d commit 5f78866

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

lib/rules/jsx-no-literals.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ module.exports = {
2626
properties: {
2727
noStrings: {
2828
type: 'boolean'
29+
},
30+
allowedStrings: {
31+
type: 'array'
2932
}
3033
},
3134
additionalProperties: false
@@ -34,6 +37,7 @@ module.exports = {
3437

3538
create(context) {
3639
const isNoStrings = context.options[0] ? context.options[0].noStrings : false;
40+
const allowedStrings = context.options[0] ? context.options[0].allowedStrings : false;
3741

3842
const message = isNoStrings ?
3943
'Strings not allowed in JSX files' :
@@ -55,6 +59,9 @@ module.exports = {
5559
}
5660

5761
function getValidation(node) {
62+
if (allowedStrings && allowedStrings.indexOf(node.value) > -1) {
63+
return false;
64+
}
5865
const parent = getParentIgnoringBinaryExpressions(node);
5966
const standard = !/^[\s]+$/.test(node.value) &&
6067
typeof node.value === 'string' &&

tests/lib/rules/jsx-no-literals.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,36 @@ ruleTester.run('jsx-no-literals', rule, {
198198
}
199199
`,
200200
options: [{noStrings: true}]
201+
}, {
202+
code: `
203+
class Comp1 extends Component {
204+
render() {
205+
return <div>asdf</div>
206+
}
207+
}
208+
`,
209+
options: [{allowedStrings: ['asdf']}]
210+
},
211+
{
212+
code: `
213+
class Comp1 extends Component {
214+
render() {
215+
return <div>&nbsp;</div>
216+
}
217+
}
218+
`,
219+
options: [{noStrings: true, allowedStrings: ['&nbsp;']}]
220+
},
221+
{
222+
code: `
223+
class Comp1 extends Component {
224+
render() {
225+
return <div>foo: {bar}*</div>
226+
}
227+
}
228+
`,
229+
options: [{noStrings: true, allowedStrings: ['foo: ', '*']}]
201230
}
202-
203231
],
204232

205233
invalid: [
@@ -385,6 +413,21 @@ ruleTester.run('jsx-no-literals', rule, {
385413
{message: stringsMessage('\'foo\'')},
386414
{message: stringsMessage('`bar`')}
387415
]
416+
}, {
417+
/* eslint-disable no-useless-escape */
418+
code: `
419+
class Comp1 extends Component {
420+
render() {
421+
return <div bar={\'foo\'}>asdf</div>
422+
}
423+
}
424+
`,
425+
/* eslint-enable no-useless-escape */
426+
options: [{noStrings: true, allowedStrings: ['asd']}],
427+
errors: [
428+
{message: stringsMessage('\'foo\'')},
429+
{message: stringsMessage('asdf')}
430+
]
388431
}
389432
]
390433
});

0 commit comments

Comments
 (0)