question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ts-migrate is inserting comments in JSX components

See original GitHub issue

I’m giving ts-migrate a try to migrate a large gatsby project. My issue is JS comments are inserted in components:

export function Input(props: any) {
  return props.readonly ? (
    <div className={style.readonlyInputField} style={props.style}>
      {props.value}
    </div>
  ) : (
    <div className={style.inputField} style={props.style}>
      // @ts-expect-error ts-migrate(2607) FIXME: JSX element class does not support attributes beca... Remove this comment to see the full error message
      <RawInput className={style.input} {...props} />
    </div>
  )
}

This makes them visible in the rendered output. They should be inserted with braces instead:

export function Input(props: any) {
  return props.readonly ? (
    <div className={style.readonlyInputField} style={props.style}>
      {props.value}
    </div>
  ) : (
    <div className={style.inputField} style={props.style}>
      {/* @ts-expect-error ts-migrate(2607) FIXME: JSX element class does not support attributes beca... Remove this comment to see the full error message */}
      <RawInput className={style.input} {...props} />
    </div>
  )
}

I ran npx -p ts-migrate -c "ts-migrate-full ." at the root of the project. During the migration I saw a lot of occurrences of this error:

Error: [react-props][src/components/multiStates.tsx] Error:
 TypeError: Cannot read property 'length' of undefined
    at isReactSfcFunctionDeclaration (/home/flupke/src/jitter/jitter.video/frontend/node_modules/ts-migrate-plugins/build/src/plugins/utils/react.js:32:40)
    at Array.filter (<anonymous>)
    at Object.getNumComponentsInSourceFile (/home/flupke/src/jitter/jitter.video/frontend/node_modules/ts-migrate-plugins/build/src/plugins/utils/react.js:76:10)
    at Object.createPropsTypeNameGetter (/home/flupke/src/jitter/jitter.video/frontend/node_modules/ts-migrate-plugins/build/src/plugins/utils/react-props.js:239:41)
    at Object.run (/home/flupke/src/jitter/jitter.video/frontend/node_modules/ts-migrate-plugins/build/src/plugins/react-props.js:42:48)
    at Object.migrate (/home/flupke/src/jitter/jitter.video/frontend/node_modules/ts-migrate-server/build/src/migrate/index.js:65:46)
    at async Object.handler (/home/flupke/src/jitter/jitter.video/frontend/node_modules/ts-migrate/build/cli.js:130:22)

I have 108 .tsx files in the project so this will be cumbersome to fix, did I do something wrong during the conversion?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:14
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
lnrdgmzcommented, Nov 6, 2021

I’m experiencing the same problem. I tried to pinpoint the problem by stepping through with the debugger, but I was unable to find the point of failure.

I changed versions of TypeScript to see if that could be the issue, and found the issue does not exist when using v4.2.4. With this version, I get the expected output after running yarn ts-migrate migrate:

export default ({
  name
}: any) => {
  return (
    // @ts-expect-error ts-migrate(7026) FIXME: JSX element implicitly has type 'any' because no i... Remove this comment to see the full error message
    <div>
      {/* @ts-expect-error ts-migrate(7026) FIXME: JSX element implicitly has type 'any' because no i... Remove this comment to see the full error message */}
      <div>Hello, {name}!</div>
    {/* @ts-expect-error ts-migrate(7026) FIXME: JSX element implicitly has type 'any' because no i... Remove this comment to see the full error message */}
    </div>
  )
}

With versions 4.3.2 and 4.4.4, I get the following:

export default ({
  name
}: any) => {
  return (
    // @ts-expect-error ts-migrate(7026) FIXME: JSX element implicitly has type 'any' because no i... Remove this comment to see the full error message
    <div>
      // @ts-expect-error ts-migrate(7026) FIXME: JSX element implicitly has type 'any' because no i... Remove this comment to see the full error message
      <div>Hello, {name}!</div>
    // @ts-expect-error ts-migrate(7026) FIXME: JSX element implicitly has type 'any' because no i... Remove this comment to see the full error message
    </div>
  )
}
3reactions
sharmilajesupaulcommented, Feb 16, 2022

@Rudeg @brieb @lencioni +1 i’m also able to repro this bug in typescript@4.4.4 A meta question: can we add a help-wanted label? so that the community can pick those up and contribute these fixes?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ts-migrate: A Tool for Migrating to TypeScript at Scale - Medium
Take a JavaScript or partial TypeScript project and convert it completely. We'll need to add some any types and @ts-ignore comments so the ......
Read more >
ts-migrate - npm
A tool for migrating frontend application from JavaScript to TypeScript. Latest version: 0.1.35, last published: 25 days ago.
Read more >
Introduce TypeScript to react(js) project with ts-migrate
Most cases will be solved by installing @types files for npm packages so what we need to do is to remove comments. Modify...
Read more >
"Automatically Migrating to TypeScript with ts ... - YouTube
(Evan Shaw) Learn about ts-migrate, an open source tool to automate the process of converting a JavaScript code base to TypeScript.
Read more >
How to comment HTML within React written in TypeScript?
Wrap the /* the regular JavaScript comment */ in curly braces: {/* <div>{this.state.myVal} Not Working</div> */}. A multi-line comment would ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found