Skip to content

Commit 3f50e48

Browse files
wkillerudsapegin
authored andcommitted
Fix: Allow trailing comma in named imports in examples (styleguidist#1296)
Fixes styleguidist#1295
1 parent 30ba0f5 commit 3f50e48

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/client/utils/__tests__/transpileImports.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,34 @@ const E = snake$0.default || snake$0;
5757
`);
5858
});
5959

60+
test('transpile multiline named imports without trailing comma', () => {
61+
const result = transpileImports(`import {
62+
B,
63+
C
64+
} from 'cat'
65+
`);
66+
expect(result).toMatchInlineSnapshot(`
67+
"const cat$0 = require('cat');
68+
const B = cat$0.B;
69+
const C = cat$0.C;
70+
"
71+
`);
72+
});
73+
74+
test('transpile multiline named imports with trailing comma', () => {
75+
const result = transpileImports(`import {
76+
B,
77+
C,
78+
} from 'cat'
79+
`);
80+
expect(result).toMatchInlineSnapshot(`
81+
"const cat$0 = require('cat');
82+
const B = cat$0.B;
83+
const C = cat$0.C;
84+
"
85+
`);
86+
});
87+
6088
test('return code if there are no imports', () => {
6189
const code = `<Button />`;
6290
const result = transpileImports(code);

src/client/utils/rewriteImports.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default function(str, fn = 'require') {
4545
num = 0;
4646
return str
4747
.replace(NAMED, (_, asterisk, base, req, dep) =>
48-
generate(req ? req.split(',') : [], dep, base, fn)
48+
generate(req ? req.split(',').filter(dep => dep.trim()) : [], dep, base, fn)
4949
)
5050
.replace(UNNAMED, (_, dep) => `${fn}('${dep}');`);
5151
}

0 commit comments

Comments
 (0)