beenotung / tsc-macro

Compose macro in Typescript, expand back into Typescript
https://www.npmjs.com/package/tsc-macro
BSD 2-Clause "Simplified" License
11 stars 1 forks source link

How does one define a new macro? #1

Open Griffork opened 4 years ago

Griffork commented 4 years ago

I didn't see it in your documentation or testing samples.

beenotung commented 4 years ago

Thanks for the question.

I've added more examples and test specs in the example directory.

Also added a 'How it works' section in the readme.

Griffork commented 4 years ago

Thanks, although I still have no idea how it works.

How does reports.macro.ts become reports.ts, what keywords are used what gets replaced, what do you export when you're making a macro file, how do you import macros into other files in your project and get type checking?

While waiting for you to get back I actually wrote my own macro generator that works a bit more like typescript-transform-macros but with added features, so I probably won't use this. I do appreciate that you replied though!

beenotung commented 4 years ago

How it works: tsc-macro scans each *.macro.ts files, eval the file, then save the result into *.ts file. Therefore, the generated file content rely on that last statement in the file.

How to import the macro-generated code: Other files can import the generated *.ts file directly.

Alternatives: This works like string-based template file. If you prefer AST-based template, checkout TypeDraft

I'm glad you've work out another tool to add macro functionality to typescript, I'm not 100% satisfied with the current version. Would you like to share your tool?

Griffork commented 4 years ago

@beenotung at this point I don't have the bandwidth to keep working on the tool, and the tool is missing some basic functionality like using namespaced macros included from other files. However, it does what I need it to at the moment.

I'm working on another project currently so I don't intend to finish off the macro tool yet, and I'm certainly not going to make it public until it meets my standards for minimum quality/functionality.

Making the tool available for others to use is something that I will probably look to do in the future, particularly after the transform API for typescript is no longer considered "experimental".

beenotung commented 4 years ago

I understand it can takes quite some time to prepare a project before making it open sourced. I looked into AST based approach for macro but they seems too complicated for me, thanks for sharing about the transform API for typescript anyway.

The current implementation of tsc-macro works fine in a single file, but it's tricky if you need to "import a macro expression from other file"

Here are some more usage examples:

Code transformation (kind-of DSL): dimension.macro.ts -> dimension.ts

Inline function call: mse.macro.ts -> mse.ts

beenotung commented 4 years ago

Update: A recent patch makes tsc-macro supports importing local packages and relative files. This patch is published as tsc-macro@0.1.2 on npm as well