UNPKG

typescript-docs-verifier

Version:

Verifies that typescript examples in markdown files actually compile.

136 lines 5.96 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.LocalImportSubstituter = void 0; const path = __importStar(require("path")); class ExportResolver { packageName; packageMain; packageExports; constructor(packageName, packageMain, packageExports) { if (!packageMain && !packageExports) { throw new Error("Failed to find a valid main or exports entry in package.json file"); } this.packageName = packageName; this.packageMain = packageMain; this.packageExports = packageExports; } findMatchingExport(path = ".") { if (!this.packageExports) { throw new Error("No exports defined in package.json"); } if (typeof this.packageExports === "string" && path === ".") { return this.packageExports; } const conditionalExports = this.packageExports; const conditionalExportEntry = conditionalExports["node-addons"] ?? conditionalExports.node ?? conditionalExports.import ?? conditionalExports.require ?? conditionalExports.default; if (conditionalExportEntry && path === ".") { return conditionalExportEntry; } const subpathExports = this.packageExports; const lookupPath = path === "." ? path : `.${path}`; const [matchingExportPath, matchingSubpath] = Object.entries(subpathExports).find(([exportedPath]) => { if (lookupPath === exportedPath) { return true; } const [prefix, suffix] = exportedPath.split("*"); return (exportedPath.includes("*") && lookupPath.startsWith(prefix) && lookupPath.endsWith(suffix || "")); }) ?? []; if (!matchingExportPath || !matchingSubpath) { throw new Error(`Unable to resolve export for path "${this.packageName}${path === "." ? "" : path}"`); } const [exportPrefix, exportSuffix = ""] = matchingExportPath.split("*"); const internalPath = lookupPath.substring(exportPrefix.length, lookupPath.length - exportSuffix.length); const subpathEntry = typeof matchingSubpath === "string" ? matchingSubpath : (matchingSubpath["node-addons"] ?? matchingSubpath.node ?? matchingSubpath.import ?? matchingSubpath.require ?? matchingSubpath.default); if (subpathEntry) { const [internalPrefix, internalSuffix = ""] = subpathEntry.split("*"); return `${internalPrefix}${internalPath}${internalSuffix}`; } throw new Error(`Unable to resolve export for path "${this.packageName}${path === "." ? "" : path}"`); } resolveExportPath(path) { if (!this.packageExports) { if (!this.packageMain) { throw new Error("Failed to find main or exports entry in package.json"); } return path ?? this.packageMain; } const matchingExport = this.findMatchingExport(path); return matchingExport; } } class LocalImportSubstituter { packageName; packageRoot; exportResolver; constructor(packageDefinition) { this.packageName = packageDefinition.name; this.packageRoot = packageDefinition.packageRoot; this.exportResolver = new ExportResolver(packageDefinition.name, packageDefinition.main, packageDefinition.exports); } substituteLocalPackageImports(code) { const escapedPackageName = this.packageName.replace(/\\/g, "\\/"); const projectImportRegex = new RegExp(`(from\\s+)?('|")(?:${escapedPackageName})(/[^'"]+)?('|"|)`); const codeLines = code.split("\n"); const localisedLines = codeLines.map((line) => { const match = projectImportRegex.exec(line); if (!match) { return line; } const { 1: from, 2: openQuote, 3: subPath, 4: closeQuote, index } = match; const prefix = line.substring(0, index); const resolvedExportPath = this.exportResolver.resolveExportPath(subPath); const fullExportPath = path .join(this.packageRoot, resolvedExportPath) .replace(/\\/g, "\\\\"); return `${prefix}${from || ""}${openQuote}${fullExportPath}${closeQuote}`; }); return localisedLines.join("\n"); } } exports.LocalImportSubstituter = LocalImportSubstituter; //# sourceMappingURL=LocalImportSubstituter.js.map