Skip to content

Commit 17116a0

Browse files
davidyorrljharb
authored andcommitted
Check template literals in no-literals
1 parent cf2edd9 commit 17116a0

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

lib/rules/jsx-no-literals.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ module.exports = {
6464
if (getValidation(node)) {
6565
reportLiteralNode(node);
6666
}
67+
},
68+
69+
TemplateLiteral: function(node) {
70+
if (isNoStrings && node.parent.type === 'JSXExpressionContainer') {
71+
reportLiteralNode(node);
72+
}
6773
}
6874

6975
};

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,36 @@ ruleTester.run('jsx-no-literals', rule, {
138138
code: '<Foo bar={100} />',
139139
parser: 'babel-eslint',
140140
options: [{noStrings: true}]
141+
}, {
142+
code: '<Foo bar={null} />',
143+
parser: 'babel-eslint',
144+
options: [{noStrings: true}]
145+
}, {
146+
code: '<Foo bar={{}} />',
147+
parser: 'babel-eslint',
148+
options: [{noStrings: true}]
149+
}, {
150+
code: [
151+
'class Comp1 extends Component {',
152+
' asdf() {}',
153+
' render() {',
154+
' return <Foo bar={this.asdf} />;',
155+
' }',
156+
'}'
157+
].join('\n'),
158+
parser: 'babel-eslint',
159+
options: [{noStrings: true}]
160+
}, {
161+
code: [
162+
'class Comp1 extends Component {',
163+
' render() {',
164+
' let foo = `bar`;',
165+
' return <div />;',
166+
' }',
167+
'}'
168+
].join('\n'),
169+
parser: 'babel-eslint',
170+
options: [{noStrings: true}]
141171
}, {
142172
code: `
143173
<Foo bar="test">
@@ -161,6 +191,32 @@ ruleTester.run('jsx-no-literals', rule, {
161191
}, {
162192
code: '<Foo bar={100} />',
163193
options: [{noStrings: true}]
194+
}, {
195+
code: '<Foo bar={null} />',
196+
options: [{noStrings: true}]
197+
}, {
198+
code: '<Foo bar={{}} />',
199+
options: [{noStrings: true}]
200+
}, {
201+
code: [
202+
'class Comp1 extends Component {',
203+
' asdf() {}',
204+
' render() {',
205+
' return <Foo bar={this.asdf} />;',
206+
' }',
207+
'}'
208+
].join('\n'),
209+
options: [{noStrings: true}]
210+
}, {
211+
code: [
212+
'class Comp1 extends Component {',
213+
' render() {',
214+
' let foo = `bar`;',
215+
' return <div />;',
216+
' }',
217+
'}'
218+
].join('\n'),
219+
options: [{noStrings: true}]
164220
}
165221

166222
],
@@ -289,6 +345,32 @@ ruleTester.run('jsx-no-literals', rule, {
289345
`,
290346
options: [{noStrings: true}],
291347
errors: [{message: 'Strings not allowed in JSX files'}]
348+
}, {
349+
code: [
350+
'<Foo>',
351+
' {`Test`}',
352+
'</Foo>'
353+
].join('\n'),
354+
parser: 'babel-eslint',
355+
options: [{noStrings: true}],
356+
errors: [{message: 'Strings not allowed in JSX files'}]
357+
}, {
358+
code: [
359+
'<Foo>',
360+
' {`Test`}',
361+
'</Foo>'
362+
].join('\n'),
363+
options: [{noStrings: true}],
364+
errors: [{message: 'Strings not allowed in JSX files'}]
365+
}, {
366+
code: '<Foo bar={`Test`} />',
367+
parser: 'babel-eslint',
368+
options: [{noStrings: true}],
369+
errors: [{message: 'Strings not allowed in JSX files'}]
370+
}, {
371+
code: '<Foo bar={`Test`} />',
372+
options: [{noStrings: true}],
373+
errors: [{message: 'Strings not allowed in JSX files'}]
292374
}
293375
]
294376
});

0 commit comments

Comments
 (0)