jsx-no-comment-textnodes.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. const report = require('../util/report');
  8. // ------------------------------------------------------------------------------
  9. // Rule Definition
  10. // ------------------------------------------------------------------------------
  11. const messages = {
  12. putCommentInBraces: 'Comments inside children section of tag should be placed inside braces',
  13. };
  14. function checkText(node, context) {
  15. // since babel-eslint has the wrong node.raw, we'll get the source text
  16. const rawValue = context.getSourceCode().getText(node);
  17. if (/^\s*\/(\/|\*)/m.test(rawValue)) {
  18. // inside component, e.g. <div>literal</div>
  19. if (
  20. node.parent.type !== 'JSXAttribute'
  21. && node.parent.type !== 'JSXExpressionContainer'
  22. && node.parent.type.indexOf('JSX') !== -1
  23. ) {
  24. report(context, messages.putCommentInBraces, 'putCommentInBraces', {
  25. node,
  26. });
  27. }
  28. }
  29. }
  30. module.exports = {
  31. meta: {
  32. docs: {
  33. description: 'Disallow comments from being inserted as text nodes',
  34. category: 'Possible Errors',
  35. recommended: true,
  36. url: docsUrl('jsx-no-comment-textnodes'),
  37. },
  38. messages,
  39. schema: [],
  40. },
  41. create(context) {
  42. // --------------------------------------------------------------------------
  43. // Public
  44. // --------------------------------------------------------------------------
  45. return {
  46. Literal(node) {
  47. checkText(node, context);
  48. },
  49. JSXText(node) {
  50. checkText(node, context);
  51. },
  52. };
  53. },
  54. };