Skip to content

Commit 1a9ddbe

Browse files
committed
Even more ES2015ification
1 parent 61cdd09 commit 1a9ddbe

File tree

11 files changed

+632
-680
lines changed

11 files changed

+632
-680
lines changed

cli.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env node
22
'use strict';
33

4-
var path = require('path');
5-
var debug = require('debug')('ava');
4+
const path = require('path');
5+
const debug = require('debug')('ava');
66

77
// Prefer the local installation of AVA.
8-
var resolveCwd = require('resolve-cwd');
9-
var localCLI = resolveCwd('ava/cli');
8+
const resolveCwd = require('resolve-cwd');
9+
const localCLI = resolveCwd('ava/cli');
1010

1111
// Use path.relative() to detect local AVA installation,
1212
// because __filename's case is inconsistent on Windows
@@ -22,7 +22,7 @@ if (localCLI && path.relative(localCLI, __filename) !== '') {
2222
try {
2323
require('./lib/cli').run();
2424
} catch (err) {
25-
console.error('\n ' + err.message);
25+
console.error(`\n ${err.message}`);
2626
process.exit(1);
2727
}
2828
}

lib/ava-error.js

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ class AvaError extends Error {
44
constructor(message) {
55
super(message);
66
this.name = 'AvaError';
7-
this.message = message;
87
}
98
}
109

