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-17.6.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.5.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.4.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.3.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.2.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.1.1 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.1.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-17.0.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-16.29.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-16.28.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-16.27.1 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-16.27.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-16.26.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-16.25.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-16.24.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-16.23.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-16.22.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-16.21.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-16.20.1 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js
govuk_publishing_components-16.20.0 node_modules/eslint-plugin-react/lib/rules/jsx-props-no-multi-spaces.js