-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: parse jsdoc from exported function (#254)
- Loading branch information
1 parent
641df79
commit d73c418
Showing
13 changed files
with
197 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export { cookies } from './cookies/index.js'; | ||
export { clipboard } from './navigator/clipboard.js'; | ||
export { default as qsd } from './query/decoder.js'; | ||
export { default as qse } from './query/encoder.js'; | ||
export { qsd, qse } from './query/index.js'; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { json } from '@sveltejs/kit'; | ||
import ts from 'typescript'; | ||
import { exports } from '$mauss/package.json'; | ||
|
||
export const prerender = true; | ||
|
||
export interface Schema { | ||
[modules: string]: Array<{ | ||
name: string; | ||
docs: string[]; | ||
signature: string; | ||
}>; | ||
} | ||
|
||
export async function GET() { | ||
const program = ts.createProgram( | ||
Object.keys(exports).flatMap((m) => { | ||
if (m.slice(2).includes('.')) return []; | ||
return `../mauss/src/${m.slice(2) || 'core'}/index.ts`; | ||
}), | ||
{}, | ||
); | ||
|
||
const schema: Schema = {}; | ||
for (const exp in exports) { | ||
if (exp.slice(2).includes('.')) continue; | ||
const module = exp.slice(2) || 'core'; | ||
const source = program.getSourceFile(`../mauss/src/${module}/index.ts`); | ||
if (!source) continue; | ||
|
||
const tc = program.getTypeChecker(); | ||
schema[module] = []; | ||
|
||
ts.forEachChild(source, (node) => { | ||
if (ts.isExportDeclaration(node)) { | ||
const symbols = tc.getExportsOfModule(tc.getSymbolAtLocation(node.moduleSpecifier)); | ||
symbols.forEach((symbol) => { | ||
const decl = symbol.valueDeclaration || symbol.declarations[0]; | ||
if (!ts.isFunctionDeclaration(decl)) return; | ||
|
||
schema[module].push({ | ||
name: symbol.getName(), | ||
docs: parse.jsdoc(decl), | ||
get signature() { | ||
const signature = tc.getSignatureFromDeclaration(decl); | ||
return `function ${this.name}${tc.signatureToString(signature)}`; | ||
}, | ||
}); | ||
}); | ||
} else if ( | ||
ts.isFunctionDeclaration(node) && | ||
node.modifiers?.some((mod) => mod.kind === ts.SyntaxKind.ExportKeyword) | ||
) { | ||
schema[module].push({ | ||
name: node.name.text, | ||
docs: parse.jsdoc(node), | ||
get signature() { | ||
const signature = tc.getSignatureFromDeclaration(node); | ||
return `function ${this.name}${tc.signatureToString(signature)}`; | ||
}, | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
return json(schema); | ||
} | ||
|
||
const parse = { | ||
jsdoc(declaration: ts.FunctionDeclaration) { | ||
return ts.getJSDocCommentsAndTags(declaration).flatMap((doc) => { | ||
const lines = doc.getText().slice(1, -1).split('\n'); | ||
const clean = lines.map((l) => l.replace(/^[\s*]+|[\s*]+$/g, '')); | ||
return clean.filter((l) => l); | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters