bio-dts 0.1.2 → 0.2.1

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,6 +1,4 @@
1
- import { print } from 'recast';
2
-
3
- import { isArray, isNode, keys, parseDts, path } from './util.js';
1
+ import { isArray, isNode, keys, parseDts, path, print } from './util.js';
4
2
 
5
3
 
6
4
  /**
@@ -38,6 +36,12 @@ export default function transform(src) {
38
36
  }
39
37
 
40
38
  // TODO(nikku): transform illegal typescript methods into override calls
39
+
40
+ // TODO(nikku): remove annotations
41
+ //
42
+ // * @typedef
43
+ // * @class
44
+ // * @constructor
41
45
  });
42
46
 
43
47
  return print(ast).code;
@@ -1,6 +1,4 @@
1
- import { print } from 'recast';
2
-
3
- import { matcher, parse, path } from './util.js';
1
+ import { matcher, parse, path, print } from './util.js';
4
2
 
5
3
  import {
6
4
  builders as b
@@ -148,19 +146,31 @@ export default function transform(src) {
148
146
  const ast = parse(src);
149
147
  const body = path(ast).get('program', 'body');
150
148
 
149
+ const inheritsImports = findInheritsImports(body);
150
+
151
151
  const classes = findConstructors(body);
152
152
 
153
+ // query
153
154
  for (const cls of classes) {
154
- const members = cls.members = [
155
+ cls.members = [
155
156
  ...findEmbeddedMethods(cls),
156
157
  ...findProtoMethods(cls, body)
157
158
  ];
158
159
 
159
- const staticMembers = cls.staticMembers = [
160
+ cls.staticMembers = [
160
161
  ...findStaticMembers(cls, body)
161
162
  ];
162
163
 
163
- const superDeclaration = cls.superDeclaration = findSuperDeclarations(cls, body);
164
+ cls.superDeclaration = findSuperDeclarations(cls, body, inheritsImports);
165
+ }
166
+
167
+ // transform
168
+ for (const cls of classes) {
169
+ const {
170
+ members,
171
+ staticMembers,
172
+ superDeclaration
173
+ } = cls;
164
174
 
165
175
  if (superDeclaration) {
166
176
  cls.ctor.get('body', 'body').unshift(superCall());
@@ -189,26 +199,34 @@ export default function transform(src) {
189
199
  });
190
200
 
191
201
  if (superDeclaration) {
192
- superDeclaration.importDeclarations.forEach(m => m.node.replace());
193
202
  superDeclaration.inheritsExpression.node.replace();
194
203
  }
195
204
  }
196
205
 
206
+ inheritsImports.forEach(m => m.node.replace());
207
+
197
208
  return print(ast).code;
198
209
  }
199
210
 
200
- function findSuperDeclarations(cls, nodes) {
201
-
211
+ function findInheritsImports(nodes) {
202
212
  const importsMatches = matcher`
203
213
  import $1 from 'inherits-browser';
204
214
  `;
205
215
 
206
- const importDeclarations = importsMatches(nodes);
216
+ return importsMatches(nodes).map(m => {
217
+ return {
218
+ node: m[0],
219
+ name: m[1].value.name
220
+ };
221
+ });
222
+ }
223
+
224
+ function findSuperDeclarations(cls, nodes, importDeclarations) {
207
225
 
208
- for (const [ _, identifier ] of importDeclarations) {
226
+ for (const { name: inheritsName } of importDeclarations) {
209
227
 
210
228
  const inheritsMatches = matcher`
211
- ${identifier.value.name}(${cls.name}, $1);
229
+ ${inheritsName}(${cls.name}, $1);
212
230
  `;
213
231
 
214
232
  const inheritsExpressions = inheritsMatches(nodes);
@@ -232,7 +250,6 @@ function findSuperDeclarations(cls, nodes) {
232
250
  const superCalls = superCallMatches(cls.ctor.get('body', 'body'));
233
251
 
234
252
  return {
235
- importDeclarations: importDeclarations.map(m => ({ node: m[0] })),
236
253
  superCalls: superCalls.map(m => ({ node: m[0] })),
237
254
  clazz,
238
255
  inheritsExpression
package/lib/util.js CHANGED
@@ -1,4 +1,4 @@
1
- import { parse as recastParse } from 'recast';
1
+ import { parse as recastParse, print as recastPrint } from 'recast';
2
2
  import { Path as PathConstructor } from 'ast-types';
3
3
 
4
4
  /**
@@ -13,6 +13,13 @@ import * as typescriptDtsParser from './parsers/typescript-dts.js';
13
13
 
14
14
  const DBG = /match/.test(process?.env?.LOG_DEBUG);
15
15
 
16
+ const formatOptions = {
17
+ useTabs: false,
18
+ reuseWhitespace: false,
19
+ tabWidth: 2,
20
+ quote: 'single'
21
+ };
22
+
16
23
  /**
17
24
  * Return numeric or string keys of the given object.
18
25
  *
@@ -51,16 +58,22 @@ export function hasProperty(obj, property) {
51
58
  */
52
59
  export function parse(code) {
53
60
  return recastParse(code, {
54
- parser: typescriptParser
61
+ parser: typescriptParser,
62
+ ...formatOptions
55
63
  });
56
64
  }
57
65
 
58
66
  export function parseDts(code) {
59
67
  return recastParse(code, {
60
- parser: typescriptDtsParser
68
+ parser: typescriptDtsParser,
69
+ ...formatOptions
61
70
  });
62
71
  }
63
72
 
73
+ export function print(node) {
74
+ return recastPrint(node, formatOptions);
75
+ }
76
+
64
77
  /**
65
78
  * @param {any} node
66
79
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bio-dts",
3
- "version": "0.1.2",
3
+ "version": "0.2.1",
4
4
  "description": "Generate sane and clean types from JavaScript sources",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -9,7 +9,7 @@
9
9
  "all": "npm run lint && npm run test && npm run test:dts",
10
10
  "lint": "eslint .",
11
11
  "test": "mocha test",
12
- "test:dts": "node bin/bio-dts.js --outDir dist -r test/fixtures"
12
+ "test:dts": "node bin/cmd.js --outDir dist -r test/fixtures"
13
13
  },
14
14
  "author": "Nico Rehwaldt",
15
15
  "license": "MIT",