facebook / jscodeshift

A JavaScript codemod toolkit.
https://jscodeshift.com
MIT License
9.22k stars 477 forks source link

Parsing .d.ts files #539

Open ceremonious opened 1 year ago

ceremonious commented 1 year ago

Is it possible to use jscodeshift to modify .d.ts files?

I'm trying to parse a file that looks like this:

import * as runtime from '@prisma/client/runtime/index';
declare const prisma: unique symbol
export type PrismaPromise<A> = Promise<A> & {[prisma]: true}
type UnwrapPromise<P extends any> = P extends Promise<infer R> ? R : P
type UnwrapTuple<Tuple extends readonly unknown[]> = {
  [K in keyof Tuple]: K extends `${number}` ? Tuple[K] extends PrismaPromise<infer X> ? X : UnwrapPromise<Tuple[K]> : UnwrapPromise<Tuple[K]>
};

export type PrismaVersion = {
  client: string
}

export const prismaVersion: PrismaVersion 

//...more content

If I use Babel as a parser, I get this error: SyntaxError: Missing semicolon. (2:7)

If I use Typescript, I get this error: SyntaxError: Missing initializer in const declaration. (13:41)

Is there a different parser or parser config I can use?

goughjo02 commented 1 year ago

yeah if i have type definitions in my tsx file it throws with the "missing semicolon" error

goughjo02 commented 1 year ago

@ceremonious try adding --parser=flow to the command at the command line, i think it is working for me

NullVoxPopuli commented 6 months ago

parser=flow to the command at the command line, i think it is working for me

the parsing is successful, but output doesn't match.

For example, I'm seeing

export declare const two: number;

converted to

export declare var two: number;

This changes expectations around mutability