Skip to content

Commit 84e5e5a

Browse files
committed
fix(flow): remove import declaration if import contains only flow types
1 parent 2f6ec8a commit 84e5e5a

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

src/__tests__/__snapshots__/es-test.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ async function test() {
1414
}"
1515
`;
1616

17+
exports[`flow should strip whole import if it contains only types 1`] = `
18+
"'use strict';
19+
20+
function test() {
21+
console.log('test');
22+
}"
23+
`;
24+
1725
exports[`transform if Async await enabled 1`] = `
1826
"\\"use strict\\";
1927

src/__tests__/es-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ testParseCode('Flow Support can be disabled using flow option', {
130130
`,
131131
});
132132

133+
test('flow should strip whole import if it contains only types', () => {
134+
const transformed = transform(`
135+
import { type A, type B } from 'some-package';
136+
function test() {
137+
console.log('test');
138+
}
139+
`);
140+
141+
expect(transformed.code).toMatchSnapshot();
142+
});
143+
133144
testParseCode('Support import shorthand', {
134145
opts,
135146
throws: false,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* @flow */
2+
/**
3+
* TEMP_FIX (babel 6.x) for import { type A, type B } from 'moduleA';
4+
* getting converted to "import 'moduleA';" but it should be removed
5+
*
6+
* the current fix is already present in babel plugin v7 so remove
7+
* when upgrade to babel 7
8+
*/
9+
import syntaxFlow from 'babel-plugin-syntax-flow';
10+
11+
export default function transformFlowStripTypesImportFix() {
12+
return {
13+
inherits: syntaxFlow,
14+
15+
visitor: {
16+
ImportDeclaration(path: Object) {
17+
if (!path.node.specifiers.length) {
18+
return;
19+
}
20+
21+
let typeCount = 0;
22+
23+
path.node.specifiers.forEach(({ importKind }) => {
24+
if (importKind === 'type' || importKind === 'typeof') {
25+
typeCount += 1;
26+
}
27+
});
28+
29+
if (typeCount === path.node.specifiers.length) {
30+
path.remove();
31+
}
32+
},
33+
},
34+
};
35+
}

src/presets/es.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import transformFlowStripTypes from 'babel-plugin-transform-flow-strip-types';
88
import syntaxFlow from 'babel-plugin-syntax-flow';
99
import syntaxDynamicImport from 'babel-plugin-syntax-dynamic-import';
1010

11+
import transformFlowStripTypesImportFix from '../plugins/transform-flow-strip-types-import-fix';
12+
1113
export type Opts = {
1214
flow: boolean,
1315
asyncAwait: boolean,
@@ -64,7 +66,7 @@ export default (context: any, opts: Opts) => {
6466
[transformObjectRestSpread, { useBuiltIns: true }],
6567

6668
// flow support
67-
...(opts.flow ? [transformFlowStripTypes, syntaxFlow] : []),
69+
...(opts.flow ? [transformFlowStripTypesImportFix, transformFlowStripTypes, syntaxFlow] : []),
6870
].filter(Boolean),
6971
};
7072
};

0 commit comments

Comments
 (0)