Skip to content

bug?: Using ts parser does not replace correctly #511

@rxliuli

Description

@rxliuli

I'm using Jscodeshift to replace the following code in astexplorer testing, but I get different results using babel/ts

export const systemCoreApi = wrapApiClient<ISystemApi>({  namespace: 'system' })

use babel parser

export const parser = "babel";

// Press ctrl+space for code completion
export default function transformer(file, api) {
  const j = api.jscodeshift;
  const root = j(file.source);

  root
    // .find(j.TSTypeReference)
    .find(j.Identifier, { name: "ISystemApi" })
    .replaceWith((path) => {
      path.node.name = "ISystemCoreApi";
      return path.node;
    });

  return root.toSource();
}

output

export const systemCoreApi = wrapApiClient<ISystemCoreApi>({  namespace: 'system' })

use ts parser

// jscodeshift can take a parser, like "babel", "babylon", "flow", "ts", or "tsx"
// Read more: https://github.com/facebook/jscodeshift#parser
export const parser = "ts";

// Press ctrl+space for code completion
export default function transformer(file, api) {
  const j = api.jscodeshift;
  const root = j(file.source);

  root
    // .find(j.TSTypeReference)
    .find(j.Identifier, { name: "ISystemApi" })
    .replaceWith((path) => {
      path.node.name = "ISystemCoreApi";
      return path.node;
    });

  return root.toSource();
}

output

export const systemCoreApi = wrapApiClient<ISystemApi>({  namespace: 'system' })

Lastly, I'm wondering how to use the @typescript-eslint/parser parser, it seems to work best for parsing ts in ast explorer (the result of ts parsing is very different from other parsers and jscodeshift's api)

image
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions