Sha256: 9f1f2344b9310c7ca6d0d85bad207643e83b9c6e7057c459139e9f99f1d65ebf

Contents?: true

Size: 1.78 KB

Versions: 43

Compression:

Stored size: 1.78 KB

Contents

/**
 * @fileoverview Validate closing tag location in JSX
 * @author Ross Solomon
 */
'use strict';

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

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
  meta: {
    docs: {
      description: 'Validate closing tag location for multiline JSX',
      category: 'Stylistic Issues',
      recommended: false,
      url: docsUrl('jsx-closing-tag-location')
    },
    fixable: 'whitespace'
  },

  create: function(context) {
    return {
      JSXClosingElement: function(node) {
        if (!node.parent) {
          return;
        }

        const opening = node.parent.openingElement;
        if (opening.loc.start.line === node.loc.start.line) {
          return;
        }

        if (opening.loc.start.column === node.loc.start.column) {
          return;
        }

        let message;
        if (!astUtil.isNodeFirstInLine(context, node)) {
          message = 'Closing tag of a multiline JSX expression must be on its own line.';
        } else {
          message = 'Expected closing tag to match indentation of opening.';
        }

        context.report({
          node: node,
          loc: node.loc,
          message,
          fix: function(fixer) {
            const indent = Array(opening.loc.start.column + 1).join(' ');
            if (astUtil.isNodeFirstInLine(context, node)) {
              return fixer.replaceTextRange(
                [node.range[0] - node.loc.start.column, node.range[0]],
                indent
              );
            }

            return fixer.insertTextBefore(node, `\n${indent}`);
          }
        });
      }
    };
  }
};

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-closing-tag-location.js
govuk_publishing_components-17.21.0 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.20.0 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.19.1 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.19.0 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.18.0 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.17.0 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.16.0 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.15.0 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.14.0 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.13.0 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.12.2 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.12.1 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.12.0 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.11.0 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.10.0 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.9.0 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.8.0 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.7.0 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js
govuk_publishing_components-17.6.1 node_modules/eslint-plugin-react/lib/rules/jsx-closing-tag-location.js