Skip to content

Commit

Permalink
[New] jsx-no-literals: print node value in warning message
Browse files Browse the repository at this point in the history
  • Loading branch information
jlgonzalezdev authored and ljharb committed Oct 5, 2018
1 parent c2ae5d3 commit e415132
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
3 changes: 2 additions & 1 deletion lib/rules/jsx-no-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' :
Expand All @@ -41,7 +42,7 @@ module.exports = {
function reportLiteralNode(node) {
context.report({
node: node,
message: message
message: `${message}: “${sourceCode.getText(node).trim()}”`
});
}

Expand Down
46 changes: 23 additions & 23 deletions tests/lib/rules/jsx-no-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ ruleTester.run('jsx-no-literals', rule, {
}
`,
parser: 'babel-eslint',
errors: [{message: 'Missing JSX expression container around literal string'}]
errors: [{message: 'Missing JSX expression container around literal string:test'}]
}, {
code: `
class Comp1 extends Component {
Expand All @@ -211,7 +211,7 @@ ruleTester.run('jsx-no-literals', rule, {
}
`,
parser: 'babel-eslint',
errors: [{message: 'Missing JSX expression container around literal string'}]
errors: [{message: 'Missing JSX expression container around literal string:test'}]
}, {
code: `
class Comp1 extends Component {
Expand All @@ -222,7 +222,7 @@ ruleTester.run('jsx-no-literals', rule, {
}
`,
parser: 'babel-eslint',
errors: [{message: 'Missing JSX expression container around literal string'}]
errors: [{message: 'Missing JSX expression container around literal string:test'}]
}, {
code: `
class Comp1 extends Component {
Expand All @@ -233,7 +233,7 @@ ruleTester.run('jsx-no-literals', rule, {
}
`,
parser: 'babel-eslint',
errors: [{message: 'Missing JSX expression container around literal string'}]
errors: [{message: 'Missing JSX expression container around literal string:test'}]
}, {
code: `
var Hello = createReactClass({
Expand All @@ -244,7 +244,7 @@ ruleTester.run('jsx-no-literals', rule, {
});
`,
parser: 'babel-eslint',
errors: [{message: 'Missing JSX expression container around literal string'}]
errors: [{message: 'Missing JSX expression container around literal string:hello'}]
}, {
code: `
class Comp1 extends Component {
Expand All @@ -258,7 +258,7 @@ ruleTester.run('jsx-no-literals', rule, {
}
`,
parser: 'babel-eslint',
errors: [{message: 'Missing JSX expression container around literal string'}]
errors: [{message: 'Missing JSX expression container around literal string:asdjfl'}]
}, {
code: `
class Comp1 extends Component {
Expand All @@ -274,7 +274,7 @@ ruleTester.run('jsx-no-literals', rule, {
}
`,
parser: 'babel-eslint',
errors: [{message: 'Missing JSX expression container around literal string'}]
errors: [{message: 'Missing JSX expression container around literal string:asdjfl\n test\n foo'}]
}, {
code: `
class Comp1 extends Component {
Expand All @@ -290,7 +290,7 @@ ruleTester.run('jsx-no-literals', rule, {
}
`,
parser: 'babel-eslint',
errors: [{message: 'Missing JSX expression container around literal string'}]
errors: [{message: 'Missing JSX expression container around literal string:test'}]
}, {
code: `
<Foo bar="test">
Expand All @@ -299,23 +299,23 @@ ruleTester.run('jsx-no-literals', rule, {
`,
parser: 'babel-eslint',
options: [{noStrings: true}],
errors: [{message: 'Strings not allowed in JSX files'}]
errors: [{message: 'Strings not allowed in JSX files:\'Test\''}]
}, {
code: `
<Foo bar="test">
{'Test'}
</Foo>
`,
options: [{noStrings: true}],
errors: [{message: 'Strings not allowed in JSX files'}]
errors: [{message: 'Strings not allowed in JSX files:\'Test\''}]
}, {
code: `
<Foo bar="test">
{'Test' + name}
</Foo>
`,
options: [{noStrings: true}],
errors: [{message: 'Strings not allowed in JSX files'}]
errors: [{message: 'Strings not allowed in JSX files:\'Test\''}]
}, {
code: `
<Foo bar="test">
Expand All @@ -324,55 +324,55 @@ ruleTester.run('jsx-no-literals', rule, {
`,
parser: 'babel-eslint',
options: [{noStrings: true}],
errors: [{message: 'Strings not allowed in JSX files'}]
errors: [{message: 'Strings not allowed in JSX files:Test'}]
}, {
code: `
<Foo bar="test">
Test
</Foo>
`,
options: [{noStrings: true}],
errors: [{message: 'Strings not allowed in JSX files'}]
errors: [{message: 'Strings not allowed in JSX files:Test'}]
}, {
code: `
<Foo>
{\`Test\`}
</Foo>
`,
options: [{noStrings: true}],
errors: [{message: 'Strings not allowed in JSX files'}]
errors: [{message: 'Strings not allowed in JSX files:`Test`'}]
}, {
code: '<Foo bar={`Test`} />',
options: [{noStrings: true}],
errors: [{message: 'Strings not allowed in JSX files'}]
errors: [{message: 'Strings not allowed in JSX files:`Test`'}]
}, {
code: '<Foo bar={`${baz}`} />',
options: [{noStrings: true}],
errors: [{message: 'Strings not allowed in JSX files'}]
errors: [{message: 'Strings not allowed in JSX files:`${baz}`'}]
}, {
code: '<Foo bar={`Test ${baz}`} />',
options: [{noStrings: true}],
errors: [{message: 'Strings not allowed in JSX files'}]
errors: [{message: 'Strings not allowed in JSX files:`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: 'Strings not allowed in JSX files:`foo`'},
{message: 'Strings not allowed in JSX files:\'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: 'Strings not allowed in JSX files:`foo`'},
{message: 'Strings not allowed in JSX files:`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: 'Strings not allowed in JSX files:\'foo\''},
{message: 'Strings not allowed in JSX files:`bar`'}
]
}
]
Expand Down

0 comments on commit e415132

Please sign in to comment.