[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Disallow or enforce spaces around equal signs in JSX attributes.
|
---|
| 3 | * @author ryym
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | 'use strict';
|
---|
| 7 |
|
---|
| 8 | const docsUrl = require('../util/docsUrl');
|
---|
| 9 | const getSourceCode = require('../util/eslint').getSourceCode;
|
---|
| 10 | const report = require('../util/report');
|
---|
| 11 |
|
---|
| 12 | // ------------------------------------------------------------------------------
|
---|
| 13 | // Rule Definition
|
---|
| 14 | // ------------------------------------------------------------------------------
|
---|
| 15 |
|
---|
| 16 | const messages = {
|
---|
| 17 | noSpaceBefore: 'There should be no space before \'=\'',
|
---|
| 18 | noSpaceAfter: 'There should be no space after \'=\'',
|
---|
| 19 | needSpaceBefore: 'A space is required before \'=\'',
|
---|
| 20 | needSpaceAfter: 'A space is required after \'=\'',
|
---|
| 21 | };
|
---|
| 22 |
|
---|
[0c6b92a] | 23 | /** @type {import('eslint').Rule.RuleModule} */
|
---|
[d565449] | 24 | module.exports = {
|
---|
| 25 | meta: {
|
---|
| 26 | docs: {
|
---|
| 27 | description: 'Enforce or disallow spaces around equal signs in JSX attributes',
|
---|
| 28 | category: 'Stylistic Issues',
|
---|
| 29 | recommended: false,
|
---|
| 30 | url: docsUrl('jsx-equals-spacing'),
|
---|
| 31 | },
|
---|
| 32 | fixable: 'code',
|
---|
| 33 |
|
---|
| 34 | messages,
|
---|
| 35 |
|
---|
| 36 | schema: [{
|
---|
| 37 | enum: ['always', 'never'],
|
---|
| 38 | }],
|
---|
| 39 | },
|
---|
| 40 |
|
---|
| 41 | create(context) {
|
---|
| 42 | const config = context.options[0] || 'never';
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * Determines a given attribute node has an equal sign.
|
---|
| 46 | * @param {ASTNode} attrNode - The attribute node.
|
---|
| 47 | * @returns {boolean} Whether or not the attriute node has an equal sign.
|
---|
| 48 | */
|
---|
| 49 | function hasEqual(attrNode) {
|
---|
| 50 | return attrNode.type !== 'JSXSpreadAttribute' && attrNode.value !== null;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | // --------------------------------------------------------------------------
|
---|
| 54 | // Public
|
---|
| 55 | // --------------------------------------------------------------------------
|
---|
| 56 |
|
---|
| 57 | return {
|
---|
| 58 | JSXOpeningElement(node) {
|
---|
| 59 | node.attributes.forEach((attrNode) => {
|
---|
| 60 | if (!hasEqual(attrNode)) {
|
---|
| 61 | return;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | const sourceCode = getSourceCode(context);
|
---|
| 65 | const equalToken = sourceCode.getTokenAfter(attrNode.name);
|
---|
| 66 | const spacedBefore = sourceCode.isSpaceBetweenTokens(attrNode.name, equalToken);
|
---|
| 67 | const spacedAfter = sourceCode.isSpaceBetweenTokens(equalToken, attrNode.value);
|
---|
| 68 |
|
---|
| 69 | if (config === 'never') {
|
---|
| 70 | if (spacedBefore) {
|
---|
| 71 | report(context, messages.noSpaceBefore, 'noSpaceBefore', {
|
---|
| 72 | node: attrNode,
|
---|
| 73 | loc: equalToken.loc.start,
|
---|
| 74 | fix(fixer) {
|
---|
| 75 | return fixer.removeRange([attrNode.name.range[1], equalToken.range[0]]);
|
---|
| 76 | },
|
---|
| 77 | });
|
---|
| 78 | }
|
---|
| 79 | if (spacedAfter) {
|
---|
| 80 | report(context, messages.noSpaceAfter, 'noSpaceAfter', {
|
---|
| 81 | node: attrNode,
|
---|
| 82 | loc: equalToken.loc.start,
|
---|
| 83 | fix(fixer) {
|
---|
| 84 | return fixer.removeRange([equalToken.range[1], attrNode.value.range[0]]);
|
---|
| 85 | },
|
---|
| 86 | });
|
---|
| 87 | }
|
---|
| 88 | } else if (config === 'always') {
|
---|
| 89 | if (!spacedBefore) {
|
---|
| 90 | report(context, messages.needSpaceBefore, 'needSpaceBefore', {
|
---|
| 91 | node: attrNode,
|
---|
| 92 | loc: equalToken.loc.start,
|
---|
| 93 | fix(fixer) {
|
---|
| 94 | return fixer.insertTextBefore(equalToken, ' ');
|
---|
| 95 | },
|
---|
| 96 | });
|
---|
| 97 | }
|
---|
| 98 | if (!spacedAfter) {
|
---|
| 99 | report(context, messages.needSpaceAfter, 'needSpaceAfter', {
|
---|
| 100 | node: attrNode,
|
---|
| 101 | loc: equalToken.loc.start,
|
---|
| 102 | fix(fixer) {
|
---|
| 103 | return fixer.insertTextAfter(equalToken, ' ');
|
---|
| 104 | },
|
---|
| 105 | });
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | });
|
---|
| 109 | },
|
---|
| 110 | };
|
---|
| 111 | },
|
---|
| 112 | };
|
---|