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