Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] jsx-no-literals with allowStrings doesn't work in props #2736

Merged
merged 1 commit into from Aug 9, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/rules/jsx-no-literals.js
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's also add the same test case for each of the other parsers (default, and both typescript-eslints)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please clarify?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case works for babel-eslint; but the same test case should pass on the default parser, and on the old typescript-eslint, and on the new typescript eslint parser.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, added!

}, 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