ShtukbaseStandard
包文件→ .shtukbase/packages/std-editorconfig/checks/ec03_indent.ts

checks/ec03_indent.ts

33 行, 1.7 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // EC-R03 (observe): indentation follows indent_style (spaces or tabs), and lines fit max_line_length. A heuristic:
  2. // multi-line strings, here-documents and aligned continuations can trip it, so the norm observes.
  3. import { finding, PKG, targets } from './eclib.ts';
  4. import { finish, norm, readTextFile } from './lib/std.ts';
  5. const n = norm(PKG, 'EC-R03', 'observe');
  6. const out: string[] = [];
  7. if (n.active) {
  8. const { ec, files, maxBytes } = targets();
  9. for (const f of files) {
  10. const p = ec.props(f);
  11. const style = p.indent_style;
  12. const max = Number(p.max_line_length);
  13. if (style !== 'space' && style !== 'tab' && !(max > 0)) continue;
  14. const text = readTextFile(f, maxBytes);
  15. if (text === null) continue;
  16. const tabs: number[] = [];
  17. const spaces: number[] = [];
  18. const long: number[] = [];
  19. text.split('\n').forEach((raw, i) => {
  20. const l = raw.replace(/\r$/, '');
  21. const lead = /^[ \t]*/.exec(l)?.[0] ?? '';
  22. if (style === 'space' && lead.includes('\t')) tabs.push(i + 1);
  23. // tab style: a run of two or more spaces before the text; one space (` * ` in a block comment) is allowed.
  24. if (style === 'tab' && / {2,}/.test(lead) && l.trim()) spaces.push(i + 1);
  25. if (max > 0 && [...l].length > max) long.push(i + 1);
  26. });
  27. if (tabs.length) out.push(finding(f, tabs, 'indented with tabs, but indent_style = space', 'replace the tabs in the indent with spaces'));
  28. if (spaces.length) out.push(finding(f, spaces, 'indented with spaces, but indent_style = tab', 'replace the spaces in the indent with tabs'));
  29. if (long.length) out.push(finding(f, long, `the line is longer than max_line_length = ${max}`, 'wrap the line'));
  30. }
  31. }
  32. finish(n, out);