Sha256: 47eba21724e4ba634105839f846230b934f642414f0fedbded10fe7a18c7230d

Contents?: true

Size: 1.79 KB

Versions: 43

Compression:

Stored size: 1.79 KB

Contents

/**
 * @fileoverview Disallow multiple spaces between inline JSX props
 * @author Adrian Moennich
 */

'use strict';

const docsUrl = require('../util/docsUrl');

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

module.exports = {
  meta: {
    docs: {
      description: 'Disallow multiple spaces between inline JSX props',
      category: 'Stylistic Issues',
      recommended: false,
      url: docsUrl('jsx-props-no-multi-spaces')
    },
    fixable: 'code',
    schema: []
  },

  create: function (context) {
    const sourceCode = context.getSourceCode();

    function getPropName(propNode) {
      switch (propNode.type) {
        case 'JSXSpreadAttribute':
          return sourceCode.getText(propNode.argument);
        case 'JSXIdentifier':
          return propNode.name;
        case 'JSXMemberExpression':
          return `${getPropName(propNode.object)}.${propNode.property.name}`;
        default:
          return propNode.name.name;
      }
    }

    function checkSpacing(prev, node) {
      if (prev.loc.end.line !== node.loc.end.line) {
        return;
      }
      const between = sourceCode.text.slice(prev.range[1], node.range[0]);
      if (between !== ' ') {
        context.report({
          node: node,
          message: `Expected only one space between "${getPropName(prev)}" and "${getPropName(node)}"`,
          fix: function(fixer) {
            return fixer.replaceTextRange([prev.range[1], node.range[0]], ' ');
          }
        });
      }
    }

    return {
      JSXOpeningElement: function (node) {
        node.attributes.reduce((prev, prop) => {
          checkSpacing(prev, prop);
          return prop;
        }, node.name);
      }
    };
  }
};

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
govuk_publishing_components-18.0.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.21.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.20.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.19.1 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.19.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.18.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.17.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.16.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.15.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.14.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.13.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.12.2 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.12.1 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.12.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.11.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.10.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.9.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.8.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.7.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.6.1 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js