@vesk/compiler 0.2.18 → 0.2.20

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.
@@ -1,3 +1,5 @@
1
+ /** True when a node is a type-only import/export (`import type`, `export type`). */
2
+ export declare function isTypeOnlyImportExport(node: any): boolean;
1
3
  /** True when the AST contains any TypeScript-only node. */
2
4
  export declare function hasTsSyntax(ast: any): boolean;
3
5
  /** True when a Program-body statement is type-only (safe to drop from emitted JS). */
@@ -10,6 +12,8 @@ export declare function isTypeOnlyStatement(node: any): boolean;
10
12
  * - type arguments on call/new expressions and type parameter declarations
11
13
  * - whole type-only statements (interfaces, type aliases, declare/abstract
12
14
  * members), replaced by null entries
15
+ * - type-only imports/exports (`import type`, `export type { ... }`, and
16
+ * inline `type` specifiers inside mixed import/export statements)
13
17
  *
14
18
  * Returns a new tree (zimmerframe walks are immutable); callers must use the
15
19
  * return value.
@@ -1 +1 @@
1
- {"version":3,"file":"strip-ts.d.ts","sourceRoot":"","sources":["../src/strip-ts.ts"],"names":[],"mappings":"AAwBA,2DAA2D;AAC3D,wBAAgB,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAY7C;AAYD,sFAAsF;AACtF,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAMtD;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,CA6D1C;AAWD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAiBnD"}
1
+ {"version":3,"file":"strip-ts.d.ts","sourceRoot":"","sources":["../src/strip-ts.ts"],"names":[],"mappings":"AAwBA,oFAAoF;AACpF,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAOzD;AAUD,2DAA2D;AAC3D,wBAAgB,WAAW,CAAC,GAAG,EAAE,GAAG,GAAG,OAAO,CAgB7C;AAYD,sFAAsF;AACtF,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAUtD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,GAAG,GAAG,GAAG,CAmF1C;AAWD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAiBnD"}
package/dist/strip-ts.js CHANGED
@@ -20,6 +20,27 @@ const TS_NODE_TYPES = new Set([
20
20
  'TSImportEqualsDeclaration',
21
21
  'TSParameterProperty',
22
22
  ]);
23
+ /** True when a node is a type-only import/export (`import type`, `export type`). */
24
+ export function isTypeOnlyImportExport(node) {
25
+ if (!node)
26
+ return false;
27
+ if (node.type === 'ImportDeclaration')
28
+ return node.importKind === 'type';
29
+ if (node.type === 'ExportNamedDeclaration' || node.type === 'ExportAllDeclaration') {
30
+ return node.exportKind === 'type';
31
+ }
32
+ return false;
33
+ }
34
+ /** True when a specifier is type-only inside a mixed import/export. */
35
+ function isTypeOnlySpecifier(node) {
36
+ if (!node)
37
+ return false;
38
+ if (node.importKind === 'type')
39
+ return true;
40
+ if (node.exportKind === 'type')
41
+ return true;
42
+ return false;
43
+ }
23
44
  /** True when the AST contains any TypeScript-only node. */
24
45
  export function hasTsSyntax(ast) {
25
46
  let found = false;
@@ -29,6 +50,10 @@ export function hasTsSyntax(ast) {
29
50
  found = true;
30
51
  return;
31
52
  }
53
+ if (isTypeOnlyImportExport(node) || isTypeOnlySpecifier(node)) {
54
+ found = true;
55
+ return;
56
+ }
32
57
  return context.next();
33
58
  },
34
59
  });
@@ -48,8 +73,13 @@ export function isTypeOnlyStatement(node) {
48
73
  if (!node)
49
74
  return true;
50
75
  if (node.type === 'ExportNamedDeclaration' || node.type === 'ExportDefaultDeclaration') {
76
+ if (node.exportKind === 'type')
77
+ return true;
51
78
  return !!node.declaration && isTypeOnlyStatement(node.declaration);
52
79
  }
80
+ if (node.type === 'ImportDeclaration' || node.type === 'ExportAllDeclaration') {
81
+ return isTypeOnlyImportExport(node);
82
+ }
53
83
  return TYPE_ONLY_STATEMENTS.has(node.type);
54
84
  }
55
85
  /**
@@ -60,6 +90,8 @@ export function isTypeOnlyStatement(node) {
60
90
  * - type arguments on call/new expressions and type parameter declarations
61
91
  * - whole type-only statements (interfaces, type aliases, declare/abstract
62
92
  * members), replaced by null entries
93
+ * - type-only imports/exports (`import type`, `export type { ... }`, and
94
+ * inline `type` specifiers inside mixed import/export statements)
63
95
  *
64
96
  * Returns a new tree (zimmerframe walks are immutable); callers must use the
65
97
  * return value.
@@ -113,6 +145,33 @@ export function stripTsTypes(ast) {
113
145
  node.typeAnnotation = null;
114
146
  return context.next();
115
147
  },
148
+ ImportDeclaration(node, context) {
149
+ if (isTypeOnlyImportExport(node))
150
+ return null;
151
+ if (Array.isArray(node.specifiers)) {
152
+ const kept = node.specifiers.filter((s) => !isTypeOnlySpecifier(s));
153
+ if (kept.length === 0)
154
+ return null;
155
+ node.specifiers = kept;
156
+ }
157
+ return context.next();
158
+ },
159
+ ExportNamedDeclaration(node, context) {
160
+ if (isTypeOnlyImportExport(node))
161
+ return null;
162
+ if (Array.isArray(node.specifiers)) {
163
+ const kept = node.specifiers.filter((s) => !isTypeOnlySpecifier(s));
164
+ if (kept.length === 0 && node.declaration == null)
165
+ return null;
166
+ node.specifiers = kept;
167
+ }
168
+ return context.next();
169
+ },
170
+ ExportAllDeclaration(node, context) {
171
+ if (isTypeOnlyImportExport(node))
172
+ return null;
173
+ return context.next();
174
+ },
116
175
  TSInterfaceDeclaration(node) {
117
176
  return null;
118
177
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vesk/compiler",
3
- "version": "0.2.18",
3
+ "version": "0.2.20",
4
4
  "type": "module",
5
5
  "description": "Vesk compiler — lexer, parser, semantic analysis, IR, codegen",
6
6
  "main": "./dist/index.js",
@@ -20,7 +20,7 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "@sveltejs/acorn-typescript": "^1.0.0",
23
- "@vesk/types": "^0.2.18",
23
+ "@vesk/types": "^0.2.20",
24
24
  "acorn": "^8.15.0",
25
25
  "esrap": "^2.3.0",
26
26
  "is-reference": "^3.0.0",