Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for type casting/predicates on Typescript transform files? #467

Open
kitsune7 opened this issue Dec 10, 2021 · 6 comments
Open

Support for type casting/predicates on Typescript transform files? #467

kitsune7 opened this issue Dec 10, 2021 · 6 comments
Labels

Comments

@kitsune7
Copy link

jscodeshift version: 0.13.0
node version: 16.13.0

command:

npx jscodeshift -t src/transforms/sort-imports --extensions=ts,tsx --parser=tsx "${TRANSFORM_FILE}.ts" --print --dry

I'm using Typescript in a project running jscodeshift and I get the following error when I try to run one of my transform files:

  22 | export const hasSpecifiers = (
  23 |   importNode: ImportDeclaration | null
> 24 | ): importNode is ImportWithSpecifiers => {
     |  ^
  25 |   return !!importNode?.specifiers?.length;
  26 | };
  27 |
    at Object._raise (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/src/parser/error.js:147:45)
    at Object.raiseWithData (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/src/parser/error.js:142:17)
    at Object.raise (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/src/parser/error.js:91:17)
    at Object.semicolon (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/src/parser/util.js:127:10)
    at Object.parseVarStatement (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/src/parser/statement.js:808:10)
    at Object.parseStatementContent (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/src/parser/statement.js:307:21)
    at Object.parseStatement (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/src/parser/statement.js:247:17)
    at Object.parseStatement (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/src/plugins/flow/index.js:1831:26)
    at Object.parseExportDeclaration (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/src/parser/statement.js:1965:17)
    at Object.parseExportDeclaration (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/src/plugins/flow/index.js:2150:22) {
  loc: Position { line: 24, column: 1 },
  pos: 580,
  code: 'BABEL_PARSE_ERROR',
  reasonCode: 'MissingSemicolon'
}

I've gotten a similar errors in the same project while trying to use type casting:

  27 |       if (!aIsImport && !bIsImport) return 0; // Don't sort body statements
  28 |
> 29 |       const sourceA = getImportSource(a as ImportDeclaration);
     |                                         ^
  30 |       const sourceB = getImportSource(b as ImportDeclaration);
  31 |
  32 |       const aIsThirdParty = isThirdPartySource(sourceA);
    at Object._raise (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/lib/index.js:541:17)
    at Object.raiseWithData (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/lib/index.js:534:17)
    at Object.raise (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/lib/index.js:495:17)
    at Object.unexpected (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/lib/index.js:3580:16)
    at Object.expect (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/lib/index.js:3554:28)
    at Object.parseCallExpressionArguments (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/lib/index.js:11775:14)
    at Object.parseCoverCallAndAsyncArrowHead (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/lib/index.js:11698:29)
    at Object.parseSubscript (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/lib/index.js:11628:19)
    at Object.parseSubscript (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/lib/index.js:6227:18)
    at Object.parseSubscripts (/Users/christopher.bradshawgetweave.com/Git/codemods/node_modules/.pnpm/@babel+parser@7.16.4/node_modules/@babel/parser/lib/index.js:11601:19) {
  loc: Position { line: 29, column: 40 },
  pos: 921,
  code: 'BABEL_PARSE_ERROR',
  reasonCode: 'UnexpectedToken'
}

Is there a way to support these features of Typescript being used on the transform files or is there a way to override the parser for the transform script itself and not just the transform target? The tsx parser on the target files works great and tests will actually run the same transforms without any problems, but it's an issue if I try to use the codemod for anything other than a test.

@pcattori
Copy link

I'm also experiencing the same issue.

pcattori added a commit to remix-run/remix that referenced this issue Apr 11, 2022
pcattori added a commit to remix-run/remix that referenced this issue Apr 12, 2022
pcattori added a commit to remix-run/remix that referenced this issue Apr 12, 2022
@ElonVolo ElonVolo added the bug label May 6, 2022
@jimmynotjim
Copy link

Ran into this issue today as well while trying to create an object lookup based on the value returned from a JSXAttribute. Would be happy to help create some tests cases or even tackle this if someone can point me in the right direction.

@ElonVolo
Copy link
Collaborator

Would you be able to up to github a temporary repository that reproduces the error?

@jimmynotjim
Copy link

Of course. Give me a day or two to wrap some things up and I'll get back to you with a link.

@jimmynotjim
Copy link

Sorry for the delay, I'm a bit stumped. I created an example project but I can't reproduce the error. As far as I can tell the example is configured exactly the same as my current project that I encountered the issue, but at this point I have to assume it's a configuration problem on my end and not an issue with jscodeshift. If anyone else wants to dig into it, here's my example repo.

https://github.com/jimmynotjim/JSCodeshift-type-casting-error

This is exactly where the issue exists in my current project but runs just fine in the example:
https://github.com/jimmynotjim/JSCodeshift-type-casting-error/blob/main/codemods/tsCastCodemodExample.ts#L52

@jimmynotjim
Copy link

jimmynotjim commented Aug 1, 2022

I was able to figure out the issue. In my haste to limit the reproduction repo I didn't match my current repo file organization 1:1. In my current repo, I co-locate the transform, tests, and fixtures in a directory with the transform name, and export the transformer via an index file. When running the transform, I've been utilizing node's behavior of mapping the directory name to the index file.

It looks something like this

codemodName/
    index.ts
    codemodName.ts
    codemodName.test.ts
    codemodName.testFixtures.ts

I've updated my reproduction repo to match my current repo's file organization. So now:

this throws an error:
jscodeshift --parser tsx --extensions tsx -t ./codemods/tsCastCodemodExample ./src/components

but this works:
jscodeshift --parser tsx --extensions tsx -t ./codemods/tsCastCodemodExample/tsCastCodemodExample.ts ./src/components

I'm unfortunately not experienced enough to understand the difference here. Is there a way to keep the shorter path to the directory or is the full file path a requirement in this situation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants