package file→ .shtukbase/packages/std-editorconfig/checks/ec03_indent.ts
checks/ec03_indent.ts
33 lines, 1.7 KB.
- // EC-R03 (observe): indentation follows indent_style (spaces or tabs), and lines fit max_line_length. A heuristic:
- // multi-line strings, here-documents and aligned continuations can trip it, so the norm observes.
- import { finding, PKG, targets } from './eclib.ts';
- import { finish, norm, readTextFile } from './lib/std.ts';
- const n = norm(PKG, 'EC-R03', 'observe');
- const out: string[] = [];
- if (n.active) {
- const { ec, files, maxBytes } = targets();
- for (const f of files) {
- const p = ec.props(f);
- const style = p.indent_style;
- const max = Number(p.max_line_length);
- if (style !== 'space' && style !== 'tab' && !(max > 0)) continue;
- const text = readTextFile(f, maxBytes);
- if (text === null) continue;
- const tabs: number[] = [];
- const spaces: number[] = [];
- const long: number[] = [];
- text.split('\n').forEach((raw, i) => {
- const l = raw.replace(/\r$/, '');
- const lead = /^[ \t]*/.exec(l)?.[0] ?? '';
- if (style === 'space' && lead.includes('\t')) tabs.push(i + 1);
- // tab style: a run of two or more spaces before the text; one space (` * ` in a block comment) is allowed.
- if (style === 'tab' && / {2,}/.test(lead) && l.trim()) spaces.push(i + 1);
- if (max > 0 && [...l].length > max) long.push(i + 1);
- });
- if (tabs.length) out.push(finding(f, tabs, 'indented with tabs, but indent_style = space', 'replace the tabs in the indent with spaces'));
- if (spaces.length) out.push(finding(f, spaces, 'indented with spaces, but indent_style = tab', 'replace the spaces in the indent with tabs'));
- if (long.length) out.push(finding(f, long, `the line is longer than max_line_length = ${max}`, 'wrap the line'));
- }
- }
- finish(n, out);