-
Notifications
You must be signed in to change notification settings - Fork 8
/
codeToJsdocComments.mjs
47 lines (41 loc) · 1.55 KB
/
codeToJsdocComments.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { parseAsync } from "@babel/core";
/**
* Gets JSDoc comments from source code, using Babel.
* @kind function
* @name codeToJsdocComments
* @param {string} code Code containing the JSDoc comments.
* @param {string} codeFilePath File path for the code containing the JSDoc comments.
* @returns {Promise<Array<object>>} Resolves JSDoc comments, from the Babel parse result.
* @ignore
*/
export default async function codeToJsdocComments(code, codeFilePath) {
if (typeof code !== "string")
throw new TypeError("Argument 1 `code` must be a string.");
if (typeof codeFilePath !== "string")
throw new TypeError("Argument 2 `codeFilePath` must be a string.");
if (codeFilePath === "")
throw new TypeError(
"Argument 2 `codeFilePath` must be a populated string."
);
const { comments } = await parseAsync(code, {
// Provide the code file path for more useful Babel parse errors.
filename: codeFilePath,
// Allow parsing code containing modern syntax even if a project doesn’t
// have Babel config to handle it.
parserOpts: {
plugins: [
"classProperties",
["decorators", { decoratorsBeforeExport: false }],
],
},
});
return comments.filter(
({ type, value }) =>
// A comment block starts with `/*`, whereas a comment line starts with
// `//`. JSDoc can only be a comment block.
type === "CommentBlock" &&
// The value excludes the start `/*` and end `*/`. A JSDoc comment block
// starts with `/**` followed by whitespace.
value.match(/^\*\s/u)
);
}