typescript-compiler-api - 使用 TypeScript Compiler API 从 @types/long 中提取导出的符号
问题描述
我正在尝试破解一些用于 X-to-Javascript 转译器的绑定生成器,并且无法从@types/long
包中获取所有导出的符号集。TypeScript 定义大致如下:
export = Long;
export as namespace Long;
declare const Long: Long.LongConstructor;
type Long = Long.Long;
declare namespace Long {
interface LongConstructor {
/* methods and properties */
}
interface Long
{
/* methods and properties */
}
}
我拿不到declare const Long: Long.LongConstructor
东西,所以我可以在生成的绑定中生成相应的值声明。
说明我当前方法的示例 JS 脚本:
const path = require("path");
const ts = require("typescript");
const util = require("util")
var get_flags = function(e, flags) {
var selected = [];
for (var flag in e) {
if (typeof flag == "string") {
var val = e[flag];
if ((flags & val) === val) selected.push(flag);
}
}
return selected.join(" | ");
};
var options = ts.getDefaultCompilerOptions();
var chost = ts.createCompilerHost(options);
var filename = "node_modules/@types/long/index.d.ts";
var program = ts.createProgram([filename], options, chost, program);
var checker = program.getTypeChecker();
var filename = path.resolve(filename);
var source_file = program.getSourceFiles().find((f) => f.path == filename);
var m = checker.getSymbolAtLocation(source_file);
var exports = checker.getExportsAndPropertiesOfModule(m);
exports.forEach((e) => {
console.log(e.escapedName, get_flags(ts.SymbolFlags, e.flags), e.declarations[0].getText());
});
我从中得到的输出:
LongConstructor None | Interface | PropertyExcludes | NamespaceModuleExcludes interface LongConstructor {
/* methods and properties */
}
Long None | Interface | PropertyExcludes | NamespaceModuleExcludes interface Long
{
/* methods and properties */
}
prototype None | Property | PropertyExcludes | NamespaceModuleExcludes prototype: Long;
MAX_UNSIGNED_VALUE None | Property | PropertyExcludes | NamespaceModuleExcludes MAX_UNSIGNED_VALUE: Long;
MAX_VALUE None | Property | PropertyExcludes | NamespaceModuleExcludes MAX_VALUE: Long;
MIN_VALUE None | Property | PropertyExcludes | NamespaceModuleExcludes MIN_VALUE: Long;
NEG_ONE None | Property | PropertyExcludes | NamespaceModuleExcludes NEG_ONE: Long;
ONE None | Property | PropertyExcludes | NamespaceModuleExcludes ONE: Long;
UONE None | Property | PropertyExcludes | NamespaceModuleExcludes UONE: Long;
UZERO None | Property | PropertyExcludes | NamespaceModuleExcludes UZERO: Long;
ZERO None | Property | PropertyExcludes | NamespaceModuleExcludes ZERO: Long;
fromBits None | Method | PropertyExcludes | NamespaceModuleExcludes fromBits( lowBits:number, highBits:number, unsigned?:boolean ): Long;
fromInt None | Method | PropertyExcludes | NamespaceModuleExcludes fromInt( value: number, unsigned?: boolean ): Long;
fromNumber None | Method | PropertyExcludes | NamespaceModuleExcludes fromNumber( value: number, unsigned?: boolean ): Long;
fromString None | Method | PropertyExcludes | NamespaceModuleExcludes fromString( str: string, unsigned?: boolean | number, radix?: number ): Long;
fromBytes None | Method | PropertyExcludes | NamespaceModuleExcludes fromBytes( bytes: number[], unsigned?: boolean, le?: boolean ): Long;
fromBytesLE None | Method | PropertyExcludes | NamespaceModuleExcludes fromBytesLE( bytes: number[], unsigned?: boolean ): Long;
fromBytesBE None | Method | PropertyExcludes | NamespaceModuleExcludes fromBytesBE( bytes: number[], unsigned?: boolean ): Long;
isLong None | Method | PropertyExcludes | NamespaceModuleExcludes isLong( obj: any ): obj is Long;
fromValue None | Method | PropertyExcludes | NamespaceModuleExcludes fromValue( val: Long | number | string | {low: number, high: number, unsigned: boolean} ): Long;
我看不出这个常数是如何在出口中暴露出来的……有没有可靠的方法来获取有关它的信息?
解决方案
推荐阅读
- kubernetes - 是否可以在 GKE 的区域集群中创建仅限区域的节点池?
- github - 带有生成文件的 GitHub 版本
- r - 如何从“规范”类的对象中提取组件?
- mongodb - 使用 Mongodump shell 时,如何从副本集中指定数据库和集合?
- python-3.x - ValueError:无法在位置 0 (Pandas) 解析字符串“2.90 3.50 2.35”
- lua - Roblox Studio 我的脚本只能运行一次
- google-sheets - 计数值在范围内增加的次数 - Google 表格
- node.js - 任何人都可以引导我朝着正确的方向前进,让这个 Azure 函数在它输出“无法找到模块'@azure/functions'的地方?
- concatenation - Libreoffice calc 连接单元格内容和括号
- javascript - 如何在使用 driver.js 创建的弹出框上使用 MathJax?