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

prettier plugin: class with generic, extending namespaced base class, fails #1532

Open
byCedric opened this issue Oct 10, 2024 · 0 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@byCedric
Copy link

byCedric commented Oct 10, 2024

Bug Description

The flow-api-translator has been a blessing. Unfortunately, I ran into a bug specifically when:

  • When using a generic in the class definition
  • When extending a namespace class import

E.g.

// Input
import * as Test from 'test-lib'
class MyClass<T> extends Test.BaseClass {}
export default MyClass

This results in the following error:

/Users/cedric/.../node_modules/prettier/doc.js:931
              } else if (isConcat(doc) || doc.type === "fill") {
                                              ^

TypeError: Cannot read properties of undefined (reading 'type')
    at fits (/Users/cedric/.../node_modules/prettier/doc.js:931:47)
    at printDocToString (/Users/cedric/.../node_modules/prettier/doc.js:1073:44)
    at coreFormat (/Users/cedric/.../node_modules/prettier/index.js:8838:22)
    at formatWithCursor2 (/Users/cedric/.../node_modules/prettier/index.js:9021:18)
    at /Users/cedric/.../node_modules/prettier/index.js:38183:12
    at Object.format (/Users/cedric/.../node_modules/prettier/index.js:38197:12)
    at print (/Users/cedric/.../node_modules/hermes-transform/dist/transform/print.js:88:25)
    at translateFlowDefToTSDef (/Users/cedric/.../node_modules/flow-api-translator/dist/index.js:64:37)

Funny enough, using a named import actually works:

// Input
import { BaseClass } from 'test-lib'
class MyClass<T> extends BaseClass {}
export default MyClass

// Output
import { BaseClass } from "test-lib";
declare class MyClass<T> extends BaseClass {}
declare const $$EXPORT_DEFAULT_DECLARATION$$: typeof MyClass;
export default $$EXPORT_DEFAULT_DECLARATION$$;

Small side note, when using export default $$..$$ that is a typeof MyClass, the generic is wiped away. It would be nice if this could be written as just export default MyClass instead - it would retain the generic.

Used package versions:

  • flow-api-translator@0.24.0
  • prettier@2.8.8
  • prettier-plugin-hermes-parser@0.24.0

Steps To Reproduce

  1. npm i --save prettier@2.8.8 prettier-plugin-hermes-parser@0.24.0 flow-api-translator@0.24.0
  2. Create test.js with:
const code = `
import * as Test from 'test-lib'
class MyClass<T> extends Test.BaseClass {}
export default MyClass
`

require('flow-api-translator')
  .translateFlowToTSDef(code, { parser: 'babel' })
  .then(console.log)
  1. node ./test.js

The Expected Behavior

It converts the flow code to a typescript definition for named and namespace imports.

@byCedric byCedric added the bug Something isn't working label Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants