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
Allow for stricter no-literals rule #1202
Changes from 4 commits
ae9c67e
d069049
a01b23c
1c6ac4e
cc503b3
f5d00c0
3bdc7da
8a2bc81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
/** | ||
* @fileoverview Prevent using string literals in React component definition | ||
* @author Caleb Morris | ||
* @author David Buchan-Swanson | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
var NO_STRINGS = 'no-strings'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const |
||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
|
@@ -17,6 +20,8 @@ module.exports = { | |
}, | ||
|
||
schema: [{ | ||
enum: [NO_STRINGS] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than add a string option, let's add this to the options object, for future extensibility. |
||
}, { | ||
type: 'object', | ||
properties: {}, | ||
additionalProperties: false | ||
|
@@ -25,27 +30,38 @@ module.exports = { | |
|
||
create: function(context) { | ||
|
||
const isNoStrings = context.options[0] === NO_STRINGS; | ||
|
||
const message = isNoStrings ? | ||
'Strings not allowed in JSX files' : | ||
'Missing JSX expression container around literal string'; | ||
|
||
function reportLiteralNode(node) { | ||
context.report({ | ||
node: node, | ||
message: 'Missing JSX expression container around literal string' | ||
message: message | ||
}); | ||
} | ||
|
||
function getValidation(node) { | ||
const standard = !/^[\s]+$/.test(node.value) && | ||
node.parent && | ||
node.parent.type.indexOf('JSX') !== -1 && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was the existing validation. I just removed part of it if you enable the option. |
||
node.parent.type !== 'JSXAttribute'; | ||
if (isNoStrings) { | ||
return standard; | ||
} | ||
return standard && node.parent.type !== 'JSXExpressionContainer'; | ||
} | ||
|
||
// -------------------------------------------------------------------------- | ||
// Public | ||
// -------------------------------------------------------------------------- | ||
|
||
return { | ||
|
||
Literal: function(node) { | ||
if ( | ||
!/^[\s]+$/.test(node.value) && | ||
node.parent && | ||
node.parent.type !== 'JSXExpressionContainer' && | ||
node.parent.type !== 'JSXAttribute' && | ||
node.parent.type.indexOf('JSX') !== -1 | ||
) { | ||
if (getValidation(node)) { | ||
reportLiteralNode(node); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
/** | ||
* @fileoverview Prevent using unwrapped literals in a React component definition | ||
* @author Caleb morris | ||
* @author David Buchan-Swanson | ||
*/ | ||
'use strict'; | ||
|
||
|
@@ -109,6 +110,22 @@ ruleTester.run('jsx-no-literals', rule, { | |
'</Foo>' | ||
].join('\n'), | ||
parser: 'babel-eslint' | ||
}, { | ||
code: ` | ||
<Foo bar="test"> | ||
{intl.formatText(message)} | ||
</Foo> | ||
`, | ||
parser: 'babel-eslint', | ||
options: ['no-strings'] | ||
}, { | ||
code: ` | ||
<Foo bar="test"> | ||
{translate('my.translate.key')} | ||
</Foo> | ||
`, | ||
parser: 'babel-eslint', | ||
options: ['no-strings'] | ||
} | ||
], | ||
|
||
|
@@ -202,6 +219,24 @@ ruleTester.run('jsx-no-literals', rule, { | |
].join('\n'), | ||
parser: 'babel-eslint', | ||
errors: [{message: 'Missing JSX expression container around literal string'}] | ||
}, { | ||
code: ` | ||
<Foo bar="test"> | ||
{'Test'} | ||
</Foo> | ||
`, | ||
parser: 'babel-eslint', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also add identical test cases that use the default parser. |
||
options: ['no-strings'], | ||
errors: [{message: 'Strings not allowed in JSX files'}] | ||
}, { | ||
code: ` | ||
<Foo bar="test"> | ||
Test | ||
</Foo> | ||
`, | ||
parser: 'babel-eslint', | ||
options: ['no-strings'], | ||
errors: [{message: 'Strings not allowed in JSX files'}] | ||
} | ||
] | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ftr tho, this shouldn't ever happen, since straight quotes are typographically incorrect - and this would be a bug in the highlighter. I'm not sure this is worth mentioning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the original text, essentially, with some modifications because of the different structure. If what you're saying is true, then this rule has/had no purpose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It still has many purposes; but dealing with quotes isn't one of them imo :-)
Fine to leave the text as-is.