UNPKG

@kstasi/jest-tolk

Version:

<p align="center"> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/tonkite/tonkite/main/assets/logo-dark.svg"> <img alt="tonkite logo" src="https://raw.githubusercontent.com/tonkite/tonkite/main/a

85 lines (84 loc) 3.32 kB
"use strict"; /** * Copyright 2024 Scaleton Labs * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractDocBlock = extractDocBlock; exports.extractGetMethods = extractGetMethods; const web_tree_sitter_1 = __importDefault(require("web-tree-sitter")); const tree_sitter_tolk_1 = require("@tonkite/tree-sitter-tolk"); function extractDocBlock(node) { if (node.type !== 'comment') { return null; } if (!node.previousSibling) { return node.text; } // NOTE: It's a comment of another statement. if (node.previousSibling.endPosition.row === node.startPosition.row) { return null; } const previous = extractDocBlock(node.previousSibling); return previous ? previous + '\n' + node.text : node.text; } function extractArgTypes(node) { const parameters = node.childForFieldName('parameters'); if (!parameters) { return null; } const argTypes = parameters.children .filter((child) => child.type === 'parameter_declaration') .map((child) => { const type = child.childForFieldName('type')?.text ?? ''; const name = child.childForFieldName('name')?.text ?? ''; return { type, name }; }); return argTypes.length ? argTypes : null; } async function extractGetMethods(sourceCode) { await web_tree_sitter_1.default.init(); const parser = new web_tree_sitter_1.default(); parser.setLanguage(await (0, tree_sitter_tolk_1.loadTolk)()); const { rootNode } = parser.parse(sourceCode); const methods = []; for (const node of rootNode.children) { if (node.type !== 'get_method_declaration') { continue; } const annotations = node.children .find((child) => child.type === 'annotation_list') ?.children.filter((child) => child.type === 'annotation') ?? []; const methodId = annotations .find((annotation) => annotation.child(1)?.text === 'method_id') ?.child(3)?.text; const methodName = node.childForFieldName('name')?.text; const docBlock = node.previousSibling ? extractDocBlock(node.previousSibling)?.replace(/(^|\n)(\/\/\s*)/g, '$1') : null; const argTypes = extractArgTypes(node); methods.push({ methodId: methodId ? parseInt(methodId.replace(/^0x/, ''), methodId.startsWith('0x') ? 16 : 10) : undefined, methodName: methodName, docBlock: docBlock ?? undefined, argTypes: argTypes ?? undefined, }); } return methods; }