Skip to content
This repository was archived by the owner on Sep 16, 2023. It is now read-only.

Commit a253c12

Browse files
committed
Tweak code style.
1 parent ed66ac1 commit a253c12

File tree

12 files changed

+267
-248
lines changed

12 files changed

+267
-248
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ $ npm i --save-dev babel-plugin-lodash babel-cli babel-preset-env
1616

1717
Transforms
1818
```js
19-
import _ from 'lodash';
20-
import { add } from 'lodash/fp';
19+
import _ from 'lodash'
20+
import { add } from 'lodash/fp'
2121

22-
const addOne = add(1);
23-
_.map([1, 2, 3], addOne);
22+
const addOne = add(1)
23+
_.map([1, 2, 3], addOne)
2424
```
2525

2626
roughly to
2727
```js
28-
import _add from 'lodash/fp/add';
29-
import _map from 'lodash/map';
28+
import _add from 'lodash/fp/add'
29+
import _map from 'lodash/map'
3030

31-
const addOne = _add(1);
32-
_map([1, 2, 3], addOne);
31+
const addOne = _add(1)
32+
_map([1, 2, 3], addOne)
3333
```
3434

3535
## Usage
@@ -68,7 +68,7 @@ $ babel --plugins lodash --presets es2015 script.js
6868
require('babel-core').transform('code', {
6969
'plugins': ['lodash'],
7070
'presets': [['env', { 'targets': { 'node': 4 } }]]
71-
});
71+
})
7272
```
7373

7474
###### webpack.config.js

src/Map.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default function MapCtor(entries) {
2-
return Object.setPrototypeOf(new Map(entries), Object.getPrototypeOf(this));
3-
};
2+
return Object.setPrototypeOf(new Map(entries), Object.getPrototypeOf(this))
3+
}
44

5-
MapCtor.prototype = Map.prototype;
5+
MapCtor.prototype = Map.prototype

src/MapCache.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
1-
import _ from 'lodash';
2-
import Map from './Map';
1+
import _ from 'lodash'
2+
import Map from './Map'
33

4-
const BREAK = {};
4+
const BREAK = {}
55

66
/*----------------------------------------------------------------------------*/
77

88
export default class MapCache extends Map {
99
clear() {
10-
super.clear();
11-
return this;
10+
super.clear()
11+
return this
1212
}
1313

1414
findKey(iteratee) {
15-
let result;
15+
let result
16+
1617
try {
1718
this.forEach((value, key, map) => {
1819
if (iteratee(value, key, map)) {
19-
result = key;
20-
throw BREAK;
20+
result = key
21+
throw BREAK
2122
}
22-
});
23+
})
2324
} catch (e) {
2425
if (e !== BREAK) {
25-
throw e;
26+
throw e
2627
}
2728
}
28-
return result;
29+
30+
return result
2931
}
30-
};
32+
}

src/ModuleCache.js

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,61 @@
1-
import _ from 'lodash';
2-
import fs from 'fs';
3-
import glob from 'glob';
4-
import MapCache from './MapCache';
5-
import Module from 'module';
6-
import { normalizePath } from './util';
7-
import path from 'path';
1+
import _ from 'lodash'
2+
import fs from 'fs'
3+
import glob from 'glob'
4+
import MapCache from './MapCache'
5+
import Module from 'module'
6+
import { normalizePath } from './util'
7+
import path from 'path'
88

99
/*----------------------------------------------------------------------------*/
1010

1111
export default class ModuleCache extends MapCache {
1212
constructor(moduleRoot) {
13-
super();
13+
super()
1414

15-
moduleRoot = _.toString(moduleRoot);
16-
if (!moduleRoot) {
17-
return;
15+
moduleRoot = _.toString(moduleRoot)
16+
17+
if (! moduleRoot) {
18+
return
1819
}
19-
const pkgPath = path.join(moduleRoot, 'package.json');
20-
const pkgMain = fs.existsSync(pkgPath) && require(pkgPath).main || 'index.js';
21-
const mainPath = normalizePath(path.dirname(path.resolve(moduleRoot, pkgMain)));
20+
21+
const pkgPath = path.join(moduleRoot, 'package.json')
22+
const pkgMain = fs.existsSync(pkgPath) && require(pkgPath).main || 'index.js'
23+
const mainPath = normalizePath(path.dirname(path.resolve(moduleRoot, pkgMain)))
2224

2325
// Sort paths by the “main” entry first.
2426
const dirPaths = _.orderBy(glob.sync(path.join(moduleRoot, '**/'), {
2527
'ignore': path.join(moduleRoot, 'node_modules/**/')
26-
}), dirPath => _.startsWith(dirPath, mainPath), ['desc']);
27-
28-
_.each(dirPaths, dirPath => {
29-
const base = path.relative(moduleRoot, dirPath);
30-
const filePaths = glob.sync(path.join(dirPath, '*.js'));
31-
const pairs = _.map(filePaths, filePath => {
32-
const name = path.basename(filePath, '.js');
33-
return [name.toLowerCase(), name];
34-
});
35-
this.set(base, new MapCache(pairs));
36-
});
28+
}), dirPath => _.startsWith(dirPath, mainPath), ['desc'])
29+
30+
_.each(dirPaths, (dirPath) => {
31+
const base = path.relative(moduleRoot, dirPath)
32+
const filePaths = glob.sync(path.join(dirPath, '*.js'))
33+
const pairs = _.map(filePaths, (filePath) => {
34+
const name = path.basename(filePath, '.js')
35+
return [name.toLowerCase(), name]
36+
})
37+
this.set(base, new MapCache(pairs))
38+
})
3739
}
3840

39-
static resolve(id, from=process.cwd()) {
41+
static resolve(id, from = process.cwd()) {
4042
const dirs = path.dirname(Module._resolveFilename(id, _.assign(new Module, {
4143
'paths': Module._nodeModulePaths(from)
42-
}))).split(path.sep);
44+
}))).split(path.sep)
45+
46+
let { length } = dirs
4347

44-
let { length } = dirs;
4548
while (length--) {
46-
const dirSub = dirs.slice(0, length + 1);
47-
const dirPath = dirSub.join('/');
48-
const pkgPath = path.join(dirPath, 'package.json');
49+
const dirSub = dirs.slice(0, length + 1)
50+
const dirPath = dirSub.join('/')
51+
const pkgPath = path.join(dirPath, 'package.json')
4952

50-
if ((length && dirs[length - 1] == 'node_modules') ||
51-
(fs.existsSync(pkgPath) && require(pkgPath).name == id)) {
52-
return dirPath;
53+
if ((length && dirs[length - 1] === 'node_modules') ||
54+
(fs.existsSync(pkgPath) && require(pkgPath).name === id)) {
55+
return dirPath
5356
}
5457
}
55-
return dirs.join('/');
58+
59+
return dirs.join('/')
5660
}
57-
};
61+
}

src/Package.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import _ from 'lodash';
2-
import requirePackageName from 'require-package-name';
1+
import _ from 'lodash'
2+
import requirePackageName from 'require-package-name'
33

4-
const reLodash = /^lodash(?:-compat|-es)?$/;
4+
const reLodash = /^lodash(?:-compat|-es)?$/
55

66
/*----------------------------------------------------------------------------*/
77

88
export default class Package {
99
constructor(pkgPath) {
10-
pkgPath = _.toString(pkgPath);
11-
const pkgName = requirePackageName(pkgPath);
10+
pkgPath = _.toString(pkgPath)
11+
const pkgName = requirePackageName(pkgPath)
1212

13-
this.base = pkgPath.replace(new RegExp(pkgName + '/?'), '');
14-
this.id = pkgName;
15-
this.isLodash = _.constant(reLodash.test(this.id));
16-
this.path = pkgPath;
13+
this.base = pkgPath.replace(new RegExp(pkgName + '/?'), '')
14+
this.id = pkgName
15+
this.isLodash = _.constant(reLodash.test(this.id))
16+
this.path = pkgPath
1717
}
18-
};
18+
}

src/Store.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import _ from 'lodash';
2-
import MapCache from './MapCache';
3-
import { normalizePath } from './util';
4-
import Package from './Package';
1+
import _ from 'lodash'
2+
import MapCache from './MapCache'
3+
import { normalizePath } from './util'
4+
import Package from './Package'
55

66
/*----------------------------------------------------------------------------*/
77

88
export default class Store extends MapCache {
99
constructor(pkgPaths) {
10-
super();
11-
_.each(pkgPaths, pkgPath => this.set(pkgPath));
10+
super()
11+
_.each(pkgPaths, (pkgPath) => this.set(pkgPath))
1212
}
1313

1414
get(pkgPath) {
15-
return super.get(normalizePath(pkgPath));
15+
return super.get(normalizePath(pkgPath))
1616
}
1717

18-
set(pkgPath, pkgStore=new Package(normalizePath(pkgPath))) {
19-
return super.set(normalizePath(pkgPath), pkgStore);
18+
set(pkgPath, pkgStore = new Package(normalizePath(pkgPath))) {
19+
return super.set(normalizePath(pkgPath), pkgStore)
2020
}
21-
};
21+
}

src/config.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
1-
import _ from 'lodash';
2-
import MapCache from './MapCache';
3-
import ModuleCache from './ModuleCache';
1+
import _ from 'lodash'
2+
import MapCache from './MapCache'
3+
import ModuleCache from './ModuleCache'
44

55
const defaultIds = [
66
'lodash',
77
'lodash-es',
88
'lodash-compat'
9-
];
9+
]
1010

11-
let oldCwd;
12-
const ids = [];
13-
const modules = new MapCache;
11+
let oldCwd
12+
const ids = []
13+
const modules = new MapCache
1414

1515
/*----------------------------------------------------------------------------*/
1616

17-
export default function config({ cwd=process.cwd(), id=defaultIds }={}) {
17+
export default function config({ cwd = process.cwd(), id = defaultIds } = {}) {
1818
if (oldCwd !== cwd) {
19-
oldCwd = cwd;
20-
modules.clear();
19+
oldCwd = cwd
20+
modules.clear()
2121
}
22-
_.each(_.castArray(id), id => {
23-
if (!modules.get(id)) {
24-
const moduleRoot = ModuleCache.resolve(id, cwd);
22+
23+
_.each(_.castArray(id), (id) => {
24+
if (! modules.get(id)) {
25+
const moduleRoot = ModuleCache.resolve(id, cwd)
26+
2527
if (moduleRoot) {
26-
ids.push(id);
27-
modules.set(id, new ModuleCache(moduleRoot));
28+
ids.push(id)
29+
modules.set(id, new ModuleCache(moduleRoot))
2830
}
2931
}
30-
});
31-
return { ids, modules };
32-
};
32+
})
33+
34+
return { ids, modules }
35+
}

src/importModule.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
import _ from 'lodash';
2-
import { addDefault } from "babel-helper-module-imports";
3-
import mapping from './mapping';
1+
import _ from 'lodash'
2+
import { addDefault } from "babel-helper-module-imports"
3+
import mapping from './mapping'
44

55
/*----------------------------------------------------------------------------*/
66

77
function resolvePath(pkgStore, name, path) {
8-
let { base, id } = pkgStore;
9-
const lower = name.toLowerCase();
10-
const module = mapping.modules.get(id);
8+
let { base, id } = pkgStore
9+
const lower = name.toLowerCase()
10+
const module = mapping.modules.get(id)
1111

12-
if (!module.get(base).has(lower)) {
13-
base = base ? '' : module.findKey(map => map.has(lower));
14-
if (!base) {
12+
if (! module.get(base).has(lower)) {
13+
base = base ? '' : module.findKey(map => map.has(lower))
14+
15+
if (! base) {
1516
throw path.buildCodeFrameError([
1617
`The '${ id }' method \`${ name }\` is not a known module.`,
1718
'Please report bugs to https://github.com/lodash/babel-plugin-lodash/issues.'
18-
].join('\n'));
19+
].join('\n'))
1920
}
2021
}
21-
return id + '/' + (base ? base + '/' : '') + module.get(base).get(lower);
22+
23+
return id + '/' + (base ? base + '/' : '') + module.get(base).get(lower)
2224
}
2325

2426
function importModule(pkgStore, name, path) {
25-
return addDefault(path, resolvePath(pkgStore, name, path), { nameHint: name });
27+
return addDefault(path, resolvePath(pkgStore, name, path), { nameHint: name })
2628
}
2729

28-
export default _.memoize(importModule, (pkgStore, name) => (pkgStore.path + '/' + name).toLowerCase());
30+
export default _.memoize(importModule, (pkgStore, name) => (pkgStore.path + '/' + name).toLowerCase())

0 commit comments

Comments
 (0)