Skip to content

Commit 95746a7

Browse files
authored
docs(packages): add @example blocks to packages module exports (#175)
* docs(env): add @example blocks to env module exports * docs(packages): add @example blocks to packages module exports
1 parent 6871860 commit 95746a7

9 files changed

Lines changed: 277 additions & 0 deletions

File tree

src/packages/edit.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ function getUtil() {
167167

168168
/**
169169
* Get the EditablePackageJson class for package.json manipulation.
170+
*
171+
* @example
172+
* ```typescript
173+
* const EditablePackageJson = getEditablePackageJsonClass()
174+
* const pkg = await EditablePackageJson.load('/tmp/my-project')
175+
* console.log(pkg.content.name)
176+
* ```
170177
*/
171178
/*@__NO_SIDE_EFFECTS__*/
172179
export function getEditablePackageJsonClass(): EditablePackageJsonConstructor {
@@ -467,6 +474,11 @@ export function getEditablePackageJsonClass(): EditablePackageJsonConstructor {
467474

468475
/**
469476
* Convert a package.json object to an editable instance.
477+
*
478+
* @example
479+
* ```typescript
480+
* const editable = pkgJsonToEditable({ name: 'my-pkg', version: '1.0.0' })
481+
* ```
470482
*/
471483
/*@__NO_SIDE_EFFECTS__*/
472484
export function pkgJsonToEditable(
@@ -485,6 +497,14 @@ export function pkgJsonToEditable(
485497

486498
/**
487499
* Convert package.json to editable instance with file persistence.
500+
*
501+
* @example
502+
* ```typescript
503+
* const editable = await toEditablePackageJson(
504+
* { name: 'my-pkg', version: '1.0.0' },
505+
* { path: '/tmp/my-project' }
506+
* )
507+
* ```
488508
*/
489509
/*@__NO_SIDE_EFFECTS__*/
490510
export async function toEditablePackageJson(
@@ -519,6 +539,14 @@ export async function toEditablePackageJson(
519539

520540
/**
521541
* Convert package.json to editable instance with file persistence synchronously.
542+
*
543+
* @example
544+
* ```typescript
545+
* const editable = toEditablePackageJsonSync(
546+
* { name: 'my-pkg', version: '1.0.0' },
547+
* { path: '/tmp/my-project' }
548+
* )
549+
* ```
522550
*/
523551
/*@__NO_SIDE_EFFECTS__*/
524552
export function toEditablePackageJsonSync(

src/packages/exports.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ import { isObject, isObjectObject } from '../objects'
99

1010
/**
1111
* Find types definition for a specific subpath in package exports.
12+
*
13+
* @example
14+
* ```typescript
15+
* const exports = { '.': { types: './dist/index.d.ts', import: './dist/index.js' } }
16+
* const types = findTypesForSubpath(exports, './dist/index.js')
17+
* // types === './dist/index.d.ts'
18+
* ```
1219
*/
1320
/*@__NO_SIDE_EFFECTS__*/
1421
export function findTypesForSubpath(
@@ -53,6 +60,12 @@ export function findTypesForSubpath(
5360

5461
/**
5562
* Get subpaths from package exports.
63+
*
64+
* @example
65+
* ```typescript
66+
* const exports = { '.': './index.js', './utils': './utils.js' }
67+
* getSubpaths(exports) // ['.', './utils']
68+
* ```
5669
*/
5770
/*@__NO_SIDE_EFFECTS__*/
5871
export function getSubpaths(entryExports: unknown): string[] {
@@ -67,6 +80,12 @@ export function getSubpaths(entryExports: unknown): string[] {
6780

6881
/**
6982
* Get file paths from package exports.
83+
*
84+
* @example
85+
* ```typescript
86+
* const exports = { '.': './dist/index.js', './utils': './dist/utils.js' }
87+
* getExportFilePaths(exports) // ['./dist/index.js', './dist/utils.js']
88+
* ```
7089
*/
7190
/*@__NO_SIDE_EFFECTS__*/
7291
export function getExportFilePaths(entryExports: unknown): string[] {
@@ -119,6 +138,12 @@ export function getExportFilePaths(entryExports: unknown): string[] {
119138

120139
/**
121140
* Check if package exports use conditional patterns (e.g., import/require).
141+
*
142+
* @example
143+
* ```typescript
144+
* isConditionalExports({ import: './index.mjs', require: './index.cjs' }) // true
145+
* isConditionalExports({ '.': './index.js' }) // false
146+
* ```
122147
*/
123148
/*@__NO_SIDE_EFFECTS__*/
124149
export function isConditionalExports(entryExports: unknown): boolean {
@@ -145,6 +170,12 @@ export function isConditionalExports(entryExports: unknown): boolean {
145170

146171
/**
147172
* Check if package exports use subpath patterns (keys starting with '.').
173+
*
174+
* @example
175+
* ```typescript
176+
* isSubpathExports({ '.': './index.js', './utils': './utils.js' }) // true
177+
* isSubpathExports({ import: './index.mjs' }) // false
178+
* ```
148179
*/
149180
/*@__NO_SIDE_EFFECTS__*/
150181
export function isSubpathExports(entryExports: unknown): boolean {
@@ -165,6 +196,15 @@ export function isSubpathExports(entryExports: unknown): boolean {
165196

166197
/**
167198
* Normalize package.json exports field to canonical format.
199+
*
200+
* @example
201+
* ```typescript
202+
* resolvePackageJsonEntryExports('./index.js')
203+
* // { '.': './index.js' }
204+
*
205+
* resolvePackageJsonEntryExports({ '.': './index.js' })
206+
* // { '.': './index.js' }
207+
* ```
168208
*/
169209
/*@__NO_SIDE_EFFECTS__*/
170210
export function resolvePackageJsonEntryExports(entryExports: unknown): unknown {

src/packages/licenses.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ export interface LicenseVisitor {
7474

7575
/**
7676
* Collect licenses that are incompatible (copyleft).
77+
*
78+
* @example
79+
* ```typescript
80+
* const nodes = [{ license: 'MIT' }, { license: 'GPL-3.0' }]
81+
* const incompatible = collectIncompatibleLicenses(nodes)
82+
* // incompatible contains only the GPL-3.0 node
83+
* ```
7784
*/
7885
/*@__NO_SIDE_EFFECTS__*/
7986
export function collectIncompatibleLicenses(
@@ -91,6 +98,12 @@ export function collectIncompatibleLicenses(
9198

9299
/**
93100
* Collect warnings from license nodes.
101+
*
102+
* @example
103+
* ```typescript
104+
* const nodes = [{ license: 'UNLICENSED' }]
105+
* collectLicenseWarnings(nodes) // ['Package is unlicensed']
106+
* ```
94107
*/
95108
/*@__NO_SIDE_EFFECTS__*/
96109
export function collectLicenseWarnings(licenseNodes: LicenseNode[]): string[] {
@@ -112,6 +125,13 @@ export function collectLicenseWarnings(licenseNodes: LicenseNode[]): string[] {
112125

113126
/**
114127
* Create an AST node from a raw node.
128+
*
129+
* @example
130+
* ```typescript
131+
* const raw = { license: 'MIT' }
132+
* const node = createAstNode(raw)
133+
* // node.type === 'License'
134+
* ```
115135
*/
116136
/*@__NO_SIDE_EFFECTS__*/
117137
export function createAstNode(rawNode: SpdxAstNode): InternalAstNode {
@@ -122,6 +142,17 @@ export function createAstNode(rawNode: SpdxAstNode): InternalAstNode {
122142

123143
/**
124144
* Create a binary operation AST node.
145+
*
146+
* @example
147+
* ```typescript
148+
* const raw = {
149+
* left: { license: 'MIT' },
150+
* conjunction: 'OR' as const,
151+
* right: { license: 'Apache-2.0' }
152+
* }
153+
* const node = createBinaryOperationNode(raw)
154+
* // node.type === 'BinaryOperation'
155+
* ```
125156
*/
126157
/*@__NO_SIDE_EFFECTS__*/
127158
export function createBinaryOperationNode(
@@ -156,6 +187,12 @@ export function createBinaryOperationNode(
156187

157188
/**
158189
* Create a license AST node.
190+
*
191+
* @example
192+
* ```typescript
193+
* const node = createLicenseNode({ license: 'MIT' })
194+
* // node.type === 'License' && node.license === 'MIT'
195+
* ```
159196
*/
160197
/*@__NO_SIDE_EFFECTS__*/
161198
export function createLicenseNode(
@@ -170,6 +207,12 @@ export function createLicenseNode(
170207

171208
/**
172209
* Parse an SPDX license expression into an AST.
210+
*
211+
* @example
212+
* ```typescript
213+
* const ast = parseSpdxExp('MIT OR Apache-2.0')
214+
* // ast is a BinaryOperation node with MIT and Apache-2.0 leaves
215+
* ```
173216
*/
174217
/*@__NO_SIDE_EFFECTS__*/
175218
export function parseSpdxExp(spdxExp: string): SpdxAstNode | undefined {
@@ -184,6 +227,12 @@ export function parseSpdxExp(spdxExp: string): SpdxAstNode | undefined {
184227

185228
/**
186229
* Parse package license field into structured license nodes.
230+
*
231+
* @example
232+
* ```typescript
233+
* const nodes = resolvePackageLicenses('MIT', '/tmp/my-project')
234+
* // [{ license: 'MIT' }]
235+
* ```
187236
*/
188237
/*@__NO_SIDE_EFFECTS__*/
189238
export function resolvePackageLicenses(
@@ -238,6 +287,16 @@ export function resolvePackageLicenses(
238287

239288
/**
240289
* Traverse SPDX license AST and invoke visitor callbacks for each node.
290+
*
291+
* @example
292+
* ```typescript
293+
* const ast = parseSpdxExp('MIT OR Apache-2.0')
294+
* const licenses: string[] = []
295+
* if (ast) {
296+
* visitLicenses(ast, { License(node) { licenses.push(node.license) } })
297+
* }
298+
* // licenses === ['MIT', 'Apache-2.0']
299+
* ```
241300
*/
242301
/*@__NO_SIDE_EFFECTS__*/
243302
export function visitLicenses(ast: SpdxAstNode, visitor: LicenseVisitor): void {

src/packages/manifest.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ const pkgScopePrefixRegExp = /^@socketregistry\//
3232

3333
/**
3434
* Create a package.json object for a Socket registry package.
35+
*
36+
* @example
37+
* ```typescript
38+
* const pkgJson = createPackageJson('is-number', 'packages/npm/is-number', {
39+
* version: '1.0.0',
40+
* description: 'Check if a value is a number'
41+
* })
42+
* ```
3543
*/
3644
/*@__NO_SIDE_EFFECTS__*/
3745
export function createPackageJson(
@@ -125,6 +133,11 @@ export function createPackageJson(
125133

126134
/**
127135
* Fetch the manifest for a package.
136+
*
137+
* @example
138+
* ```typescript
139+
* const manifest = await fetchPackageManifest('lodash@4.17.21')
140+
* ```
128141
*/
129142
/*@__NO_SIDE_EFFECTS__*/
130143
export async function fetchPackageManifest(
@@ -170,6 +183,11 @@ export async function fetchPackageManifest(
170183

171184
/**
172185
* Fetch the packument (package document) for a package.
186+
*
187+
* @example
188+
* ```typescript
189+
* const packument = await fetchPackagePackument('lodash')
190+
* ```
173191
*/
174192
/*@__NO_SIDE_EFFECTS__*/
175193
export async function fetchPackagePackument(

src/packages/normalize.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ function getEscapedScopeRegExp(): RegExp {
2525

2626
/**
2727
* Normalize a package.json object with standard npm package normalization.
28+
*
29+
* @example
30+
* ```typescript
31+
* const pkgJson = { name: 'my-pkg', version: '1.0.0' }
32+
* const normalized = normalizePackageJson(pkgJson)
33+
* ```
2834
*/
2935
/*@__NO_SIDE_EFFECTS__*/
3036
export function normalizePackageJson(
@@ -67,6 +73,12 @@ export function normalizePackageJson(
6773

6874
/**
6975
* Extract escaped scope from a Socket registry package name.
76+
*
77+
* @example
78+
* ```typescript
79+
* resolveEscapedScope('babel__core') // 'babel__'
80+
* resolveEscapedScope('lodash') // undefined
81+
* ```
7082
*/
7183
/*@__NO_SIDE_EFFECTS__*/
7284
export function resolveEscapedScope(
@@ -79,6 +91,11 @@ export function resolveEscapedScope(
7991

8092
/**
8193
* Resolve original package name from Socket registry package name.
94+
*
95+
* @example
96+
* ```typescript
97+
* resolveOriginalPackageName('@socketregistry/is-number') // 'is-number'
98+
* ```
8299
*/
83100
/*@__NO_SIDE_EFFECTS__*/
84101
export function resolveOriginalPackageName(sockRegPkgName: string): string {
@@ -93,6 +110,11 @@ export function resolveOriginalPackageName(sockRegPkgName: string): string {
93110

94111
/**
95112
* Convert escaped scope to standard npm scope format.
113+
*
114+
* @example
115+
* ```typescript
116+
* unescapeScope('babel__') // '@babel'
117+
* ```
96118
*/
97119
/*@__NO_SIDE_EFFECTS__*/
98120
export function unescapeScope(escapedScope: string): string {

0 commit comments

Comments
 (0)