From eafd19ded6ebb34b35e56830b0f4363ac4414910 Mon Sep 17 00:00:00 2001 From: Jose Gonzalez Date: Fri, 5 Oct 2018 12:38:29 -0400 Subject: [PATCH] [New] `jsx-no-literals`: print node value in warning message --- lib/rules/jsx-no-literals.js | 3 +- tests/lib/rules/jsx-no-literals.js | 54 +++++++++++++++++------------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/lib/rules/jsx-no-literals.js b/lib/rules/jsx-no-literals.js index d421e4748b..d6bff3ad00 100644 --- a/lib/rules/jsx-no-literals.js +++ b/lib/rules/jsx-no-literals.js @@ -33,6 +33,7 @@ module.exports = { create: function(context) { const isNoStrings = context.options[0] ? context.options[0].noStrings : false; + const sourceCode = context.getSourceCode(); const message = isNoStrings ? 'Strings not allowed in JSX files' : @@ -41,7 +42,7 @@ module.exports = { function reportLiteralNode(node) { context.report({ node: node, - message: message + message: `${message}: “${sourceCode.getText(node).trim()}”` }); } diff --git a/tests/lib/rules/jsx-no-literals.js b/tests/lib/rules/jsx-no-literals.js index 7edac90594..b816a4da3e 100644 --- a/tests/lib/rules/jsx-no-literals.js +++ b/tests/lib/rules/jsx-no-literals.js @@ -24,6 +24,14 @@ const parserOptions = { // Tests // ------------------------------------------------------------------------------ +function stringsMessage(str) { + return `Strings not allowed in JSX files: “${str}”`; +} + +function jsxMessage(str) { + return `Missing JSX expression container around literal string: “${str}”`; +} + const ruleTester = new RuleTester({parserOptions}); ruleTester.run('jsx-no-literals', rule, { @@ -201,7 +209,7 @@ ruleTester.run('jsx-no-literals', rule, { } `, parser: 'babel-eslint', - errors: [{message: 'Missing JSX expression container around literal string'}] + errors: [{message: jsxMessage('test')}] }, { code: ` class Comp1 extends Component { @@ -211,7 +219,7 @@ ruleTester.run('jsx-no-literals', rule, { } `, parser: 'babel-eslint', - errors: [{message: 'Missing JSX expression container around literal string'}] + errors: [{message: jsxMessage('test')}] }, { code: ` class Comp1 extends Component { @@ -222,7 +230,7 @@ ruleTester.run('jsx-no-literals', rule, { } `, parser: 'babel-eslint', - errors: [{message: 'Missing JSX expression container around literal string'}] + errors: [{message: jsxMessage('test')}] }, { code: ` class Comp1 extends Component { @@ -233,7 +241,7 @@ ruleTester.run('jsx-no-literals', rule, { } `, parser: 'babel-eslint', - errors: [{message: 'Missing JSX expression container around literal string'}] + errors: [{message: jsxMessage('test')}] }, { code: ` var Hello = createReactClass({ @@ -244,7 +252,7 @@ ruleTester.run('jsx-no-literals', rule, { }); `, parser: 'babel-eslint', - errors: [{message: 'Missing JSX expression container around literal string'}] + errors: [{message: jsxMessage('hello')}] }, { code: ` class Comp1 extends Component { @@ -258,7 +266,7 @@ ruleTester.run('jsx-no-literals', rule, { } `, parser: 'babel-eslint', - errors: [{message: 'Missing JSX expression container around literal string'}] + errors: [{message: jsxMessage('asdjfl')}] }, { code: ` class Comp1 extends Component { @@ -274,7 +282,7 @@ ruleTester.run('jsx-no-literals', rule, { } `, parser: 'babel-eslint', - errors: [{message: 'Missing JSX expression container around literal string'}] + errors: [{message: jsxMessage('asdjfl\n test\n foo')}] }, { code: ` class Comp1 extends Component { @@ -290,7 +298,7 @@ ruleTester.run('jsx-no-literals', rule, { } `, parser: 'babel-eslint', - errors: [{message: 'Missing JSX expression container around literal string'}] + errors: [{message: jsxMessage('test')}] }, { code: ` @@ -299,7 +307,7 @@ ruleTester.run('jsx-no-literals', rule, { `, parser: 'babel-eslint', options: [{noStrings: true}], - errors: [{message: 'Strings not allowed in JSX files'}] + errors: [{message: stringsMessage('\'Test\'')}] }, { code: ` @@ -307,7 +315,7 @@ ruleTester.run('jsx-no-literals', rule, { `, options: [{noStrings: true}], - errors: [{message: 'Strings not allowed in JSX files'}] + errors: [{message: stringsMessage('\'Test\'')}] }, { code: ` @@ -315,7 +323,7 @@ ruleTester.run('jsx-no-literals', rule, { `, options: [{noStrings: true}], - errors: [{message: 'Strings not allowed in JSX files'}] + errors: [{message: stringsMessage('\'Test\'')}] }, { code: ` @@ -324,7 +332,7 @@ ruleTester.run('jsx-no-literals', rule, { `, parser: 'babel-eslint', options: [{noStrings: true}], - errors: [{message: 'Strings not allowed in JSX files'}] + errors: [{message: stringsMessage('Test')}] }, { code: ` @@ -332,7 +340,7 @@ ruleTester.run('jsx-no-literals', rule, { `, options: [{noStrings: true}], - errors: [{message: 'Strings not allowed in JSX files'}] + errors: [{message: stringsMessage('Test')}] }, { code: ` @@ -340,39 +348,39 @@ ruleTester.run('jsx-no-literals', rule, { `, options: [{noStrings: true}], - errors: [{message: 'Strings not allowed in JSX files'}] + errors: [{message: stringsMessage('`Test`')}] }, { code: '', options: [{noStrings: true}], - errors: [{message: 'Strings not allowed in JSX files'}] + errors: [{message: stringsMessage('`Test`')}] }, { code: '', options: [{noStrings: true}], - errors: [{message: 'Strings not allowed in JSX files'}] + errors: [{message: stringsMessage('`${baz}`')}] }, { code: '', options: [{noStrings: true}], - errors: [{message: 'Strings not allowed in JSX files'}] + errors: [{message: stringsMessage('`Test ${baz}`')}] }, { code: '', options: [{noStrings: true}], errors: [ - {message: 'Strings not allowed in JSX files'}, - {message: 'Strings not allowed in JSX files'} + {message: stringsMessage('`foo`')}, + {message: stringsMessage('\'bar\'')} ] }, { code: '', options: [{noStrings: true}], errors: [ - {message: 'Strings not allowed in JSX files'}, - {message: 'Strings not allowed in JSX files'} + {message: stringsMessage('`foo`')}, + {message: stringsMessage('`bar`')} ] }, { code: '', options: [{noStrings: true}], errors: [ - {message: 'Strings not allowed in JSX files'}, - {message: 'Strings not allowed in JSX files'} + {message: stringsMessage('\'foo\'')}, + {message: stringsMessage('`bar`')} ] } ]