1 | import { StringChecks } from '../parser/StringChecks';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var DocNodeManager = (function () {
|
11 | function DocNodeManager() {
|
12 | this._docNodeDefinitionsByKind = new Map();
|
13 | this._docNodeDefinitionsByConstructor = new Map();
|
14 | }
|
15 | |
16 |
|
17 |
|
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 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
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 |
|
76 |
|
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 |
|
90 |
|
91 |
|
92 | DocNodeManager._nodeKindRegExp = /^[_a-z][_a-z0-9]*$/i;
|
93 | return DocNodeManager;
|
94 | }());
|
95 | export { DocNodeManager };
|
96 |
|
\ | No newline at end of file |