1 | /**
|
---|
2 | * @fileoverview Comments inside children section of tag should be placed inside braces.
|
---|
3 | * @author Ben Vinegar
|
---|
4 | */
|
---|
5 |
|
---|
6 | 'use strict';
|
---|
7 |
|
---|
8 | const docsUrl = require('../util/docsUrl');
|
---|
9 | const getText = require('../util/eslint').getText;
|
---|
10 | const report = require('../util/report');
|
---|
11 |
|
---|
12 | // ------------------------------------------------------------------------------
|
---|
13 | // Rule Definition
|
---|
14 | // ------------------------------------------------------------------------------
|
---|
15 |
|
---|
16 | const messages = {
|
---|
17 | putCommentInBraces: 'Comments inside children section of tag should be placed inside braces',
|
---|
18 | };
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * @param {Context} context
|
---|
22 | * @param {ASTNode} node
|
---|
23 | * @returns {void}
|
---|
24 | */
|
---|
25 | function checkText(context, node) {
|
---|
26 | // since babel-eslint has the wrong node.raw, we'll get the source text
|
---|
27 | const rawValue = getText(context, node);
|
---|
28 | if (/^\s*\/(\/|\*)/m.test(rawValue)) {
|
---|
29 | // inside component, e.g. <div>literal</div>
|
---|
30 | if (
|
---|
31 | node.parent.type !== 'JSXAttribute'
|
---|
32 | && node.parent.type !== 'JSXExpressionContainer'
|
---|
33 | && node.parent.type.indexOf('JSX') !== -1
|
---|
34 | ) {
|
---|
35 | report(context, messages.putCommentInBraces, 'putCommentInBraces', {
|
---|
36 | node,
|
---|
37 | });
|
---|
38 | }
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | /** @type {import('eslint').Rule.RuleModule} */
|
---|
43 | module.exports = {
|
---|
44 | meta: {
|
---|
45 | docs: {
|
---|
46 | description: 'Disallow comments from being inserted as text nodes',
|
---|
47 | category: 'Possible Errors',
|
---|
48 | recommended: true,
|
---|
49 | url: docsUrl('jsx-no-comment-textnodes'),
|
---|
50 | },
|
---|
51 |
|
---|
52 | messages,
|
---|
53 |
|
---|
54 | schema: [],
|
---|
55 | },
|
---|
56 |
|
---|
57 | create(context) {
|
---|
58 | // --------------------------------------------------------------------------
|
---|
59 | // Public
|
---|
60 | // --------------------------------------------------------------------------
|
---|
61 |
|
---|
62 | return {
|
---|
63 | Literal(node) {
|
---|
64 | checkText(context, node);
|
---|
65 | },
|
---|
66 | JSXText(node) {
|
---|
67 | checkText(context, node);
|
---|
68 | },
|
---|
69 | };
|
---|
70 | },
|
---|
71 | };
|
---|