【发布时间】:2022-02-01 00:46:47
【问题描述】:
我知道ts-ast-viewer,但我不知道他们如何从联合中提取元素列表。
我已经尝试了几种现有的解决方案,包括this,但似乎大多数已经过时了。一些ts.[methods] 现已弃用。
这是启动调试编译器 API 的初始代码:
import * as ts from "typescript";
const code = "type Foo = 'one'|'two'|'three'";
const sourceFile = ts.createSourceFile(
"foo.ts",
code,
ts.ScriptTarget.Latest,
true
);
function print(node: ts.Node, name: string) {
console.log({ node });
}
print(sourceFile, "Foo");
我知道来自TS wiki 的示例。这是我稍作修改的版本:
//@ts-ignore
import * as ts from "typescript";
const code = "type Foo = 'one'|'two'|'three'";
const sourceFile = ts.createSourceFile(
"foo.ts",
code,
ts.ScriptTarget.Latest,
true
);
function extract(identifiers: string[]): void {
//@ts-ignore
const unfoundNodes = [];
//@ts-ignore
const foundNodes = [];
//@ts-ignore
ts.forEachChild(sourceFile, (node) => {
let name = "";
//@ts-ignore
if (ts.isFunctionDeclaration(node)) {
//@ts-ignore
name = node.name.text;
//@ts-ignore
node.body = undefined;
//@ts-ignore
} else if (ts.isVariableStatement(node)) {
//@ts-ignore
name = node.declarationList.declarations[0].name.getText(sourceFile);
//@ts-ignore
} else if (ts.isInterfaceDeclaration(node)) {
name = node.name.text;
}
//@ts-ignore
const container = identifiers.includes(name) ? foundNodes : unfoundNodes;
//@ts-ignore
container.push([name, node]);
});
//@ts-ignore
return (unfoundNodes[0][1] as any).type.types.map(
(elem: any) => elem.literal.text
);
}
// Run the extract function with the script's arguments
console.log(extract(["Foo"]));
而且它有效。但是,如果我将源代码从 "type Foo = 'one'|'two'|'three'" 更改为 "type Foo = keyof Array<any>" - 它不会。
我知道我的版本是错误的。
另外,我尝试过this 示例,但forEachDescendant 似乎在node 上不存在。
如何写一个函数来获取并集的元素数组?
预期功能:
import * as ts from "typescript";
const sourceCode = "type Foo = keyof Array<number>";
union(sourceCode, "Foo") // ["forEach", "reduce", "map" ...]
我需要这个用于调试目的。
【问题讨论】:
-
不确定我是否理解这个问题。
exatrct已经在const sourceCode = "type Foo = 'one'|'two'";上工作以获取["one", "two"]。什么不起作用? -
@TitianCernicova-Dragomir 但它不适用于
type Foo = keyof Array<number> -
所以你想要联合的解析类型。
-
是的,我想从
type Foo = keyof Array<number>获得["forEach", "reduce", "map" ...]。对不起,不够清楚。在我的问题中也做了更新
标签: typescript typescript-compiler-api