jsx-space-before-closing.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * @fileoverview Validate spacing before closing bracket in JSX.
  3. * @author ryym
  4. * @deprecated
  5. */
  6. 'use strict';
  7. const getTokenBeforeClosingBracket = require('../util/getTokenBeforeClosingBracket');
  8. const docsUrl = require('../util/docsUrl');
  9. let isWarnedForDeprecation = false;
  10. // ------------------------------------------------------------------------------
  11. // Rule Definition
  12. // ------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. deprecated: true,
  16. docs: {
  17. description: 'Validate spacing before closing bracket in JSX',
  18. category: 'Stylistic Issues',
  19. recommended: false,
  20. url: docsUrl('jsx-space-before-closing')
  21. },
  22. fixable: 'code',
  23. schema: [{
  24. enum: ['always', 'never']
  25. }]
  26. },
  27. create: function(context) {
  28. const configuration = context.options[0] || 'always';
  29. const sourceCode = context.getSourceCode();
  30. const NEVER_MESSAGE = 'A space is forbidden before closing bracket';
  31. const ALWAYS_MESSAGE = 'A space is required before closing bracket';
  32. // --------------------------------------------------------------------------
  33. // Public
  34. // --------------------------------------------------------------------------
  35. return {
  36. JSXOpeningElement: function(node) {
  37. if (!node.selfClosing) {
  38. return;
  39. }
  40. const leftToken = getTokenBeforeClosingBracket(node);
  41. const closingSlash = sourceCode.getTokenAfter(leftToken);
  42. if (leftToken.loc.end.line !== closingSlash.loc.start.line) {
  43. return;
  44. }
  45. if (configuration === 'always' && !sourceCode.isSpaceBetweenTokens(leftToken, closingSlash)) {
  46. context.report({
  47. loc: closingSlash.loc.start,
  48. message: ALWAYS_MESSAGE,
  49. fix: function(fixer) {
  50. return fixer.insertTextBefore(closingSlash, ' ');
  51. }
  52. });
  53. } else if (configuration === 'never' && sourceCode.isSpaceBetweenTokens(leftToken, closingSlash)) {
  54. context.report({
  55. loc: closingSlash.loc.start,
  56. message: NEVER_MESSAGE,
  57. fix: function(fixer) {
  58. const previousToken = sourceCode.getTokenBefore(closingSlash);
  59. return fixer.removeRange([previousToken.range[1], closingSlash.range[0]]);
  60. }
  61. });
  62. }
  63. },
  64. Program: function() {
  65. if (isWarnedForDeprecation || /\=-(f|-format)=/.test(process.argv.join('='))) {
  66. return;
  67. }
  68. /* eslint-disable no-console */
  69. console.log('The react/jsx-space-before-closing rule is deprecated. ' +
  70. 'Please use the react/jsx-tag-spacing rule with the ' +
  71. '"beforeSelfClosing" option instead.');
  72. /* eslint-enable no-console */
  73. isWarnedForDeprecation = true;
  74. }
  75. };
  76. }
  77. };