简体   繁体   中英

How to transpile VariableDeclarator to AssignmentExpression?

I'm trying to take something like var a = 5; and transpile it to something like thing.a = 5 .

Using this code below in my visitor, it tells me unexpected token .

VariableDeclarator: {
  enter: function (path, state) {
    path.replaceWith( 
      t.assignmentExpression(
        '=',
        t.memberExpression(
          t.identifier('abc'),
          t.identifier('def')
        ),
        t.stringLiteral('xyz')
      )
    )
  }
}

What am I not taking into account here?

What's the canonical way to accomplish this?

Turns out I was operating on a Declarator rather than a Declaration. So what I was doing was causing it to compile to something like var a.4 = 'def' . Naturally, that fails.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM