[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 |
|
---|
| 23 | module.exports = {
|
---|
| 24 | meta: {
|
---|
| 25 | docs: {
|
---|
| 26 | description: 'Enforce or disallow spaces around equal signs in JSX attributes',
|
---|
| 27 | category: 'Stylistic Issues',
|
---|
| 28 | recommended: false,
|
---|
| 29 | url: docsUrl('jsx-equals-spacing'),
|
---|
| 30 | },
|
---|
| 31 | fixable: 'code',
|
---|
| 32 |
|
---|
| 33 | messages,
|
---|
| 34 |
|
---|
| 35 | schema: [{
|
---|
| 36 | enum: ['always', 'never'],
|
---|
| 37 | }],
|
---|
| 38 | },
|
---|
| 39 |
|
---|
| 40 | create(context) {
|
---|
| 41 | const config = context.options[0] || 'never';
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * Determines a given attribute node has an equal sign.
|
---|
| 45 | * @param {ASTNode} attrNode - The attribute node.
|
---|
| 46 | * @returns {boolean} Whether or not the attriute node has an equal sign.
|
---|
| 47 | */
|
---|
| 48 | function hasEqual(attrNode) {
|
---|
| 49 | return attrNode.type !== 'JSXSpreadAttribute' && attrNode.value !== null;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | // --------------------------------------------------------------------------
|
---|
| 53 | // Public
|
---|
| 54 | // --------------------------------------------------------------------------
|
---|
| 55 |
|
---|
| 56 | return {
|
---|
| 57 | JSXOpeningElement(node) {
|
---|
| 58 | node.attributes.forEach((attrNode) => {
|
---|
| 59 | if (!hasEqual(attrNode)) {
|
---|
| 60 | return;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | const sourceCode = getSourceCode(context);
|
---|
| 64 | const equalToken = sourceCode.getTokenAfter(attrNode.name);
|
---|
| 65 | const spacedBefore = sourceCode.isSpaceBetweenTokens(attrNode.name, equalToken);
|
---|
| 66 | const spacedAfter = sourceCode.isSpaceBetweenTokens(equalToken, attrNode.value);
|
---|
| 67 |
|
---|
| 68 | if (config === 'never') {
|
---|
| 69 | if (spacedBefore) {
|
---|
| 70 | report(context, messages.noSpaceBefore, 'noSpaceBefore', {
|
---|
| 71 | node: attrNode,
|
---|
| 72 | loc: equalToken.loc.start,
|
---|
| 73 | fix(fixer) {
|
---|
| 74 | return fixer.removeRange([attrNode.name.range[1], equalToken.range[0]]);
|
---|
| 75 | },
|
---|
| 76 | });
|
---|
| 77 | }
|
---|
| 78 | if (spacedAfter) {
|
---|
| 79 | report(context, messages.noSpaceAfter, 'noSpaceAfter', {
|
---|
| 80 | node: attrNode,
|
---|
| 81 | loc: equalToken.loc.start,
|
---|
| 82 | fix(fixer) {
|
---|
| 83 | return fixer.removeRange([equalToken.range[1], attrNode.value.range[0]]);
|
---|
| 84 | },
|
---|
| 85 | });
|
---|
| 86 | }
|
---|
| 87 | } else if (config === 'always') {
|
---|
| 88 | if (!spacedBefore) {
|
---|
| 89 | report(context, messages.needSpaceBefore, 'needSpaceBefore', {
|
---|
| 90 | node: attrNode,
|
---|
| 91 | loc: equalToken.loc.start,
|
---|
| 92 | fix(fixer) {
|
---|
| 93 | return fixer.insertTextBefore(equalToken, ' ');
|
---|
| 94 | },
|
---|
| 95 | });
|
---|
| 96 | }
|
---|
| 97 | if (!spacedAfter) {
|
---|
| 98 | report(context, messages.needSpaceAfter, 'needSpaceAfter', {
|
---|
| 99 | node: attrNode,
|
---|
| 100 | loc: equalToken.loc.start,
|
---|
| 101 | fix(fixer) {
|
---|
| 102 | return fixer.insertTextAfter(equalToken, ' ');
|
---|
| 103 | },
|
---|
| 104 | });
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | });
|
---|
| 108 | },
|
---|
| 109 | };
|
---|
| 110 | },
|
---|
| 111 | };
|
---|