Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for JSDoc extraction #248

Draft
wants to merge 30 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
2220c6b
Experimental auto JSDoc extraction
GalacticHypernova Jun 16, 2023
a7dfea9
Missing imports
GalacticHypernova Jun 16, 2023
d0e4687
Destructuring
GalacticHypernova Jun 16, 2023
dd1ac23
Removal of 'undefined' in non-existent JSDoc
GalacticHypernova Jun 16, 2023
666d4e2
Non-Nuxt-locked vue-router handling
GalacticHypernova Jun 16, 2023
8d40bc5
Proper error handling
GalacticHypernova Jun 17, 2023
d3b5c0a
Fixing up after destructuring, comments, Nuxt-compatible non-locked v…
GalacticHypernova Jun 17, 2023
0d55c6d
Update utils.ts
GalacticHypernova Jun 17, 2023
4a0a2c7
TS destructuring
GalacticHypernova Jun 17, 2023
4a62c05
Reverting broken destructuring
GalacticHypernova Jun 20, 2023
371aa66
Merge branch 'main' into patch-1
GalacticHypernova Jun 20, 2023
220d63e
Returning comments
GalacticHypernova Jun 20, 2023
d370648
Merge branch 'unjs:main' into patch-1
GalacticHypernova Jul 1, 2023
60def88
FIxing non-nuxt-locked vue-router, migrating to patterns in stead of TS
GalacticHypernova Jul 1, 2023
a60d592
Merge branch 'main' into patch-1
GalacticHypernova Aug 29, 2023
9676362
Update utils.ts
GalacticHypernova Aug 29, 2023
cf72948
Merge branch 'main' into patch-1
GalacticHypernova Sep 20, 2023
bf0f1b7
Merge branch 'main' into patch-1
GalacticHypernova Oct 5, 2023
306c590
Merge branch 'main' into patch-1
GalacticHypernova Oct 10, 2023
3c7cfd3
Merge branch 'main' into patch-1
GalacticHypernova Oct 20, 2023
8f0fecc
Merge branch 'main' into patch-1
GalacticHypernova Oct 23, 2023
8455fe7
Merge branch 'main' into patch-1
GalacticHypernova Dec 3, 2023
86361a4
Merge branch 'main' into patch-1
GalacticHypernova Jan 22, 2024
c768dd2
refactor: use map with readSync to get the main entry
GalacticHypernova Jan 22, 2024
4ed29d2
Update the map
GalacticHypernova Jan 22, 2024
fe8142f
Update utils.ts
GalacticHypernova Jan 22, 2024
e579e88
chore: remove redundant check
GalacticHypernova Jan 22, 2024
f7031e2
style: lint
GalacticHypernova Jan 22, 2024
4bf2eef
chore: space
GalacticHypernova Jan 22, 2024
c0fe713
style: lint
GalacticHypernova Jan 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { isAbsolute, relative } from 'pathe'
import { isAbsolute, relative, resolve } from 'pathe'
import type { StaticImport } from 'mlly'
import { findStaticImports, parseStaticImport, resolvePath } from 'mlly'
import MagicString from 'magic-string'
import type { Import, InlinePreset, MagicStringResult, TypeDeclarationOptions } from './types'
import { stripCommentsAndStrings } from './regexp'
import fs from "fs"
const files = new Map()

export function defineUnimportPreset(preset: InlinePreset): InlinePreset {
return preset
Expand Down Expand Up @@ -118,18 +120,42 @@ export function toExports(imports: Import[], fileDir?: string, includeType = fal
})
.join('\n')
}
export function extractJSDoc(modulePath: string, functionName: string) {
try {
const jsDocRE = new RegExp(`(\\/\\*\\*[?;,.:\/@\\-\\s\\w\\{\\}\\[\\]\\(\\)\\<\\>\\"\`\|*]*\\*\\/)(?:\nexport d?e?c?l?a?r?e? (?:function|const) ${functionName})`,'i')
modulePath = resolve(modulePath.slice(6))
if (!files.has(modulePath)) {
if (fs.existsSync(modulePath + "/package.json")) {
const pkg = JSON.parse(fs.readFileSync(modulePath + "/package.json", "utf8"))
files.set(modulePath, readFileSync(modulePath + "/" + pkg.main, "utf8"))
}
else {
for (const ext of [".ts",".js",".mjs",".cjs"]) {
if (fs.existsSync(modulePath + ext)) {
files.set(modulePath, fs.readFileSync(modulePath+ext, "utf8"))
break
}
}
}
}
const jsDoc = files.get(modulePath)?.match(jsDocRE)
return jsDoc;
}
catch (err) {
throw new Error(err)
}
}

export function stripFileExtension(path: string) {
return path.replace(/\.[a-zA-Z]+$/, '')
}

export function toTypeDeclarationItems(imports: Import[], options?: TypeDeclarationOptions) {
return imports
.map((i) => {
const from = options?.resolvePath?.(i) || stripFileExtension(i.typeFrom || i.from)
return `const ${i.as}: typeof import('${from}')${i.name !== '*' ? `['${i.name}']` : ''}`
})
.sort()
export function toTypeDeclarationItems (imports: Import[], options?: TypeDeclarationOptions) {
return imports.map((i) => {
const from = options?.resolvePath?.(i) || stripFileExtension(i.typeFrom || i.from)
const jsDoc = extractJSDoc(from, i.as)
return `${jsDoc === null || jsDoc === undefined ? '' : jsDoc[0].slice(0, jsDoc[0].indexOf("*/\nexport")+3) + "\n\t"}const ${i.as}: typeof import('${from}')${i.name !== "*" ? `['${i.name}']` : ""}`;
}).sort();
}

export function toTypeDeclarationFile(imports: Import[], options?: TypeDeclarationOptions) {
Expand Down