How to transpile VariableDeclarator to AssignmentExpression?

260 views Asked by At

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?

1

There are 1 answers

0
realisation On

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.