Skip to content

Commit 2204871

Browse files
authored
Add prettier (#409)
* Add prettier * Add test/helpers
1 parent dbec80d commit 2204871

File tree

16 files changed

+494
-211
lines changed

16 files changed

+494
-211
lines changed

.eslintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
},
66
"extends": [
77
"eslint-config-babel"
8-
]
8+
],
9+
"rules": {
10+
"arrow-parens": "off"
11+
}
912
}

package.json

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
"eslint": "^3.8.1",
3232
"eslint-config-babel": "^6.0.0",
3333
"eslint-plugin-flowtype": "^2.25.0",
34+
"husky": "^0.13.2",
35+
"lint-staged": "^3.3.1",
3436
"nyc": "^10.0.0",
37+
"prettier": "^1.2.2",
3538
"react": "^15.1.0",
3639
"react-intl": "^2.1.2",
3740
"react-intl-webpack-plugin": "^0.0.3",
@@ -41,9 +44,11 @@
4144
"scripts": {
4245
"clean": "rimraf lib/",
4346
"build": "babel src/ --out-dir lib/",
47+
"format": "prettier --write --trailing-comma all \"src/**/*.js\" \"test/*.test.js\" \"test/helpers/*.js\" && prettier --write --trailing-comma es5 \"scripts/*.js\"",
4448
"lint": "eslint src test",
45-
"preversion": "yarn run test",
49+
"precommit": "lint-staged",
4650
"prepublish": "yarn run clean && yarn run build",
51+
"preversion": "yarn run test",
4752
"test": "yarn run lint && cross-env BABEL_ENV=test yarn run build && yarn run test-only",
4853
"test-only": "nyc ava"
4954
},
@@ -90,5 +95,27 @@
9095
"src/**/*.js"
9196
],
9297
"babel": "inherit"
98+
},
99+
"lint-staged": {
100+
"scripts/*.js": [
101+
"prettier --trailing-comma es5 --write",
102+
"git add"
103+
],
104+
"src/**/*.js": [
105+
"prettier --trailing-comma all --write",
106+
"git add"
107+
],
108+
"test/**/*.test.js": [
109+
"prettier --trailing-comma all --write",
110+
"git add"
111+
],
112+
"test/helpers/*.js": [
113+
"prettier --trailing-comma all --write ",
114+
"git add"
115+
],
116+
"package.json": [
117+
"node ./scripts/yarn-install.js",
118+
"git add yarn.lock"
119+
]
93120
}
94121
}

scripts/yarn-install.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
const exec = require("child_process").exec;
4+
5+
const runIfYarn = fn => {
6+
exec("yarn -V", error => {
7+
if (error === null) fn();
8+
});
9+
};
10+
runIfYarn(() => {
11+
console.log("`package.json` was changed. Running yarn...🐈");
12+
exec("yarn");
13+
});

src/fs-cache.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const os = require("os");
1515
const path = require("path");
1616
const zlib = require("zlib");
1717

18-
let defaultCacheDirectory = null; // Lazily instantiated when needed
18+
let defaultCacheDirectory = null; // Lazily instantiated when needed
1919

