UNPKG

3.89 kBJavaScriptView Raw
1"use strict";
2var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5 return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6};
7var _a, _Directives_CACHE;
8Object.defineProperty(exports, "__esModule", { value: true });
9exports.Directives = void 0;
10const ts = require("typescript");
11const jsii_diagnostic_1 = require("./jsii-diagnostic");
12/**
13 * TSDoc-style directives that can be attached to a symbol.
14 */
15class Directives {
16 /**
17 * Obtains the `Directives` for a given TypeScript AST node.
18 *
19 * @param node the node for which directives are requested.
20 * @param onDiagnostic a callback invoked whenever a diagnostic message is
21 * emitted when parsing directives.
22 */
23 static of(node, onDiagnostic) {
24 const found = __classPrivateFieldGet(_a, _a, "f", _Directives_CACHE).get(node);
25 if (found != null) {
26 return found;
27 }
28 const directives = new _a(node, onDiagnostic);
29 __classPrivateFieldGet(_a, _a, "f", _Directives_CACHE).set(node, directives);
30 return directives;
31 }
32 constructor(node, onDiagnostic) {
33 for (const tag of ts.getJSDocTags(node)) {
34 switch (tag.tagName.text) {
35 case 'internal':
36 this.tsInternal ?? (this.tsInternal = tag);
37 break;
38 case 'jsii':
39 const comments = getComments(tag);
40 if (comments.length === 0) {
41 onDiagnostic(jsii_diagnostic_1.JsiiDiagnostic.JSII_2000_MISSING_DIRECTIVE_ARGUMENT.create(tag));
42 continue;
43 }
44 for (const { text, jsdocNode } of comments) {
45 switch (text) {
46 case 'ignore':
47 this.ignore ?? (this.ignore = jsdocNode);
48 break;
49 default:
50 onDiagnostic(jsii_diagnostic_1.JsiiDiagnostic.JSII_2999_UNKNOWN_DIRECTIVE.create(jsdocNode, text));
51 break;
52 }
53 }
54 break;
55 default: // Ignore
56 }
57 }
58 }
59}
60exports.Directives = Directives;
61_a = Directives;
62_Directives_CACHE = { value: new WeakMap() };
63function getComments(tag) {
64 if (tag.comment == null) {
65 return [];
66 }
67 if (typeof tag.comment === 'string') {
68 const text = tag.comment.trim();
69 return text
70 ? text.split(/[\n,]/).flatMap((line) => {
71 line = line.trim();
72 return line ? [{ text: line, jsdocNode: tag }] : [];
73 })
74 : [];
75 }
76 // Possible per the type signature in the compiler, however not sure in which conditions.
77 return tag.comment.flatMap((jsdocNode) => {
78 let text;
79 switch (jsdocNode.kind) {
80 case ts.SyntaxKind.JSDocText:
81 text = jsdocNode.text;
82 break;
83 case ts.SyntaxKind.JSDocLink:
84 case ts.SyntaxKind.JSDocLinkCode:
85 case ts.SyntaxKind.JSDocLinkPlain:
86 text = jsdocNode.name
87 ? `${jsdocNode.name.getText(jsdocNode.name.getSourceFile())}: ${jsdocNode.text}`
88 : jsdocNode.text;
89 break;
90 }
91 text = text.trim();
92 return text ? [{ text, jsdocNode }] : [];
93 });
94}
95//# sourceMappingURL=directives.js.map
\No newline at end of file