jsx-no-comment-textnodes.js 1.7 KB

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