Skip to content

Add support for evaluating basic typescript#82

Open
TikiTDO wants to merge 1 commit intokentcdodds:mainfrom
TikiTDO:patch-1
Open

Add support for evaluating basic typescript#82
TikiTDO wants to merge 1 commit intokentcdodds:mainfrom
TikiTDO:patch-1

Conversation

@TikiTDO
Copy link

@TikiTDO TikiTDO commented Sep 28, 2020

What: Allows preval to parse typescript (and potentially any dialect supported by babel with some extra work)

Why: Preval currently chokes on typescript.

This PR serves as a proof-of-concept to show that a TS file can be transformed into plain JS before being handed off the the require-from-string helper.

This could be generalized with a more extensive set of transformers, or potentially even a babel option for the plugin to allow an arbitrary set of extra plugins.

How: Check for typescript files when parsing a @preval tagged file, and optionally parse it with the typescript transformer enabled.

Note, this example only handled the file comment approach, since that code already used babel for parsing the input file. Expanding this it to support the other three approaches would require some changes to use babel handlers such as transformFileSync or transformSync

@codecov
Copy link

codecov bot commented Sep 28, 2020

Codecov Report

Merging #82 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #82   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            4         4           
  Lines          106       106           
  Branches        22        22           
=========================================
  Hits           106       106           
Impacted Files Coverage Δ
src/index.js 100.00% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7b0e710...95f47b4. Read the comment docs.

@kentcdodds
Copy link
Owner

Hi @TikiTDO,

Thanks for this. We used to transform the code (using babel), but it ended up causing issues. I can't remember what they were though. It was years ago. I guess I'm willing to merge this if you're willing to help maintain things if it breaks something 😬

@TikiTDO
Copy link
Author

TikiTDO commented Oct 5, 2020

@kentcdodds how about the idea of making this an babel option for this plugin? I hit a few problems with this patch myself (it worked for node 14, but did not work for node 10 due to how modules are handled), so I don't think it would be easy to get it working generically.

However, if we add a way tell babel what options to use when parsing the code then it would be be possible to adapt it to any particular environment by letting the users enable the set of plugins that work for them.

@kentcdodds
Copy link
Owner

That would be fine with me. Got any ideas?

Base automatically changed from master to main January 25, 2021 23:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants