-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjsdocDataTypeToMdAst.mjs
71 lines (63 loc) · 2.35 KB
/
jsdocDataTypeToMdAst.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import CodeLocation from "./CodeLocation.mjs";
import InvalidJsdocError from "./InvalidJsdocError.mjs";
import typeAstToMdAst from "./typeAstToMdAst.mjs";
import typeToTypeAst from "./typeToTypeAst.mjs";
/**
* Converts JSDoc data containing a JSDoc type to a markdown AST, linking JSDoc
* member references.
* @kind function
* @name jsdocDataTypeToMdAst
* @param {JsdocData} jsdocData JSDoc data containing a JSDoc type.
* @param {Array<JsdocMember>} members Outlined JSDoc members.
* @param {CodeFilesMap} codeFiles Map of code file paths and their code.
* @param {boolean} optional Is the type optional. Sometimes this is indicated outside the type string, e.g. when a parameter tag name is wrapped in `[` and `]`.
* @param {boolean} parameter Is the type a parameter, which supports more features including optional (`*=`) and rest (`...*`) parameters.
* @returns {object} Markdown AST.
* @ignore
*/
export default function jsdocDataTypeToMdAst(
jsdocData,
members,
codeFiles,
optional,
parameter
) {
if (typeof jsdocData !== "object")
throw new TypeError("Argument 1 `jsdocData` must be an object.");
if (typeof jsdocData.codeFileLocation !== "object")
throw new TypeError(
"Argument 1 `jsdocData` property `codeFileLocation` must be an object."
);
if (typeof jsdocData.codeFileLocation.filePath !== "string")
throw new TypeError(
"Argument 1 `jsdocData` property `codeFileLocation` property `filePath` must be a string."
);
if (!(jsdocData.codeFileLocation.codeLocation instanceof CodeLocation))
throw new TypeError(
"Argument 1 `jsdocData` property `codeFileLocation` property `codeLocation` must be a `CodeLocation` instance."
);
if (typeof jsdocData.data !== "string")
throw new TypeError(
"Argument 1 `jsdocData` property `data` must be a string."
);
if (!Array.isArray(members))
throw new TypeError("Argument 2 `members` must be an array.");
if (!(codeFiles instanceof Map))
throw new TypeError("Argument 3 `codeFiles` must be a `Map` instance.");
try {
return typeAstToMdAst(
typeToTypeAst({
type: jsdocData.data,
parameter,
optional,
}),
members
);
} catch (error) {
throw new InvalidJsdocError(
error.message,
jsdocData.codeFileLocation,
codeFiles.get(jsdocData.codeFileLocation.filePath)
);
}
}