Skip to content

Commit de268ec

Browse files
karolina-benitezljharb
authored andcommitted
[Fix] jsx-no-literals with allowStrings doesn't work in props
Fixes #2720
1 parent 9abc71d commit de268ec

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

lib/rules/jsx-no-literals.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ module.exports = {
120120
},
121121

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

125125
if (config.noStrings && !config.ignoreProps && isNodeValueString) {
126126
const customMessage = 'Invalid prop value';

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

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,63 @@ function invalidProp(str) {
4242
const ruleTester = new RuleTester({parserOptions});
4343
ruleTester.run('jsx-no-literals', rule, {
4444

45-
valid: [
45+
valid: [].concat(
4646
{
47+
code: `
48+
class Comp1 extends Component {
49+
render() {
50+
return (
51+
<div>
52+
<button type="button"></button>
53+
</div>
54+
);
55+
}
56+
}
57+
`,
58+
options: [{noStrings: true, allowedStrings: ['button', 'submit']}]
59+
}, {
60+
code: `
61+
class Comp1 extends Component {
62+
render() {
63+
return (
64+
<div>
65+
<button type="button"></button>
66+
</div>
67+
);
68+
}
69+
}
70+
`,
71+
options: [{noStrings: true, allowedStrings: ['button', 'submit']}],
72+
parser: parsers.BABEL_ESLINT
73+
}, parsers.TS([{
74+
code: `
75+
class Comp1 extends Component {
76+
render() {
77+
return (
78+
<div>
79+
<button type="button"></button>
80+
</div>
81+
);
82+
}
83+
}
84+
`,
85+
options: [{noStrings: true, allowedStrings: ['button', 'submit']}],
86+
parser: parsers.TYPESCRIPT_ESLINT
87+
}, {
88+
code: `
89+
class Comp1 extends Component {
90+
render() {
91+
return (
92+
<div>
93+
<button type="button"></button>
94+
</div>
95+
);
96+
}
97+
}
98+
`,
99+
options: [{noStrings: true, allowedStrings: ['button', 'submit']}],
100+
parser: parsers['@TYPESCRIPT_ESLINT']
101+
}]), {
47102
code: `
48103
class Comp1 extends Component {
49104
render() {
@@ -277,7 +332,7 @@ ruleTester.run('jsx-no-literals', rule, {
277332
parser: parsers.BABEL_ESLINT,
278333
options: [{noStrings: true, ignoreProps: false}]
279334
}
280-
],
335+
),
281336

282337
invalid: [
283338
{

0 commit comments

Comments
 (0)