Skip to content

Commit 23d0e08

Browse files
committed
refactor: lazy load comment-parser with cjsRequire
1 parent d5fa20d commit 23d0e08

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/utils/export-map.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
33

4+
import { cjsRequire } from '@pkgr/core'
45
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'
5-
import { parse as parseComment_ } from 'comment-parser'
6-
import type { Block } from 'comment-parser'
6+
import type * as commentParser from 'comment-parser'
77
import debug from 'debug'
88
import type { AST } from 'eslint'
99
import { SourceCode } from 'eslint'
@@ -36,7 +36,7 @@ const tsconfigCache = new Map<string, TsConfigJsonResolved | null | undefined>()
3636

3737
export type DocStyleParsers = Record<
3838
DocStyle,
39-
(comments: TSESTree.Comment[]) => Block | undefined
39+
(comments: TSESTree.Comment[]) => commentParser.Block | undefined
4040
>
4141

4242
export interface DeclarationMetadata {
@@ -47,7 +47,7 @@ export interface DeclarationMetadata {
4747
}
4848

4949
export interface ModuleNamespace {
50-
doc?: Block
50+
doc?: commentParser.Block
5151
namespace?: ExportMap | null
5252
}
5353

@@ -70,7 +70,10 @@ const declTypes = new Set([
7070
// https://github.com/syavorsky/comment-parser/issues/172
7171
const fixup = new Set(['deprecated', 'module'])
7272

73-
const parseComment = (comment: string): Block => {
73+
let parseComment_: typeof commentParser.parse | undefined
74+
75+
const parseComment = (comment: string): commentParser.Block => {
76+
parseComment_ ??= cjsRequire<typeof commentParser>('comment-parser').parse
7477
const restored = `/**${comment.split('\n').reduce((acc, line) => {
7578
line = line.trim()
7679
return line && line !== '*' ? acc + '\n ' + line : acc
@@ -747,7 +750,7 @@ export class ExportMap {
747750

748751
declare private mtime: number
749752

750-
declare doc: Block | undefined
753+
declare doc: commentParser.Block | undefined
751754

752755
constructor(public path: string) {}
753756

@@ -963,7 +966,7 @@ function captureDoc(
963966
...nodes: Array<TSESTree.Node | undefined>
964967
) {
965968
const metadata: {
966-
doc?: Block | undefined
969+
doc?: commentParser.Block | undefined
967970
} = {}
968971

969972
defineLazyProperty(metadata, 'doc', () => {
@@ -1030,7 +1033,9 @@ function captureJsDoc(comments: TSESTree.Comment[]) {
10301033
}
10311034

10321035
/** Parse TomDoc section from comments */
1033-
function captureTomDoc(comments: TSESTree.Comment[]): Block | undefined {
1036+
function captureTomDoc(
1037+
comments: TSESTree.Comment[],
1038+
): commentParser.Block | undefined {
10341039
// collect lines up to first paragraph break
10351040
const lines = []
10361041
for (const comment of comments) {
@@ -1053,7 +1058,7 @@ function captureTomDoc(comments: TSESTree.Comment[]): Block | undefined {
10531058
description: statusMatch[2],
10541059
},
10551060
],
1056-
} as Block
1061+
} as commentParser.Block
10571062
}
10581063
}
10591064

0 commit comments

Comments
 (0)