首页 > 解决方案 > 使用 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;

我看不出这个常数是如何在出口中暴露出来的……有没有可靠的方法来获取有关它的信息?

标签: typescript-compiler-api

解决方案


推荐阅读