2020
/**
2121
* Read the contents from the compressed file.
@@ -26,12 +26,12 @@ let defaultCacheDirectory = null; // Lazily instantiated when needed
2626
*/
2727
const read = function(filename, callback) {
2828
return fs.readFile(filename, function(err, data) {
29-
if (err) { return callback(err); }
29+
if (err) return callback(err);
3030

3131
return zlib.gunzip(data, function(err, content) {
32-
let result = {};
32+
if (err) return callback(err);
3333

34-
if (err) { return callback(err); }
34+
let result = {};
3535

3636
try {
3737
result = JSON.parse(content);
@@ -44,7 +44,6 @@ const read = function(filename, callback) {
4444
});
4545
};
4646

47-
4847
/**
4948
* Write contents into a compressed file.
5049
*
@@ -57,13 +56,12 @@ const write = function(filename, result, callback) {
5756
const content = JSON.stringify(result);
5857

5958
return zlib.gzip(content, function(err, data) {
60-
if (err) { return callback(err); }
59+
if (err) return callback(err);
6160

6261
return fs.writeFile(filename, data, callback);
6362
});
6463
};
6564

66-
6765
/**
6866
* Build the filename for the cached file
6967
*
@@ -97,12 +95,16 @@ const handleCache = function(directory, params, callback) {
9795
const options = params.options || {};
9896
const transform = params.transform;
9997
const identifier = params.identifier;
100-
const shouldFallback = typeof params.directory !== "string" && directory !== os.tmpdir();
98+
const shouldFallback =
99+
typeof params.directory !== "string" && directory !== os.tmpdir();
101100

102101
// Make sure the directory exists.
103102
mkdirp(directory, function(err) {
104103
// Fallback to tmpdir if node_modules folder not writable
105-
if (err) return shouldFallback ? handleCache(os.tmpdir(), params, callback) : callback(err);
104+
if (err)
105+
return shouldFallback
106+
? handleCache(os.tmpdir(), params, callback)
107+
: callback(err);
106108

107109
const file = path.join(directory, filename(source, identifier, options));
108110

@@ -122,7 +124,10 @@ const handleCache = function(directory, params, callback) {
122124

123125
return write(file, result, function(err) {
124126
// Fallback to tmpdir if node_modules folder not writable
125-
if (err) return shouldFallback ? handleCache(os.tmpdir(), params, callback) : callback(err);
127+
if (err)
128+
return shouldFallback
129+
? handleCache(os.tmpdir(), params, callback)
130+
: callback(err);
126131

127132
callback(null, result);
128133
});
@@ -171,7 +176,8 @@ module.exports = function(params, callback) {
171176
directory = params.directory;
172177
} else {
173178
if (defaultCacheDirectory === null) {
174-
defaultCacheDirectory = findCacheDir({ name: "babel-loader" }) || os.tmpdir();
179+
defaultCacheDirectory =
180+
findCacheDir({ name: "babel-loader" }) || os.tmpdir();
175181
}
176182
directory = defaultCacheDirectory;
177183
}

src/index.js

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ const transpile = function(source, options) {
5959
hideStack = true;
6060
}
6161
throw new BabelLoaderError(
62-
name, message, error.codeFrame, hideStack, error);
62+
name,
63+
message,
64+
error.codeFrame,
65+
hideStack,
66+
error,
67+
);
6368
} else {
6469
throw error;
6570
}
@@ -97,7 +102,9 @@ function passMetadata(s, context, metadata) {
97102

98103
module.exports = function(source, inputSourceMap) {
99104
// Handle filenames (#106)
100-
const webpackRemainingChain = loaderUtils.getRemainingRequest(this).split("!");
105+
const webpackRemainingChain = loaderUtils
106+
.getRemainingRequest(this)
107+
.split("!");
101108
const filename = webpackRemainingChain[webpackRemainingChain.length - 1];
102109

103110
// Handle options
@@ -110,10 +117,13 @@ module.exports = function(source, inputSourceMap) {
110117
cacheIdentifier: JSON.stringify({
111118
"babel-loader": pkg.version,
112119
"babel-core": babel.version,
113-
babelrc: exists(loaderOptions.babelrc) ?
114-
read(loaderOptions.babelrc) :
115-
resolveRc(path.dirname(filename)),
116-
env: loaderOptions.forceEnv || process.env.BABEL_ENV || process.env.NODE_ENV || "development",
120+
babelrc: exists(loaderOptions.babelrc)
121+
? read(loaderOptions.babelrc)
122+
: resolveRc(path.dirname(filename)),
123+
env: loaderOptions.forceEnv ||
124+
process.env.BABEL_ENV ||
125+
process.env.NODE_ENV ||
126+
"development",
117127
}),
118128
};
119129

@@ -124,10 +134,7 @@ module.exports = function(source, inputSourceMap) {
124134
}
125135

126136
if (options.sourceFileName === undefined) {
127-
options.sourceFileName = relative(
128-
options.sourceRoot,
129-
options.filename
130-
);
137+
options.sourceFileName = relative(options.sourceRoot, options.filename);
131138
}
132139

133140
const cacheDirectory = options.cacheDirectory;
@@ -140,24 +147,27 @@ module.exports = function(source, inputSourceMap) {
140147

141148
if (cacheDirectory) {
142149
const callback = this.async();
143-
return cache({
144-
directory: cacheDirectory,
145-
identifier: cacheIdentifier,
146-
source: source,
147-
options: options,
148-
transform: transpile,
149-
}, (err, { code, map, metadata } = {}) => {
150-
if (err) return callback(err);
151-
152-
metadataSubscribers.forEach((s) => passMetadata(s, this, metadata));
153-
154-
return callback(null, code, map);
155-
});
150+
return cache(
151+
{
152+
directory: cacheDirectory,
153+
identifier: cacheIdentifier,
154+
source: source,
155+
options: options,
156+
transform: transpile,
157+
},
158+
(err, { code, map, metadata } = {}) => {
159+
if (err) return callback(err);
160+
161+
metadataSubscribers.forEach(s => passMetadata(s, this, metadata));
162+
163+
return callback(null, code, map);
164+
},
165+
);
156166
}
157167

158168
const { code, map, metadata } = transpile(source, options);
159169

160-
metadataSubscribers.forEach((s) => passMetadata(s, this, metadata));
170+
metadataSubscribers.forEach(s => passMetadata(s, this, metadata));
161171

162172
this.callback(null, code, map);
163173
};

src/resolve-rc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const find = function find(start, rel) {
2424
// Reached root
2525
return find(up, rel);
2626
}
27-
2827
};
2928

3029
module.exports = function(loc, rel) {

src/utils/exists.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ module.exports = function(cache) {
1111
cache = cache || {};
1212

1313
return function(filename) {
14+
if (!filename) return false;
1415

15-
if (!filename) { return false; }
16-
17-
cache[filename] = cache[filename] || (fs.existsSync(filename) && fs.statSync(filename).isFile());
16+
cache[filename] =
17+
cache[filename] ||
18+
(fs.existsSync(filename) && fs.statSync(filename).isFile());
1819

1920
return cache[filename];
2021
};

src/utils/read.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module.exports = function(cache) {
1111
cache = cache || {};
1212

1313
return function(filename) {
14-
1514
if (!filename) {
1615
throw new Error("filename must be a string");
1716
}

0 commit comments

Comments
 (0)