bio-dts 0.4.1 → 0.6.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,23 +82,50 @@ 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 {
88
96
  value: node
89
97
  } = nodePath;
90
98
 
91
- if (node.type !== 'TSDeclareMethod' && node.type !== 'TSDeclareFunction') {
99
+ const fnDeclaration = (
100
+ node.type === 'TSDeclareMethod' ||
101
+ node.type === 'TSDeclareFunction'
102
+ );
103
+
104
+ const fnDefinition = (
105
+ node?.typeAnnotation?.typeAnnotation?.type === 'TSFunctionType'
106
+ );
107
+
108
+ if (!fnDeclaration && !fnDefinition) {
92
109
  return;
93
110
  }
94
111
 
95
- const hostPath = nodePath.parentPath.value.type === 'ExportNamedDeclaration'
96
- ? nodePath.parentPath
97
- : nodePath;
112
+ const params = fnDeclaration
113
+ ? node.params
114
+ : node?.typeAnnotation?.typeAnnotation?.parameters;
115
+
116
+ const returnType = fnDeclaration
117
+ ? node.returnType
118
+ : node?.typeAnnotation?.typeAnnotation?.typeAnnotation;
119
+
120
+ const hostPath = (
121
+ [ 'ExportDefaultDeclaration', 'ExportNamedDeclaration' ].includes(nodePath.parentPath.value.type)
122
+ ? nodePath.parentPath
123
+ : nodePath
124
+ );
98
125
 
99
126
  const {
100
127
  variations
101
- } = node.params.slice().reverse().reduce((res, param) => {
128
+ } = params.slice().reverse().reduce((res, param) => {
102
129
 
103
130
  let {
104
131
  required,
@@ -160,26 +187,37 @@ export default function transform(src) {
160
187
  const replacements = variations.slice().reverse().map(variation => {
161
188
 
162
189
  const builder = {
190
+ 'ClassProperty': b.tsDeclareMethod,
163
191
  'TSDeclareFunction': b.tsDeclareFunction,
164
192
  'TSDeclareMethod': b.tsDeclareMethod
165
193
  }[node.type];
166
194
 
195
+ const hostBuilder = {
196
+ 'ExportNamedDeclaration': b.exportNamedDeclaration,
197
+ 'ExportDefaultDeclaration': b.exportDefaultDeclaration
198
+ }[hostPath.value.type];
199
+
167
200
  const variationComments = commentPath.value ? [ keepCommentParams(commentPath, variation) ] : [ ];
168
201
 
169
202
  const newNode = builder.from({
170
- ...node,
203
+ key: node.key,
171
204
  params: variation.slice().reverse(),
205
+ id: node.id,
206
+ declare: node.declare || false,
207
+ returnType: b.tsTypeAnnotation.from({
208
+ ...returnType
209
+ }),
172
210
  comments: hostPath === nodePath ? variationComments : []
173
211
  });
174
212
 
175
213
  return (
176
- hostPath === nodePath
177
- ? newNode
178
- : b.exportNamedDeclaration.from({
214
+ hostBuilder
215
+ ? hostBuilder.from({
179
216
  ...hostPath.value,
180
217
  declaration: newNode,
181
218
  comments: variationComments
182
219
  })
220
+ : newNode
183
221
  );
184
222
  });
185
223
 
@@ -274,7 +312,13 @@ export default function transform(src) {
274
312
  });
275
313
 
276
314
  for (const [ path, args ] of replacements) {
277
- path.replace(...args);
315
+ try {
316
+ path.replace(...args);
317
+ } catch (err) {
318
+ console.error('Failed to replace path', path, ...args);
319
+
320
+ throw err;
321
+ }
278
322
  }
279
323
 
280
324
  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.6.0",
4
4
  "description": "Generate sane and clean types from JavaScript sources",
5
5
  "type": "module",
6
6
  "main": "index.js",