eslint-plugin-stylistic-compat
Version:
Compatibility version of @stylistic/eslint-plugin for Node.js >= 12 and ESLint v8
180 lines (176 loc) • 7.95 kB
JavaScript
'use strict';
var _Set = require('/runtime-corejs3/core-js/set');
var _forEachInstanceProperty = require('/runtime-corejs3/core-js/instance/for-each');
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/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/instance/at');
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/array/from');
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');
var noTrailingSpaces = utils.createRule({
name: "no-trailing-spaces",
package: "js",
meta: {
type: "layout",
docs: {
description: "Disallow trailing whitespace at the end of lines"
},
fixable: "whitespace",
schema: [{
type: "object",
properties: {
skipBlankLines: {
type: "boolean",
default: false
},
ignoreComments: {
type: "boolean",
default: false
}
},
additionalProperties: false
}],
messages: {
trailingSpace: "Trailing spaces not allowed."
}
},
create(context) {
const sourceCode = context.sourceCode;
const BLANK_CLASS = "[ \xA0\u2000-\u200B\u3000]";
const SKIP_BLANK = `^${BLANK_CLASS}*$`;
const NONBLANK = `${BLANK_CLASS}+$`;
const options = context.options[0] || {};
const skipBlankLines = options.skipBlankLines || false;
const ignoreComments = options.ignoreComments || false;
function report(node, location, fixRange) {
context.report({
node,
loc: location,
messageId: "trailingSpace",
fix(fixer) {
return fixer.removeRange(fixRange);
}
});
}
function getCommentLineNumbers(comments) {
const lines = /* @__PURE__ */new _Set();
_forEachInstanceProperty(comments).call(comments, comment => {
const endLine = comment.type === "Block" ? comment.loc.end.line - 1 : comment.loc.end.line;
for (let i = comment.loc.start.line; i <= endLine; i++) lines.add(i);
});
return lines;
}
return {
Program: function checkTrailingSpaces(node) {
const re = new RegExp(NONBLANK, "u");
const skipMatch = new RegExp(SKIP_BLANK, "u");
const lines = sourceCode.lines;
const linebreaks = sourceCode.getText().match(utils.createGlobalLinebreakMatcher());
const comments = sourceCode.getAllComments();
const commentLineNumbers = getCommentLineNumbers(comments);
let totalLength = 0;
for (let i = 0, ii = lines.length; i < ii; i++) {
const lineNumber = i + 1;
const linebreakLength = linebreaks && linebreaks[i] ? linebreaks[i].length : 1;
const lineLength = lines[i].length + linebreakLength;
const matches = re.exec(lines[i]);
if (matches) {
const location = {
start: {
line: lineNumber,
column: matches.index
},
end: {
line: lineNumber,
column: lineLength - linebreakLength
}
};
const rangeStart = totalLength + location.start.column;
const rangeEnd = totalLength + location.end.column;
const containingNode = sourceCode.getNodeByRangeIndex(rangeStart);
if (containingNode && containingNode.type === "TemplateElement" && rangeStart > containingNode.parent.range[0] && rangeEnd < containingNode.parent.range[1]) {
totalLength += lineLength;
continue;
}
if (skipBlankLines && skipMatch.test(lines[i])) {
totalLength += lineLength;
continue;
}
const fixRange = [rangeStart, rangeEnd];
if (!ignoreComments || !commentLineNumbers.has(lineNumber)) report(node, location, fixRange);
}
totalLength += lineLength;
}
}
};
}
});
module.exports = noTrailingSpaces;