[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 |
|
---|
| 26 | module.exports = {
|
---|
| 27 | meta: {
|
---|
| 28 | deprecated: true,
|
---|
| 29 | replacedBy: ['jsx-tag-spacing'],
|
---|
| 30 | docs: {
|
---|
| 31 | description: 'Enforce spacing before closing bracket in JSX',
|
---|
| 32 | category: 'Stylistic Issues',
|
---|
| 33 | recommended: false,
|
---|
| 34 | url: docsUrl('jsx-space-before-closing'),
|
---|
| 35 | },
|
---|
| 36 | fixable: 'code',
|
---|
| 37 |
|
---|
| 38 | messages,
|
---|
| 39 |
|
---|
| 40 | schema: [{
|
---|
| 41 | enum: ['always', 'never'],
|
---|
| 42 | }],
|
---|
| 43 | },
|
---|
| 44 |
|
---|
| 45 | create(context) {
|
---|
| 46 | const configuration = context.options[0] || 'always';
|
---|
| 47 |
|
---|
| 48 | // --------------------------------------------------------------------------
|
---|
| 49 | // Public
|
---|
| 50 | // --------------------------------------------------------------------------
|
---|
| 51 |
|
---|
| 52 | return {
|
---|
| 53 | JSXOpeningElement(node) {
|
---|
| 54 | if (!node.selfClosing) {
|
---|
| 55 | return;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | const sourceCode = getSourceCode(context);
|
---|
| 59 |
|
---|
| 60 | const leftToken = getTokenBeforeClosingBracket(node);
|
---|
| 61 | const closingSlash = sourceCode.getTokenAfter(leftToken);
|
---|
| 62 |
|
---|
| 63 | if (leftToken.loc.end.line !== closingSlash.loc.start.line) {
|
---|
| 64 | return;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | if (configuration === 'always' && !sourceCode.isSpaceBetweenTokens(leftToken, closingSlash)) {
|
---|
| 68 | report(context, messages.needSpaceBeforeClose, 'needSpaceBeforeClose', {
|
---|
| 69 | loc: closingSlash.loc.start,
|
---|
| 70 | fix(fixer) {
|
---|
| 71 | return fixer.insertTextBefore(closingSlash, ' ');
|
---|
| 72 | },
|
---|
| 73 | });
|
---|
| 74 | } else if (configuration === 'never' && sourceCode.isSpaceBetweenTokens(leftToken, closingSlash)) {
|
---|
| 75 | report(context, messages.noSpaceBeforeClose, 'noSpaceBeforeClose', {
|
---|
| 76 | loc: closingSlash.loc.start,
|
---|
| 77 | fix(fixer) {
|
---|
| 78 | const previousToken = sourceCode.getTokenBefore(closingSlash);
|
---|
| 79 | return fixer.removeRange([previousToken.range[1], closingSlash.range[0]]);
|
---|
| 80 | },
|
---|
| 81 | });
|
---|
| 82 | }
|
---|
| 83 | },
|
---|
| 84 |
|
---|
| 85 | Program() {
|
---|
| 86 | if (isWarnedForDeprecation) {
|
---|
| 87 | return;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | log('The react/jsx-space-before-closing rule is deprecated. '
|
---|
| 91 | + 'Please use the react/jsx-tag-spacing rule with the '
|
---|
| 92 | + '"beforeSelfClosing" option instead.');
|
---|
| 93 | isWarnedForDeprecation = true;
|
---|
| 94 | },
|
---|
| 95 | };
|
---|
| 96 | },
|
---|
| 97 | };
|
---|