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

feat(bundle-source): Support TypeScript type erasure #2627

Merged
merged 2 commits into from
Nov 12, 2024
Merged

Conversation

kriskowal
Copy link
Member

@kriskowal kriskowal commented Nov 9, 2024

Closes: #2415

Description

This change introduces support for TypeScript through type-erasure, using ts-blank-space, which converts type annotations to equivalent blank space. As is consistent with node --experimental-strip-types, this only applies to modules with the .ts, .mts, or .cts extensions in packages that are not under node_modules, to discourage publishing TypeScript as a source language to npm.

Security Considerations

The choice of ts-blank-space is intended to minimize runtime behavior difference between TypeScript and JavaScript, such that a reviewer or a debugger of the generated JavaScript aligns with the expected behavior and original text, to the extent that is possible. This should compose well with #2444.

Scaling Considerations

None.

Documentation Considerations

Contains README and NEWS.

Testing Considerations

Contains spot check tests for TypeScript in the endoScript and endoZipBase64 formats. We stand on much more rigorous testing of the underlying workspace-language-for-extension feature in Compartment Mapper #2625.

Compatibility Considerations

This does not break any prior usage.

Upgrade Considerations

None.

@kriskowal kriskowal requested a review from turadg November 9, 2024 00:22
@kriskowal kriskowal force-pushed the kriskowal-compartment-mapper-module-vs-commonjs-options branch from d6ce6de to f156aea Compare November 9, 2024 00:39
@kriskowal kriskowal force-pushed the kriskowal-ts branch 2 times, most recently from 18f730e to 6b08628 Compare November 9, 2024 00:44
@kriskowal kriskowal force-pushed the kriskowal-compartment-mapper-module-vs-commonjs-options branch from f156aea to ff2ec1b Compare November 9, 2024 00:44
Comment on lines 70 to 71
This will also not function properly for TypeScript modules that generate
JavaScript runtime code, like enums.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"enums" aren't necessarily TypeScript. (JSDoc has @enum).

Suggested change
This will also not function properly for TypeScript modules that generate
JavaScript runtime code, like enums.
This will also not function properly for TypeScript modules that have [runtime impacting syntax](https://github.com/bloomberg/ts-blank-space/blob/main/docs/unsupported_syntax.md), such as `enum`.

t.is(bundle.moduleFormat, 'endoScript');
const { source } = bundle;
const compartment = new Compartment();
const ns = compartment.evaluate(source);
t.is(ns.meaning, 42);
});

test('endo script format supports typescript type erasure', async t => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need more test cases to help make clear what to expect.

IIUC this these work:

// tsFromTs.ts
export { fortune } from './fortune.ts
// tsFromJs.ts
export { meaning } from './meaning.js

And this won't,

// jsFromTs.js
export { fortune } from './fortune.ts

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve posted tests alongside these that verify that the full cross-product of {js,ts} can import and export {js,ts}, and also an expected failing test that you can’t import 'fortune.js' in TypeScript, if there is no fortune.js, even if there is fortune.ts.

@kriskowal kriskowal force-pushed the kriskowal-compartment-mapper-module-vs-commonjs-options branch from ff2ec1b to db2ad9a Compare November 9, 2024 00:51
@kriskowal kriskowal force-pushed the kriskowal-compartment-mapper-module-vs-commonjs-options branch from db2ad9a to b1719f8 Compare November 12, 2024 00:23
@kriskowal kriskowal force-pushed the kriskowal-compartment-mapper-module-vs-commonjs-options branch from b1719f8 to 60b295c Compare November 12, 2024 00:33
@kriskowal kriskowal force-pushed the kriskowal-ts branch 2 times, most recently from fe0022a to 2296b4f Compare November 12, 2024 00:40
@kriskowal kriskowal force-pushed the kriskowal-compartment-mapper-module-vs-commonjs-options branch from 60b295c to 300f46c Compare November 12, 2024 00:40
Copy link
Member

@turadg turadg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🫶

@kriskowal kriskowal force-pushed the kriskowal-compartment-mapper-module-vs-commonjs-options branch from 300f46c to 389de7b Compare November 12, 2024 00:49
Base automatically changed from kriskowal-compartment-mapper-module-vs-commonjs-options to master November 12, 2024 01:01
@kriskowal kriskowal merged commit 83ebf71 into master Nov 12, 2024
15 checks passed
@kriskowal kriskowal deleted the kriskowal-ts branch November 12, 2024 01:13
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