Skip to content

Commit e415132

Browse files
jlgonzalezdevljharb
authored andcommitted
[New] jsx-no-literals: print node value in warning message
1 parent c2ae5d3 commit e415132

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

lib/rules/jsx-no-literals.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ module.exports = {
3333

3434
create: function(context) {
3535
const isNoStrings = context.options[0] ? context.options[0].noStrings : false;
36+
const sourceCode = context.getSourceCode();
3637

3738
const message = isNoStrings ?
3839
'Strings not allowed in JSX files' :
@@ -41,7 +42,7 @@ module.exports = {
4142
function reportLiteralNode(node) {
4243
context.report({
4344
node: node,
44-
message: message
45+
message: `${message}: “${sourceCode.getText(node).trim()}”`
4546
});
4647
}
4748

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

+23-23
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ ruleTester.run('jsx-no-literals', rule, {
201201
}
202202
`,
203203
parser: 'babel-eslint',
204-
errors: [{message: 'Missing JSX expression container around literal string'}]
204+
errors: [{message: 'Missing JSX expression container around literal string:test'}]
205205
}, {
206206
code: `
207207
class Comp1 extends Component {
@@ -211,7 +211,7 @@ ruleTester.run('jsx-no-literals', rule, {
211211
}
212212
`,
213213
parser: 'babel-eslint',
214-
errors: [{message: 'Missing JSX expression container around literal string'}]
214+
errors: [{message: 'Missing JSX expression container around literal string:test'}]
215215
}, {
216216
code: `
217217
class Comp1 extends Component {
@@ -222,7 +222,7 @@ ruleTester.run('jsx-no-literals', rule, {
222222
}
223223
`,
224224
parser: 'babel-eslint',
225-
errors: [{message: 'Missing JSX expression container around literal string'}]
225+
errors: [{message: 'Missing JSX expression container around literal string:test'}]
226226
}, {
227227
code: `
228228
class Comp1 extends Component {
@@ -233,7 +233,7 @@ ruleTester.run('jsx-no-literals', rule, {
233233
}
234234
`,
235235
parser: 'babel-eslint',
236-
errors: [{message: 'Missing JSX expression container around literal string'}]
236+
errors: [{message: 'Missing JSX expression container around literal string:test'}]
237237
}, {
238238
code: `
239239
var Hello = createReactClass({
@@ -244,7 +244,7 @@ ruleTester.run('jsx-no-literals', rule, {
244244
});
245245
`,
246246
parser: 'babel-eslint',
247-
errors: [{message: 'Missing JSX expression container around literal string'}]
247+
errors: [{message: 'Missing JSX expression container around literal string:hello'}]
248248
}, {
249249
code: `
250250
class Comp1 extends Component {
@@ -258,7 +258,7 @@ ruleTester.run('jsx-no-literals', rule, {
258258
}
259259
`,
260260
parser: 'babel-eslint',
261-
errors: [{message: 'Missing JSX expression container around literal string'}]
261+
errors: [{message: 'Missing JSX expression container around literal string:asdjfl'}]
262262
}, {
263263
code: `
264264
class Comp1 extends Component {
@@ -274,7 +274,7 @@ ruleTester.run('jsx-no-literals', rule, {
274274
}
275275
`,
276276
parser: 'babel-eslint',
277-
errors: [{message: 'Missing JSX expression container around literal string'}]
277+
errors: [{message: 'Missing JSX expression container around literal string:asdjfl\n test\n foo'}]
278278
}, {
279279
code: `
280280
class Comp1 extends Component {
@@ -290,7 +290,7 @@ ruleTester.run('jsx-no-literals', rule, {
290290
}
291291
`,
292292
parser: 'babel-eslint',
293-
errors: [{message: 'Missing JSX expression container around literal string'}]
293+
errors: [{message: 'Missing JSX expression container around literal string:test'}]
294294
}, {
295295
code: `
296296
<Foo bar="test">
@@ -299,23 +299,23 @@ ruleTester.run('jsx-no-literals', rule, {
299299
`,
300300
parser: 'babel-eslint',
301301
options: [{noStrings: true}],
302-
errors: [{message: 'Strings not allowed in JSX files'}]
302+
errors: [{message: 'Strings not allowed in JSX files:\'Test\''}]
303303
}, {
304304
code: `
305305
<Foo bar="test">
306306
{'Test'}
307307
</Foo>
308308
`,
309309
options: [{noStrings: true}],
310-
errors: [{message: 'Strings not allowed in JSX files'}]
310+
errors: [{message: 'Strings not allowed in JSX files:\'Test\''}]
311311
}, {
312312
code: `
313313
<Foo bar="test">
314314
{'Test' + name}
315315
</Foo>
316316
`,
317317
options: [{noStrings: true}],
318-
errors: [{message: 'Strings not allowed in JSX files'}]
318+
errors: [{message: 'Strings not allowed in JSX files:\'Test\''}]
319319
}, {
320320
code: `
321321
<Foo bar="test">
@@ -324,55 +324,55 @@ ruleTester.run('jsx-no-literals', rule, {
324324
`,
325325
parser: 'babel-eslint',
326326
options: [{noStrings: true}],
327-
errors: [{message: 'Strings not allowed in JSX files'}]
327+
errors: [{message: 'Strings not allowed in JSX files:Test'}]
328328
}, {
329329
code: `
330330
<Foo bar="test">
331331
Test
332332
</Foo>
333333
`,
334334
options: [{noStrings: true}],
335-
errors: [{message: 'Strings not allowed in JSX files'}]
335+
errors: [{message: 'Strings not allowed in JSX files:Test'}]
336336
}, {
337337
code: `
338338
<Foo>
339339
{\`Test\`}
340340
</Foo>
341341
`,
342342
options: [{noStrings: true}],
343-
errors: [{message: 'Strings not allowed in JSX files'}]
343+
errors: [{message: 'Strings not allowed in JSX files:`Test`'}]
344344
}, {
345345
code: '<Foo bar={`Test`} />',
346346
options: [{noStrings: true}],
347-
errors: [{message: 'Strings not allowed in JSX files'}]
347+
errors: [{message: 'Strings not allowed in JSX files:`Test`'}]
348348
}, {
349349
code: '<Foo bar={`${baz}`} />',
350350
options: [{noStrings: true}],
351-
errors: [{message: 'Strings not allowed in JSX files'}]
351+
errors: [{message: 'Strings not allowed in JSX files:`${baz}`'}]
352352
}, {
353353
code: '<Foo bar={`Test ${baz}`} />',
354354
options: [{noStrings: true}],
355-
errors: [{message: 'Strings not allowed in JSX files'}]
355+
errors: [{message: 'Strings not allowed in JSX files:`Test ${baz}`'}]
356356
}, {
357357
code: '<Foo bar={`foo` + \'bar\'} />',
358358
options: [{noStrings: true}],
359359
errors: [
360-
{message: 'Strings not allowed in JSX files'},
361-
{message: 'Strings not allowed in JSX files'}
360+
{message: 'Strings not allowed in JSX files:`foo`'},
361+
{message: 'Strings not allowed in JSX files:\'bar\''}
362362
]
363363
}, {
364364
code: '<Foo bar={`foo` + `bar`} />',
365365
options: [{noStrings: true}],
366366
errors: [
367-
{message: 'Strings not allowed in JSX files'},
368-
{message: 'Strings not allowed in JSX files'}
367+
{message: 'Strings not allowed in JSX files:`foo`'},
368+
{message: 'Strings not allowed in JSX files:`bar`'}
369369
]
370370
}, {
371371
code: '<Foo bar={\'foo\' + `bar`} />',
372372
options: [{noStrings: true}],
373373
errors: [
374-
{message: 'Strings not allowed in JSX files'},
375-
{message: 'Strings not allowed in JSX files'}
374+
{message: 'Strings not allowed in JSX files:\'foo\''},
375+
{message: 'Strings not allowed in JSX files:`bar`'}
376376
]
377377
}
378378
]

0 commit comments

Comments
 (0)