UNPKG

vite-plugin-react18-pages

Version:

<p> <a href="https://www.npmjs.com/package/vite-plugin-react-pages" target="_blank" rel="noopener"><img src="https://img.shields.io/npm/v/vite-plugin-react-pages.svg" alt="npm package" /></a> </p>

156 lines 7.33 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.collectInterfaceInfo = void 0; const ts = __importStar(require("typescript")); const defaultTsConfig = { moduleResolution: ts.ModuleResolutionKind.NodeJs, }; function collectInterfaceInfo(fileName, exportName, options = defaultTsConfig) { var _a; // Build a program using the set of root file names in fileNames let program = ts.createProgram([fileName], options); // Get the checker, we will use it to find more about classes let checker = program.getTypeChecker(); const sourceFile = program.getSourceFile(fileName); // inspired by // https://github.com/microsoft/rushstack/blob/6ca0cba723ad8428e6e099f12715ce799f29a73f/apps/api-extractor/src/analyzer/ExportAnalyzer.ts#L702 // and https://stackoverflow.com/a/58885450 const fileSymbol = checker.getSymbolAtLocation(sourceFile); if (!fileSymbol || !fileSymbol.exports) { throw new Error(`unexpected fileSymbol`); } const escapedExportName = ts.escapeLeadingUnderscores(exportName); const exportSymbol = fileSymbol.exports.get(escapedExportName); if (!exportSymbol) { throw new Error(`Named export ${exportName} is not found in file`); } const sourceDeclareSymbol = getAliasedSymbolIfNecessary(exportSymbol); const sourceDeclare = (_a = sourceDeclareSymbol.declarations) === null || _a === void 0 ? void 0 : _a[0]; if (!sourceDeclare) { throw new Error(`Can't find sourceDeclare for ${exportName}`); } const interfaceInfo = collectInterfaceInfo(sourceDeclare, sourceDeclareSymbol); return interfaceInfo; function getAliasedSymbolIfNecessary(symbol) { if ((symbol.flags & ts.SymbolFlags.Alias) !== 0) return checker.getAliasedSymbol(symbol); return symbol; } function collectInterfaceInfo(node, symbol) { var _a, _b; if (!ts.isInterfaceDeclaration(node)) throw new Error(`target is not an InterfaceDeclaration`); const type = checker.getTypeAtLocation(node); if (!symbol) throw new Error(`can't find symbol`); const name = node.name.getText(); const commentText = (_a = getComment(node, node.getSourceFile().getFullText())) !== null && _a !== void 0 ? _a : ''; const description = ts.displayPartsToString(symbol.getDocumentationComment(checker)); const propertiesInfo = []; // extract property info (_b = symbol.members) === null || _b === void 0 ? void 0 : _b.forEach((symbol) => { var _a, _b, _c, _d; const name = symbol.name; const declaration = symbol.valueDeclaration; if (!(declaration && (ts.isPropertySignature(declaration) || ts.isMethodSignature(declaration)))) { // known member in interface symbol // that we don't handle if (symbol.getFlags() & ts.SymbolFlags.TypeParameter) return; console.warn(`unexpected declaration type in interface. name: ${name}, kind: ${ts.SyntaxKind[declaration === null || declaration === void 0 ? void 0 : declaration.kind]}`); return; } const commentText = (_a = getComment(declaration, declaration.getSourceFile().getFullText())) !== null && _a !== void 0 ? _a : ''; const typeText = (_c = (_b = declaration.type) === null || _b === void 0 ? void 0 : _b.getFullText()) !== null && _c !== void 0 ? _c : ''; const description = ts.displayPartsToString(symbol.getDocumentationComment(checker)); const isOptional = !!(symbol.getFlags() & ts.SymbolFlags.Optional); // get defaultValue from jsDocTags const jsDocTags = symbol.getJsDocTags(); const defaultValueTag = jsDocTags.find((t) => t.name === 'defaultValue' || 'default'); const defaultValue = (_d = defaultValueTag === null || defaultValueTag === void 0 ? void 0 : defaultValueTag.text) === null || _d === void 0 ? void 0 : _d[0].text; propertiesInfo.push({ name, // commentText, type: typeText, description, defaultValue, optional: isOptional, // fullText: declaration.getFullText(), }); }); const interfaceInfo = { name, // commentText, description, properties: propertiesInfo, // fullText: node.getFullText(), }; return interfaceInfo; } /** True if this is visible outside this file, false otherwise */ function isNodeExported(node) { return ((ts.getCombinedModifierFlags(node) & ts.ModifierFlags.Export) !== 0 && !!node.parent && node.parent.kind === ts.SyntaxKind.SourceFile); } } exports.collectInterfaceInfo = collectInterfaceInfo; function getJSDocCommentRanges(node, text) { // Compiler internal: // https://github.com/microsoft/TypeScript/blob/66ecfcbd04b8234855a673adb85e5cff3f8458d4/src/compiler/utilities.ts#L1202 return ts.getJSDocCommentRanges.apply(ts, arguments); } function getComment(declaration, sourceFileFullText) { const ranges = getJSDocCommentRanges(declaration, sourceFileFullText); if (!ranges || !ranges.length) return; const range = ranges[ranges.length - 1]; if (!range) return; const text = sourceFileFullText.slice(range.pos, range.end); return text; } /** * ref: * * https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API * * https://stackoverflow.com/questions/59838013/how-can-i-use-the-ts-compiler-api-to-find-where-a-variable-was-defined-in-anothe * * https://stackoverflow.com/questions/60249275/typescript-compiler-api-generate-the-full-properties-arborescence-of-a-type-ide * * https://stackoverflow.com/questions/47429792/is-it-possible-to-get-comments-as-nodes-in-the-ast-using-the-typescript-compiler * * Instructions of learning ts compiler: * https://stackoverflow.com/a/58885450 * * https://learning-notes.mistermicheels.com/javascript/typescript/compiler-api/ */ //# sourceMappingURL=extract.js.map