jscodeshift icon indicating copy to clipboard operation
jscodeshift copied to clipboard

Creates syntax error when transforming Flow types without identifier

Open silvenon opened this issue 5 years ago • 2 comments

In certain cases jscodeshift causes a syntax error when transforming types (in my case Flow, but maybe TS, too). Seee this demo.

Transform:

export default function transformer(file, api) {
  const j = api.jscodeshift;
  const root = j(file.source);
  
  return root
    .find(j.StringTypeAnnotation)
    .replaceWith(j.numberTypeAnnotation())
    .toSource()
}

export const parser = 'flow'

Source:

type Ob = {
  [string]: number,
}

Result:

type Ob = {
  [: number]: number,
}

silvenon avatar Jan 29 '20 17:01 silvenon

That extra colon was probably supposed to be an identifier, but I can't figure out why jscodeshift is adding it. I expected the same bug to happen when I replace it with the node of the same type:

return root
    .find(j.StringTypeAnnotation)
    .replaceWith(j.stringTypeAnnotation())
    .toSource()

But it works fine.

silvenon avatar Jan 30 '20 12:01 silvenon

Also, I just found out that the codemod transforms this:

export type Foo = string

into:

export type type Foo = number

Notice the extra type. 😓This only happens within the export declaration, type Foo = string would be transformed correctly.

Maybe this is all related somehow.

silvenon avatar Jan 30 '20 20:01 silvenon