jsii
Version:
[](https://cdk.dev) [](https://github.com/aws/jsii
139 lines • 5.4 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.Directives = void 0;
const ts = __importStar(require("typescript"));
const jsii_diagnostic_1 = require("./jsii-diagnostic");
/**
* TSDoc-style directives that can be attached to a symbol.
*/
class Directives {
/**
* Obtains the `Directives` for a given TypeScript AST node.
*
* @param node the node for which directives are requested.
* @param onDiagnostic a callback invoked whenever a diagnostic message is
* emitted when parsing directives.
*/
static of(node, onDiagnostic) {
const found = Directives.#CACHE.get(node);
if (found != null) {
return found;
}
const directives = new Directives(node, onDiagnostic);
Directives.#CACHE.set(node, directives);
return directives;
}
static #CACHE = new WeakMap();
/** Whether the node has the `@internal` JSDoc tag. */
tsInternal;
/** Whether the node has the `@jsii ignore` directive set. */
ignore;
/** Whether the node has the `@jsii struct` directive set. */
struct;
/** Suppression codes from `@jsii suppress <code>` directives. */
suppressions = [];
constructor(node, onDiagnostic) {
const suppressions = [];
for (const tag of ts.getJSDocTags(node)) {
switch (tag.tagName.text) {
case 'internal':
this.tsInternal ??= tag;
break;
case 'jsii':
const comments = getComments(tag);
if (comments.length === 0) {
onDiagnostic(jsii_diagnostic_1.JsiiDiagnostic.JSII_2000_MISSING_DIRECTIVE_ARGUMENT.create(tag));
continue;
}
for (const { text, jsdocNode } of comments) {
const [subcommand, ...rest] = text.split(/\s+/);
switch (subcommand) {
case 'ignore':
this.ignore ??= jsdocNode;
break;
case 'suppress': {
const code = rest[0];
if (code) {
suppressions.push(code);
}
break;
}
default:
onDiagnostic(jsii_diagnostic_1.JsiiDiagnostic.JSII_2999_UNKNOWN_DIRECTIVE.create(jsdocNode, text));
break;
}
}
break;
default: // Ignore
}
}
this.suppressions = suppressions;
}
}
exports.Directives = Directives;
function getComments(tag) {
if (tag.comment == null) {
return [];
}
if (typeof tag.comment === 'string') {
const text = tag.comment.trim();
return text
? text.split(/[\n,]/).flatMap((line) => {
line = line.trim();
return line ? [{ text: line, jsdocNode: tag }] : [];
})
: [];
}
// Possible per the type signature in the compiler, however not sure in which conditions.
return tag.comment.flatMap((jsdocNode) => {
let text;
switch (jsdocNode.kind) {
case ts.SyntaxKind.JSDocText:
text = jsdocNode.text;
break;
case ts.SyntaxKind.JSDocLink:
case ts.SyntaxKind.JSDocLinkCode:
case ts.SyntaxKind.JSDocLinkPlain:
text = jsdocNode.name
? `${jsdocNode.name.getText(jsdocNode.name.getSourceFile())}: ${jsdocNode.text}`
: jsdocNode.text;
break;
}
text = text.trim();
return text ? [{ text, jsdocNode }] : [];
});
}
//# sourceMappingURL=directives.js.map