Skip to content

Commit

Permalink
Merge pull request #2008 from jlgonzalezdev/master
Browse files Browse the repository at this point in the history
[New] `jsx-no-literals`: print node value in warning message
  • Loading branch information
ljharb authored Oct 11, 2018
2 parents c2ae5d3 + eafd19d commit 5dddbf4
Show file tree
Hide file tree
Showing 2 changed files with 33 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
54 changes: 31 additions & 23 deletions tests/lib/rules/jsx-no-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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({
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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">
Expand All @@ -299,23 +307,23 @@ 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">
{'Test'}
</Foo>
`,
options: [{noStrings: true}],
errors: [{message: 'Strings not allowed in JSX files'}]
errors: [{message: stringsMessage('\'Test\'')}]
}, {
code: `
<Foo bar="test">
{'Test' + name}
</Foo>
`,
options: [{noStrings: true}],
errors: [{message: 'Strings not allowed in JSX files'}]
errors: [{message: stringsMessage('\'Test\'')}]
}, {
code: `
<Foo bar="test">
Expand All @@ -324,55 +332,55 @@ 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">
Test
</Foo>
`,
options: [{noStrings: true}],
errors: [{message: 'Strings not allowed in JSX files'}]
errors: [{message: stringsMessage('Test')}]
}, {
code: `
<Foo>
{\`Test\`}
</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`')}
]
}
]
Expand Down

0 comments on commit 5dddbf4

Please sign in to comment.