UNPKG

eslint-plugin-smarter-tabs

Version:
86 lines (85 loc) 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.rules = void 0; function getIndentLevel(line) { const match = line.match(/^\t+/); return match ? match[0].length : 0; } function lintNode(context, node) { var _a, _b; const lines = context.getSourceCode().getText(node).split(/\r?\n/); for (const [index, line] of lines.entries()) { const lineNumber = ((_b = (_a = node.loc) === null || _a === void 0 ? void 0 : _a.start.line) !== null && _b !== void 0 ? _b : 0) + index; if (/^\t+\/\//.test(line)) { continue; } const inlineTab = line.match(/(\S *)(\t+)/); if (inlineTab && inlineTab.index !== undefined) { context.report({ message: 'Inline tabulation', loc: { start: { line: lineNumber, column: inlineTab.index + inlineTab[1].length, }, end: { line: lineNumber, column: inlineTab.index + inlineTab[1].length + inlineTab[2].length, }, }, }); continue; } const spacesUsedForIndentation = line.match(/^(\t*) /); const indentLevel = getIndentLevel(line); const prevIndentLevel = index > 0 ? getIndentLevel(lines[index - 1]) : indentLevel; const nextIndentLevel = index < (lines.length - 1) ? getIndentLevel(lines[index + 1]) : indentLevel; if (spacesUsedForIndentation && (![nextIndentLevel, prevIndentLevel].includes(indentLevel) || (indentLevel !== prevIndentLevel && prevIndentLevel > nextIndentLevel))) { context.report({ message: 'Spaces used for indentation', loc: { start: { line: lineNumber, column: indentLevel > prevIndentLevel ? (spacesUsedForIndentation[1].length - (indentLevel - prevIndentLevel)) : 0, }, end: { line: lineNumber, column: spacesUsedForIndentation[1].length, }, }, }); continue; } const isPrevLineEmpty = index > 0 ? (lines[index - 1].length === 0) : undefined; if (prevIndentLevel !== undefined && isPrevLineEmpty === false && (indentLevel - prevIndentLevel) >= 2) { context.report({ message: 'Mismatched indentation', loc: { start: { line: lineNumber, column: indentLevel - (indentLevel - prevIndentLevel - 1), }, end: { line: lineNumber, column: indentLevel - 1, }, }, }); } } } exports.rules = { 'smarter-tabs': { meta: { type: 'layout', docs: { description: 'enforce the usage of smart tabs', category: 'stylistic issues', url: 'https://github.com/cheap-glitch/eslint-plugin-smarter-tabs#readme', }, }, create: context => ({ '[parent.type="Program"]': (node) => lintNode(context, node), }), }, };