Skip to content

Commit 3195805

Browse files
authored
Merge pull request #1302 from davidyorr/master
Fix false positives in no-literals
2 parents 38a15ea + eb88977 commit 3195805

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

lib/rules/jsx-no-literals.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module.exports = {
4444

4545
function getValidation(node) {
4646
const standard = !/^[\s]+$/.test(node.value) &&
47+
typeof node.value === 'string' &&
4748
node.parent &&
4849
node.parent.type.indexOf('JSX') !== -1 &&
4950
node.parent.type !== 'JSXAttribute';
@@ -63,6 +64,12 @@ module.exports = {
6364
if (getValidation(node)) {
6465
reportLiteralNode(node);
6566
}
67+
},
68+
69+
TemplateLiteral: function(node) {
70+
if (isNoStrings && node.parent.type === 'JSXExpressionContainer') {
71+
reportLiteralNode(node);
72+
}
6673
}
6774

6875
};

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,43 @@ ruleTester.run('jsx-no-literals', rule, {
140140
</Foo>
141141
`,
142142
options: [{noStrings: true}]
143+
}, {
144+
code: '<Foo bar={true} />',
145+
options: [{noStrings: true}]
146+
}, {
147+
code: '<Foo bar={false} />',
148+
options: [{noStrings: true}]
149+
}, {
150+
code: '<Foo bar={100} />',
151+
options: [{noStrings: true}]
152+
}, {
153+
code: '<Foo bar={null} />',
154+
options: [{noStrings: true}]
155+
}, {
156+
code: '<Foo bar={{}} />',
157+
options: [{noStrings: true}]
158+
}, {
159+
code: [
160+
'class Comp1 extends Component {',
161+
' asdf() {}',
162+
' render() {',
163+
' return <Foo bar={this.asdf} />;',
164+
' }',
165+
'}'
166+
].join('\n'),
167+
options: [{noStrings: true}]
168+
}, {
169+
code: [
170+
'class Comp1 extends Component {',
171+
' render() {',
172+
' let foo = `bar`;',
173+
' return <div />;',
174+
' }',
175+
'}'
176+
].join('\n'),
177+
options: [{noStrings: true}]
143178
}
179+
144180
],
145181

146182
invalid: [
@@ -267,6 +303,18 @@ ruleTester.run('jsx-no-literals', rule, {
267303
`,
268304
options: [{noStrings: true}],
269305
errors: [{message: 'Strings not allowed in JSX files'}]
306+
}, {
307+
code: [
308+
'<Foo>',
309+
' {`Test`}',
310+
'</Foo>'
311+
].join('\n'),
312+
options: [{noStrings: true}],
313+
errors: [{message: 'Strings not allowed in JSX files'}]
314+
}, {
315+
code: '<Foo bar={`Test`} />',
316+
options: [{noStrings: true}],
317+
errors: [{message: 'Strings not allowed in JSX files'}]
270318
}
271319
]
272320
});

0 commit comments

Comments
 (0)