1

我想从特定文件中获取所有接口名称。

例如:

文件1.ts

   private interface first{
...
}

private interface second{
...
}

private interface third{
...
}

文件2.ts

const interfacesList = GetInterfacesFrom(filePath); //in this case filePath = file1.ts

我应该会回来的:["first","second","third"];

更新

我想在打字稿中建立一个像EntityFramework这样的机制;

但是我需要知道如何将接口/类的名称与 api 路径自动匹配,以知道我是否有要调用的 api 接口或类...

4

2 回答 2

1

通过在 typescript 子项目中使用编译器 API,我在代码中获得了接口名称。打字稿版本 4.1.3

这是我的承诺功能:

const getInterfacesNamesFromFile = (filepath: string): Promise<string[]> => {
    return new Promise ((res, rej) => {
        // absolute path to interface file
        const absoluteFilepath = path.join(path.resolve(...basePath), filepath);
        const mainProgramFilepath = path.join(__dirname, '../', '../', 'src', 'main.ts');
        let program = ts.createProgram([mainProgramFilepath], {allowJs: true});
        const sourceFile = program.getSourceFile(absoluteFilepath);
        const interfaceNames: string[] = [];
        ts.forEachChild(sourceFile, node => {
            if (ts.isInterfaceDeclaration(node) {
                interfaceNames.push(node.name.text);
            }
        })
        res(interfaceNames);
    });
}

编辑

上面的函数没有涵盖从其他文件导入类型,这意味着任何从某个地方导入的接口文件都失败了。

为了使这些工作,您可能需要配置 createProgram 参数以匹配您的项目。这里有一些建议(NodeJS 项目):

import path from 'path';
import dir from 'node-dir';
import * as ts from 'typescript';

// compiler options has enums, so check definitions inside
const tsconfig: ts.CompilerOptions = {
  moduleResolution: 2, // "node"
  module: 1, // CommonJS
  esModuleInterop: true,
  target: 2, // ES6, says ES2015, should be same
  baseUrl: "../src", // .. if you have typescript subproject
  ...
}
// get source files in array, same as tsconfig includes: ['src/**/*']
const rootNames = await new Promise((res, rej) => {
  dir.files(path.resolve('src'), (err, files) => {
    err ? rej() : res(files);
  });
});

let program = ts.createProgram(rootNames, tsconfig);

然后修改上述函数以使用带有配置的创建程序。

于 2021-08-17T14:26:50.303 回答
0

您需要使用编译器 API

该 API 允许您解析源代码并从中获取符号

于 2017-03-09T03:01:04.337 回答