syntaxase-wasm 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -21,6 +21,8 @@ interface User { name: string }
21
21
  const user: User = { name: "Ada" };
22
22
  `);
23
23
 
24
+ const strippedTsx = stripTypes(`const view = <Card<User> user={user as User} />;`, { lang: "tsx" });
25
+
24
26
  const transformed = transform(`const view: JSX.Element = <h1>Hello</h1>;`, { jsx: true });
25
27
  ```
26
28
 
@@ -33,6 +35,9 @@ option. It does not validate that constraint: runtime TypeScript constructs such
33
35
  as enums may remain unchanged or lose TypeScript-only modifiers without gaining
34
36
  the JavaScript code needed to preserve their semantics.
35
37
 
38
+ By default, `stripTypes` parses TypeScript without JSX. Pass `{ lang: "tsx" }`
39
+ to strip TypeScript syntax from TSX while leaving JSX unchanged.
40
+
36
41
  `transform` additionally lowers supported runtime TypeScript constructs and,
37
42
  when enabled, JSX.
38
43
 
package/index.d.ts CHANGED
@@ -28,8 +28,12 @@ export interface TransformOptions {
28
28
  jsx?: boolean | JSXConfig;
29
29
  }
30
30
 
31
+ export interface StripTypesOptions {
32
+ lang?: "ts" | "tsx";
33
+ }
34
+
31
35
  /** Transform erasable TypeScript and supported runtime TypeScript/JSX syntax to JavaScript. */
32
36
  export function transform(sourceText: string, options?: TransformOptions): string;
33
37
 
34
38
  /** Erase only fixed-width TypeScript syntax while preserving source length and line layout. */
35
- export function stripTypes(sourceText: string): string;
39
+ export function stripTypes(sourceText: string, options?: StripTypesOptions): string;
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import wasmBytes from "./wasm.js";
2
- import { assertSourceText, MODE_STRIP_TYPES, resolveTransformOptions } from "./options.js";
2
+ import { assertSourceText, resolveStripTypesOptions, resolveTransformOptions } from "./options.js";
3
3
 
4
4
  const wasmModule = new WebAssembly.Module(wasmBytes);
5
5
  const wasmInstance = new WebAssembly.Instance(wasmModule);
@@ -17,9 +17,9 @@ export function transform(sourceText, options = {}) {
17
17
  }
18
18
 
19
19
  /** Erase only fixed-width TypeScript syntax while preserving source length and line layout. */
20
- export function stripTypes(sourceText) {
20
+ export function stripTypes(sourceText, options = {}) {
21
21
  assertSourceText(sourceText);
22
- return invoke(sourceText, MODE_STRIP_TYPES, "", "");
22
+ return invoke(sourceText, resolveStripTypesOptions(options), "", "");
23
23
  }
24
24
 
25
25
  function invoke(sourceText, mode, firstOption, secondOption) {
package/options.js CHANGED
@@ -5,6 +5,7 @@ const MODE_AUTOMATIC_DEVELOPMENT = 2;
5
5
  const MODE_CLASSIC = 3;
6
6
  const MODE_PRESERVE = 4;
7
7
  export const MODE_STRIP_TYPES = 5;
8
+ export const MODE_STRIP_TYPES_TSX = 6;
8
9
 
9
10
  const IDENTIFIER_NAME = /^[$_\p{ID_Start}][$\u200c\u200d_\p{ID_Continue}]*$/u;
10
11
  const RESERVED_IDENTIFIER_REFERENCES = new Set([
@@ -69,6 +70,25 @@ export function resolveTransformOptions(options) {
69
70
  return resolveJSXConfig(options.jsx);
70
71
  }
71
72
 
73
+ export function resolveStripTypesOptions(options) {
74
+ if (options === null || typeof options !== "object" || Array.isArray(options)) {
75
+ throw new TypeError("stripTypes options must be an object");
76
+ }
77
+ for (const key of Reflect.ownKeys(options)) {
78
+ if (key !== "lang") {
79
+ throw new TypeError(`stripTypes options contains unknown option ${String(key)}`);
80
+ }
81
+ }
82
+
83
+ if (!Object.hasOwn(options, "lang") || options.lang === "ts") {
84
+ return MODE_STRIP_TYPES;
85
+ }
86
+ if (options.lang === "tsx") {
87
+ return MODE_STRIP_TYPES_TSX;
88
+ }
89
+ throw new TypeError('stripTypes options.lang must be "ts" or "tsx"');
90
+ }
91
+
72
92
  function assertOptions(options) {
73
93
  if (options === null || typeof options !== "object" || Array.isArray(options)) {
74
94
  throw new TypeError("transform options must be an object");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "syntaxase-wasm",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Lightning-fast type stripping and JSX lowering",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "repository": {