jsx-no-comment-textnodes.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * @fileoverview Comments inside children section of tag should be placed inside braces.
  3. * @author Ben Vinegar
  4. */
  5. 'use strict';
  6. const docsUrl = require('../util/docsUrl');
  7. // ------------------------------------------------------------------------------
  8. // Rule Definition
  9. // ------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. docs: {
  13. description: 'Comments inside children section of tag should be placed inside braces',
  14. category: 'Possible Errors',
  15. recommended: true,
  16. url: docsUrl('jsx-no-comment-textnodes')
  17. },
  18. schema: [{
  19. type: 'object',
  20. properties: {},
  21. additionalProperties: false
  22. }]
  23. },
  24. create: function(context) {
  25. function reportLiteralNode(node) {
  26. context.report(node, 'Comments inside children section of tag should be placed inside braces');
  27. }
  28. // --------------------------------------------------------------------------
  29. // Public
  30. // --------------------------------------------------------------------------
  31. return {
  32. Literal: function(node) {
  33. const sourceCode = context.getSourceCode();
  34. // since babel-eslint has the wrong node.raw, we'll get the source text
  35. const rawValue = sourceCode.getText(node);
  36. if (/^\s*\/(\/|\*)/m.test(rawValue)) {
  37. // inside component, e.g. <div>literal</div>
  38. if (node.parent.type !== 'JSXAttribute' &&
  39. node.parent.type !== 'JSXExpressionContainer' &&
  40. node.parent.type.indexOf('JSX') !== -1) {
  41. reportLiteralNode(node);
  42. }
  43. }
  44. }
  45. };
  46. }
  47. };