Repository: yannickcr/eslint-plugin-react (js)

Issue:
Printing Node value to the report
Hi guys, this is not a bug, is more like a new feature that I needed for a project I'm working on.

Basically I needed to detect and print to the report the hardcoded text found in a bunch of JSX templates so I did some tweaks to the jsx-no-literals rule to be able to print the node value.

I'm just leaving this here in case somebody else might need something like that, I'll be creating a PR for this. :)
 

Pull request description:
Adding option to print node value
Basically I did some tweaks to the jsx-no-literals rule to be able to print the node value. :)

Fixes #2007.

Patch:
diff --git a/lib/rules/jsx-no-literals.js b/lib/rules/jsx-no-literals.js
index d421e474..d6bff3ad 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()}”`
       });
     }
 


Test patch:
diff --git a/tests/lib/rules/jsx-no-literals.js b/tests/lib/rules/jsx-no-literals.js
index 7edac905..b816a4da 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: `
         <Foo bar="test">
@@ -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: `
         <Foo bar="test">
@@ -307,7 +315,7 @@ ruleTester.run('jsx-no-literals', rule, {
         </Foo>
       `,
       options: [{noStrings: true}],
-      errors: [{message: 'Strings not allowed in JSX files'}]
+      errors: [{message: stringsMessage('\'Test\'')}]
     }, {
       code: `
         <Foo bar="test">
@@ -315,7 +323,7 @@ ruleTester.run('jsx-no-literals', rule, {
         </Foo>
       `,
       options: [{noStrings: true}],
-      errors: [{message: 'Strings not allowed in JSX files'}]
+      errors: [{message: stringsMessage('\'Test\'')}]
     }, {
       code: `
         <Foo bar="test">
@@ -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: `
         <Foo bar="test">
@@ -332,7 +340,7 @@ ruleTester.run('jsx-no-literals', rule, {
         </Foo>
       `,
       options: [{noStrings: true}],
-      errors: [{message: 'Strings not allowed in JSX files'}]
+      errors: [{message: stringsMessage('Test')}]
     }, {
       code: `
         <Foo>
@@ -340,39 +348,39 @@ ruleTester.run('jsx-no-literals', rule, {
         </Foo>
       `,
       options: [{noStrings: true}],
-      errors: [{message: 'Strings not allowed in JSX files'}]
+      errors: [{message: stringsMessage('`Test`')}]
     }, {
       code: '<Foo bar={`Test`} />',
       options: [{noStrings: true}],
-      errors: [{message: 'Strings not allowed in JSX files'}]
+      errors: [{message: stringsMessage('`Test`')}]
     }, {
       code: '<Foo bar={`${baz}`} />',
       options: [{noStrings: true}],
-      errors: [{message: 'Strings not allowed in JSX files'}]
+      errors: [{message: stringsMessage('`${baz}`')}]
     }, {
       code: '<Foo bar={`Test ${baz}`} />',
       options: [{noStrings: true}],
-      errors: [{message: 'Strings not allowed in JSX files'}]
+      errors: [{message: stringsMessage('`Test ${baz}`')}]
     }, {
       code: '<Foo bar={`foo` + \'bar\'} />',
       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: '<Foo bar={`foo` + `bar`} />',
       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: '<Foo bar={\'foo\' + `bar`} />',
       options: [{noStrings: true}],
       errors: [
-        {message: 'Strings not allowed in JSX files'},
-        {message: 'Strings not allowed in JSX files'}
+        {message: stringsMessage('\'foo\'')},
+        {message: stringsMessage('`bar`')}
       ]
     }
   ]