lib/beautify-stack.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if (!debug.enabled) {
1717

1818
const stackUtils = new StackUtils({internals: ignoreStackLines});
1919

20-
module.exports = function (stack) {
20+
module.exports = stack => {
2121
if (!stack) {
2222
return '';
2323
}

lib/caching-precompiler.js

+59-71
Original file line numberDiff line numberDiff line change
@@ -9,83 +9,71 @@ const autoBind = require('auto-bind');
99
const md5Hex = require('md5-hex');
1010
const babelConfigHelper = require('./babel-config');
1111

12-
function CachingPrecompiler(options) {
13-
if (!(this instanceof CachingPrecompiler)) {
14-
throw new TypeError('Class constructor CachingPrecompiler cannot be invoked without \'new\'');
15-
}
16-
17-
autoBind(this);
18-
19-
options = options || {};
12+
class CachingPrecompiler {
13+
constructor(options) {
14+
autoBind(this);
2015

21-
this.babelConfig = babelConfigHelper.validate(options.babel);
22-
this.cacheDirPath = options.path;
23-
this.powerAssert = Boolean(options.powerAssert);
24-
this.fileHashes = {};
25-
this.transform = this._createTransform();
26-
}
27-
28-
module.exports = CachingPrecompiler;
16+
options = options || {};
2917

30-
CachingPrecompiler.prototype.precompileFile = function (filePath) {
31-
if (!this.fileHashes[filePath]) {
32-
const source = stripBomBuf(fs.readFileSync(filePath));
33-
34-
this.transform(source, filePath);
18+
this.babelConfig = babelConfigHelper.validate(options.babel);
19+
this.cacheDirPath = options.path;
20+
this.powerAssert = Boolean(options.powerAssert);
21+
this.fileHashes = {};
22+
this.transform = this._createTransform();
3523
}
24+
precompileFile(filePath) {
25+
if (!this.fileHashes[filePath]) {
26+
const source = stripBomBuf(fs.readFileSync(filePath));
27+
this.transform(source, filePath);
28+
}
3629

37-
return this.fileHashes[filePath];
38-
};
39-
40-
// Conditionally called by caching-transform when precompiling is required
41-
CachingPrecompiler.prototype._init = function () {
42-
this.babel = require('babel-core');
43-
44-
return this._transform;
45-
};
46-
47-
CachingPrecompiler.prototype._transform = function (code, filePath, hash) {
48-
code = code.toString();
49-
50-
var options = babelConfigHelper.build(this.babelConfig, this.powerAssert, filePath, code);
51-
var result = this.babel.transform(code, options);
52-
53-
// Save source map
54-
var mapPath = path.join(this.cacheDirPath, hash + '.js.map');
55-
fs.writeFileSync(mapPath, JSON.stringify(result.map));
56-
57-
// Append source map comment to transformed code
58-
// So that other libraries (like nyc) can find the source map
59-
var dirPath = path.dirname(filePath);
60-
var relativeMapPath = path.relative(dirPath, mapPath);
61-
var comment = convertSourceMap.generateMapFileComment(relativeMapPath);
62-
63-
return `${result.code}\n${comment}`;
64-
};
65-
66-
CachingPrecompiler.prototype._createTransform = function () {
67-
const pluginPackages = babelConfigHelper.pluginPackages;
68-
const avaPackage = require.resolve('../package.json');
69-
const packages = [avaPackage].concat(pluginPackages);
30+
return this.fileHashes[filePath];
31+
}
32+
// Conditionally called by caching-transform when precompiling is required
33+
_init() {
34+
this.babel = require('babel-core');
35+
return this._transform;
36+
}
37+
_transform(code, filePath, hash) {
38+
code = code.toString();
7039

71-
const majorNodeVersion = process.version.split('.')[0];
72-
const babelConfig = JSON.stringify(this.babelConfig);
73-
const packageSalt = babelConfig + majorNodeVersion + this.powerAssert;
40+
const options = babelConfigHelper.build(this.babelConfig, this.powerAssert, filePath, code);
41+
const result = this.babel.transform(code, options);
7442

75-
const salt = packageHash.sync(packages, packageSalt);
43+
// Save source map
44+
const mapPath = path.join(this.cacheDirPath, `${hash}.js.map`);
45+
fs.writeFileSync(mapPath, JSON.stringify(result.map));
7646

77-
return cachingTransform({
78-
factory: this._init,
79-
cacheDir: this.cacheDirPath,
80-
hash: this._generateHash,
81-
salt,
82-
ext: '.js'
83-
});
84-
};
47+
// Append source map comment to transformed code
48+
// So that other libraries (like nyc) can find the source map
49+
const dirPath = path.dirname(filePath);
50+
const relativeMapPath = path.relative(dirPath, mapPath);
51+
const comment = convertSourceMap.generateMapFileComment(relativeMapPath);
8552

86-
CachingPrecompiler.prototype._generateHash = function (code, filePath, salt) {
87-
const hash = md5Hex([code, filePath, salt]);
88-
this.fileHashes[filePath] = hash;
53+
return `${result.code}\n${comment}`;
54+
}
55+
_createTransform() {
56+
const pluginPackages = babelConfigHelper.pluginPackages;
57+
const avaPackage = require.resolve('../package.json');
58+
const packages = [avaPackage].concat(pluginPackages);
59+
const majorNodeVersion = process.version.split('.')[0];
60+
const babelConfig = JSON.stringify(this.babelConfig);
61+
const packageSalt = babelConfig + majorNodeVersion + this.powerAssert;
62+
const salt = packageHash.sync(packages, packageSalt);
63+
64+
return cachingTransform({
65+
factory: this._init,
66+
cacheDir: this.cacheDirPath,
67+
hash: this._generateHash,
68+
salt,
69+
ext: '.js'
70+
});
71+
}
72+
_generateHash(code, filePath, salt) {
73+
const hash = md5Hex([code, filePath, salt]);
74+
this.fileHashes[filePath] = hash;
75+
return hash;
76+
}
77+
}
8978

90-
return hash;
91-
};
79+
module.exports = CachingPrecompiler;

lib/cli.js

+65-65
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
11
'use strict';
2-
var path = require('path');
3-
var updateNotifier = require('update-notifier');
4-
var figures = require('figures');
5-
var arrify = require('arrify');
6-
var meow = require('meow');
7-
var Promise = require('bluebird');
8-
var pkgConf = require('pkg-conf');
9-
var isCi = require('is-ci');
10-
var hasFlag = require('has-flag');
11-
var Api = require('../api');
12-
var colors = require('./colors');
13-
var verboseReporter = require('./reporters/verbose');
14-
var miniReporter = require('./reporters/mini');
15-
var tapReporter = require('./reporters/tap');
16-
var Logger = require('./logger');
17-
var Watcher = require('./watcher');
18-
var babelConfig = require('./babel-config');
2+
const path = require('path');
3+
const updateNotifier = require('update-notifier');
4+
const figures = require('figures');
5+
const arrify = require('arrify');
6+
const meow = require('meow');
7+
const Promise = require('bluebird');
8+
const pkgConf = require('pkg-conf');
9+
const isCi = require('is-ci');
10+
const hasFlag = require('has-flag');
11+
const Api = require('../api');
12+
const colors = require('./colors');
13+
const verboseReporter = require('./reporters/verbose');
14+
const miniReporter = require('./reporters/mini');
15+
const tapReporter = require('./reporters/tap');
16+
const Logger = require('./logger');
17+
const Watcher = require('./watcher');
18+
const babelConfig = require('./babel-config');
1919

2020
// Bluebird specific
2121
Promise.longStackTraces();
2222

23-
exports.run = function () {
24-
var conf = pkgConf.sync('ava');
25-
26-
var filepath = pkgConf.filepath(conf);
27-
var pkgDir = filepath === null ? process.cwd() : path.dirname(filepath);
28-
29-
var cli = meow([
30-
'Usage',
31-
' ava [<file|directory|glob> ...]',
32-
'',
33-
'Options',
34-
' --init Add AVA to your project',
35-
' --fail-fast Stop after first test failure',
36-
' --serial, -s Run tests serially',
37-
' --tap, -t Generate TAP output',
38-
' --verbose, -v Enable verbose output',
39-
' --no-cache Disable the transpiler cache',
40-
' --no-power-assert Disable Power Assert',
41-
' --match, -m Only run tests with matching title (Can be repeated)',
42-
' --watch, -w Re-run tests when tests and source files change',
43-
' --source, -S Pattern to match source files so tests can be re-run (Can be repeated)',
44-
' --timeout, -T Set global timeout',
45-
' --concurrency, -c Maximum number of test files running at the same time (EXPERIMENTAL)',
46-
' --update-snapshots, -u Update snapshots',
47-
'',
48-
'Examples',
49-
' ava',
50-
' ava test.js test2.js',
51-
' ava test-*.js',
52-
' ava test',
53-
' ava --init',
54-
' ava --init foo.js',
55-
'',
56-
'Default patterns when no arguments:',
57-
'test.js test-*.js test/**/*.js **/__tests__/**/*.js **/*.test.js'
58-
], {
23+
exports.run = () => {
24+
const conf = pkgConf.sync('ava');
25+
26+
const filepath = pkgConf.filepath(conf);
27+
const pkgDir = filepath === null ? process.cwd() : path.dirname(filepath);
28+
29+
const cli = meow(`
30+
Usage
31+
ava [<file|directory|glob> ...]
32+
33+
Options
34+
--init Add AVA to your project
35+
--fail-fast Stop after first test failure
36+
--serial, -s Run tests serially
37+
--tap, -t Generate TAP output
38+
--verbose, -v Enable verbose output
39+
--no-cache Disable the transpiler cache
40+
--no-power-assert Disable Power Assert
41+
--match, -m Only run tests with matching title (Can be repeated)
42+
--watch, -w Re-run tests when tests and source files change
43+
--source, -S Pattern to match source files so tests can be re-run (Can be repeated)
44+
--timeout, -T Set global timeout
45+
--concurrency, -c Maximum number of test files running at the same time (EXPERIMENTAL)
46+
--update-snapshots, -u Update snapshots
47+
48+
Examples
49+
ava
50+
ava test.js test2.js
51+
ava test-*.js
52+
ava test
53+
ava --init
54+
ava --init foo.js
55+
56+
Default patterns when no arguments:
57+
test.js test-*.js test/**/*.js **/__tests__/**/*.js **/*.test.js'
58+
`, {
5959
string: [
6060
'_',
6161
'timeout',
@@ -103,7 +103,7 @@ exports.run = function () {
103103
throw new Error(colors.error(figures.cross) + ' The --require and -r flags are deprecated. Requirements should be configured in package.json - see documentation.');
104104
}
105105

106-
var api = new Api({
106+
const api = new Api({
107107
failFast: cli.flags.failFast,
108108
serial: cli.flags.serial,
109109
require: arrify(conf.require),
@@ -113,13 +113,13 @@ exports.run = function () {
113113
match: arrify(cli.flags.match),
114114
babelConfig: babelConfig.validate(conf.babel),
115115
resolveTestsFrom: cli.input.length === 0 ? pkgDir : process.cwd(),
116-
pkgDir: pkgDir,
116+
pkgDir,
117117
timeout: cli.flags.timeout,
118118
concurrency: cli.flags.concurrency ? parseInt(cli.flags.concurrency, 10) : 0,
119119
updateSnapshots: cli.flags.updateSnapshots
120120
});
121121

122-
var reporter;
122+
let reporter;
123123

124124
if (cli.flags.tap && !cli.flags.watch) {
125125
reporter = tapReporter();
@@ -130,11 +130,11 @@ exports.run = function () {
130130
}
131131

132132
reporter.api = api;
133-
var logger = new Logger(reporter);
133+
const logger = new Logger(reporter);
134134

135135
logger.start();
136136

137-
api.on('test-run', function (runStatus) {
137+
api.on('test-run', runStatus => {
138138
reporter.api = runStatus;
139139
runStatus.on('test', logger.test);
140140
runStatus.on('error', logger.unhandledError);
@@ -143,32 +143,32 @@ exports.run = function () {
143143
runStatus.on('stderr', logger.stderr);
144144
});
145145

146-
var files = cli.input.length ? cli.input : arrify(conf.files);
146+
const files = cli.input.length ? cli.input : arrify(conf.files);
147147

148148
if (cli.flags.watch) {
149149
try {
150-
var watcher = new Watcher(logger, api, files, arrify(cli.flags.source));
150+
const watcher = new Watcher(logger, api, files, arrify(cli.flags.source));
151151
watcher.observeStdin(process.stdin);
152152
} catch (err) {
153153
if (err.name === 'AvaError') {
154154
// An AvaError may be thrown if chokidar is not installed. Log it nicely.
155-
console.error(' ' + colors.error(figures.cross) + ' ' + err.message);
155+
console.error(` ${colors.error(figures.cross)} ${err.message}`);
156156
logger.exit(1);
157157
} else {
158-
// Rethrow so it becomes an uncaught exception.
158+
// Rethrow so it becomes an uncaught exception
159159
throw err;
160160
}
161161
}
162162
} else {
163163
api.run(files)
164-
.then(function (runStatus) {
164+
.then(runStatus => {
165165
logger.finish(runStatus);
166166
logger.exit(runStatus.failCount > 0 || runStatus.rejectionCount > 0 || runStatus.exceptionCount > 0 ? 1 : 0);
167167
})
168-
.catch(function (err) {
168+
.catch(err => {
169169
// Don't swallow exceptions. Note that any expected error should already
170170
// have been logged.
171-
setImmediate(function () {
171+
setImmediate(() => {
172172
throw err;
173173
});
174174
});

0 commit comments

Comments
 (0)