Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding option to print node value #2008

Merged
merged 1 commit into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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