Skip to content

Commit

Permalink
Add allowed strings, jsx-no-literal fixes #2377
Browse files Browse the repository at this point in the history
  • Loading branch information
bhollander-indeed committed Aug 12, 2019
1 parent 1aab93d commit 41bf386
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/rules/jsx-no-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ module.exports = {
properties: {
noStrings: {
type: 'boolean'
},
allowedStrings: {
type: 'array'
}
},
additionalProperties: false
Expand All @@ -34,6 +37,7 @@ module.exports = {

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

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

function getValidation(node) {
if (allowedStrings && allowedStrings.indexOf(node.value) > -1) {
return false;
}
const parent = getParentIgnoringBinaryExpressions(node);
const standard = !/^[\s]+$/.test(node.value) &&
typeof node.value === 'string' &&
Expand Down
45 changes: 44 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,21 @@ ruleTester.run('jsx-no-literals', rule, {
{message: stringsMessage('\'foo\'')},
{message: stringsMessage('`bar`')}
]
}, {
/* eslint-disable no-useless-escape */
code: `
class Comp1 extends Component {
render() {
return <div bar={\'foo\'}>asdf</div>
}
}
`,
/* eslint-enable no-useless-escape */
options: [{noStrings: true, allowedStrings: ['asd']}],
errors: [
{message: stringsMessage('\'foo\'')},
{message: stringsMessage('asdf')}
]
}
]
});

0 comments on commit 41bf386

Please sign in to comment.