@alxwlw/repo-rules 1.8.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alxwlw/repo-rules",
3
- "version": "1.8.0",
3
+ "version": "1.9.0",
4
4
  "description": "Whole-repo lint rules from the alxwlw defect corpus — long files, unjustified suppressions, skipped/focused tests, Number(x) || <default>, removed assertions — with a shrink-only baseline.",
5
5
  "license": "MIT",
6
6
  "repository": {
package/src/diff.mjs CHANGED
@@ -2,56 +2,17 @@
2
2
  /** `removed-assertions` (DP-014, spec § 4). */
3
3
  import { git, RepoRulesError } from './git.mjs';
4
4
  import { matcher } from './glob.mjs';
5
- import { commentStart, familyOf, hasReason } from './source.mjs';
5
+ import { assertionPattern, familyOf, hasReason, lineOf } from './source.mjs';
6
6
 
7
7
  /** @typedef {import('./config.mjs').Config} Config */
8
8
 
9
- const JS_ASSERT = /\bexpect\s*\(|\bassert\s*\(|\bassert\.\w+\s*\(/g;
10
- const SOL_ASSERT = /\bassert[A-Z]\w*\s*\(/g;
11
- const PY_ASSERT = /^\s*assert\b|\bself\.assert[A-Z]\w*\s*\(/g;
12
-
13
- /**
14
- * @param {string} path
15
- * @returns {RegExp | null}
16
- */
17
- function assertionPattern(path) {
18
- if (/\.(?:ts|tsx|js|jsx|mjs|cjs|mts|cts)$/.test(path)) return JS_ASSERT;
19
- if (path.endsWith('.sol')) return SOL_ASSERT;
20
- if (path.endsWith('.py')) return PY_ASSERT;
21
- return null;
22
- }
23
-
24
- /**
25
- * The code of a diff line: its comments cut out (DP-030). A `/* … *\/` closed on the same line is
26
- * whitespace and the code after it stays; `//`, an unclosed `/*`, a leading `*` and Python `#` cut to the end.
27
- * @param {string} text
28
- * @param {import('./source.mjs').Family} family
29
- * @returns {string}
30
- */
31
- function codeOf(text, family) {
32
- let code = '';
33
- let rest = text;
34
- // After the first span the rest is no line start: the `;` keeps a `*` there from reading as a continuation.
35
- let pad = '';
36
- for (;;) {
37
- const c = commentStart(pad + rest, family);
38
- if (c < 0) return code + rest;
39
- const cut = c - pad.length;
40
- const end = !rest.startsWith('/*', cut) ? -1 : rest.indexOf('*/', cut + 2);
41
- if (end < 0) return code + rest.slice(0, cut);
42
- code += `${rest.slice(0, cut)} `;
43
- rest = rest.slice(end + 2);
44
- pad = ';';
45
- }
46
- }
47
-
48
9
  /**
49
10
  * Assertions in the code part of a diff line: a commented-out `// expect(…)` is not one (DP-030).
50
11
  * @param {string} text
51
12
  * @param {Side} side
52
13
  */
53
14
  function countIn(text, side) {
54
- return [...(side.family ? codeOf(text, side.family) : text).matchAll(side.re)].length;
15
+ return [...(side.family ? lineOf(text, side.family).code : text).matchAll(side.re)].length;
55
16
  }
56
17
 
57
18
  /** @type {Record<string, string>} */
package/src/rules.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  // @ts-check
2
2
  /** The whole-repo rules of spec § 2. Each returns `{ line, message }` hits, with `line` 1-based. */
3
- import { commentStart, dashReason, hasReason } from './source.mjs';
3
+ import { dashReason, hasReason, lineOf } from './source.mjs';
4
4
 
5
5
  /** @typedef {import('./source.mjs').Family} Family */
6
6
  /** @typedef {{ line: number, message: string }} Hit */
@@ -26,10 +26,8 @@ export function suppressionReason(lines, family) {
26
26
  /** @type {Hit[]} */
27
27
  const hits = [];
28
28
  lines.forEach((raw, i) => {
29
- const line = raw.endsWith('\r') ? raw.slice(0, -1) : raw;
30
- const c = commentStart(line, family);
31
- if (c < 0) return;
32
- const comment = line.slice(c);
29
+ const { comment } = lineOf(raw, family);
30
+ if (comment === '') return;
33
31
  /** @type {[string, boolean] | null} */
34
32
  let found = null;
35
33
  if (family === 'py') {
@@ -70,9 +68,7 @@ export function focusedSkippedTests(lines) {
70
68
  /** @type {Hit[]} */
71
69
  const hits = [];
72
70
  lines.forEach((raw, i) => {
73
- const line = raw.endsWith('\r') ? raw.slice(0, -1) : raw;
74
- const c = commentStart(line, 'js');
75
- const code = c < 0 ? line : line.slice(0, c);
71
+ const { code, comment: own } = lineOf(raw, 'js');
76
72
  if (ONLY.test(code)) {
77
73
  hits.push({ line: i + 1, message: '.only in a test file' });
78
74
  return;
@@ -85,8 +81,7 @@ export function focusedSkippedTests(lines) {
85
81
  let arg = code.slice((m.index ?? 0) + m[0].length).trimStart();
86
82
  if (arg === '' && i + 1 < lines.length) arg = /** @type {string} */ (lines[i + 1]).trimStart();
87
83
  if (m[1] === 'skip' && !m[0].includes('.each') && arg !== '' && !/^[)'"`]/.test(arg)) return;
88
- const own = c < 0 ? '' : line.slice(c);
89
- const prev = i > 0 ? /** @type {string} */ (lines[i - 1]).replace(/\r$/, '') : '';
84
+ const prev = i > 0 ? lineOf(/** @type {string} */ (lines[i - 1]), 'js').text : '';
90
85
  const prevIsComment = /^\s*(?:\/\/|\/\*|\*)/.test(prev);
91
86
  if (hasReason(dashReason(own)) || (prevIsComment && hasReason(dashReason(prev)))) return;
92
87
  hits.push({ line: i + 1, message: `${m[1] ? `.${m[1]}` : m[2]} without a reason` });
@@ -129,9 +124,7 @@ export function numberOrDefault(lines) {
129
124
  /** @type {Hit[]} */
130
125
  const hits = [];
131
126
  lines.forEach((raw, i) => {
132
- const line = raw.endsWith('\r') ? raw.slice(0, -1) : raw;
133
- const c = commentStart(line, 'js');
134
- const code = c < 0 ? line : line.slice(0, c);
127
+ const { code } = lineOf(raw, 'js');
135
128
  for (const m of code.matchAll(NUMBER_CALL)) {
136
129
  const close = closingParen(code, (m.index ?? 0) + m[0].length - 1);
137
130
  if (close < 0) continue;
package/src/source.mjs CHANGED
@@ -3,18 +3,45 @@
3
3
 
4
4
  /** @typedef {'js' | 'sol' | 'py'} Family */
5
5
 
6
- const JS_EXT = /\.(?:ts|tsx|js|jsx|mjs|cjs|mts|cts|vue|svelte)$/;
6
+ /**
7
+ * The one language table: extension, comment family, and the assertion pattern of that family's
8
+ * test files. Vue and Svelte files are read as JS, by scan and by removed-assertions alike.
9
+ * @type {{ ext: RegExp, family: Family, assertions: RegExp }[]}
10
+ */
11
+ const LANGUAGES = [
12
+ {
13
+ ext: /\.(?:ts|tsx|js|jsx|mjs|cjs|mts|cts|vue|svelte)$/,
14
+ family: 'js',
15
+ assertions: /\bexpect\s*\(|\bassert\s*\(|\bassert\.\w+\s*\(/g,
16
+ },
17
+ { ext: /\.sol$/, family: 'sol', assertions: /\bassert[A-Z]\w*\s*\(/g },
18
+ { ext: /\.py$/, family: 'py', assertions: /^\s*assert\b|\bself\.assert[A-Z]\w*\s*\(/g },
19
+ ];
20
+
21
+ /**
22
+ * @param {string} path
23
+ * @returns {(typeof LANGUAGES)[number] | null}
24
+ */
25
+ function languageOf(path) {
26
+ if (path.endsWith('.d.ts')) return null;
27
+ return LANGUAGES.find((l) => l.ext.test(path)) ?? null;
28
+ }
7
29
 
8
30
  /**
9
31
  * @param {string} path
10
32
  * @returns {Family | null}
11
33
  */
12
34
  export function familyOf(path) {
13
- if (path.endsWith('.d.ts')) return null;
14
- if (JS_EXT.test(path)) return 'js';
15
- if (path.endsWith('.sol')) return 'sol';
16
- if (path.endsWith('.py')) return 'py';
17
- return null;
35
+ return languageOf(path)?.family ?? null;
36
+ }
37
+
38
+ /**
39
+ * The assertion pattern of a test file by its path; null when the path has no language.
40
+ * @param {string} path
41
+ * @returns {RegExp | null}
42
+ */
43
+ export function assertionPattern(path) {
44
+ return languageOf(path)?.assertions ?? null;
18
45
  }
19
46
 
20
47
  /**
@@ -49,6 +76,44 @@ export function commentStart(line, family) {
49
76
  return -1;
50
77
  }
51
78
 
79
+ /** @typedef {{ text: string, code: string, comment: string }} Line */
80
+
81
+ /**
82
+ * The views of one raw line: `text` without the trailing CR, `code` with its comments cut out (DP-030),
83
+ * `comment` from the comment opener to the end (`''` when there is none). A `/* … *\/` closed on the
84
+ * same line is whitespace and the code after it stays; `//`, an unclosed `/*`, a leading `*` and
85
+ * Python `#` cut to the end.
86
+ * @param {string} raw
87
+ * @param {Family} family
88
+ * @returns {Line}
89
+ */
90
+ export function lineOf(raw, family) {
91
+ const text = raw.endsWith('\r') ? raw.slice(0, -1) : raw;
92
+ const start = commentStart(text, family);
93
+ const comment = start < 0 ? '' : text.slice(start);
94
+ let code = '';
95
+ let rest = text;
96
+ // After the first span the rest is no line start: the `;` keeps a `*` there from reading as a continuation.
97
+ let pad = '';
98
+ for (;;) {
99
+ const c = commentStart(pad + rest, family);
100
+ if (c < 0) {
101
+ code += rest;
102
+ break;
103
+ }
104
+ const cut = c - pad.length;
105
+ const end = !rest.startsWith('/*', cut) ? -1 : rest.indexOf('*/', cut + 2);
106
+ if (end < 0) {
107
+ code += rest.slice(0, cut);
108
+ break;
109
+ }
110
+ code += `${rest.slice(0, cut)} `;
111
+ rest = rest.slice(end + 2);
112
+ pad = ';';
113
+ }
114
+ return { text, code, comment };
115
+ }
116
+
52
117
  /**
53
118
  * At least three non-space characters.
54
119
  * @param {string | undefined} text