Skip to content

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

Open
@byCedric

Description

@byCedric

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:

Steps To Reproduce

  1. npm i --save [email protected] [email protected] [email protected]
  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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions