thx / gogocode

GoGoCode is a transformer for JavaScript/Typescript/HTML based on AST but providing a more intuitive API.
MIT License
5.61k stars 425 forks source link

带 @ 装饰器 find 报错 #118

Open ediaos opened 2 years ago

ediaos commented 2 years ago
kangdy commented 2 years ago

同样遇到这个问题

ken1691 commented 2 years ago
  • version: 1.0.41
  • playground demo: playground link
  • your description: 带 @ 装饰器无法 find ,提示 Error: parse error!Leading decorators must be attached to a class declaration.

这个问题你是怎么处理的呀? 我也是遇到这个问题。

ediaos commented 2 years ago

const decorators = classAST.node.declaration?.decorators 就是装饰器的内容,然后再二次处理,比如替换下 @ 字符 再查找

SublimeCT commented 1 year ago

可以像这样获取到这个 class, 然后再修改装饰器:

function transform(fileInfo, api, options) {
  const $ = api.gogocode
  const source = fileInfo.source
  // return your transformed code here
  return $(source)
    .find('@abc() class $_$ {}')
    .each(d => {
      d.node.decorators[0].expression.callee.name = 'def'
      d.node.decorators[0].expression.arguments[0].value = '456'
    })
    .generate()
}
@def(456)
class {
  a: 1
  b: 2
}