flow-remove-types 2.266.0 → 2.267.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.
package/flow-remove-types CHANGED
@@ -28,6 +28,9 @@ var usage = 'Usage: flow-remove-types [options] [sources] \n' +
28
28
  ' completely rather than only removing the type. THIS IS NOT\n' +
29
29
  ' SPEC COMPLIANT! Instead, use `declare foo: string;` for\n' +
30
30
  ' type-only fields.\n' +
31
+ ' --remove-empty-imports\n' +
32
+ ' If true, removes empty import statements that remain after removing\n' +
33
+ ' all type and typeof imports (e.g. `import {} from \'some-module\'`).\n' +
31
34
  ' -m, --sourcemaps Also output source map files. Optionally pass "inline"\n' +
32
35
  ' -q, --quiet Does not produce any output concerning successful progress.\n' +
33
36
 
@@ -85,6 +88,7 @@ var outFile;
85
88
  var all;
86
89
  var pretty;
87
90
  var ignoreUninitializedFields;
91
+ var removeEmptyImports;
88
92
  var sourceMaps;
89
93
  var inlineSourceMaps;
90
94
  var quiet;
@@ -112,6 +116,8 @@ while (i < process.argv.length) {
112
116
  pretty = true;
113
117
  } else if (arg === '--ignore-uninitialized-fields') {
114
118
  ignoreUninitializedFields = true;
119
+ } else if (arg === '--remove-empty-imports') {
120
+ removeEmptyImports = true;
115
121
  } else if (arg === '-m' || arg === '--sourcemaps') {
116
122
  sourceMaps = true;
117
123
  if (process.argv[i] === 'inline') {
@@ -254,6 +260,7 @@ function transformSource(content, filepath) {
254
260
  all: all,
255
261
  pretty: pretty,
256
262
  ignoreUninitializedFields: ignoreUninitializedFields,
263
+ removeEmptyImports: removeEmptyImports,
257
264
  });
258
265
  } catch (error) {
259
266
  if (error.loc) {
package/index.js CHANGED
@@ -28,6 +28,10 @@ var vlq = require('vlq');
28
28
  * completely rather than only removing the type. THIS IS NOT SPEC
29
29
  * COMPLIANT! Instead, use `declare foo: string;` for type-only fields.
30
30
  *
31
+ * - removeEmptyImports: (default: false)
32
+ * If true, removes empty import statements that remain after removing
33
+ * all type and typeof imports (e.g. `import {} from 'some-module'`).
34
+ *
31
35
  * Returns an object with two methods:
32
36
  *
33
37
  * - .toString()
@@ -69,6 +73,7 @@ module.exports = function flowRemoveTypes(source, options) {
69
73
  ignoreUninitializedFields: Boolean(
70
74
  options && options.ignoreUninitializedFields,
71
75
  ),
76
+ removeEmptyImports: Boolean(options && options.removeEmptyImports),
72
77
  };
73
78
 
74
79
  // Remove the flow pragma.
@@ -239,6 +244,55 @@ var removeFlowVisitor = {
239
244
  if (node.importKind === 'type' || node.importKind === 'typeof') {
240
245
  return removeNode(context, node);
241
246
  }
247
+
248
+ if (
249
+ context.removeEmptyImports &&
250
+ node.importKind === 'value' &&
251
+ node.specifiers.length > 0
252
+ ) {
253
+ if (node.specifiers[0].type !== 'ImportDefaultSpecifier') {
254
+ // Remove import declaration if all specifiers are type imports
255
+ for (var i = 0; i < node.specifiers.length; i++) {
256
+ if (
257
+ node.specifiers[i].importKind !== 'type' &&
258
+ node.specifiers[i].importKind !== 'typeof'
259
+ ) {
260
+ return;
261
+ }
262
+ }
263
+ return removeNode(context, node);
264
+ } else if (node.specifiers.length > 1) {
265
+ // Remove non-default specifiers completely
266
+ // if all non-default specifiers are type imports
267
+ for (var i = 1; i < node.specifiers.length; i++) {
268
+ if (
269
+ node.specifiers[i].importKind !== 'type' &&
270
+ node.specifiers[i].importKind !== 'typeof'
271
+ ) {
272
+ return;
273
+ }
274
+ }
275
+
276
+ var defaultSpecifier = node.specifiers[0];
277
+ var firstSpecifier = node.specifiers[1];
278
+ var lastSpecifier = node.specifiers[node.specifiers.length - 1];
279
+
280
+ var idxStart = findTokenIndexAtStartOfNode(
281
+ context.ast.tokens,
282
+ firstSpecifier,
283
+ );
284
+ var idxEnd = findTokenIndexAtEndOfNode(
285
+ context.ast.tokens,
286
+ lastSpecifier,
287
+ );
288
+
289
+ removeTrailingCommaNode(context, defaultSpecifier);
290
+ // remove opening brace
291
+ removeNode(context, context.ast.tokens[idxStart - 1]);
292
+ // remove closing brace
293
+ removeNode(context, context.ast.tokens[idxEnd + 1]);
294
+ }
295
+ }
242
296
  },
243
297
 
244
298
  ImportSpecifier: function (context, node) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flow-remove-types",
3
- "version": "2.266.0",
3
+ "version": "2.267.0",
4
4
  "description": "Removes Flow type annotations from JavaScript files with speed and simplicity.",
5
5
  "author": {
6
6
  "name": "Flow Team",