@@ -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) {
|
|
0 commit comments