bio-dts 0.4.1 → 0.5.0

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.
@@ -82,6 +82,14 @@ export default function transform(src) {
82
82
  }));
83
83
  }
84
84
 
85
+
86
+ /**
87
+ * Ensure optional args methods are properly escaped
88
+ *
89
+ * @param {import('./util.js').Path} nodePath
90
+ *
91
+ * @return {any[]} replacements
92
+ */
85
93
  function fixOptionalArgsMethods(nodePath) {
86
94
 
87
95
  const {
@@ -92,9 +100,11 @@ export default function transform(src) {
92
100
  return;
93
101
  }
94
102
 
95
- const hostPath = nodePath.parentPath.value.type === 'ExportNamedDeclaration'
96
- ? nodePath.parentPath
97
- : nodePath;
103
+ const hostPath = (
104
+ [ 'ExportDefaultDeclaration', 'ExportNamedDeclaration' ].includes(nodePath.parentPath.value.type)
105
+ ? nodePath.parentPath
106
+ : nodePath
107
+ );
98
108
 
99
109
  const {
100
110
  variations
@@ -164,6 +174,11 @@ export default function transform(src) {
164
174
  'TSDeclareMethod': b.tsDeclareMethod
165
175
  }[node.type];
166
176
 
177
+ const hostBuilder = {
178
+ 'ExportNamedDeclaration': b.exportNamedDeclaration,
179
+ 'ExportDefaultDeclaration': b.exportDefaultDeclaration
180
+ }[hostPath.value.type];
181
+
167
182
  const variationComments = commentPath.value ? [ keepCommentParams(commentPath, variation) ] : [ ];
168
183
 
169
184
  const newNode = builder.from({
@@ -173,13 +188,13 @@ export default function transform(src) {
173
188
  });
174
189
 
175
190
  return (
176
- hostPath === nodePath
177
- ? newNode
178
- : b.exportNamedDeclaration.from({
191
+ hostBuilder
192
+ ? hostBuilder.from({
179
193
  ...hostPath.value,
180
194
  declaration: newNode,
181
195
  comments: variationComments
182
196
  })
197
+ : newNode
183
198
  );
184
199
  });
185
200
 
@@ -274,7 +289,13 @@ export default function transform(src) {
274
289
  });
275
290
 
276
291
  for (const [ path, args ] of replacements) {
277
- path.replace(...args);
292
+ try {
293
+ path.replace(...args);
294
+ } catch (err) {
295
+ console.error('Failed to replace path', path, ...args);
296
+
297
+ throw err;
298
+ }
278
299
  }
279
300
 
280
301
  return print(ast).code;
@@ -38,19 +38,33 @@ function clazzDefinition(cls, ctor) {
38
38
 
39
39
  const members = [
40
40
  ctor,
41
- ...cls.members.filter(
42
- m => m.member.type === 'FunctionExpression'
43
- ).map(m => {
41
+ ...cls.members.filter(m => m.source === 'prototype').map(m => {
42
+
43
+ // class method
44
+ if (m.member.type === 'FunctionExpression') {
45
+ return {
46
+ 'type': 'ClassMethod',
47
+ 'static': false,
48
+ 'key': {
49
+ 'type': 'Identifier',
50
+ 'name': m.name
51
+ },
52
+ 'kind': 'method',
53
+ 'params': m.member.params,
54
+ 'body': m.member.body,
55
+ 'comments': m.comments
56
+ };
57
+ }
58
+
59
+ // save to transform into class property
44
60
  return {
45
- 'type': 'ClassMethod',
61
+ 'type': 'ClassProperty',
46
62
  'static': false,
47
63
  'key': {
48
64
  'type': 'Identifier',
49
65
  'name': m.name
50
66
  },
51
- 'kind': 'method',
52
- 'params': m.member.params,
53
- 'body': m.member.body,
67
+ 'value': m.member,
54
68
  'comments': m.comments
55
69
  };
56
70
  })
@@ -153,8 +167,8 @@ export default function transform(src) {
153
167
  // query
154
168
  for (const cls of classes) {
155
169
  cls.members = [
156
- ...findEmbeddedMethods(cls),
157
- ...findProtoMethods(cls, body)
170
+ ...findEmbeddedMembers(cls),
171
+ ...findProtoMembers(cls, body)
158
172
  ];
159
173
 
160
174
  cls.staticMembers = [
@@ -189,7 +203,7 @@ export default function transform(src) {
189
203
  cls.node.get('comments').replace(additionalComments);
190
204
 
191
205
  members.forEach(m => {
192
- if (m.member.type === 'FunctionExpression') {
206
+ if (m.source === 'prototype') {
193
207
  m.node.replace();
194
208
  }
195
209
  });
@@ -321,7 +335,7 @@ function findStaticMembers(cls, nodes) {
321
335
  });
322
336
  }
323
337
 
324
- function findProtoMethods(cls, nodes) {
338
+ function findProtoMembers(cls, nodes) {
325
339
 
326
340
  const names = matcher`
327
341
  ${cls.name}.prototype.$1 = $2;
@@ -334,12 +348,13 @@ function findProtoMethods(cls, nodes) {
334
348
  name: identifier.value.name,
335
349
  member: member.value,
336
350
  comments: node.get('comments').value,
337
- node
351
+ node,
352
+ source: 'prototype'
338
353
  };
339
354
  });
340
355
  }
341
356
 
342
- function findEmbeddedMethods(cls) {
357
+ function findEmbeddedMembers(cls) {
343
358
 
344
359
  const memberDeclarations = matcher`
345
360
  this.$1 = $2;
@@ -352,7 +367,8 @@ function findEmbeddedMethods(cls) {
352
367
  name: identifier.value.name,
353
368
  member: member.value,
354
369
  comments: node.get('comments').value,
355
- node
370
+ node,
371
+ source: 'constructor'
356
372
  };
357
373
  });
358
374
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bio-dts",
3
- "version": "0.4.1",
3
+ "version": "0.5.0",
4
4
  "description": "Generate sane and clean types from JavaScript sources",
5
5
  "type": "module",
6
6
  "main": "index.js",