[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Validate spacing before closing bracket in JSX.
|
---|
| 3 | * @author ryym
|
---|
| 4 | * @deprecated
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | 'use strict';
|
---|
| 8 |
|
---|
| 9 | const getTokenBeforeClosingBracket = require('../util/getTokenBeforeClosingBracket');
|
---|
| 10 | const docsUrl = require('../util/docsUrl');
|
---|
| 11 | const log = require('../util/log');
|
---|
| 12 | const report = require('../util/report');
|
---|
| 13 | const getSourceCode = require('../util/eslint').getSourceCode;
|
---|
| 14 |
|
---|
| 15 | let isWarnedForDeprecation = false;
|
---|
| 16 |
|
---|
| 17 | // ------------------------------------------------------------------------------
|
---|
| 18 | // Rule Definition
|
---|
| 19 | // ------------------------------------------------------------------------------
|
---|
| 20 |
|
---|
| 21 | const messages = {
|
---|
| 22 | noSpaceBeforeClose: 'A space is forbidden before closing bracket',
|
---|
| 23 | needSpaceBeforeClose: 'A space is required before closing bracket',
|
---|
| 24 | };
|
---|
| 25 |
|
---|
[0c6b92a] | 26 | /** @type {import('eslint').Rule.RuleModule} */
|
---|
[d565449] | 27 | module.exports = {
|
---|
| 28 | meta: {
|
---|
| 29 | deprecated: true,
|
---|
| 30 | replacedBy: ['jsx-tag-spacing'],
|
---|
| 31 | docs: {
|
---|
| 32 | description: 'Enforce spacing before closing bracket in JSX',
|
---|
| 33 | category: 'Stylistic Issues',
|
---|
| 34 | recommended: false,
|
---|
| 35 | url: docsUrl('jsx-space-before-closing'),
|
---|
| 36 | },
|
---|
| 37 | fixable: 'code',
|
---|
| 38 |
|
---|
| 39 | messages,
|
---|
| 40 |
|
---|
| 41 | schema: [{
|
---|
| 42 | enum: ['always', 'never'],
|
---|
| 43 | }],
|
---|
| 44 | },
|
---|
| 45 |
|
---|
| 46 | create(context) {
|
---|
| 47 | const configuration = context.options[0] || 'always';
|
---|
| 48 |
|
---|
| 49 | // --------------------------------------------------------------------------
|
---|
| 50 | // Public
|
---|
| 51 | // --------------------------------------------------------------------------
|
---|
| 52 |
|
---|
| 53 | return {
|
---|
| 54 | JSXOpeningElement(node) {
|
---|
| 55 | if (!node.selfClosing) {
|
---|
| 56 | return;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | const sourceCode = getSourceCode(context);
|
---|
| 60 |
|
---|
| 61 | const leftToken = getTokenBeforeClosingBracket(node);
|
---|
[0c6b92a] | 62 | const closingSlash = /** @type {import("eslint").AST.Token} */ (sourceCode.getTokenAfter(leftToken));
|
---|
[d565449] | 63 |
|
---|
| 64 | if (leftToken.loc.end.line !== closingSlash.loc.start.line) {
|
---|
| 65 | return;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | if (configuration === 'always' && !sourceCode.isSpaceBetweenTokens(leftToken, closingSlash)) {
|
---|
| 69 | report(context, messages.needSpaceBeforeClose, 'needSpaceBeforeClose', {
|
---|
| 70 | loc: closingSlash.loc.start,
|
---|
| 71 | fix(fixer) {
|
---|
| 72 | return fixer.insertTextBefore(closingSlash, ' ');
|
---|
| 73 | },
|
---|
| 74 | });
|
---|
| 75 | } else if (configuration === 'never' && sourceCode.isSpaceBetweenTokens(leftToken, closingSlash)) {
|
---|
| 76 | report(context, messages.noSpaceBeforeClose, 'noSpaceBeforeClose', {
|
---|
| 77 | loc: closingSlash.loc.start,
|
---|
| 78 | fix(fixer) {
|
---|
| 79 | const previousToken = sourceCode.getTokenBefore(closingSlash);
|
---|
| 80 | return fixer.removeRange([previousToken.range[1], closingSlash.range[0]]);
|
---|
| 81 | },
|
---|
| 82 | });
|
---|
| 83 | }
|
---|
| 84 | },
|
---|
| 85 |
|
---|
| 86 | Program() {
|
---|
| 87 | if (isWarnedForDeprecation) {
|
---|
| 88 | return;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | log('The react/jsx-space-before-closing rule is deprecated. '
|
---|
| 92 | + 'Please use the react/jsx-tag-spacing rule with the '
|
---|
| 93 | + '"beforeSelfClosing" option instead.');
|
---|
| 94 | isWarnedForDeprecation = true;
|
---|
| 95 | },
|
---|
| 96 | };
|
---|
| 97 | },
|
---|
| 98 | };
|
---|