purescript-codegen / purescript-read-dts

Provide a way to work with TypeScript types declarations
BSD 3-Clause "New" or "Revised" License
23 stars 4 forks source link

How to use this to convert a *.d.t.s file to PS FFI? #1

Open bbarker opened 4 years ago

bbarker commented 4 years ago

Or is that even possible?

I certainly don't mind if it isn't perfect, but anything to get me a leg up on converting TypeScript type definitions to PureScript FFI files would be great.

paluh commented 4 years ago

Oh sorry. I've already written some spam directly to you Brandon on slack ;-)

Or is that even possible?

Yeah, it is possible, but codegen is not included at the moment in this lib. I've implemented some pieces of codegen based on this lib here:

https://github.com/dwhitney/purescript-react-basic-mui/tree/codegen-read-dts/codegen/src

This finally should be extracted from the above to a separate repo.

There are some dark / hacky corners of our current codegen. Like I've pointed this out on slack already here is the description of the main hack:

https://github.com/dwhitney/purescript-react-basic-mui/blob/codegen-read-dts/codegen/src/TS/Module.purs#L3

paluh commented 4 years ago

P.S. also union handling which is done in this module is not really smart and I want to improve it soon.

flip111 commented 4 years ago

That would be wonderful if eventually it would be possible to leverage this repository https://github.com/DefinitelyTyped/DefinitelyTyped and create purescript FFI modules automatically :)

paluh commented 4 years ago

@flip111 Thanks for your interest and comment!

We are preparing some video meetup (currently @jvliwanag [GMT+8] and me [GMT +2]). Probably next week. Our ambitious plan is to form a community organization (I think here on github) which could be an umbrella for libraries related to PS codegen from TS. Probably we are going to make a small announcement on the forum too.

If you Guys (ping @bbarker) want to join you are very welcome!

flip111 commented 4 years ago

Yes please write the details on how to join the video.

I'm in the middle of a project at the moment, but generally compiler stuff interest me a lot.

paluh commented 4 years ago

@flip111 Cool! I hope I will provide more details soon.

poscat0x04 commented 4 years ago

Hmmm, what's the status of this issue? I would like to generate binding to the vscode API using this library but I don't know where to start.

flip111 commented 4 years ago

Hi @poscat0x04 two of the things that are still work in progress are to extract typescript types from definitions files (dts) and to generate purescript code based on this. At the moment both of these things are not ready yet. We welcome you to explore the purescript-codegen space, read issues and send in pull requests if you like. Otherwise please come back later :)

paluh commented 4 years ago

@poscat0x04 , @flip111 I would not say that type extraction is not ready at the moment. I would say that it works reliably on a subset of TS type constructions. Some of them can be seen here - in this test scenarios (in every test source variable is a typescript module which we test against):

https://github.com/purescript-codegen/purescript-read-dts/blob/master/test/ReadDTS/Instantiation.purs

@poscat0x04 Could you please share/link some simple definitions which you want to process?

poscat0x04 commented 4 years ago

@paluh My ultimate goal is to write vscode extensions using purescript, which requires creating a binding to some subset of the api type definitions.

poscat0x04 commented 4 years ago

Tried to use build and instantiation but resulted in a failure:

unknown: Unknown type constructor: (Just "\"vscode\"")<Thenable>: then:  {}

This is the code I used:

main :: Effect Unit
main = do
  -- Test.runTest Test.ReadDTS.Instantiation.suite
  e <- build { strictNullChecks: true } { path: "test/vscode.d.ts", source: Nothing }
  case e of
    Right r -> do
      let
        mts = extract $ runExceptT $ traverse (\tc -> I.instantiate tc []) r
      case mts of
        Right ts -> traverse_ print $ map pprint ts
        Left e -> print $ show e
    Left e -> print $ show e
paluh commented 4 years ago

@poscat0x04 I've started a branch on which I try to solve ambient module handling. I have hard time extracting declarations from it - this still doesn't work correctly:

https://github.com/purescript-codegen/purescript-read-dts/blob/ambient-modules/src/ReadDTS.ts#L176

I'm debugging it here:

https://github.com/purescript-codegen/purescript-read-dts/blob/ambient-modules/test/Main.purs#L24

If I leave for example only Command declaration as a source value this type extraction works fine.

I'm not sure when I find time to work on modules again. If you need some more debugging tips I can try to provide them here ;-)

paluh commented 4 years ago

So finally I'm able to somehow handle ambient module declarations. I'm able to build AST for this source:

https://github.com/purescript-codegen/purescript-read-dts/blob/ambient-modules/test/Main.purs#L25

There is still a problem with const declaration. I'm going to work on it next (but I'm not sure when ;-)).

Currently "specialization for module type" has not much sens as far as I can tell... I think that given module declaration you should try to iterate over declarations and process (like specialize or something) them one by one. I have to warn you that AST unfolding has hard coded depth limit and it is known bug (it should be configurable):

https://github.com/purescript-codegen/purescript-read-dts/blob/ambient-modules/src/ReadDTS/AST.purs#L234

paluh commented 4 years ago

To wrap it up:

poscat0x04 commented 4 years ago

Tried to run the test on the vscode.d.ts file but got the error "Variable not defined: T". Is there something I'm missing or are parametric functions not being supported right now?

paluh commented 4 years ago

@poscat0x04 I have to admit that @flip111 suggestion that this lib is not ready / stable yet was more insightful then mine opinion which was based mainly on emotions - I've just invested a lot of time in the research and TS compiler api exploration so I "felt" that this code should and is valuable. After a throughout review I've decided to rewrite main parts of the lib and build it on proper unit and regression test base (because TS is huge and the compiler API is even larger ;-)).

I'm starting this work on the rewrite branch branch by establishing basic units tests infrastructure etc. https://github.com/purescript-codegen/purescript-read-dts/blob/rewrite/test/ReadDTS.purs

Sorry that we are not able to meet your expectation at the moment. I hope that with this rewrite we would be able to provide better coverage and docs. I'm going to ping you back when this lib can "digest" vscode.d.ts declarations :-)