mirror of
https://github.com/Effect-TS/effect.git
synced 2026-11-25 02:06:20 +08:00
Co-authored-by: Maxwell Brown <maxwellbrown1990@gmail.com>
36 lines
1.0 KiB
TypeScript
36 lines
1.0 KiB
TypeScript
import type * as cs from "jscodeshift"
|
|
|
|
const EXAMPLE = "* @example\n"
|
|
const EXAMPLE_WITH_FENCE = "* @example\n * ```ts\n"
|
|
|
|
export default function transformer(file: cs.FileInfo, api: cs.API) {
|
|
const j = api.jscodeshift
|
|
const root = j(file.source)
|
|
|
|
root.find(j.Comment as any).forEach((path) => {
|
|
if (path.value.type === "CommentBlock") {
|
|
const value = (path.value) as any
|
|
const comment = value.value
|
|
if (comment.includes(EXAMPLE) && !comment.includes(EXAMPLE_WITH_FENCE)) {
|
|
value.value = wrapExamplesWithFence(comment)
|
|
}
|
|
}
|
|
})
|
|
|
|
return root.toSource()
|
|
}
|
|
|
|
function wrapExamplesWithFence(jsdocComment: string) {
|
|
const start = jsdocComment.indexOf(EXAMPLE) + EXAMPLE.length
|
|
let end = jsdocComment.indexOf(" * @", start)
|
|
if (end === -1) {
|
|
end = jsdocComment.length - 1
|
|
} else {
|
|
if (jsdocComment.substring(end - 3, end) === " *\n") {
|
|
end -= 3
|
|
}
|
|
}
|
|
return jsdocComment.substring(0, start) + " * ```ts\n" + jsdocComment.substring(start, end) + " * ```\n" +
|
|
jsdocComment.substring(end)
|
|
}
|