Skip to content

Commit

Permalink
[Fix] jsx-no-literals with allowStrings doesn't work in props
Browse files Browse the repository at this point in the history
Fixes #2720
  • Loading branch information
karolina-benitez authored and ljharb committed Jul 31, 2020
1 parent 9abc71d commit de268ec
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/rules/jsx-no-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ module.exports = {
},

JSXAttribute(node) {
const isNodeValueString = node.value && node.value && node.value.type === 'Literal' && typeof node.value.value === 'string';
const isNodeValueString = node && node.value && node.value.type === 'Literal' && typeof node.value.value === 'string' && !config.allowedStrings.has(node.value.value);

if (config.noStrings && !config.ignoreProps && isNodeValueString) {
const customMessage = 'Invalid prop value';
Expand Down
59 changes: 57 additions & 2 deletions tests/lib/rules/jsx-no-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,63 @@ function invalidProp(str) {
const ruleTester = new RuleTester({parserOptions});
ruleTester.run('jsx-no-literals', rule, {

valid: [
valid: [].concat(
{
code: `
class Comp1 extends Component {
render() {
return (
<div>
<button type="button"></button>
</div>
);
}
}
`,
options: [{noStrings: true, allowedStrings: ['button', 'submit']}]
}, {
code: `
class Comp1 extends Component {
render() {
return (
<div>
<button type="button"></button>
</div>
);
}
}
`,
options: [{noStrings: true, allowedStrings: ['button', 'submit']}],
parser: parsers.BABEL_ESLINT
}, parsers.TS([{
code: `
class Comp1 extends Component {
render() {
return (
<div>
<button type="button"></button>
</div>
);
}
}
`,
options: [{noStrings: true, allowedStrings: ['button', 'submit']}],
parser: parsers.TYPESCRIPT_ESLINT
}, {
code: `
class Comp1 extends Component {
render() {
return (
<div>
<button type="button"></button>
</div>
);
}
}
`,
options: [{noStrings: true, allowedStrings: ['button', 'submit']}],
parser: parsers['@TYPESCRIPT_ESLINT']
}]), {
code: `
class Comp1 extends Component {
render() {
Expand Down Expand Up @@ -277,7 +332,7 @@ ruleTester.run('jsx-no-literals', rule, {
parser: parsers.BABEL_ESLINT,
options: [{noStrings: true, ignoreProps: false}]
}
],
),

invalid: [
{
Expand Down

0 comments on commit de268ec

Please sign in to comment.