UNPKG

10.8 kBJavaScriptView Raw
1import { StandardTags } from '../details/StandardTags';
2import { TSDocValidationConfiguration } from './TSDocValidationConfiguration';
3import { DocNodeManager } from './DocNodeManager';
4import { BuiltInDocNodes } from '../nodes/BuiltInDocNodes';
5import { allTsdocMessageIds, allTsdocMessageIdsSet } from '../parser/TSDocMessageId';
6/**
7 * Configuration for the TSDocParser.
8 */
9var TSDocConfiguration = /** @class */ (function () {
10 function TSDocConfiguration() {
11 this._tagDefinitions = [];
12 this._tagDefinitionsByName = new Map();
13 this._supportedTagDefinitions = new Set();
14 this._validation = new TSDocValidationConfiguration();
15 this._docNodeManager = new DocNodeManager();
16 this._supportedHtmlElements = new Set();
17 this.clear(false);
18 // Register the built-in node kinds
19 BuiltInDocNodes.register(this);
20 }
21 /**
22 * Resets the `TSDocConfiguration` object to its initial empty state.
23 * @param noStandardTags - The `TSDocConfiguration` constructor normally adds definitions for the
24 * standard TSDoc tags. Set `noStandardTags` to true for a completely empty `tagDefinitions` collection.
25 */
26 TSDocConfiguration.prototype.clear = function (noStandardTags) {
27 if (noStandardTags === void 0) { noStandardTags = false; }
28 this._tagDefinitions.length = 0;
29 this._tagDefinitionsByName.clear();
30 this._supportedTagDefinitions.clear();
31 this._validation.ignoreUndefinedTags = false;
32 this._validation.reportUnsupportedTags = false;
33 this._validation.reportUnsupportedHtmlElements = false;
34 this._supportedHtmlElements.clear();
35 if (!noStandardTags) {
36 // Define all the standard tags
37 this.addTagDefinitions(StandardTags.allDefinitions);
38 }
39 };
40 Object.defineProperty(TSDocConfiguration.prototype, "tagDefinitions", {
41 /**
42 * The TSDoc tags that are defined in this configuration.
43 *
44 * @remarks
45 * The subset of "supported" tags is tracked by {@link TSDocConfiguration.supportedTagDefinitions}.
46 */
47 get: function () {
48 return this._tagDefinitions;
49 },
50 enumerable: false,
51 configurable: true
52 });
53 Object.defineProperty(TSDocConfiguration.prototype, "supportedTagDefinitions", {
54 /**
55 * Returns the subset of {@link TSDocConfiguration.tagDefinitions}
56 * that are supported in this configuration.
57 *
58 * @remarks
59 * This property is only used when
60 * {@link TSDocValidationConfiguration.reportUnsupportedTags} is enabled.
61 */
62 get: function () {
63 var _this = this;
64 return this.tagDefinitions.filter(function (x) { return _this.isTagSupported(x); });
65 },
66 enumerable: false,
67 configurable: true
68 });
69 Object.defineProperty(TSDocConfiguration.prototype, "validation", {
70 /**
71 * Enable/disable validation checks performed by the parser.
72 */
73 get: function () {
74 return this._validation;
75 },
76 enumerable: false,
77 configurable: true
78 });
79 Object.defineProperty(TSDocConfiguration.prototype, "supportedHtmlElements", {
80 /**
81 * The HTML element names that are supported in this configuration. Used in conjunction with the `reportUnsupportedHtmlElements` setting.
82 */
83 get: function () {
84 return Array.from(this._supportedHtmlElements.values());
85 },
86 enumerable: false,
87 configurable: true
88 });
89 Object.defineProperty(TSDocConfiguration.prototype, "docNodeManager", {
90 /**
91 * Register custom DocNode subclasses.
92 */
93 get: function () {
94 return this._docNodeManager;
95 },
96 enumerable: false,
97 configurable: true
98 });
99 /**
100 * Return the tag that was defined with the specified name, or undefined
101 * if not found.
102 */
103 TSDocConfiguration.prototype.tryGetTagDefinition = function (tagName) {
104 return this._tagDefinitionsByName.get(tagName.toUpperCase());
105 };
106 /**
107 * Return the tag that was defined with the specified name, or undefined
108 * if not found.
109 */
110 TSDocConfiguration.prototype.tryGetTagDefinitionWithUpperCase = function (alreadyUpperCaseTagName) {
111 return this._tagDefinitionsByName.get(alreadyUpperCaseTagName);
112 };
113 /**
114 * Define a new TSDoc tag to be recognized by the TSDocParser, and mark it as unsupported.
115 * Use {@link TSDocConfiguration.setSupportForTag} to mark it as supported.
116 *
117 * @remarks
118 * If a tag is "defined" this means that the parser recognizes it and understands its syntax.
119 * Whereas if a tag is "supported", this means it is defined AND the application implements the tag.
120 */
121 TSDocConfiguration.prototype.addTagDefinition = function (tagDefinition) {
122 var existingDefinition = this._tagDefinitionsByName.get(tagDefinition.tagNameWithUpperCase);
123 if (existingDefinition === tagDefinition) {
124 return;
125 }
126 if (existingDefinition) {
127 throw new Error("A tag is already defined using the name " + existingDefinition.tagName);
128 }
129 this._tagDefinitions.push(tagDefinition);
130 this._tagDefinitionsByName.set(tagDefinition.tagNameWithUpperCase, tagDefinition);
131 };
132 /**
133 * Calls {@link TSDocConfiguration.addTagDefinition} for a list of definitions,
134 * and optionally marks them as supported.
135 * @param tagDefinitions - the definitions to be added
136 * @param supported - if specified, calls the {@link TSDocConfiguration.setSupportForTag}
137 * method to mark the definitions as supported or unsupported
138 */
139 TSDocConfiguration.prototype.addTagDefinitions = function (tagDefinitions, supported) {
140 for (var _i = 0, tagDefinitions_1 = tagDefinitions; _i < tagDefinitions_1.length; _i++) {
141 var tagDefinition = tagDefinitions_1[_i];
142 this.addTagDefinition(tagDefinition);
143 if (supported !== undefined) {
144 this.setSupportForTag(tagDefinition, supported);
145 }
146 }
147 };
148 /**
149 * Returns true if the tag is supported in this configuration.
150 */
151 TSDocConfiguration.prototype.isTagSupported = function (tagDefinition) {
152 this._requireTagToBeDefined(tagDefinition);
153 return this._supportedTagDefinitions.has(tagDefinition);
154 };
155 /**
156 * Specifies whether the tag definition is supported in this configuration.
157 * The parser may issue warnings for unsupported tags.
158 *
159 * @remarks
160 * If a tag is "defined" this means that the parser recognizes it and understands its syntax.
161 * Whereas if a tag is "supported", this means it is defined AND the application implements the tag.
162 *
163 * This function automatically sets {@link TSDocValidationConfiguration.reportUnsupportedTags}
164 * to true.
165 */
166 TSDocConfiguration.prototype.setSupportForTag = function (tagDefinition, supported) {
167 this._requireTagToBeDefined(tagDefinition);
168 if (supported) {
169 this._supportedTagDefinitions.add(tagDefinition);
170 }
171 else {
172 this._supportedTagDefinitions.delete(tagDefinition);
173 }
174 this.validation.reportUnsupportedTags = true;
175 };
176 /**
177 * Specifies whether the tag definition is supported in this configuration.
178 * This operation sets {@link TSDocValidationConfiguration.reportUnsupportedTags} to `true`.
179 *
180 * @remarks
181 * The parser may issue warnings for unsupported tags.
182 * If a tag is "defined" this means that the parser recognizes it and understands its syntax.
183 * Whereas if a tag is "supported", this means it is defined AND the application implements the tag.
184 */
185 TSDocConfiguration.prototype.setSupportForTags = function (tagDefinitions, supported) {
186 for (var _i = 0, tagDefinitions_2 = tagDefinitions; _i < tagDefinitions_2.length; _i++) {
187 var tagDefinition = tagDefinitions_2[_i];
188 this.setSupportForTag(tagDefinition, supported);
189 }
190 };
191 /**
192 * Assigns the `supportedHtmlElements` property, replacing any previous elements.
193 * This operation sets {@link TSDocValidationConfiguration.reportUnsupportedHtmlElements} to `true`.
194 */
195 TSDocConfiguration.prototype.setSupportedHtmlElements = function (htmlTags) {
196 this._supportedHtmlElements.clear();
197 this._validation.reportUnsupportedHtmlElements = true;
198 for (var _i = 0, htmlTags_1 = htmlTags; _i < htmlTags_1.length; _i++) {
199 var htmlTag = htmlTags_1[_i];
200 this._supportedHtmlElements.add(htmlTag);
201 }
202 };
203 /**
204 * Returns true if the html element is supported in this configuration.
205 */
206 TSDocConfiguration.prototype.isHtmlElementSupported = function (htmlTag) {
207 return this._supportedHtmlElements.has(htmlTag);
208 };
209 /**
210 * Returns true if the specified {@link TSDocMessageId} string is implemented by this release of the TSDoc parser.
211 * This can be used to detect misspelled identifiers.
212 *
213 * @privateRemarks
214 *
215 * Why this API is associated with TSDocConfiguration: In the future, if we enable support for custom extensions
216 * of the TSDoc parser, we may provide a way to register custom message identifiers.
217 */
218 TSDocConfiguration.prototype.isKnownMessageId = function (messageId) {
219 return allTsdocMessageIdsSet.has(messageId);
220 };
221 Object.defineProperty(TSDocConfiguration.prototype, "allTsdocMessageIds", {
222 /**
223 * Returns the list of {@link TSDocMessageId} strings that are implemented by this release of the TSDoc parser.
224 *
225 * @privateRemarks
226 *
227 * Why this API is associated with TSDocConfiguration: In the future, if we enable support for custom extensions
228 * of the TSDoc parser, we may provide a way to register custom message identifiers.
229 */
230 get: function () {
231 return allTsdocMessageIds;
232 },
233 enumerable: false,
234 configurable: true
235 });
236 TSDocConfiguration.prototype._requireTagToBeDefined = function (tagDefinition) {
237 var matching = this._tagDefinitionsByName.get(tagDefinition.tagNameWithUpperCase);
238 if (matching) {
239 if (matching === tagDefinition) {
240 return;
241 }
242 }
243 throw new Error('The specified TSDocTagDefinition is not defined for this TSDocConfiguration');
244 };
245 return TSDocConfiguration;
246}());
247export { TSDocConfiguration };
248//# sourceMappingURL=TSDocConfiguration.js.map
\No newline at end of file