Skip to content

Commit 9ad4c69

Browse files
committed
Rename no-comment-textnodes to jsx-no-comment-textnodes
This rule is JSX-specific, not React-specific. Changing its name makes this clearer. I've kept the old one around as deprecated. We can remove it at the next major version bump. Addresses jsx-eslint#668 and jsx-eslint#686.
1 parent 063e343 commit 9ad4c69

File tree

6 files changed

+59
-22
lines changed

6 files changed

+59
-22
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ Finally, enable all of the rules that you would like to use. Use [our preset](#
8181

8282
* [react/display-name](docs/rules/display-name.md): Prevent missing `displayName` in a React component definition
8383
* [react/forbid-prop-types](docs/rules/forbid-prop-types.md): Forbid certain propTypes
84-
* [react/no-comment-textnodes](docs/rules/no-comment-textnodes.md): Prevent comments from being inserted as text nodes
8584
* [react/no-danger](docs/rules/no-danger.md): Prevent usage of dangerous JSX properties
8685
* [react/no-deprecated](docs/rules/no-deprecated.md): Prevent usage of deprecated methods
8786
* [react/no-did-mount-set-state](docs/rules/no-did-mount-set-state.md): Prevent usage of `setState` in `componentDidMount`
@@ -120,6 +119,7 @@ Finally, enable all of the rules that you would like to use. Use [our preset](#
120119
* [react/jsx-key](docs/rules/jsx-key.md): Validate JSX has key prop when in array or iterator
121120
* [react/jsx-max-props-per-line](docs/rules/jsx-max-props-per-line.md): Limit maximum of props on a single line in JSX
122121
* [react/jsx-no-bind](docs/rules/jsx-no-bind.md): Prevent usage of `.bind()` and arrow functions in JSX props
122+
* [react/jsx-no-comment-textnodes](docs/rules/jsx-no-comment-textnodes.md): Prevent comments from being inserted as text nodes
123123
* [react/jsx-no-duplicate-props](docs/rules/jsx-no-duplicate-props.md): Prevent duplicate props in JSX
124124
* [react/jsx-no-literals](docs/rules/jsx-no-literals.md): Prevent usage of unwrapped JSX strings
125125
* [react/jsx-no-target-blank](docs/rules/jsx-no-target-blank.md): Prevent usage of unsafe `target='_blank'`

docs/rules/no-comment-textnodes.md renamed to docs/rules/jsx-no-comment-textnodes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Prevent comments from being inserted as text nodes (no-comment-textnodes)
1+
# Prevent comments from being inserted as text nodes (jsx-no-comment-textnodes)
22

33
This rule prevents comment strings (e.g. beginning with `//` or `/*`) from being accidentally
44
injected as a text node in JSX statements.

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var rules = {
88
'wrap-multilines': require('./lib/rules/wrap-multilines'),
99
'self-closing-comp': require('./lib/rules/self-closing-comp'),
1010
'no-comment-textnodes': require('./lib/rules/no-comment-textnodes'),
11+
'jsx-no-comment-textnodes': require('./lib/rules/jsx-no-comment-textnodes'),
1112
'no-danger': require('./lib/rules/no-danger'),
1213
'no-set-state': require('./lib/rules/no-set-state'),
1314
'no-is-mounted': require('./lib/rules/no-is-mounted'),

lib/rules/jsx-no-comment-textnodes.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @fileoverview Comments inside children section of tag should be placed inside braces.
3+
* @author Ben Vinegar
4+
*/
5+
'use strict';
6+
7+
// ------------------------------------------------------------------------------
8+
// Rule Definition
9+
// ------------------------------------------------------------------------------
10+
11+
module.exports = function(context) {
12+
function reportLiteralNode(node) {
13+
context.report(node, 'Comments inside children section of tag should be placed inside braces');
14+
}
15+
16+
// --------------------------------------------------------------------------
17+
// Public
18+
// --------------------------------------------------------------------------
19+
20+
return {
21+
Literal: function(node) {
22+
if (/^\s*\/(\/|\*)/m.test(node.value)) {
23+
// inside component, e.g. <div>literal</div>
24+
if (node.parent.type !== 'JSXAttribute' &&
25+
node.parent.type !== 'JSXExpressionContainer' &&
26+
node.parent.type.indexOf('JSX') !== -1) {
27+
reportLiteralNode(node);
28+
}
29+
}
30+
}
31+
};
32+
};
33+
34+
module.exports.schema = [{
35+
type: 'object',
36+
properties: {},
37+
additionalProperties: false
38+
}];

lib/rules/no-comment-textnodes.js

+16-18
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
/**
22
* @fileoverview Comments inside children section of tag should be placed inside braces.
33
* @author Ben Vinegar
4+
* @deprecated
45
*/
56
'use strict';
67

78
// ------------------------------------------------------------------------------
89
// Rule Definition
910
// ------------------------------------------------------------------------------
1011

11-
module.exports = function(context) {
12-
function reportLiteralNode(node) {
13-
context.report(node, 'Comments inside children section of tag should be placed inside braces');
14-
}
15-
16-
// --------------------------------------------------------------------------
17-
// Public
18-
// --------------------------------------------------------------------------
12+
var util = require('util');
13+
var jsxNoCommentTextnodes = require('./jsx-no-comment-textnodes');
14+
var isWarnedForDeprecation = false;
1915

20-
return {
21-
Literal: function(node) {
22-
if (/^\s*\/(\/|\*)/m.test(node.value)) {
23-
// inside component, e.g. <div>literal</div>
24-
if (node.parent.type !== 'JSXAttribute' &&
25-
node.parent.type !== 'JSXExpressionContainer' &&
26-
node.parent.type.indexOf('JSX') !== -1) {
27-
reportLiteralNode(node);
28-
}
16+
module.exports = function(context) {
17+
return util._extend(jsxNoCommentTextnodes(context), {
18+
Program: function() {
19+
if (isWarnedForDeprecation || /\=-(f|-format)=/.test(process.argv.join('='))) {
20+
return;
2921
}
22+
23+
/* eslint-disable no-console */
24+
console.log('The react/no-comment-textnodes rule is deprecated. Please ' +
25+
'use the react/jsx-no-comment-textnodes rule instead.');
26+
/* eslint-enable no-console */
27+
isWarnedForDeprecation = true;
3028
}
31-
};
29+
});
3230
};
3331

3432
module.exports.schema = [{

tests/lib/rules/no-comment-textnodes.js renamed to tests/lib/rules/jsx-no-comment-textnodes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
// Requirements
99
// ------------------------------------------------------------------------------
1010

11-
var rule = require('../../../lib/rules/no-comment-textnodes');
11+
var rule = require('../../../lib/rules/jsx-no-comment-textnodes');
1212
var RuleTester = require('eslint').RuleTester;
1313

1414
// ------------------------------------------------------------------------------
1515
// Tests
1616
// ------------------------------------------------------------------------------
1717

1818
var ruleTester = new RuleTester();
19-
ruleTester.run('jsx-needs-i18n', rule, {
19+
ruleTester.run('jsx-no-comment-textnodes', rule, {
2020

2121
valid: [
2222
{

0 commit comments

Comments
 (0)