@voidh4x0r/slopgate 0.1.0 → 0.1.1

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/dist/diff.js CHANGED
@@ -4,7 +4,7 @@ import { promisify } from 'node:util';
4
4
  const exec = promisify(execFile);
5
5
  const DIFF_HEADER = /^diff --git (.+)$/;
6
6
  const NEW_FILE = /^\+\+\+ (.+)$/;
7
- const HUNK = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/;
7
+ const HUNK = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
8
8
  /**
9
9
  * Git quotes paths that contain spaces, quotes or non-ASCII bytes, and appends
10
10
  * a tab when the path contains a space. `core.quotePath=false` avoids the octal
@@ -90,7 +90,46 @@ export function parseUnifiedDiff(stdout) {
90
90
  const set = { lines: new Map(), full: new Set() };
91
91
  let currentFile;
92
92
  let previousWasDevNull = false;
93
+ /** Content lines left in the current hunk, tracked per side. */
94
+ let oldRemaining = 0;
95
+ let newRemaining = 0;
96
+ /** Next line number in the new file. */
97
+ let newLine = 0;
98
+ let store;
93
99
  for (const line of stdout.split(/\r?\n/)) {
100
+ // Inside a hunk every line is content, even one that looks like a header:
101
+ // JavaScript `++ x;` produces a `+++ x;` line. Consume by hunk counts.
102
+ if (oldRemaining > 0 || newRemaining > 0) {
103
+ if (line.startsWith('\\'))
104
+ continue; // ""
105
+ if (line.startsWith('+')) {
106
+ if (newRemaining > 0) {
107
+ store?.add(newLine);
108
+ newLine += 1;
109
+ newRemaining -= 1;
110
+ }
111
+ }
112
+ else if (line.startsWith('-')) {
113
+ if (oldRemaining > 0)
114
+ oldRemaining -= 1;
115
+ }
116
+ else if (line.startsWith(' ')) {
117
+ if (oldRemaining > 0)
118
+ oldRemaining -= 1;
119
+ if (newRemaining > 0) {
120
+ newLine += 1;
121
+ newRemaining -= 1;
122
+ }
123
+ }
124
+ else {
125
+ // Malformed hunk: consume defensively rather than parsing it as a header.
126
+ if (oldRemaining > 0)
127
+ oldRemaining -= 1;
128
+ if (newRemaining > 0)
129
+ newRemaining -= 1;
130
+ }
131
+ continue;
132
+ }
94
133
  if (line.startsWith('--- ')) {
95
134
  previousWasDevNull = line.trim() === '--- /dev/null';
96
135
  continue;
@@ -115,19 +154,18 @@ export function parseUnifiedDiff(stdout) {
115
154
  if (!currentFile)
116
155
  continue;
117
156
  const hunk = HUNK.exec(line);
118
- if (!hunk?.[1])
157
+ if (!hunk?.[1] || !hunk[3])
119
158
  continue;
120
- const start = Number.parseInt(hunk[1], 10);
121
- const count = hunk[2] === undefined ? 1 : Number.parseInt(hunk[2], 10);
122
- if (!Number.isFinite(start))
159
+ oldRemaining = hunk[2] === undefined ? 1 : Number.parseInt(hunk[2], 10);
160
+ newRemaining = hunk[4] === undefined ? 1 : Number.parseInt(hunk[4], 10);
161
+ newLine = Number.parseInt(hunk[3], 10);
162
+ if (!Number.isFinite(newLine))
123
163
  continue;
124
- let store = set.lines.get(currentFile);
164
+ store = set.lines.get(currentFile);
125
165
  if (!store) {
126
166
  store = new Set();
127
167
  set.lines.set(currentFile, store);
128
168
  }
129
- for (let offset = 0; offset < count; offset += 1)
130
- store.add(start + offset);
131
169
  }
132
170
  return set;
133
171
  }
package/dist/engine.js CHANGED
@@ -5,6 +5,7 @@ import { loadProjectDeps } from "./deps.js";
5
5
  import { discoverFiles } from "./files.js";
6
6
  import { getParser } from "./parser.js";
7
7
  import { ALL_RULES, RULE_BY_ID } from "./rules/index.js";
8
+ import { collectCommentRanges } from "./rules/helpers.js";
8
9
  import { isSuppressed, readSuppressions } from "./suppress.js";
9
10
  function collectLocalPythonModules(files, deps) {
10
11
  const valid = /^[A-Za-z_][A-Za-z0-9_]*$/;
@@ -38,22 +39,6 @@ function toFinding(ruleId, draft, defaults, file) {
38
39
  ...(draft.help ? { help: draft.help } : {}),
39
40
  };
40
41
  }
41
- function commentLinesOf(root) {
42
- const lines = new Set();
43
- const stack = [root];
44
- while (stack.length > 0) {
45
- const node = stack.pop();
46
- if (!node)
47
- break;
48
- if (node.type === 'comment' || node.type === 'line_comment' || node.type === 'block_comment') {
49
- lines.add(node.startPosition.row + 1);
50
- continue;
51
- }
52
- for (const child of node.namedChildren)
53
- stack.push(child);
54
- }
55
- return lines;
56
- }
57
42
  export async function scanProject(options) {
58
43
  const started = performance.now();
59
44
  const target = path.resolve(options.root);
@@ -102,7 +87,9 @@ export async function scanProject(options) {
102
87
  skipped += 1;
103
88
  continue;
104
89
  }
105
- const source = buffer.toString('utf8');
90
+ // Editors on Windows write a UTF-8 BOM; it must not shift line/column
91
+ // numbers in findings.
92
+ const source = buffer.toString('utf8').replace(/^\uFEFF/, '');
106
93
  byLanguage[file.language] += 1;
107
94
  const parser = await getParser(file.language);
108
95
  const tree = parser.parse(source);
@@ -113,14 +100,17 @@ export async function scanProject(options) {
113
100
  const hadParseError = tree.rootNode.hasError;
114
101
  if (hadParseError)
115
102
  parseErrors += 1;
103
+ const root = asAst(tree.rootNode);
104
+ const comments = collectCommentRanges(root);
116
105
  const ctx = {
117
106
  file: file.rel,
118
107
  lines: source.split(/\r?\n/),
119
- root: asAst(tree.rootNode),
108
+ root,
120
109
  language: file.language,
121
110
  deps,
111
+ comments,
122
112
  };
123
- const suppressions = readSuppressions(source, commentLinesOf(ctx.root));
113
+ const suppressions = readSuppressions(source, new Set(comments.map((comment) => comment.startLine)));
124
114
  for (const rule of enabled) {
125
115
  if (!rule.meta.languages.includes(file.language))
126
116
  continue;
package/dist/files.d.ts CHANGED
@@ -24,7 +24,8 @@ export interface DiscoverResult {
24
24
  export declare function toPosix(p: string): string;
25
25
  /**
26
26
  * Translate a small glob dialect into a RegExp anchored at the whole path.
27
- * Supported: `**` (any depth), `*` (within one segment), `?`.
27
+ * Supported: `**` (any depth), `*` (within one segment), `?`, and `{a,b}`
28
+ * alternatives (nested braces allowed).
28
29
  */
29
30
  export declare function globToRegExp(glob: string): RegExp;
30
31
  export declare function matchesAny(relPath: string, patterns: readonly RegExp[]): boolean;
package/dist/files.js CHANGED
@@ -25,27 +25,67 @@ const EXTENSION_LANGUAGE = {
25
25
  '.cts': 'typescript',
26
26
  '.tsx': 'tsx',
27
27
  '.jsx': 'tsx',
28
- '.js': 'typescript',
29
- '.mjs': 'typescript',
30
- '.cjs': 'typescript',
28
+ // JSX is legal in .js, and the tsx grammar is a superset of plain JS.
29
+ '.js': 'tsx',
30
+ '.mjs': 'tsx',
31
+ '.cjs': 'tsx',
31
32
  };
32
33
  const MAX_FILE_BYTES = 1024 * 1024;
33
34
  export function toPosix(p) {
34
35
  return p.replace(/\\/g, '/');
35
36
  }
36
- /**
37
- * Translate a small glob dialect into a RegExp anchored at the whole path.
38
- * Supported: `**` (any depth), `*` (within one segment), `?`.
39
- */
40
- export function globToRegExp(glob) {
41
- const normalized = toPosix(glob).replace(/^\.\//, '');
37
+ /** Split `a,{b,c},d` on commas that are not inside braces. */
38
+ function splitAlternatives(value) {
39
+ const parts = [];
40
+ let depth = 0;
41
+ let start = 0;
42
+ for (let i = 0; i < value.length; i += 1) {
43
+ const ch = value[i];
44
+ if (ch === '{') {
45
+ depth += 1;
46
+ }
47
+ else if (ch === '}') {
48
+ depth -= 1;
49
+ }
50
+ else if (ch === ',' && depth === 0) {
51
+ parts.push(value.slice(start, i));
52
+ start = i + 1;
53
+ }
54
+ }
55
+ parts.push(value.slice(start));
56
+ return parts;
57
+ }
58
+ function translateGlob(glob) {
42
59
  let re = '';
43
- for (let i = 0; i < normalized.length; i += 1) {
44
- const ch = normalized[i] ?? '';
60
+ for (let i = 0; i < glob.length; i += 1) {
61
+ const ch = glob[i] ?? '';
62
+ if (ch === '{') {
63
+ let depth = 0;
64
+ let end = -1;
65
+ for (let j = i; j < glob.length; j += 1) {
66
+ if (glob[j] === '{')
67
+ depth += 1;
68
+ else if (glob[j] === '}') {
69
+ depth -= 1;
70
+ if (depth === 0) {
71
+ end = j;
72
+ break;
73
+ }
74
+ }
75
+ }
76
+ if (end === -1) {
77
+ re += '\\{';
78
+ continue;
79
+ }
80
+ const alternatives = splitAlternatives(glob.slice(i + 1, end)).map(translateGlob);
81
+ re += `(?:${alternatives.join('|')})`;
82
+ i = end;
83
+ continue;
84
+ }
45
85
  if (ch === '*') {
46
- if (normalized[i + 1] === '*') {
86
+ if (glob[i + 1] === '*') {
47
87
  i += 1;
48
- if (normalized[i + 1] === '/') {
88
+ if (glob[i + 1] === '/') {
49
89
  i += 1;
50
90
  re += '(?:[^/]+/)*';
51
91
  }
@@ -67,7 +107,16 @@ export function globToRegExp(glob) {
67
107
  re += ch;
68
108
  }
69
109
  }
70
- return new RegExp('^' + re + '$');
110
+ return re;
111
+ }
112
+ /**
113
+ * Translate a small glob dialect into a RegExp anchored at the whole path.
114
+ * Supported: `**` (any depth), `*` (within one segment), `?`, and `{a,b}`
115
+ * alternatives (nested braces allowed).
116
+ */
117
+ export function globToRegExp(glob) {
118
+ const normalized = toPosix(glob).replace(/^\.\//, '');
119
+ return new RegExp('^' + translateGlob(normalized) + '$');
71
120
  }
72
121
  export function matchesAny(relPath, patterns) {
73
122
  return patterns.some((re) => re.test(relPath));
@@ -120,19 +169,16 @@ export async function discoverFiles(options) {
120
169
  const abs = path.join(dir, entry.name);
121
170
  const rel = toPosix(path.relative(root, abs));
122
171
  if (entry.isSymbolicLink()) {
123
- skipped += 1;
124
172
  continue;
125
173
  }
126
174
  if (entry.isDirectory()) {
127
175
  if (ignoreDirs.has(entry.name) || matchesAny(rel + '/', excludeRe)) {
128
- skipped += 1;
129
176
  continue;
130
177
  }
131
178
  await walk(abs);
132
179
  continue;
133
180
  }
134
181
  if (!entry.isFile()) {
135
- skipped += 1;
136
182
  continue;
137
183
  }
138
184
  if (matchesAny(rel, excludeRe)) {
package/dist/parser.js CHANGED
@@ -37,5 +37,9 @@ export async function getParser(language) {
37
37
  return parser;
38
38
  })();
39
39
  parserCache.set(language, created);
40
+ // A failed grammar load must not be cached forever: the next call retries.
41
+ created.catch(() => {
42
+ parserCache.delete(language);
43
+ });
40
44
  return created;
41
45
  }
@@ -5,6 +5,11 @@ const SARIF_LEVEL = {
5
5
  warning: 'warning',
6
6
  info: 'note',
7
7
  };
8
+ function encodeUriPath(file) {
9
+ // Each segment is encoded separately so `/` stays a path separator while
10
+ // spaces, `#`, `?` and non-ASCII characters become valid URI references.
11
+ return file.split('/').map(encodeURIComponent).join('/');
12
+ }
8
13
  export function renderSarif(findings, version) {
9
14
  const usedRules = new Map();
10
15
  for (const finding of findings) {
@@ -45,7 +50,7 @@ export function renderSarif(findings, version) {
45
50
  locations: [
46
51
  {
47
52
  physicalLocation: {
48
- artifactLocation: { uri: finding.file },
53
+ artifactLocation: { uri: encodeUriPath(finding.file) },
49
54
  region: {
50
55
  startLine: finding.start.line,
51
56
  startColumn: finding.start.column,
@@ -1,5 +1,5 @@
1
1
  import type { AstNode } from '../ast.ts';
2
- import type { Position, RuleContext } from '../types.ts';
2
+ import type { CommentRange, Position, RuleContext } from '../types.ts';
3
3
  export declare function startOf(node: AstNode): Position;
4
4
  export declare function endOf(node: AstNode): Position;
5
5
  /** Trimmed first line of a node, capped for display. */
@@ -13,16 +13,18 @@ export declare function descendantsOfType(root: AstNode, types: readonly string[
13
13
  /** Nearest ancestor whose type is in `types` (excluding the node itself). */
14
14
  export declare function ancestorOfType(node: AstNode, types: readonly string[]): AstNode | undefined;
15
15
  /**
16
- * Comments before the first statement of a block are often attached to the
17
- * parent node instead of the block, so fall back to scanning the source lines.
16
+ * Real comment nodes in the tree. A `//` inside a string or a `#` inside a URL
17
+ * is not a comment, so rules must not scan raw source text for these markers.
18
18
  */
19
- export declare function hasCommentBetween(ctx: RuleContext, fromLine: number, toLine: number): boolean;
20
- export declare function collapseWhitespace(value: string): string;
19
+ export declare function collectCommentRanges(root: AstNode): CommentRange[];
21
20
  /**
22
- * Callee name of a call expression, normalised to the final segment:
23
- * `foo.bar.baz(...)` -> `baz`, `expect(...)` -> `expect`.
21
+ * True when a real comment overlaps the given line range. Comments before the
22
+ * first statement of a block are often attached to the parent node, so callers
23
+ * pass the enclosing statement's range (for example the whole `try`, not just
24
+ * the `except` clause).
24
25
  */
25
- export declare function calleeName(callNode: AstNode): string | undefined;
26
+ export declare function hasCommentBetween(ctx: RuleContext, fromLine: number, toLine: number): boolean;
27
+ export declare function collapseWhitespace(value: string): string;
26
28
  export declare function truncate(value: string, max: number): string;
27
29
  /**
28
30
  * Test files and fixtures are full of intentional stubs, dead branches and
@@ -31,6 +33,14 @@ export declare function truncate(value: string, max: number): string;
31
33
  * that look at tests themselves.
32
34
  */
33
35
  export declare function isTestPath(file: string): boolean;
36
+ /** Type stubs declare every function with `...`; stub rules must not fire. */
37
+ export declare function isStubFile(file: string): boolean;
38
+ /**
39
+ * Modules whose whole purpose is to provide no-op stand-ins (CPython's
40
+ * `multiprocessing/dummy`, single-threaded shims). Their empty bodies are the
41
+ * implementation, not an unfinished one.
42
+ */
43
+ export declare function isNoopModulePath(file: string): boolean;
34
44
  /** True when the file name follows a test-module convention. */
35
45
  export declare function looksLikeTestModule(file: string): boolean;
36
46
  /** Text of the function's leading docstring, if it has one. */
@@ -59,33 +59,27 @@ export function ancestorOfType(node, types) {
59
59
  return undefined;
60
60
  }
61
61
  /**
62
- * Comments before the first statement of a block are often attached to the
63
- * parent node instead of the block, so fall back to scanning the source lines.
62
+ * Real comment nodes in the tree. A `//` inside a string or a `#` inside a URL
63
+ * is not a comment, so rules must not scan raw source text for these markers.
64
+ */
65
+ export function collectCommentRanges(root) {
66
+ return descendantsOfType(root, ['comment', 'line_comment', 'block_comment']).map((node) => ({
67
+ startLine: node.startPosition.row + 1,
68
+ endLine: node.endPosition.row + 1,
69
+ }));
70
+ }
71
+ /**
72
+ * True when a real comment overlaps the given line range. Comments before the
73
+ * first statement of a block are often attached to the parent node, so callers
74
+ * pass the enclosing statement's range (for example the whole `try`, not just
75
+ * the `except` clause).
64
76
  */
65
77
  export function hasCommentBetween(ctx, fromLine, toLine) {
66
- for (let line = fromLine; line <= toLine; line += 1) {
67
- const text = ctx.lines[line - 1] ?? '';
68
- if (/(^|\s)#/.test(text) || text.includes('//') || text.trimStart().startsWith('/*')) {
69
- return true;
70
- }
71
- }
72
- return false;
78
+ return ctx.comments.some((comment) => comment.startLine <= toLine && comment.endLine >= fromLine);
73
79
  }
74
80
  export function collapseWhitespace(value) {
75
81
  return value.replace(/\s+/g, ' ').trim();
76
82
  }
77
- /**
78
- * Callee name of a call expression, normalised to the final segment:
79
- * `foo.bar.baz(...)` -> `baz`, `expect(...)` -> `expect`.
80
- */
81
- export function calleeName(callNode) {
82
- const fn = callNode.childForFieldName('function') ?? callNode.namedChildren[0];
83
- if (!fn)
84
- return undefined;
85
- const text = collapseWhitespace(fn.text);
86
- const segments = text.split('.');
87
- return segments[segments.length - 1];
88
- }
89
83
  export function truncate(value, max) {
90
84
  const single = collapseWhitespace(value);
91
85
  return single.length <= max ? single : single.slice(0, max - 3) + '...';
@@ -97,14 +91,35 @@ export function truncate(value, max) {
97
91
  * that look at tests themselves.
98
92
  */
99
93
  export function isTestPath(file) {
100
- const parts = file.toLowerCase().split('/');
94
+ const lower = file.toLowerCase();
95
+ const parts = lower.split('/');
101
96
  const base = parts[parts.length - 1] ?? '';
102
97
  if (/^test_.*\.pyi?$/.test(base) || /_tests?\.pyi?$/.test(base))
103
98
  return true;
104
99
  if (/\.(test|spec)\.(ts|tsx|js|jsx|mts|cts|mjs|cjs)$/.test(base))
105
100
  return true;
101
+ // Test/build infrastructure that pytest, nox and Sphinx import directly.
102
+ if (base === 'conftest.py' || base === 'noxfile.py')
103
+ return true;
104
+ if (lower === 'docs/conf.py' || lower.endsWith('/docs/conf.py'))
105
+ return true;
106
106
  return parts.some((part) => part === 'test' || part === 'tests' || part === '__tests__');
107
107
  }
108
+ /** Type stubs declare every function with `...`; stub rules must not fire. */
109
+ export function isStubFile(file) {
110
+ return file.toLowerCase().endsWith('.pyi');
111
+ }
112
+ /**
113
+ * Modules whose whole purpose is to provide no-op stand-ins (CPython's
114
+ * `multiprocessing/dummy`, single-threaded shims). Their empty bodies are the
115
+ * implementation, not an unfinished one.
116
+ */
117
+ export function isNoopModulePath(file) {
118
+ return file
119
+ .toLowerCase()
120
+ .split('/')
121
+ .some((part) => /dummy|noop|no-op|shims?/.test(part));
122
+ }
108
123
  /** True when the file name follows a test-module convention. */
109
124
  export function looksLikeTestModule(file) {
110
125
  const base = (file.split('/').pop() ?? '').toLowerCase();
@@ -1,6 +1,6 @@
1
1
  import { normalizePythonName } from "../deps.js";
2
2
  import { IMPLICIT_MODULES, IMPORT_TO_DISTRIBUTION, PYTHON_STDLIB } from "../data/python-stdlib.js";
3
- import { ancestorOfType, collapseWhitespace, descendantsOfType, endOf, hasCommentBetween, hasDocstring, docstringOf, insideCleanupFunction, insideMainGuard, isComment, isTestPath, looksLikeTestModule, snippetOf, startOf, statementsOf, truncate, walk, } from "./helpers.js";
3
+ import { ancestorOfType, collapseWhitespace, descendantsOfType, endOf, hasCommentBetween, hasDocstring, docstringOf, insideCleanupFunction, insideMainGuard, isComment, isNoopModulePath, isStubFile, isTestPath, looksLikeTestModule, snippetOf, startOf, statementsOf, truncate, walk, } from "./helpers.js";
4
4
  const TERMINATORS = new Set([
5
5
  'return_statement',
6
6
  'raise_statement',
@@ -15,9 +15,10 @@ function pythonImports(root) {
15
15
  if (isComment(child))
16
16
  continue;
17
17
  const target = child.type === 'aliased_import' ? child.childForFieldName('name') : child;
18
- const rootName = target?.namedChildren.find((part) => part.type === 'identifier')?.text;
19
- if (rootName)
20
- imports.push({ node, module: rootName });
18
+ const full = collapseWhitespace(target?.text ?? '');
19
+ const first = full.split('.')[0];
20
+ if (first)
21
+ imports.push({ node, module: full, root: first });
21
22
  }
22
23
  continue;
23
24
  }
@@ -25,10 +26,10 @@ function pythonImports(root) {
25
26
  if (node.namedChildren.some((child) => child.type === 'relative_import'))
26
27
  continue;
27
28
  const moduleNode = node.namedChildren.find((child) => child.type === 'dotted_name');
28
- const rootName = moduleNode?.namedChildren.find((part) => part.type === 'identifier')?.text ??
29
- moduleNode?.text.split('.')[0];
30
- if (rootName)
31
- imports.push({ node, module: rootName });
29
+ const full = collapseWhitespace(moduleNode?.text ?? '');
30
+ const first = full.split('.')[0];
31
+ if (first)
32
+ imports.push({ node, module: full, root: first });
32
33
  }
33
34
  return imports;
34
35
  }
@@ -39,12 +40,27 @@ function isOptionalImport(node) {
39
40
  return false;
40
41
  return /ImportError|ModuleNotFoundError/.test(tryNode.text);
41
42
  }
43
+ const DISTRIBUTION_PREFIXES = Object.keys(IMPORT_TO_DISTRIBUTION).sort((a, b) => b.length - a.length);
44
+ /** Longest dotted import prefix that has an explicit distribution mapping. */
45
+ function distributionFor(module) {
46
+ for (const prefix of DISTRIBUTION_PREFIXES) {
47
+ if (module === prefix || module.startsWith(`${prefix}.`)) {
48
+ return IMPORT_TO_DISTRIBUTION[prefix];
49
+ }
50
+ }
51
+ return undefined;
52
+ }
42
53
  function isDeclaredPython(module, ctx) {
43
54
  const deps = ctx.deps.python;
44
- const normalized = normalizePythonName(module);
45
- if (deps.declared.has(normalized))
46
- return true;
47
- const distribution = IMPORT_TO_DISTRIBUTION[module];
55
+ const parts = module.split('.');
56
+ // A manifest declares distributions, but same-name packages (`requests`)
57
+ // and dotted namespace packages (`google.protobuf`) both appear here.
58
+ for (let index = parts.length; index > 0; index -= 1) {
59
+ const candidate = parts.slice(0, index).join('.');
60
+ if (deps.declared.has(normalizePythonName(candidate)))
61
+ return true;
62
+ }
63
+ const distribution = distributionFor(module);
48
64
  if (distribution && deps.declared.has(normalizePythonName(distribution)))
49
65
  return true;
50
66
  return false;
@@ -170,6 +186,19 @@ function insideAbstractMethod(node) {
170
186
  }
171
187
  return false;
172
188
  }
189
+ /** `if sys.platform == "java": from java.lang import System` is platform-specific. */
190
+ function insidePlatformGuard(node) {
191
+ let current = node.parent;
192
+ while (current) {
193
+ if (current.type === 'if_statement') {
194
+ const condition = current.childForFieldName('condition')?.text ?? '';
195
+ if (/sys\.platform|platform\.system\s*\(/.test(condition))
196
+ return true;
197
+ }
198
+ current = current.parent;
199
+ }
200
+ return false;
201
+ }
173
202
  const phantomImport = {
174
203
  meta: {
175
204
  id: 'py/phantom-import',
@@ -185,23 +214,25 @@ const phantomImport = {
185
214
  return [];
186
215
  const drafts = [];
187
216
  const manifests = ctx.deps.python.manifests;
188
- for (const { node, module } of pythonImports(ctx.root)) {
189
- if (PYTHON_STDLIB.has(module))
217
+ for (const { node, module, root } of pythonImports(ctx.root)) {
218
+ if (PYTHON_STDLIB.has(root))
190
219
  continue;
191
- if (IMPLICIT_MODULES.has(module))
220
+ if (IMPLICIT_MODULES.has(root))
192
221
  continue;
193
- if (ctx.deps.ignoredModules.has(module))
222
+ if (ctx.deps.ignoredModules.has(root) || ctx.deps.ignoredModules.has(module))
194
223
  continue;
195
224
  // Private C-extension modules (`_socket`, `_decimal`, `_winapi`, ...) are
196
225
  // builtins; no project declares them in a manifest.
197
- if (module.startsWith('_'))
226
+ if (root.startsWith('_'))
198
227
  continue;
199
228
  if (isDeclaredPython(module, ctx))
200
229
  continue;
201
- if (ctx.deps.python.localModules.has(module))
230
+ if (ctx.deps.python.localModules.has(root))
202
231
  continue;
203
232
  if (isOptionalImport(node))
204
233
  continue;
234
+ if (insidePlatformGuard(node))
235
+ continue;
205
236
  drafts.push({
206
237
  message: `\`${module}\` is imported but not declared in any manifest and not present in this repository`,
207
238
  start: startOf(node),
@@ -234,6 +265,8 @@ const noopTest = {
234
265
  run(ctx) {
235
266
  if (ctx.language !== 'python')
236
267
  return [];
268
+ if (isStubFile(ctx.file))
269
+ return [];
237
270
  const drafts = [];
238
271
  for (const fn of descendantsOfType(ctx.root, ['function_definition'])) {
239
272
  const name = testFunctionName(fn);
@@ -285,7 +318,10 @@ const noopTest = {
285
318
  const helperDecorator = /contextmanager|staticmethod|classmethod|lru_cache|cached_property/.test(decoratorText);
286
319
  // Module-level `test_*` functions that take several parameters, or that
287
320
  // are wrapped by a helper decorator, are usually functions *for* tests.
288
- const likelyHelper = !inClass && (parameterCount >= 2 || helperDecorator);
321
+ // camelCase `testR(r)` is a helper convention too; pytest's own tests
322
+ // use `test_` or test classes.
323
+ const camelCase = !name.startsWith('test_');
324
+ const likelyHelper = !inClass && (parameterCount >= 2 || helperDecorator || (camelCase && parameterCount >= 1));
289
325
  // unittest-style methods routinely delegate assertions to helpers.
290
326
  const confidence = inClass || delegatesToSelf || likelyHelper ? 'medium' : 'high';
291
327
  drafts.push({
@@ -363,6 +399,9 @@ const stubBody = {
363
399
  return [];
364
400
  if (isTestPath(ctx.file))
365
401
  return [];
402
+ // Type stubs and deliberate no-op shims are made of `pass`/`...`.
403
+ if (isStubFile(ctx.file) || isNoopModulePath(ctx.file))
404
+ return [];
366
405
  const drafts = [];
367
406
  for (const fn of descendantsOfType(ctx.root, ['function_definition'])) {
368
407
  const name = fn.childForFieldName('name')?.text;
@@ -454,7 +493,9 @@ const swallowedException = {
454
493
  // Optional-dependency idiom: swallowing a missing import is the point.
455
494
  if (/ImportError|ModuleNotFoundError/.test(typeText))
456
495
  continue;
457
- const commented = hasCommentBetween(ctx, clause.startPosition.row + 1, body.endPosition.row + 1);
496
+ // Explain-the-suppression comments usually sit between the `try` body
497
+ // and the `except`, so the window starts at the whole statement.
498
+ const commented = hasCommentBetween(ctx, tryNode.startPosition.row + 1, body.endPosition.row + 1);
458
499
  const bare = typeText.length === 0;
459
500
  const broad = bare || /^(\(?\s*)?(Exception|BaseException)(\s*\))?$/.test(typeText);
460
501
  const cleanup = insideCleanupFunction(clause);