-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support loader with jsdoc support
- Loading branch information
Showing
7 changed files
with
144 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ node_modules | |
coverage | ||
dist | ||
.tmp | ||
/*.js | ||
/*.d.ts |
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 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,90 @@ | ||
import { transform as babelTransform } from '@babel/standalone/babel.min.js' | ||
import type { PluginObj } from '@babel/core' | ||
import * as t from '@babel/types' | ||
|
||
function jsdocSchema () { | ||
return <PluginObj>{ | ||
visitor: { | ||
ObjectProperty (p) { | ||
if (p.node.leadingComments) { | ||
const lines = p.node.leadingComments | ||
.filter(c => c.type === 'CommentBlock') | ||
.map(c => c.value.split('\n').map(l => l.replace(/^[\s*]+|[\s*]$/, ''))) | ||
.flat() | ||
.filter(Boolean) | ||
|
||
const comments: string[] = [] | ||
while (lines.length && !lines[0].startsWith('@')) { | ||
comments.push(lines.shift()) | ||
} | ||
|
||
const blockTags: Record<string, string> = {} | ||
let lastTag = null | ||
for (const line of lines) { | ||
const m = line.match(/@(\w+) ?(.*)/) | ||
if (m) { | ||
blockTags[m[1]] = m[2] | ||
lastTag = m[1] | ||
} else { | ||
blockTags[lastTag] = | ||
(blockTags[lastTag] ? blockTags[lastTag] + '\n' : '') + line | ||
} | ||
} | ||
|
||
const schema: any = {} | ||
if (comments.length) { | ||
schema.title = comments.shift() | ||
} | ||
if (comments.length) { | ||
schema.description = comments.join('\n') | ||
} | ||
for (const tag in blockTags) { | ||
schema[tag] = blockTags[tag] | ||
} | ||
|
||
const schemaAstProps = | ||
Object.entries(schema).map(e => t.objectProperty(t.identifier(e[0]), t.stringLiteral(e[1] as string))) | ||
const schemaAst = t.objectExpression(schemaAstProps) | ||
|
||
if (p.node.value.type === 'ObjectExpression') { | ||
const schemaProp = p.node.value.properties.find(prop => | ||
('key' in prop) && prop.key.type === 'Identifier' && prop.key.name === '$schema' | ||
) | ||
if (schemaProp && 'value' in schemaProp) { | ||
if (schemaProp.value.type === 'ObjectExpression') { | ||
// Object has $schema | ||
schemaProp.value.properties.push(...schemaAstProps) | ||
} else { | ||
// Object has $schema which is not an object | ||
// SKIP | ||
} | ||
} else { | ||
// Object has not $schema | ||
p.node.value.properties.unshift(t.objectProperty(t.identifier('$schema'), schemaAst)) | ||
} | ||
} else { | ||
// Literal value | ||
p.node.value = t.objectExpression([ | ||
t.objectProperty(t.identifier('$default'), p.node.value), | ||
t.objectProperty(t.identifier('$schema'), schemaAst) | ||
]) | ||
} | ||
p.node.leadingComments = [] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
export function transform (src) { | ||
const res = babelTransform(src, { | ||
filename: 'src.ts', | ||
presets: [ | ||
'typescript' | ||
], | ||
plugins: [ | ||
jsdocSchema | ||
] | ||
}) | ||
return res.code | ||
} |
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