Skip to content

Commit 67ee6c3

Browse files
committed
fix(docs): isolate command inventory helpers
1 parent c4fa0dc commit 67ee6c3

8 files changed

Lines changed: 68 additions & 39 deletions

File tree

‎package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@
9292
"check:hooks": "node scripts/fleet/check.mts --category=hooks",
9393
"check:native-package": "node scripts/repo/build/sea/check.mts",
9494
"check:paths": "node scripts/fleet/check/paths-are-valid.mts",
95-
"docs:commands:check": "node scripts/repo/check/docs-commands.mts",
96-
"docs:commands:test": "node scripts/fleet/test.mts test/repo/unit/check/docs-command-inventory.test.mts test/repo/unit/check/docs-commands.test.mts",
95+
"docs:commands:check": "node scripts/repo/check/docs-commands-are-complete.mts",
96+
"docs:commands:test": "node scripts/fleet/test.mts test/repo/unit/docs/commands/inventory.test.mts test/repo/unit/check/docs-commands-are-complete.test.mts",
9797
"check:test": "node scripts/fleet/commands/run.mts check test",
9898
"check:test-budget-headroom": "node scripts/fleet/check/test-budget-headroom.mts",
9999
"check:types": "node scripts/fleet/check.mts --category=types",

scripts/repo/check/docs-commands.mts renamed to scripts/repo/check/docs-commands-are-complete.mts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ import { fromMarkdown } from 'mdast-util-from-markdown'
66
import { gfmFromMarkdown } from 'mdast-util-gfm'
77
import { gfm } from 'micromark-extension-gfm'
88

9-
import { extractCommandInventory } from './docs-command-inventory.mts'
9+
import { extractCommandInventory } from '../docs/commands/inventory.mts'
1010
import { REPO_ROOT } from '../../fleet/paths.mts'
1111
import { isMainModule } from '../../fleet/process/is-main-module.mts'
1212
import { runMain } from '../../fleet/process/main/run.mts'
1313

14-
import type { CommandInventory } from './docs-command-inventory.mts'
14+
import type { CommandInventory } from '../docs/commands/inventory.mts'
1515
import type { ScriptMeta } from '../../fleet/process/main/run.mts'
1616

1717
export const SCRIPT_META: ScriptMeta = {
1818
describe: 'Check the exact CLI command and alias inventory in its reference.',
1919
help: 'Usage: pnpm run docs:commands:check',
20+
json: 'result',
2021
}
2122

