UNPKG

jsii

Version:

[![Join the chat at https://cdk.Dev](https://img.shields.io/static/v1?label=Slack&message=cdk.dev&color=brightgreen&logo=slack)](https://cdk.dev) [![Build Status](https://github.com/aws/jsii-compiler/workflows/build/badge.svg)](https://github.com/aws/jsii

124 lines 4.92 kB
"use strict"; 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.silencedWarnings = void 0; exports.parseWarningCodes = parseWarningCodes; exports.isSilenced = isSilenced; const ts = __importStar(require("typescript")); const directives_1 = require("./directives"); const jsii_diagnostic_1 = require("./jsii-diagnostic"); /** * Set of silenced warning codes (numeric JSII codes). */ exports.silencedWarnings = new Set(); /** * Parse a user-provided warning identifier into numeric JSII codes. * Accepts: "JSII5019", "5019", or a diagnostic name / partial name. * * A name containing `/` must match a full diagnostic name exactly. * A name without `/` matches any diagnostic whose category or specific * name equals the input (e.g. "reserved-word" or "language-compatibility"). */ function parseWarningCodes(input) { // JSII<number> format const jsiiMatch = /^JSII(\d+)$/i.exec(input); if (jsiiMatch) { return [parseInt(jsiiMatch[1], 10)]; } // Plain number const num = parseInt(input, 10); if (String(num) === input) { return [num]; } // Name-based lookup const matches = jsii_diagnostic_1.Code.lookupByPartialName(input); if (matches.length > 0) { return matches.map((c) => c.code); } throw new Error(`Unknown warning "${input}". Expected a JSII code (e.g. JSII5018), a number (e.g. 5018), or a diagnostic name (e.g. reserved-word, language-compatibility/reserved-word, language-compatibility).`); } /** * Check if a diagnostic is a silenced warning (globally or inline). */ function isSilenced(diagnostic) { if (diagnostic.category !== ts.DiagnosticCategory.Warning) { return false; } if (!jsii_diagnostic_1.JsiiDiagnostic.isJsiiDiagnostic(diagnostic)) { return false; } if (exports.silencedWarnings.has(diagnostic.jsiiCode)) { return true; } return isInlineSuppressed(diagnostic); } /** * Check if a diagnostic is suppressed inline via a `@jsii suppress` directive. * * Diagnostics reference a source position (typically the name identifier of a * declaration), but JSDoc tags are attached to the enclosing declaration node, * not the identifier. We therefore start at the token at the diagnostic * position and walk up the AST, checking each ancestor for `@jsii suppress` * directives. This means a directive on a class suppresses matching warnings * on all its members. */ function isInlineSuppressed(diagnostic) { if (diagnostic.file == null || diagnostic.start == null) { return false; } // `getTokenAtPosition` is exported from the `typescript` module but is not // included in the public type declarations. It has been stable since TS 2.0 // and is used extensively by the language service. We cast through `any` to // access it. Internally it descends through `node.getChildren()` to find the // deepest node at a given position. const getTokenAtPosition = ts.getTokenAtPosition; let current = getTokenAtPosition(diagnostic.file, diagnostic.start); while (current) { const directives = directives_1.Directives.of(current, () => { }); for (const code of directives.suppressions) { try { if (parseWarningCodes(code).includes(diagnostic.jsiiCode)) { return true; } } catch { // Unknown code — ignore } } current = current.parent; } return false; } //# sourceMappingURL=warnings.js.map