Skip to content

Commit

Permalink
feat: add jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Mar 4, 2024
1 parent 1836489 commit 2a72f48
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"magic-string": "^0.30.7",
"picomatch": "^4.0.1",
"unplugin": "^1.8.0",
"unplugin-replace": "link:/Users/kevin/Developer/open-source/unplugin-replace"
"unplugin-replace": "^0.2.1"
},
"devDependencies": {
"@babel/types": "^7.24.0",
Expand Down
38 changes: 23 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* This entry file is for exposing the core API.
*
* @module
*/

export {
scanFiles,
scanEnums,
Expand All @@ -6,3 +12,5 @@ export {
type EnumDeclaration,
type EnumData,
} from './core/enum'

export { type Options, resolveOptions } from './core/options'
31 changes: 28 additions & 3 deletions src/core/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,56 @@ import picomatch from 'picomatch'
import type { Expression, PrivateName } from '@babel/types'
import type { OptionsResolved } from './options'

/**
* Represents the scan options for the enum.
*/
export type ScanOptions = Pick<
OptionsResolved,
'scanDir' | 'scanMode' | 'scanPattern'
>

/**
* Represents a member of an enum.
*/
export interface EnumMember {
readonly name: string
readonly value: string | number
}

/**
* Represents a declaration of an enum.
*/
export interface EnumDeclaration {
readonly id: string
readonly range: readonly [start: number, end: number]
readonly members: ReadonlyArray<EnumMember>
}

/**
* Represents the data of all enums.
*/
export interface EnumData {
readonly declarations: {
readonly [file: string]: ReadonlyArray<EnumDeclaration>
}
readonly defines: { readonly [id_key: `${string}.${string}`]: string }
}

/**
* Evaluates a JavaScript expression and returns the result.
* @param exp - The expression to evaluate.
* @returns The evaluated result.
*/
function evaluate(exp: string): string | number {
return new Function(`return ${exp}`)()
}

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

Expand Down Expand Up @@ -184,6 +204,11 @@ export function scanEnums(options: ScanOptions) {
return enumData
}

/**
* Scans the specified directory for files based on the provided options.
* @param options - The scan options for the files.
* @returns The list of files found.
*/
export function scanFiles(options: ScanOptions): string[] {
if (options.scanMode === 'fs') {
return fg.sync(options.scanPattern, {
Expand Down
15 changes: 14 additions & 1 deletion src/core/options.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
import process from 'node:process'
import type { FilterPattern } from '@rollup/pluginutils'

/**
* Represents the options for the plugin.
*/
export interface Options {
include?: FilterPattern
exclude?: FilterPattern
enforce?: 'pre' | 'post' | undefined
scanMode?: 'git' | 'fs'
/**
* The directory to scan for enum files.
* @default process.cwd()
*/
scanDir?: string
/**
* The pattern used to match enum files.
* @default '**\/*.{cts,mts,ts,tsx}'
*/
scanPattern?: string | string[]
}

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

/**
* Represents the resolved options for the plugin.
*/
export type OptionsResolved = Overwrite<
Required<Options>,
Pick<Options, 'enforce'>
>

export function resolveOption(options: Options): OptionsResolved {
/**
* Resolves the options for the plugin.
* @param options - The options to resolve.
* @returns The resolved options.
*/
export function resolveOptions(options: Options): OptionsResolved {
return {
include: options.include || [/\.[cm]?[jt]sx?$/],
exclude: options.exclude || [/node_modules/],
Expand Down
21 changes: 20 additions & 1 deletion src/esbuild.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
import unplugin from '.'
/**
* This entry file is for esbuild plugin.
*
* @module
*/

import unplugin from './index'

/**
* Esbuild plugin
*
* @example
* ```ts
* // esbuild.config.js
* import { build } from 'esbuild'
*
* build({
* plugins: [require('unplugin-inline-enum/esbuild')()],
* })
* ```
*/
export default unplugin.esbuild
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
/**
* This entry file is for main unplugin.
* @module
*/

import { createUnplugin } from 'unplugin'

Check failure on line 6 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18)

Cannot find module 'unplugin'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?

Check failure on line 6 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 20)

Cannot find module 'unplugin'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?

Check failure on line 6 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 18)

Cannot find module 'unplugin'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?

Check failure on line 6 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 20)

Cannot find module 'unplugin'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
import { createFilter } from '@rollup/pluginutils'

Check failure on line 7 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18)

Cannot find module '@rollup/pluginutils'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?

Check failure on line 7 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 20)

Cannot find module '@rollup/pluginutils'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?

Check failure on line 7 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 18)

Cannot find module '@rollup/pluginutils'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?

Check failure on line 7 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 20)

Cannot find module '@rollup/pluginutils'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
import MagicString from 'magic-string'

Check failure on line 8 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18)

Cannot find module 'magic-string'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?

Check failure on line 8 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 20)

Cannot find module 'magic-string'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?

Check failure on line 8 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 18)

Cannot find module 'magic-string'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?

Check failure on line 8 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 20)

Cannot find module 'magic-string'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
import ReplacePlugin from 'unplugin-replace'

Check failure on line 9 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18)

Cannot find module 'unplugin-replace'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?

Check failure on line 9 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 20)

Cannot find module 'unplugin-replace'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?

Check failure on line 9 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 18)

Cannot find module 'unplugin-replace'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?

Check failure on line 9 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 20)

Cannot find module 'unplugin-replace'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
import { type Options, resolveOption } from './core/options'

Check failure on line 10 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 18)

'"./core/options"' has no exported member named 'resolveOption'. Did you mean 'resolveOptions'?

Check failure on line 10 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest, 20)

'"./core/options"' has no exported member named 'resolveOption'. Did you mean 'resolveOptions'?

Check failure on line 10 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 18)

'"./core/options"' has no exported member named 'resolveOption'. Did you mean 'resolveOptions'?

Check failure on line 10 in src/index.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 20)

'"./core/options"' has no exported member named 'resolveOption'. Did you mean 'resolveOptions'?
import { scanEnums } from './core/enum'

/**
* The main unplugin instance.
*/
export default createUnplugin<Options | undefined, true>(
(rawOptions = {}, meta) => {
const options = resolveOption(rawOptions)
Expand Down
21 changes: 20 additions & 1 deletion src/rollup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
import unplugin from '.'
/**
* This entry file is for Rollup plugin.
*
* @module
*/

import unplugin from './index'

/**
* Rollup plugin
*
* @example
* ```ts
* // rollup.config.js
* import Macros from 'unplugin-inline-enum/rollup'
*
* export default {
* plugins: [Macros()],
* }
* ```
*/
export default unplugin.rollup
21 changes: 20 additions & 1 deletion src/vite.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
import unplugin from '.'
/**
* This entry file is for Vite plugin.
*
* @module
*/

import unplugin from './index'

/**
* Vite plugin
*
* @example
* ```ts
* // vite.config.ts
* import Macros from 'unplugin-inline-enum/vite'
*
* export default defineConfig({
* plugins: [Macros()],
* })
* ```
*/
export default unplugin.vite
19 changes: 18 additions & 1 deletion src/webpack.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
import unplugin from '.'
/**
* This entry file is for webpack plugin.
*
* @module
*/

import unplugin from './index'

/**
* Webpack plugin
*
* @example
* ```ts
* // webpack.config.js
* module.exports = {
* plugins: [require('unplugin-inline-enum/webpack')()],
* }
* ```
*/
export default unplugin.webpack

0 comments on commit 2a72f48

Please sign in to comment.