eslint-plugin-stylistic-compat
Version:
Compatibility version of @stylistic/eslint-plugin for Node.js >= 12 and ESLint v8
304 lines (300 loc) • 13 kB
JavaScript
'use strict';
var _Array$from = require('/runtime-corejs3/core-js/array/from');
var _atInstanceProperty = require('/runtime-corejs3/core-js/instance/at');
var utils = require('../utils.js');
require('/runtime-corejs3/core-js/object/from-entries');
require('/runtime-corejs3/core-js/instance/map');
require('/runtime-corejs3/core-js/instance/filter');
require('/runtime-corejs3/core-js/object/entries');
require('/runtime-corejs3/core-js/set');
require('/runtime-corejs3/core-js/instance/flags');
require('/runtime-corejs3/core-js/instance/includes');
require('/runtime-corejs3/core-js/instance/starts-with');
require('../vendor.js');
require('/runtime-corejs3/core-js/object/assign');
require('/runtime-corejs3/core-js/object/create');
require('/runtime-corejs3/core-js/object/freeze');
require('/runtime-corejs3/core-js/array/is-array');
require('/runtime-corejs3/core-js/instance/last-index-of');
require('/runtime-corejs3/core-js/instance/slice');
require('/runtime-corejs3/core-js/object/define-properties');
require('/runtime-corejs3/core-js/object/keys');
require('/runtime-corejs3/core-js/instance/index-of');
require('/runtime-corejs3/core-js/symbol');
require('/runtime-corejs3/core-js/symbol/iterator');
require('/runtime-corejs3/core-js/parse-int');
require('/runtime-corejs3/core-js/parse-float');
require('/runtime-corejs3/core-js/weak-map');
require('/runtime-corejs3/core-js/object/define-property');
require('/runtime-corejs3/core-js/global-this');
require('/runtime-corejs3/core-js/instance/for-each');
require('eslint/use-at-your-own-risk');
require('eslint');
require('/runtime-corejs3/core-js/object/get-own-property-descriptor');
require('/runtime-corejs3/core-js/map');
require('/runtime-corejs3/core-js/instance/some');
require('/runtime-corejs3/core-js/instance/find');
require('/runtime-corejs3/core-js/instance/every');
require('/runtime-corejs3/core-js/instance/sort');
require('/runtime-corejs3/core-js/array/of');
require('/runtime-corejs3/core-js/instance/concat');
require('/runtime-corejs3/core-js/instance/entries');
require('/runtime-corejs3/core-js/instance/find-index');
require('/runtime-corejs3/core-js/instance/flat');
require('/runtime-corejs3/core-js/instance/keys');
require('/runtime-corejs3/core-js/instance/values');
require('/runtime-corejs3/core-js/object/get-own-property-names');
require('/runtime-corejs3/core-js/number/is-finite');
require('/runtime-corejs3/core-js/number/is-nan');
require('/runtime-corejs3/core-js/number/parse-float');
require('/runtime-corejs3/core-js/number/parse-int');
require('/runtime-corejs3/core-js/object/is');
require('/runtime-corejs3/core-js/object/is-extensible');
require('/runtime-corejs3/core-js/object/is-frozen');
require('/runtime-corejs3/core-js/object/is-sealed');
require('/runtime-corejs3/core-js/object/values');
require('/runtime-corejs3/core-js/string/from-code-point');
require('/runtime-corejs3/core-js/string/raw');
require('/runtime-corejs3/core-js/instance/code-point-at');
require('/runtime-corejs3/core-js/instance/ends-with');
require('/runtime-corejs3/core-js/instance/pad-end');
require('/runtime-corejs3/core-js/instance/pad-start');
require('/runtime-corejs3/core-js/instance/trim');
require('/runtime-corejs3/core-js/instance/trim-end');
require('/runtime-corejs3/core-js/instance/trim-left');
require('/runtime-corejs3/core-js/instance/trim-right');
require('/runtime-corejs3/core-js/instance/trim-start');
require('/runtime-corejs3/core-js/symbol/for');
require('/runtime-corejs3/core-js/symbol/key-for');
require('/runtime-corejs3/core-js/object/prevent-extensions');
require('/runtime-corejs3/core-js/object/seal');
require('/runtime-corejs3/core-js/object/get-prototype-of');
require('/runtime-corejs3/core-js/symbol/replace');
require('/runtime-corejs3/core-js/instance/bind');
require('/runtime-corejs3/core-js/instance/splice');
require('/runtime-corejs3/core-js/instance/repeat');
require('/runtime-corejs3/core-js/instance/reduce');
require('/runtime-corejs3/core-js/instance/reverse');
const messages = {
bracketLocation: "The closing bracket must be {{location}}{{details}}"
};
var jsxClosingBracketLocation = utils.createRule({
name: "jsx-closing-bracket-location",
package: "jsx",
meta: {
type: "layout",
docs: {
description: "Enforce closing bracket location in JSX"
},
fixable: "code",
messages,
schema: [{
anyOf: [{
type: "string",
enum: ["after-props", "props-aligned", "tag-aligned", "line-aligned"]
}, {
type: "object",
properties: {
location: {
type: "string",
enum: ["after-props", "props-aligned", "tag-aligned", "line-aligned"]
}
},
additionalProperties: false
}, {
type: "object",
properties: {
nonEmpty: {
oneOf: [{
type: "string",
enum: ["after-props", "props-aligned", "tag-aligned", "line-aligned"]
}, {
type: "boolean",
enum: [false]
}]
},
selfClosing: {
oneOf: [{
type: "string",
enum: ["after-props", "props-aligned", "tag-aligned", "line-aligned"]
}, {
type: "boolean",
enum: [false]
}]
}
},
additionalProperties: false
}]
}]
},
create(context) {
const MESSAGE_LOCATION = {
"after-props": "placed after the last prop",
"after-tag": "placed after the opening tag",
"props-aligned": "aligned with the last prop",
"tag-aligned": "aligned with the opening tag",
"line-aligned": "aligned with the line containing the opening tag"
};
const DEFAULT_LOCATION = "tag-aligned";
const config = context.options[0];
const options = {
nonEmpty: DEFAULT_LOCATION,
selfClosing: DEFAULT_LOCATION
};
if (typeof config === "string") {
options.nonEmpty = config;
options.selfClosing = config;
} else if (typeof config === "object") {
if ("location" in config) {
options.nonEmpty = config.location;
options.selfClosing = config.location;
}
if ("nonEmpty" in config) options.nonEmpty = config.nonEmpty;
if ("selfClosing" in config) options.selfClosing = config.selfClosing;
}
function getExpectedLocation(tokens) {
let location;
if (typeof tokens.lastProp === "undefined") location = "after-tag";else if (tokens.opening.line === tokens.lastProp.lastLine) location = "after-props";else location = tokens.selfClosing ? options.selfClosing : options.nonEmpty;
return location;
}
function getCorrectColumn(tokens, expectedLocation) {
switch (expectedLocation) {
case "props-aligned":
return tokens.lastProp.column;
case "tag-aligned":
return tokens.opening.column;
case "line-aligned":
return tokens.openingStartOfLine.column;
default:
return null;
}
}
function hasCorrectLocation(tokens, expectedLocation) {
switch (expectedLocation) {
case "after-tag":
return tokens.tag.line === tokens.closing.line;
case "after-props":
return tokens.lastProp.lastLine === tokens.closing.line;
case "props-aligned":
case "tag-aligned":
case "line-aligned":
{
const correctColumn = getCorrectColumn(tokens, expectedLocation);
return correctColumn === tokens.closing.column;
}
default:
return true;
}
}
function getIndentation(tokens, expectedLocation, correctColumn) {
const newColumn = correctColumn || 0;
let indentation;
let spaces = [];
switch (expectedLocation) {
case "props-aligned":
indentation = /^\s*/.exec(context.sourceCode.lines[tokens.lastProp.firstLine - 1])[0];
break;
case "tag-aligned":
case "line-aligned":
indentation = /^\s*/.exec(context.sourceCode.lines[tokens.opening.line - 1])[0];
break;
default:
indentation = "";
}
if (indentation.length + 1 < newColumn) {
spaces = _Array$from({
length: +correctColumn + 1 - indentation.length
});
}
return indentation + spaces.join(" ");
}
function getTokensLocations(node) {
var _exec;
const sourceCode = context.sourceCode;
const opening = sourceCode.getFirstToken(node).loc.start;
const closing = sourceCode.getLastTokens(node, node.selfClosing ? 2 : 1)[0].loc.start;
const tag = sourceCode.getFirstToken(node.name).loc.start;
let lastProp;
if (node.attributes.length) {
lastProp = node.attributes[node.attributes.length - 1];
lastProp = {
column: sourceCode.getFirstToken(lastProp).loc.start.column,
firstLine: sourceCode.getFirstToken(lastProp).loc.start.line,
lastLine: sourceCode.getLastToken(lastProp).loc.end.line
};
}
const openingLine = sourceCode.lines[opening.line - 1];
const closingLine = sourceCode.lines[closing.line - 1];
const isTab = {
openTab: /^\t/.test(openingLine),
closeTab: /^\t/.test(closingLine)
};
const openingStartOfLine = {
column: (_exec = /^\s*/.exec(openingLine)) == null ? undefined : _exec[0].length,
line: opening.line
};
return {
isTab,
tag,
opening,
closing,
lastProp,
selfClosing: node.selfClosing,
openingStartOfLine
};
}
return {
"JSXOpeningElement:exit": function (node) {
var _context, _context2;
const lastAttributeNode = _atInstanceProperty(_context = node.attributes).call(_context, -1);
const cachedLastAttributeEndPos = lastAttributeNode ? lastAttributeNode.range[1] : null;
let expectedNextLine;
const tokens = getTokensLocations(node);
let expectedLocation = getExpectedLocation(tokens);
let usingSameIndentation = true;
if (expectedLocation === "tag-aligned") usingSameIndentation = tokens.isTab.openTab === tokens.isTab.closeTab;
const lastComment = _atInstanceProperty(_context2 = context.sourceCode.getCommentsInside(node)).call(_context2, -1);
const hasTrailingComment = lastComment && lastComment.range[0] > (lastAttributeNode != null ? lastAttributeNode : node.name).range[1];
if ((expectedLocation === "after-props" || expectedLocation === "after-tag") && !(hasCorrectLocation(tokens, expectedLocation) && usingSameIndentation) && hasTrailingComment) {
expectedLocation = "line-aligned";
}
if (hasCorrectLocation(tokens, expectedLocation) && usingSameIndentation) return;
const data = {
location: MESSAGE_LOCATION[expectedLocation],
details: ""
};
const correctColumn = getCorrectColumn(tokens, expectedLocation);
if (correctColumn !== null) {
expectedNextLine = tokens.lastProp && tokens.lastProp.lastLine === tokens.closing.line;
data.details = ` (expected column ${correctColumn + 1}${expectedNextLine ? " on the next line)" : ")"}`;
}
context.report({
node,
messageId: "bracketLocation",
loc: tokens.closing,
data,
fix(fixer) {
const closingTag = tokens.selfClosing ? "/>" : ">";
switch (expectedLocation) {
case "after-tag":
if (cachedLastAttributeEndPos) return fixer.replaceTextRange([cachedLastAttributeEndPos, node.range[1]], (expectedNextLine ? "\n" : "") + closingTag);
return fixer.replaceTextRange([node.name.range[1], node.range[1]], (expectedNextLine ? "\n" : " ") + closingTag);
case "after-props":
return fixer.replaceTextRange([cachedLastAttributeEndPos, node.range[1]], (expectedNextLine ? "\n" : "") + closingTag);
case "props-aligned":
case "tag-aligned":
case "line-aligned":
{
const rangeStart = hasTrailingComment ? lastComment.range[1] : cachedLastAttributeEndPos;
return fixer.replaceTextRange([rangeStart, node.range[1]], `
${getIndentation(tokens, expectedLocation, correctColumn)}${closingTag}`);
}
}
return null;
}
});
}
};
}
});
module.exports = jsxClosingBracketLocation;