如何确定类成员是 getter 还是 setter?

问题描述 投票:0回答:1

我正在尝试使用 ts-morph 来验证我正在编写的转译器中的代码。不,它不是 TypeScript 转译器,但我需要在原始文件中支持 JS 和 TS 代码。

我有以下代码:(classObj是我原始代码中的类之一)

  const classMembers = classObj.getMembers();
  classMembers.forEach(member => {
    const name = member.getName();
    const isMethod = member.isKind(174);
    const isStatic = member.isStatic();
    const isAsync = member.isAsync ? member.isAsync() : false;
    const isPrivate = name.startsWith('#');
    console.log({ name, isMethod, isStatic, isAsync, isPrivate });
  });

我试图找出当前成员是 getter 还是 setter,并且文档非常有限,所以我不知道如何做到这一点。我还通读了 GitHub 存储库上的所有相关问题和讨论,但仍然没有找到任何解释如何执行此操作的内容。

member
上是否有一个方法可以告诉我这个成员是 getter 还是 setter?

我知道我可以打电话给

member.getFullText()
,它会返回类似这样的内容:


    static get val() {
      return this.#_val;
    }

然后我知道我可以解析该字符串以查看是否使用了

get
set
。但我假设这已经由 ts-morph 完成了,我只需要知道调用什么来解决这个问题。

这是我正在解析的示例文件:

class SomeClass {
    #_val = 'dog';
    #money = 19.95;
    bgColor = 'black';

    static async tacos() {
      return 10;
    }

    static get val() {
      return this.#_val;
    }

    static set val(newVal) {
      this.#_val = newVal;
    }

    async doSomething(volume: number): string {
      return 'Boom boom!';
    }

    #update(obj) {
      const {x, y, z} = obj;
    }
  }

这是上面代码的输出:

{
  name: '#_val',
  isMethod: false,
  isStatic: false,
  isAsync: false,
  isPrivate: true
}
{
  name: '#money',
  isMethod: false,
  isStatic: false,
  isAsync: false,
  isPrivate: true
}
{
  name: 'bgColor',
  isMethod: false,
  isStatic: false,
  isAsync: false,
  isPrivate: false
}
{
  name: 'tacos',
  isMethod: true,
  isStatic: true,
  isAsync: true,
  isPrivate: false
}
{
  name: 'val',
  isMethod: false,
  isStatic: true,
  isAsync: false,
  isPrivate: false
}
{
  name: 'val',
  isMethod: false,
  isStatic: true,
  isAsync: false,
  isPrivate: false
}
{
  name: 'doSomething',
  isMethod: true,
  isStatic: false,
  isAsync: true,
  isPrivate: false
}
{
  name: '#update',
  isMethod: true,
  isStatic: false,
  isAsync: false,
  isPrivate: true
}

任何帮助将不胜感激。 迈克

javascript abstract-syntax-tree getter-setter ts-morph
1个回答
0
投票

我明白了。

以下是我需要的更改:

我需要导入

Node
:

import { Project, Node } from "ts-morph";

然后我在

Node
上使用静态函数:

  const classMembers = classObj.getMembers();
  classMembers.forEach(member => {
    const name = member.getName();
    const isMethod = member.isKind(174);
    const isStatic = member.isStatic();
    const isAsync = member.isAsync ? member.isAsync() : false;
    const isPrivate = name.startsWith('#');
    const isGetter = Node.isGetAccessorDeclaration(member); // here
    const isSetter = Node.isSetAccessorDeclaration(member); // And here

    console.log({ name, isMethod, isStatic, isAsync, isPrivate, isGetter, isSetter });
  });

现在 getter 和 setter 的计算正在工作:

{
  name: 'val',
  isMethod: false,
  isStatic: true,
  isAsync: false,
  isPrivate: false,
  isGetter: true,
  isSetter: false
}
{
  name: 'val',
  isMethod: false,
  isStatic: true,
  isAsync: false,
  isPrivate: false,
  isGetter: false,
  isSetter: true
}
© www.soinside.com 2019 - 2024. All rights reserved.