2223
interface MarkdownNode {
@@ -54,14 +55,14 @@ function textContent(node: MarkdownNode): string {
5455
}
5556

5657
function inlineCode(node: MarkdownNode | undefined): string | undefined {
57-
if (
58-
node?.type === 'tableCell' &&
59-
nodeChildren(node).length === 1 &&
60-
nodeChildren(node)[0]?.type === 'inlineCode'
61-
) {
62-
return textContent(nodeChildren(node)[0])
58+
if (node?.type !== 'tableCell') {
59+
return undefined
6360
}
64-
return undefined
61+
const children = nodeChildren(node)
62+
const child = children[0]
63+
return children.length === 1 && child?.type === 'inlineCode'
64+
? textContent(child)
65+
: undefined
6566
}
6667

6768
function linkTarget(node: MarkdownNode | undefined): string | undefined {
@@ -118,6 +119,8 @@ function sectionRows(
118119
const headerCells = header.children
119120
if (
120121
headerCells.length !== 2 ||
122+
headerCells[0] === undefined ||
123+
headerCells[1] === undefined ||
121124
textContent(headerCells[0]) !== headers[0] ||
122125
textContent(headerCells[1]) !== headers[1]
123126
) {
@@ -153,7 +156,7 @@ function uniqueRows(
153156
}
154157
result.set(row.name, row)
155158
}
156-
return { __proto__: null, rows: result, problems }
159+
return { rows: result, problems }
157160
}
158161

159162
function validGuide(

scripts/repo/check/docs-command-custom-routes.mts renamed to scripts/repo/docs/commands/custom-routes.mts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ import {
55
parseModule,
66
stringLiteral,
77
visitAst,
8-
} from './docs-command-inventory-lib.mts'
8+
} from './util.mts'
99

10-
import type {
11-
AstNode,
12-
ParsedModule,
13-
SourceReader,
14-
} from './docs-command-inventory-lib.mts'
10+
import type { AstNode, ParsedModule, SourceReader } from './util.mts'
1511

1612
export type CustomRouteKind = 'patch' | 'sfw'
1713

@@ -256,7 +252,11 @@ function firewallCaModes(sourceReader: SourceReader): string[] {
256252
) {
257253
return
258254
}
259-
if (!isPositionalsFirstFallback(node.arguments[0])) {
255+
const argumentsList = node['arguments']
256+
if (
257+
!Array.isArray(argumentsList) ||
258+
!isPositionalsFirstFallback(argumentsList[0])
259+
) {
260260
return
261261
}
262262
allowlistValidated = true

scripts/repo/check/docs-command-inventory.mts renamed to scripts/repo/docs/commands/inventory.mts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@ import {
55
objectProperties,
66
parseModule,
77
sourceError,
8-
staticPropertyKey,
98
stringLiteral,
10-
} from './docs-command-inventory-lib.mts'
11-
import { extractCustomRoutes } from './docs-command-custom-routes.mts'
9+
} from './util.mts'
10+
import { extractCustomRoutes } from './custom-routes.mts'
1211

13-
import type { CustomRouteKind } from './docs-command-custom-routes.mts'
12+
import type { CustomRouteKind } from './custom-routes.mts'
1413

15-
import type {
16-
AstNode,
17-
ParsedModule,
18-
SourceReader,
19-
} from './docs-command-inventory-lib.mts'
14+
import type { AstNode, ParsedModule, SourceReader } from './util.mts'
2015

2116
export interface CommandAlias {
2217
alias: string
@@ -187,8 +182,9 @@ function subcommandConfig(
187182
}
188183
}
189184
findCalls(body)
190-
if (calls.length > 1) {
191-
throw sourceError(module.path, calls[1], 'multiple subcommand routers')
185+
const secondCall = calls[1]
186+
if (secondCall !== undefined) {
187+
throw sourceError(module.path, secondCall, 'multiple subcommand routers')
192188
}
193189
const call = calls[0]
194190
if (call === undefined) {

scripts/repo/check/docs-command-inventory-lib.mts renamed to scripts/repo/docs/commands/util.mts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,33 @@ import { parseAst } from 'rolldown/parseAst'
55

66
export interface AstNode extends Record<string, unknown> {
77
type: string
8+
arguments?: unknown | undefined
9+
body?: unknown | undefined
10+
callee?: unknown | undefined
11+
computed?: unknown | undefined
12+
consequent?: unknown | undefined
13+
declaration?: unknown | undefined
14+
declarations?: unknown | undefined
15+
elements?: unknown | undefined
16+
id?: unknown | undefined
17+
imported?: unknown | undefined
18+
init?: unknown | undefined
19+
key?: unknown | undefined
20+
kind?: unknown | undefined
21+
left?: unknown | undefined
22+
local?: unknown | undefined
23+
loc?: unknown | undefined
24+
name?: unknown | undefined
25+
object?: unknown | undefined
26+
operator?: unknown | undefined
27+
properties?: unknown | undefined
28+
property?: unknown | undefined
29+
right?: unknown | undefined
30+
source?: unknown | undefined
31+
specifiers?: unknown | undefined
32+
start?: unknown | undefined
33+
test?: unknown | undefined
34+
value?: unknown | undefined
835
}
936

1037
export type SourceReader = (sourcePath: string) => string
@@ -20,7 +47,7 @@ export interface ParsedModule {
2047
export const isRecord = isPlainObject
2148

2249
export function isNode(value: unknown): value is AstNode {
23-
return isRecord(value) && typeof value.type === 'string'
50+
return isRecord(value) && typeof value['type'] === 'string'
2451
}
2552

2653
export function nodeArray(value: unknown, message: string): AstNode[] {
@@ -95,8 +122,10 @@ export function sourceError(
95122
reason: string,
96123
): Error {
97124
const loc =
98-
isRecord(node.loc) && isRecord(node.loc.start) ? node.loc.start : undefined
99-
const line = loc && typeof loc.line === 'number' ? loc.line : 1
125+
isRecord(node.loc) && isRecord(node.loc['start'])
126+
? node.loc['start']
127+
: undefined
128+
const line = loc && typeof loc['line'] === 'number' ? loc['line'] : 1
100129
return new Error(`${sourcePath}:${line}: ${reason}`)
101130
}
102131

@@ -165,9 +194,9 @@ function annotateSourceLocations(root: AstNode, source: string): void {
165194
visitAst(root, node => {
166195
const loc = isRecord(node.loc) ? node.loc : undefined
167196
const start =
168-
loc !== undefined && isRecord(loc.start) ? loc.start : undefined
197+
loc !== undefined && isRecord(loc['start']) ? loc['start'] : undefined
169198
if (
170-
(start !== undefined && typeof start.line === 'number') ||
199+
(start !== undefined && typeof start['line'] === 'number') ||
171200
typeof node.start !== 'number'
172201
) {
173202
return

‎test/integration/cli/cmd-config-auto.test.mts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ describe('socket config auto', async () => {
5959
- apiToken -- The Socket API token required to access most Socket API endpoints
6060
- defaultOrg -- The default org slug to use; usually the org your Socket API token has access to. When set, all orgSlug arguments are implied to be this value.
6161
- enforcedOrgs -- Orgs in this list have their security policies enforced on this machine
62+
- oauthSession -- The selected Socket OAuth issuer and client
6263
- org -- Alias for defaultOrg
6364
- skipAskToPersistDefaultOrg -- This flag prevents the Socket CLI from asking you to persist the org slug when you selected one interactively"
6465
`)

test/repo/unit/check/docs-commands.test.mts renamed to test/repo/unit/check/docs-commands-are-complete.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'vitest'
22

3-
import { checkCommandDocumentation } from '../../../../scripts/repo/check/docs-commands.mts'
3+
import { checkCommandDocumentation } from '../../../../scripts/repo/check/docs-commands-are-complete.mts'
44

55
const inventory = {
66
commands: ['alpha', 'alpha child', 'patch list'],

test/repo/unit/check/docs-command-inventory.test.mts renamed to test/repo/unit/docs/commands/inventory.test.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { describe, expect, it } from 'vitest'
22

3-
import { extractCommandInventory } from '../../../../scripts/repo/check/docs-command-inventory.mts'
4-
import { extractCustomRoutes } from '../../../../scripts/repo/check/docs-command-custom-routes.mts'
5-
import { checkCommandDocumentation } from '../../../../scripts/repo/check/docs-commands.mts'
3+
import { extractCommandInventory } from '../../../../../scripts/repo/docs/commands/inventory.mts'
4+
import { extractCustomRoutes } from '../../../../../scripts/repo/docs/commands/custom-routes.mts'
5+
import { checkCommandDocumentation } from '../../../../../scripts/repo/check/docs-commands-are-complete.mts'
66

77
function sourceReader(
88
files: Record<string, string>,

0 commit comments

Comments
 (0)