Skip to content

Commit 2a72f48

Browse files
committed
feat: add jsdoc
1 parent 1836489 commit 2a72f48

File tree

10 files changed

+160
-24
lines changed

10 files changed

+160
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
"magic-string": "^0.30.7",
8686
"picomatch": "^4.0.1",
8787
"unplugin": "^1.8.0",
88-
"unplugin-replace": "link:/Users/kevin/Developer/open-source/unplugin-replace"
88+
"unplugin-replace": "^0.2.1"
8989
},
9090
"devDependencies": {
9191
"@babel/types": "^7.24.0",

pnpm-lock.yaml

Lines changed: 23 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* This entry file is for exposing the core API.
3+
*
4+
* @module
5+
*/
6+
17
export {
28
scanFiles,
39
scanEnums,
@@ -6,3 +12,5 @@ export {
612
type EnumDeclaration,
713
type EnumData,
814
} from './core/enum'
15+
16+
export { type Options, resolveOptions } from './core/options'

src/core/enum.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,56 @@ import picomatch from 'picomatch'
88
import type { Expression, PrivateName } from '@babel/types'
99
import type { OptionsResolved } from './options'
1010

11+
/**
12+
* Represents the scan options for the enum.
13+
*/
1114
export type ScanOptions = Pick<
1215
OptionsResolved,
1316
'scanDir' | 'scanMode' | 'scanPattern'
1417
>
1518

19+
/**
20+
* Represents a member of an enum.
21+
*/
1622
export interface EnumMember {
1723
readonly name: string
1824
readonly value: string | number
1925
}
2026

27+
/**
28+
* Represents a declaration of an enum.
29+
*/
2130
export interface EnumDeclaration {
2231
readonly id: string
2332
readonly range: readonly [start: number, end: number]
2433
readonly members: ReadonlyArray<EnumMember>
2534
}
2635

36+
/**
37+
* Represents the data of all enums.
38+
*/
2739
export interface EnumData {
2840
readonly declarations: {
2941
readonly [file: string]: ReadonlyArray<EnumDeclaration>
3042
}
3143
readonly defines: { readonly [id_key: `${string}.${string}`]: string }
3244
}
3345

46+
/**
47+
* Evaluates a JavaScript expression and returns the result.
48+
* @param exp - The expression to evaluate.
49+
* @returns The evaluated result.
50+
*/
3451
function evaluate(exp: string): string | number {
3552
return new Function(`return ${exp}`)()
3653
}
3754

38-
// this is called in the build script entry once
39-
// so the data can be shared across concurrent Rollup processes
40-
export function scanEnums(options: ScanOptions) {
55+
/**
56+
* Scans the specified directory for enums based on the provided options.
57+
* @param options - The scan options for the enum.
58+
* @returns The data of all enums found.
59+
*/
60+
export function scanEnums(options: ScanOptions): EnumData {
4161
const declarations: { [file: string]: EnumDeclaration[] } =
4262
Object.create(null)
4363

@@ -184,6 +204,11 @@ export function scanEnums(options: ScanOptions) {
184204
return enumData
185205
}
186206

207+
/**
208+
* Scans the specified directory for files based on the provided options.
209+
* @param options - The scan options for the files.
210+
* @returns The list of files found.
211+
*/
187212
export function scanFiles(options: ScanOptions): string[] {
188213
if (options.scanMode === 'fs') {
189214
return fg.sync(options.scanPattern, {

src/core/options.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,42 @@
11
import process from 'node:process'
22
import type { FilterPattern } from '@rollup/pluginutils'
33

4+
/**
5+
* Represents the options for the plugin.
6+
*/
47
export interface Options {
58
include?: FilterPattern
69
exclude?: FilterPattern
710
enforce?: 'pre' | 'post' | undefined
811
scanMode?: 'git' | 'fs'
912
/**
13+
* The directory to scan for enum files.
1014
* @default process.cwd()
1115
*/
1216
scanDir?: string
1317
/**
18+
* The pattern used to match enum files.
1419
* @default '**\/*.{cts,mts,ts,tsx}'
1520
*/
1621
scanPattern?: string | string[]
1722
}
1823

1924
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U
2025

26+
/**
27+
* Represents the resolved options for the plugin.
28+
*/
2129
export type OptionsResolved = Overwrite<
2230
Required<Options>,
2331
Pick<Options, 'enforce'>
2432
>
2533

26-
export function resolveOption(options: Options): OptionsResolved {
34+
/**
35+
* Resolves the options for the plugin.
36+
* @param options - The options to resolve.
37+
* @returns The resolved options.
38+
*/
39+
export function resolveOptions(options: Options): OptionsResolved {
2740
return {
2841
include: options.include || [/\.[cm]?[jt]sx?$/],
2942
exclude: options.exclude || [/node_modules/],

src/esbuild.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1-
import unplugin from '.'
1+
/**
2+
* This entry file is for esbuild plugin.
3+
*
4+
* @module
5+
*/
26

7+
import unplugin from './index'
8+
9+
/**
10+
* Esbuild plugin
11+
*
12+
* @example
13+
* ```ts
14+
* // esbuild.config.js
15+
* import { build } from 'esbuild'
16+
*
17+
* build({
18+
* plugins: [require('unplugin-inline-enum/esbuild')()],
19+
* })
20+
* ```
21+
*/
322
export default unplugin.esbuild

src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1+
/**
2+
* This entry file is for main unplugin.
3+
* @module
4+
*/
5+
16
import { createUnplugin } from 'unplugin'
27
import { createFilter } from '@rollup/pluginutils'
38
import MagicString from 'magic-string'
49
import ReplacePlugin from 'unplugin-replace'
510
import { type Options, resolveOption } from './core/options'
611
import { scanEnums } from './core/enum'
712

13+
/**
14+
* The main unplugin instance.
15+
*/
816
export default createUnplugin<Options | undefined, true>(
917
(rawOptions = {}, meta) => {
1018
const options = resolveOption(rawOptions)

src/rollup.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1-
import unplugin from '.'
1+
/**
2+
* This entry file is for Rollup plugin.
3+
*
4+
* @module
5+
*/
26

7+
import unplugin from './index'
8+
9+
/**
10+
* Rollup plugin
11+
*
12+
* @example
13+
* ```ts
14+
* // rollup.config.js
15+
* import Macros from 'unplugin-inline-enum/rollup'
16+
*
17+
* export default {
18+
* plugins: [Macros()],
19+
* }
20+
* ```
21+
*/
322
export default unplugin.rollup

src/vite.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1-
import unplugin from '.'
1+
/**
2+
* This entry file is for Vite plugin.
3+
*
4+
* @module
5+
*/
26

7+
import unplugin from './index'
8+
9+
/**
10+
* Vite plugin
11+
*
12+
* @example
13+
* ```ts
14+
* // vite.config.ts
15+
* import Macros from 'unplugin-inline-enum/vite'
16+
*
17+
* export default defineConfig({
18+
* plugins: [Macros()],
19+
* })
20+
* ```
21+
*/
322
export default unplugin.vite

src/webpack.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1-
import unplugin from '.'
1+
/**
2+
* This entry file is for webpack plugin.
3+
*
4+
* @module
5+
*/
26

7+
import unplugin from './index'
8+
9+
/**
10+
* Webpack plugin
11+
*
12+
* @example
13+
* ```ts
14+
* // webpack.config.js
15+
* module.exports = {
16+
* plugins: [require('unplugin-inline-enum/webpack')()],
17+
* }
18+
* ```
19+
*/
320
export default unplugin.webpack

0 commit comments

Comments
 (0)