Skip to content

Commit f17af78

Browse files
Bo Lingenrattrayalex
authored andcommitted
use cherry-pick style lodash imports
1 parent 58c2e23 commit f17af78

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

src/index.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -618,30 +618,32 @@ export default function (babel) {
618618
}
619619

620620
const specifierNames = imports[importPath];
621-
const specifiers = [];
622621

623622
if (useRequire) {
624623
// eg; `const { map, uniq } = require('lodash');`
625624
for (const specifierName of specifierNames) {
626625
const importIdentifier = t.identifier(specifierName);
627-
specifiers.push(t.objectProperty(importIdentifier, importIdentifier, false, true));
626+
627+
const requireCall = t.callExpression(t.identifier("require"), [
628+
t.stringLiteral(importPath + '/' + specifierName)
629+
]);
630+
const requireStmt = t.variableDeclaration("const", [
631+
t.variableDeclarator(importIdentifier, requireCall),
632+
]);
633+
634+
declarations.push(requireStmt);
628635
}
629-
const requirePattern = t.objectPattern(specifiers);
630-
const requireCall = t.callExpression(t.identifier("require"), [
631-
t.stringLiteral(importPath)
632-
]);
633-
const requireStmt = t.variableDeclaration("const", [
634-
t.variableDeclarator(requirePattern, requireCall),
635-
]);
636-
declarations.push(requireStmt);
637636
} else {
638637
// eg; `import { map, uniq } from 'lodash';`
639638
for (const specifierName of specifierNames) {
640639
const importIdentifier = t.identifier(specifierName);
641-
specifiers.push(t.importSpecifier(importIdentifier, importIdentifier));
640+
const importSpecifier = t.importDefaultSpecifier(importIdentifier);
641+
const importDeclaration = t.importDeclaration(
642+
[importSpecifier],
643+
t.stringLiteral(importPath + '/' + specifierName)
644+
);
645+
declarations.push(importDeclaration);
642646
}
643-
const importDeclaration = t.importDeclaration(specifiers, t.stringLiteral(importPath));
644-
declarations.push(importDeclaration);
645647
}
646648
}
647649
path.unshiftContainer("body", declarations);

test/fixtures/stdlib/kitchen-sink/expected.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { map, round } from 'lodash';
1+
import map from 'lodash/map';
2+
import round from 'lodash/round';
23

34
function looseEq(a, b) {
45
return a == b;
@@ -16,4 +17,4 @@ function uniq(x) {
1617
return x;
1718
}
1819

19-
uniq(map([0.1, 0.2, 0.5, 0.9], round));
20+
uniq(map([0.1, 0.2, 0.5, 0.9], round));
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
import { uniq } from "lodash";
2-
uniq();
1+
import uniq from "lodash/uniq";
2+
uniq();
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { map } from "lodash";
1+
import map from "lodash/map";
22

33
function uniq() {
44
return 1;
55
}
66

77
uniq();
8-
map();
8+
map();

test/fixtures/stdlib/require-true/expected.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ function looseNotEq(a, b) {
66
return a != b;
77
}
88

9-
const {
10-
map,
11-
round
12-
} = require('lodash');
9+
const map = require('lodash/map');
10+
11+
const round = require('lodash/round');
1312

1413
looseEq(1, '1');
1514
looseNotEq(1, '1');
@@ -19,4 +18,4 @@ function uniq(x) {
1918
return x;
2019
}
2120

22-
uniq(map([0.1, 0.2, 0.5, 0.9], round));
21+
uniq(map([0.1, 0.2, 0.5, 0.9], round));

0 commit comments

Comments
 (0)