Repository: webpack/webpack
Pull request #6512 (2018-02-17 11:12:21)

refactor(es6): multiline string, arr.includes, arrow functions in ./l…
…ib and ./benchmark

<!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. -->

**What kind of change does this PR introduce?**
Small refactoring of the codebase to get in sync with the rest of the code of the repo

<!-- E.g. a bugfix, feature, refactoring, build related change, etc… -->

**Did you add tests for your changes?**
was covered, modified cache plugin test to account for fixed typo in `FS_ACCURENCY`
<!-- Note that we won't merge your changes if you don't add tests -->

**If relevant, link to documentation update:**

<!-- Link PR from webpack/webpack.js.org here, or N/A -->

**Summary**

<!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? -->
<!-- Try to link to an open issue for more information. -->

**Does this PR introduce a breaking change?**
Dont think so
<!-- If this PR introduces a breaking change, please describe the impact and a migration path for existing applications. -->

**Other information**



Patch:
diff --git a/benchmark/benchmark.js b/benchmark/benchmark.js
index 49240b4c1..03adf58a4 100644
--- a/benchmark/benchmark.js
+++ b/benchmark/benchmark.js
@@ -15,7 +15,7 @@ const benchmarkOptions = {
 
 function runTimes(compiler, times, deferred) {
 	fs.writeFileSync(path.join(fixtures, "0.js"), "module.exports = " + Math.random(), "utf-8");
-	compiler.run((err, stats) => {
+	compiler.run(err => {
 		if(err) throw err;
 		if(times === 1)
 			deferred.resolve();
@@ -30,12 +30,12 @@ const tests = {
 		(size, deferred) => {
 			webpack({
 				context: fixtures,
-				entry: "./" + size + ".js",
+				entry: `./${size}.js`,
 				output: {
 					path: outputPath,
 					filename: "bundle.js"
 				}
-			}, (err, stats) => {
+			}, err => {
 				if(err) throw err;
 				deferred.resolve();
 			});
@@ -46,16 +46,16 @@ const tests = {
 		(size, deferred) => {
 			webpack({
 				context: fixtures,
-				entry: "./" + size + ".big.js",
+				entry: `./${size}.big.js`,
 				output: {
 					path: outputPath,
 					filename: "bundle.js"
 				},
 				devtool: "eval"
-			}, (err, stats) => {
+			}, err => {
 				if(err) throw err;
 				deferred.resolve();
-			})
+			});
 		}
 	],
 	"sourcemap build": [
@@ -63,16 +63,16 @@ const tests = {
 		(size, deferred) => {
 			webpack({
 				context: fixtures,
-				entry: "./" + size + ".big.js",
+				entry: `./${size}.big.js`,
 				output: {
 					path: outputPath,
 					filename: "bundle.js"
 				},
 				devtool: "source-map"
-			}, (err, stats) => {
+			}, err => {
 				if(err) throw err;
 				deferred.resolve();
-			})
+			});
 		}
 	],
 	"cheap sourcemap build": [
@@ -80,16 +80,16 @@ const tests = {
 		(size, deferred) => {
 			webpack({
 				context: fixtures,
-				entry: "./" + size + ".big.js",
+				entry: `./${size}.big.js`,
 				output: {
 					path: outputPath,
 					filename: "bundle.js"
 				},
 				devtool: "cheap-source-map"
-			}, function(err, stats) {
+			}, err => {
 				if(err) throw err;
 				deferred.resolve();
-			})
+			});
 		}
 	],
 	"build w/ chunks": [
@@ -97,15 +97,15 @@ const tests = {
 		(size, deferred) => {
 			webpack({
 				context: fixtures,
-				entry: "./" + size + ".async.js",
+				entry: `./${size}.async.js`,
 				output: {
 					path: outputPath,
 					filename: "bundle.js"
 				}
-			}, function(err, stats) {
+			}, err => {
 				if(err) throw err;
 				deferred.resolve();
-			})
+			});
 		}
 	],
 	"build w/ chunks": [
@@ -118,10 +118,10 @@ const tests = {
 					path: outputPath,
 					filename: "bundle.js"
 				}
-			}, function(err, stats) {
+			}, err => {
 				if(err) throw err;
 				deferred.resolve();
-			})
+			});
 		}
 	],
 	"incremental": [
@@ -161,7 +161,7 @@ const tests = {
 			var compiler = webpack({
 				cache: true,
 				context: fixtures,
-				entry: "./" + size + ".js",
+				entry: `./${size}.js`,
 				output: {
 					path: outputPath,
 					filename: "bundle.js"
@@ -176,7 +176,7 @@ const tests = {
 			var compiler = webpack({
 				cache: true,
 				context: fixtures,
-				entry: "./" + size + ".js",
+				entry: `./${size}.js`,
 				output: {
 					path: outputPath,
 					filename: "bundle.js"
@@ -191,7 +191,7 @@ const tests = {
 			var compiler = webpack({
 				cache: true,
 				context: fixtures,
-				entry: "./" + size + ".js",
+				entry: `./${size}.js`,
 				output: {
 					path: outputPath,
 					filename: "bundle.js"
@@ -204,17 +204,17 @@ const tests = {
 
 const suite = new Benchmark.Suite;
 
-Object.keys(tests).filter((name) => (process.argv.length > 2) ? name.indexOf(process.argv[2]) >= 0 : true)
-	.forEach((name) => {
+Object.keys(tests).filter(name => process.argv.length > 2 ? name.includes(process.argv[2]) : true)
+	.forEach(name => {
 		const test = tests[name];
-		test[0].forEach((size) => {
-			suite.add(name + " " + size, (deferred) => {
+		test[0].forEach(size => {
+			suite.add(`${name} ${size}`, deferred => {
 				test[1](size, deferred);
 			}, benchmarkOptions);
 		});
 	});
 
-suite.on("cycle", (event) => {
+suite.on("cycle", event => {
 	process.stderr.write("\n");
 	const b = event.target;
 	console.log(b.name + "\t" + Math.floor(1000 * (b.stats.mean - b.stats.moe)) + "\t" + Math.floor(1000 * (b.stats.mean + b.stats.moe)));
diff --git a/benchmark/createFixtures.js b/benchmark/createFixtures.js
index 9b9855e47..06e566ac4 100644
--- a/benchmark/createFixtures.js
+++ b/benchmark/createFixtures.js
@@ -7,16 +7,21 @@ try {
 	fs.mkdirSync(fixtures);
 } catch(e) {}
 
+function generateRequireString(conditional, suffix) {
+	const prefixedSuffix = suffix ? `.${suffix}` : "";
+	return `require(${JSON.stringify(`./${conditional}${prefixedSuffix}.js`)});`;
+}
+
 for(let i = 0; i < 10000; i++) {
 	const source = [];
 	if(i > 8)
-		source.push("require(" + JSON.stringify("./" + (i / 8 | 0) + ".js") + ");");
+		source.push(generateRequireString((i / 8 | 0)));
 	if(i > 4)
-		source.push("require(" + JSON.stringify("./" + (i / 4 | 0) + ".js") + ");");
+		source.push(generateRequireString((i / 4 | 0)));
 	if(i > 2)
-		source.push("require(" + JSON.stringify("./" + (i / 2 | 0) + ".js") + ");");
+		source.push(generateRequireString((i / 2 | 0)));
 	if(i > 0)
-		source.push("require(" + JSON.stringify("./" + (i - 1) + ".js") + ");");
+		source.push(generateRequireString((i - 1)));
 	source.push("module.exports = " + i + ";");
 	fs.writeFileSync(path.join(fixtures, i + ".js"), source.join("\n"), "utf-8");
 }
@@ -25,13 +30,13 @@ for(let i = 0; i < 10000; i++) {
 	const source = [];
 	source.push("require.ensure([], function(require) {");
 	if(i > 8)
-		source.push("require(" + JSON.stringify("./" + (i / 8 | 0) + ".async.js") + ");");
+		source.push(generateRequireString((i / 8 | 0), "async"));
 	if(i > 4)
-		source.push("require(" + JSON.stringify("./" + (i / 4 | 0) + ".async.js") + ");");
+		source.push(generateRequireString((i / 4 | 0), "async"));
 	if(i > 2)
-		source.push("require(" + JSON.stringify("./" + (i / 2 | 0) + ".async.js") + ");");
+		source.push(generateRequireString((i / 2 | 0), "async"));
 	if(i > 0)
-		source.push("require(" + JSON.stringify("./" + (i - 1) + ".async.js") + ");");
+		source.push(generateRequireString((i - 1), "async"));
 	source.push("});");
 	source.push("module.exports = " + i + ";");
 	fs.writeFileSync(path.join(fixtures, i + ".async.js"), source.join("\n"), "utf-8");
@@ -40,13 +45,13 @@ for(let i = 0; i < 10000; i++) {
 for(let i = 0; i < 100; i++) {
 	const source = [];
 	if(i > 8)
-		source.push("require(" + JSON.stringify("./" + (i / 8 | 0) + ".big.js") + ");");
+		source.push(generateRequireString((i / 8 | 0), "big"));
 	if(i > 4)
-		source.push("require(" + JSON.stringify("./" + (i / 4 | 0) + ".big.js") + ");");
+		source.push(generateRequireString((i / 4 | 0), "big"));
 	if(i > 2)
-		source.push("require(" + JSON.stringify("./" + (i / 2 | 0) + ".big.js") + ");");
+		source.push(generateRequireString((i / 2 | 0), "big"));
 	if(i > 0)
-		source.push("require(" + JSON.stringify("./" + (i - 1) + ".big.js") + ");");
+		source.push(generateRequireString((i - 1), "big"));
 	for(let j = 0; j < 300; j++)
 		source.push("if(Math.random())hello.world();test.a.b.c.d();x(1,2,3,4);var a,b,c,d,e,f;");
 	source.push("module.exports = " + i + ";");
diff --git a/lib/CachePlugin.js b/lib/CachePlugin.js
index 161418de8..271d7875d 100644
--- a/lib/CachePlugin.js
+++ b/lib/CachePlugin.js
@@ -9,7 +9,7 @@ const asyncLib = require("async");
 class CachePlugin {
 	constructor(cache) {
 		this.cache = cache || {};
-		this.FS_ACCURENCY = 2000;
+		this.FS_ACCURACY = 2000;
 	}
 
 	apply(compiler) {
@@ -19,7 +19,7 @@ class CachePlugin {
 			});
 		} else {
 			const registerCacheToCompiler = (compiler, cache) => {
-				compiler.hooks.thisCompilation.tap("CachePlugin", (compilation) => {
+				compiler.hooks.thisCompilation.tap("CachePlugin", compilation => {
 					compilation.cache = cache;
 					compilation.hooks.childCompiler.tap("CachePlugin", (childCompiler, compilerName, compilerIndex) => {
 						if(cache) {
@@ -61,13 +61,13 @@ class CachePlugin {
 					if(err) return callback(err);
 
 					for(const [file, ts] of fileTs) {
-						fileTs.set(file, ts + this.FS_ACCURENCY);
+						fileTs.set(file, ts + this.FS_ACCURACY);
 					}
 
 					callback();
 				});
 			});
-			compiler.hooks.afterCompile.tap("CachePlugin", (compilation) => {
+			compiler.hooks.afterCompile.tap("CachePlugin", compilation => {
 				compilation.compiler._lastCompilationFileDependencies = compilation.fileDependencies;
 				compilation.compiler._lastCompilationContextDependencies = compilation.contextDependencies;
 			});
@@ -76,14 +76,14 @@ class CachePlugin {
 
 	/* istanbul ignore next */
 	applyMtime(mtime) {
-		if(this.FS_ACCURENCY > 1 && mtime % 2 !== 0)
-			this.FS_ACCURENCY = 1;
-		else if(this.FS_ACCURENCY > 10 && mtime % 20 !== 0)
-			this.FS_ACCURENCY = 10;
-		else if(this.FS_ACCURENCY > 100 && mtime % 200 !== 0)
-			this.FS_ACCURENCY = 100;
-		else if(this.FS_ACCURENCY > 1000 && mtime % 2000 !== 0)
-			this.FS_ACCURENCY = 1000;
+		if(this.FS_ACCURACY > 1 && mtime % 2 !== 0)
+			this.FS_ACCURACY = 1;
+		else if(this.FS_ACCURACY > 10 && mtime % 20 !== 0)
+			this.FS_ACCURACY = 10;
+		else if(this.FS_ACCURACY > 100 && mtime % 200 !== 0)
+			this.FS_ACCURACY = 100;
+		else if(this.FS_ACCURACY > 1000 && mtime % 2000 !== 0)
+			this.FS_ACCURACY = 1000;
 	}
 }
 module.exports = CachePlugin;
diff --git a/lib/CaseSensitiveModulesWarning.js b/lib/CaseSensitiveModulesWarning.js
index e62e211c9..132fcb714 100644
--- a/lib/CaseSensitiveModulesWarning.js
+++ b/lib/CaseSensitiveModulesWarning.js
@@ -13,9 +13,11 @@ module.exports = class CaseSensitiveModulesWarning extends WebpackError {
 		this.name = "CaseSensitiveModulesWarning";
 		const sortedModules = this._sort(modules);
 		const modulesList = this._moduleMessages(sortedModules);
-		this.message = "There are multiple modules with names that only differ in casing.\n" +
-			"This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.\n" +
-			`Use equal casing. Compare these module identifiers:\n${modulesList}`;
+		this.message = `There are multiple modules with names that only differ in casing.
+This can lead to unexpected behavior when compiling on a filesystem with other case-semantic.
+Use equal casing. Compare these module identifiers:
+${modulesList}`;
+
 		this.origin = this.module = sortedModules[0];
 
 		Error.captureStackTrace(this, this.constructor);
diff --git a/lib/Chunk.js b/lib/Chunk.js
index 168fc8d56..b7086f3ff 100644
--- a/lib/Chunk.js
+++ b/lib/Chunk.js
@@ -8,6 +8,8 @@ const util = require("util");
 const SortableSet = require("./util/SortableSet");
 const GraphHelpers = require("./GraphHelpers");
 let debugId = 1000;
+const ERR_CHUNK_ENTRY = "Chunk.entry was removed. Use hasRuntime()";
+const ERR_CHUNK_INITIAL = "Chunk.initial was removed. Use canBeInitial/isOnlyInitial()";
 
 const sortById = (a, b) => {
 	if(a.id < b.id) return -1;
@@ -59,19 +61,19 @@ class Chunk {
 	}
 
 	get entry() {
-		throw new Error("Chunk.entry was removed. Use hasRuntime()");
+		throw new Error(ERR_CHUNK_ENTRY);
 	}
 
 	set entry(data) {
-		throw new Error("Chunk.entry was removed. Use hasRuntime()");
+		throw new Error(ERR_CHUNK_ENTRY);
 	}
 
 	get initial() {
-		throw new Error("Chunk.initial was removed. Use canBeInitial/isOnlyInitial()");
+		throw new Error(ERR_CHUNK_INITIAL);
 	}
 
 	set initial(data) {
-		throw new Error("Chunk.initial was removed. Use canBeInitial/isOnlyInitial()");
+		throw new Error(ERR_CHUNK_INITIAL);
 	}
 
 	hasRuntime() {


Test patch:
diff --git a/test/CachePlugin.unittest.js b/test/CachePlugin.unittest.js
index b8ce6e9a9..a91ddf473 100644
--- a/test/CachePlugin.unittest.js
+++ b/test/CachePlugin.unittest.js
@@ -20,22 +20,22 @@ describe("CachePlugin", () => {
 
 		it("sets file system accuracy to 1 for granular modification timestamp", () => {
 			env.plugin.applyMtime(1483819067001);
-			env.plugin.FS_ACCURENCY.should.be.exactly(1);
+			env.plugin.FS_ACCURACY.should.be.exactly(1);
 		});
 
 		it("sets file system accuracy to 10 for moderately granular modification timestamp", () => {
 			env.plugin.applyMtime(1483819067004);
-			env.plugin.FS_ACCURENCY.should.be.exactly(10);
+			env.plugin.FS_ACCURACY.should.be.exactly(10);
 		});
 
 		it("sets file system accuracy to 100 for moderately coarse modification timestamp", () => {
 			env.plugin.applyMtime(1483819067040);
-			env.plugin.FS_ACCURENCY.should.be.exactly(100);
+			env.plugin.FS_ACCURACY.should.be.exactly(100);
 		});
 
 		it("sets file system accuracy to 1000 for coarse modification timestamp", () => {
 			env.plugin.applyMtime(1483819067400);
-			env.plugin.FS_ACCURENCY.should.be.exactly(1000);
+			env.plugin.FS_ACCURACY.should.be.exactly(1000);
 		});
 	});
 });