Skip to content

Commit

Permalink
[Fix] jsx-no-comment-textnodes: fix for @typescript-eslint/parser
Browse files Browse the repository at this point in the history
 - `@typescript-eslint/parser` requires eslint 5+ (and node 6+)

Fixes #2599.
  • Loading branch information
Axnyff authored and ljharb committed Mar 23, 2020
1 parent 4ddf277 commit 6aa7874
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 20 deletions.
39 changes: 22 additions & 17 deletions lib/rules/jsx-no-comment-textnodes.js
Expand Up @@ -11,6 +11,24 @@ const docsUrl = require('../util/docsUrl');
// Rule Definition
// ------------------------------------------------------------------------------

function checkText(node, context) {
// since babel-eslint has the wrong node.raw, we'll get the source text
const rawValue = context.getSourceCode().getText(node);
if (/^\s*\/(\/|\*)/m.test(rawValue)) {
// inside component, e.g. <div>literal</div>
if (
node.parent.type !== 'JSXAttribute' &&
node.parent.type !== 'JSXExpressionContainer' &&
node.parent.type.indexOf('JSX') !== -1
) {
context.report({
node,
message: 'Comments inside children section of tag should be placed inside braces'
});
}
}
}

module.exports = {
meta: {
docs: {
Expand All @@ -28,29 +46,16 @@ module.exports = {
},

create(context) {
function reportLiteralNode(node) {
context.report({
node,
message: 'Comments inside children section of tag should be placed inside braces'
});
}

// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------

return {
Literal(node) {
// since babel-eslint has the wrong node.raw, we'll get the source text
const rawValue = context.getSourceCode().getText(node);
if (/^\s*\/(\/|\*)/m.test(rawValue)) {
// inside component, e.g. <div>literal</div>
if (node.parent.type !== 'JSXAttribute' &&
node.parent.type !== 'JSXExpressionContainer' &&
node.parent.type.indexOf('JSX') !== -1) {
reportLiteralNode(node);
}
}
checkText(node, context);
},
JSXText(node) {
checkText(node, context);
}
};
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -44,6 +44,7 @@
"@types/eslint": "^6.1.8",
"@types/estree": "0.0.42",
"@types/node": "^13.7.4",
"@typescript-eslint/parser": "^2.24.0",
"babel-eslint": "^8.2.6",
"coveralls": "^3.0.9",
"eslint": "^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0",
Expand Down
3 changes: 2 additions & 1 deletion tests/helpers/parsers.js
Expand Up @@ -6,5 +6,6 @@ const NODE_MODULES = '../../node_modules';

module.exports = {
BABEL_ESLINT: path.join(__dirname, NODE_MODULES, 'babel-eslint'),
TYPESCRIPT_ESLINT: path.join(__dirname, NODE_MODULES, 'typescript-eslint-parser')
TYPESCRIPT_ESLINT: path.join(__dirname, NODE_MODULES, 'typescript-eslint-parser'),
'@TYPESCRIPT_ESLINT': path.join(__dirname, NODE_MODULES, '@typescript-eslint/parser')
};
217 changes: 215 additions & 2 deletions tests/lib/rules/jsx-no-comment-textnodes.js
Expand Up @@ -9,11 +9,20 @@
// Requirements
// ------------------------------------------------------------------------------

const semver = require('semver');
const version = require('eslint/package.json').version;
const RuleTester = require('eslint').RuleTester;
const rule = require('../../../lib/rules/jsx-no-comment-textnodes');

const parsers = require('../../helpers/parsers');

function TS(tests) {
if (semver.satisfies(version, '>= 5')) {
return tests;
}
return [];
}

const parserOptions = {
ecmaVersion: 2018,
sourceType: 'module',
Expand Down Expand Up @@ -173,7 +182,133 @@ ruleTester.run('jsx-no-comment-textnodes', rule, {
code: '<pre>&#x2F;&#42; TODO: Write perfect code &#42;&#x2F;</pre>',
parser: parsers.BABEL_ESLINT
}
],
].concat(TS([
{
code: `
class Comp1 extends Component {
render() {
return (
<div>
{/* valid */}
</div>
);
}
}
`,
parser: parsers['@TYPESCRIPT_ESLINT']
}, {
code: `
class Comp1 extends Component {
render() {
return (
<>
{/* valid */}
</>
);
}
}
`,
parser: parsers['@TYPESCRIPT_ESLINT']
}, {
code: `
class Comp1 extends Component {
render() {
return (<div>{/* valid */}</div>);
}
}
`,
parser: parsers['@TYPESCRIPT_ESLINT']
}, {
code: `
class Comp1 extends Component {
render() {
const bar = (<div>{/* valid */}</div>);
return bar;
}
}
`,
parser: parsers['@TYPESCRIPT_ESLINT']
}, {
code: `
var Hello = createReactClass({
foo: (<div>{/* valid */}</div>),
render() {
return this.foo;
},
});
`,
parser: parsers['@TYPESCRIPT_ESLINT']
}, {
code: `
class Comp1 extends Component {
render() {
return (
<div>
{/* valid */}
{/* valid 2 */}
{/* valid 3 */}
</div>
);
}
}
`,
parser: parsers['@TYPESCRIPT_ESLINT']
}, {
code: `
class Comp1 extends Component {
render() {
return (
<div>
</div>
);
}
}
`,
parser: parsers['@TYPESCRIPT_ESLINT']
}, {
code: `
var foo = require('foo');
`,
parser: parsers['@TYPESCRIPT_ESLINT']
}, {
code: `
<Foo bar='test'>
{/* valid */}
</Foo>
`,
parser: parsers['@TYPESCRIPT_ESLINT']
},
{
code: `
<strong>
&nbsp;https://www.example.com/attachment/download/1
</strong>
`,
parser: parsers['@TYPESCRIPT_ESLINT']
},

// inside element declarations
{
code: `
<Foo /* valid */ placeholder={'foo'}/>
`,
parser: parsers['@TYPESCRIPT_ESLINT']
},
{
code: `
<Foo title={'foo' /* valid */}/>
`,
parser: parsers['@TYPESCRIPT_ESLINT']
},
{
code: '<pre>&#x2F;&#x2F; TODO: Write perfect code</pre>',
parser: parsers['@TYPESCRIPT_ESLINT']
},
{
code: '<pre>&#x2F;&#42; TODO: Write perfect code &#42;&#x2F;</pre>',
parser: parsers['@TYPESCRIPT_ESLINT']
}
])),

invalid: [
{
Expand Down Expand Up @@ -253,5 +388,83 @@ ruleTester.run('jsx-no-comment-textnodes', rule, {
parser: parsers.BABEL_ESLINT,
errors: [{message: 'Comments inside children section of tag should be placed inside braces'}]
}
]
].concat(TS([
{
code: `
class Comp1 extends Component {
render() {
return (<div>// invalid</div>);
}
}
`,
parser: parsers['@TYPESCRIPT_ESLINT'],
errors: [{message: 'Comments inside children section of tag should be placed inside braces'}]
}, {
code: `
class Comp1 extends Component {
render() {
return (<>// invalid</>);
}
}
`,
parser: parsers['@TYPESCRIPT_ESLINT'],
errors: [{message: 'Comments inside children section of tag should be placed inside braces'}]
}, {
code: `
class Comp1 extends Component {
render() {
return (<div>/* invalid */</div>);
}
}
`,
parser: parsers['@TYPESCRIPT_ESLINT'],
errors: [{message: 'Comments inside children section of tag should be placed inside braces'}]
}, {
code: `
class Comp1 extends Component {
render() {
return (
<div>
// invalid
</div>
);
}
}
`,
parser: parsers['@TYPESCRIPT_ESLINT'],
errors: [{message: 'Comments inside children section of tag should be placed inside braces'}]
}, {
code: `
class Comp1 extends Component {
render() {
return (
<div>
asdjfl
/* invalid */
foo
</div>
);
}
}
`,
parser: parsers['@TYPESCRIPT_ESLINT'],
errors: [{message: 'Comments inside children section of tag should be placed inside braces'}]
}, {
code: `
class Comp1 extends Component {
render() {
return (
<div>
{'asdjfl'}
// invalid
{'foo'}
</div>
);
}
}
`,
parser: parsers['@TYPESCRIPT_ESLINT'],
errors: [{message: 'Comments inside children section of tag should be placed inside braces'}]
}
]))
});

0 comments on commit 6aa7874

Please sign in to comment.