github-pages-deploy-action/node_modules/eslint/lib/rules/no-mixed-spaces-and-tabs.js

114 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

2020-03-07 11:45:40 +08:00
/**
* @fileoverview Disallow mixed spaces and tabs for indentation
* @author Jary Niebur
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "layout",
docs: {
description: "disallow mixed spaces and tabs for indentation",
category: "Stylistic Issues",
recommended: true,
url: "https://eslint.org/docs/rules/no-mixed-spaces-and-tabs"
},
schema: [
{
enum: ["smart-tabs", true, false]
}
2020-05-15 05:33:08 +08:00
],
messages: {
mixedSpacesAndTabs: "Mixed spaces and tabs."
}
2020-03-07 11:45:40 +08:00
},
create(context) {
const sourceCode = context.getSourceCode();
let smartTabs;
switch (context.options[0]) {
case true: // Support old syntax, maybe add deprecation warning here
case "smart-tabs":
smartTabs = true;
break;
default:
smartTabs = false;
}
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
return {
"Program:exit"(node) {
2020-05-15 05:33:08 +08:00
const lines = sourceCode.lines,
comments = sourceCode.getAllComments(),
ignoredCommentLines = new Set();
// Add all lines except the first ones.
comments.forEach(comment => {
for (let i = comment.loc.start.line + 1; i <= comment.loc.end.line; i++) {
ignoredCommentLines.add(i);
}
});
2020-03-07 11:45:40 +08:00
/*
* At least one space followed by a tab
* or the reverse before non-tab/-space
* characters begin.
*/
2020-06-27 02:01:06 +08:00
let regex = /^(?=( +|\t+))\1(?:\t| )/u;
2020-03-07 11:45:40 +08:00
if (smartTabs) {
/*
* At least one space followed by a tab
* before non-tab/-space characters begin.
*/
2020-06-27 02:01:06 +08:00
regex = /^(?=(\t*))\1(?=( +))\2\t/u;
2020-03-07 11:45:40 +08:00
}
lines.forEach((line, i) => {
const match = regex.exec(line);
if (match) {
2020-06-27 02:01:06 +08:00
const lineNumber = i + 1;
const loc = {
start: {
line: lineNumber,
column: match[0].length - 2
},
end: {
line: lineNumber,
column: match[0].length
}
};
2020-05-15 05:33:08 +08:00
if (!ignoredCommentLines.has(lineNumber)) {
2020-06-27 02:01:06 +08:00
const containingNode = sourceCode.getNodeByRangeIndex(sourceCode.getIndexFromLoc(loc.start));
2020-05-15 05:33:08 +08:00
if (!(containingNode && ["Literal", "TemplateElement"].includes(containingNode.type))) {
context.report({
node,
loc,
messageId: "mixedSpacesAndTabs"
});
2020-03-07 11:45:40 +08:00
}
}
}
});
}
};
}
};