UNPKG

4.74 kBJavaScriptView Raw
1import { StringChecks } from '../parser/StringChecks';
2/**
3 * Part of the {@link TSDocConfiguration} object.
4 *
5 * @remarks
6 * If you define your own custom subclasses of `DocNode`, they must be registered with the `DocNodeManager`.
7 * Use {@link DocNodeManager.registerAllowableChildren} to specify which {@link DocNodeContainer} subclasses
8 * are allowed to contain your nodes.
9 */
10var DocNodeManager = /** @class */ (function () {
11 function DocNodeManager() {
12 this._docNodeDefinitionsByKind = new Map();
13 this._docNodeDefinitionsByConstructor = new Map();
14 }
15 /**
16 * Registers a list of {@link IDocNodeDefinition} objects to be used with the associated
17 * {@link TSDocConfiguration} object.
18 */
19 DocNodeManager.prototype.registerDocNodes = function (packageName, definitions) {
20 var packageNameError = StringChecks.explainIfInvalidPackageName(packageName);
21 if (packageNameError) {
22 throw new Error('Invalid NPM package name: ' + packageNameError);
23 }
24 for (var _i = 0, definitions_1 = definitions; _i < definitions_1.length; _i++) {
25 var definition = definitions_1[_i];
26 if (!DocNodeManager._nodeKindRegExp.test(definition.docNodeKind)) {
27 throw new Error("The DocNode kind " + JSON.stringify(definition.docNodeKind) + " is not a valid identifier." +
28 " It must start with an underscore or letter, and be comprised of letters, numbers, and underscores");
29 }
30 var existingDefinition = this._docNodeDefinitionsByKind.get(definition.docNodeKind);
31 if (existingDefinition !== undefined) {
32 throw new Error("The DocNode kind \"" + definition.docNodeKind + "\" was already registered" +
33 (" by " + existingDefinition.packageName));
34 }
35 existingDefinition = this._docNodeDefinitionsByConstructor.get(definition.constructor);
36 if (existingDefinition !== undefined) {
37 throw new Error("This DocNode constructor was already registered by " + existingDefinition.packageName +
38 (" as " + existingDefinition.docNodeKind));
39 }
40 var newDefinition = {
41 docNodeKind: definition.docNodeKind,
42 constructor: definition.constructor,
43 packageName: packageName,
44 allowedChildKinds: new Set()
45 };
46 this._docNodeDefinitionsByKind.set(definition.docNodeKind, newDefinition);
47 this._docNodeDefinitionsByConstructor.set(definition.constructor, newDefinition);
48 }
49 };
50 /**
51 * Reports an error if the specified DocNode kind has not been registered.
52 */
53 DocNodeManager.prototype.throwIfNotRegisteredKind = function (docNodeKind) {
54 if (!this._docNodeDefinitionsByKind.has(docNodeKind)) {
55 throw new Error("The DocNode kind \"" + docNodeKind + "\" was not registered with this TSDocConfiguration");
56 }
57 };
58 /**
59 * For the given parent DocNode kind, registers the specified DocNode kinds as being allowable children of
60 * the parent.
61 *
62 * @remarks
63 * To prevent mistakes, `DocNodeContainer` will report an error if you try to add node that was not registered
64 * as an allowable child of the container.
65 */
66 DocNodeManager.prototype.registerAllowableChildren = function (parentKind, childKinds) {
67 var parentDefinition = this._getDefinition(parentKind);
68 for (var _i = 0, childKinds_1 = childKinds; _i < childKinds_1.length; _i++) {
69 var childKind = childKinds_1[_i];
70 this._getDefinition(childKind);
71 parentDefinition.allowedChildKinds.add(childKind);
72 }
73 };
74 /**
75 * Returns true if the specified DocNode kind has been registered as an allowable child of the specified
76 * parent DocNode kind.
77 */
78 DocNodeManager.prototype.isAllowedChild = function (parentKind, childKind) {
79 var parentDefinition = this._getDefinition(parentKind);
80 return parentDefinition.allowedChildKinds.has(childKind);
81 };
82 DocNodeManager.prototype._getDefinition = function (docNodeKind) {
83 var definition = this._docNodeDefinitionsByKind.get(docNodeKind);
84 if (definition === undefined) {
85 throw new Error("The DocNode kind \"" + docNodeKind + "\" was not registered with this TSDocConfiguration");
86 }
87 return definition;
88 };
89 // Matches an ASCII TypeScript-style identifier.
90 //
91 // Example: "_myIdentifier99"
92 DocNodeManager._nodeKindRegExp = /^[_a-z][_a-z0-9]*$/i;
93 return DocNodeManager;
94}());
95export { DocNodeManager };
96//# sourceMappingURL=DocNodeManager.js.map
\No newline at end of file