From 3fc9f92e48425f7b637b3fe5d991ebb7dd9cf054 Mon Sep 17 00:00:00 2001 From: Emanuele Date: Sat, 10 Mar 2018 00:25:23 +0000 Subject: [PATCH] chore(es6): changed 'var' into 'const' where convenient (#325) --- lib/generate-loader/index.js | 8 ++++---- lib/generate-plugin/index.js | 8 ++++---- lib/generators/loader-generator.js | 8 ++++---- lib/generators/loader-generator.test.js | 6 +++--- lib/generators/plugin-generator.js | 8 ++++---- lib/generators/webpack-generator.js | 12 ++++++------ .../__snapshots__/top-scope.test.js.snap | 2 +- .../transformations/top-scope/top-scope.test.js | 2 +- lib/migrate/__testfixtures__/failing.js | 8 ++++---- lib/utils/ast-utils.js | 2 +- lib/utils/ast-utils.test.js | 16 ++++++++-------- lib/utils/copy-utils.js | 14 +++++++------- lib/utils/hashtable.js | 4 ++-- 13 files changed, 49 insertions(+), 49 deletions(-) diff --git a/lib/generate-loader/index.js b/lib/generate-loader/index.js index 58e582ccfff..99b4280b968 100644 --- a/lib/generate-loader/index.js +++ b/lib/generate-loader/index.js @@ -1,13 +1,13 @@ -var yeoman = require("yeoman-environment"); -var LoaderGenerator = require("../generators/loader-generator").LoaderGenerator; +const yeoman = require("yeoman-environment"); +const { LoaderGenerator } = require("../generators/loader-generator"); /** * Runs a yeoman generator to create a new webpack loader project * @returns {void} */ function loaderCreator() { - var env = yeoman.createEnv(); - var generatorName = "webpack-loader-generator"; + const env = yeoman.createEnv(); + const generatorName = "webpack-loader-generator"; env.registerStub(LoaderGenerator, generatorName); diff --git a/lib/generate-plugin/index.js b/lib/generate-plugin/index.js index 35f9483138e..a6b05c02206 100644 --- a/lib/generate-plugin/index.js +++ b/lib/generate-plugin/index.js @@ -1,13 +1,13 @@ -var yeoman = require("yeoman-environment"); -var PluginGenerator = require("../generators/plugin-generator").PluginGenerator; +const yeoman = require("yeoman-environment"); +const PluginGenerator = require("../generators/plugin-generator").PluginGenerator; /** * Runs a yeoman generator to create a new webpack plugin project * @returns {void} */ function pluginCreator() { - var env = yeoman.createEnv(); - var generatorName = "webpack-plugin-generator"; + const env = yeoman.createEnv(); + const generatorName = "webpack-plugin-generator"; env.registerStub(PluginGenerator, generatorName); diff --git a/lib/generators/loader-generator.js b/lib/generators/loader-generator.js index f4bb7a45355..3586bf4fe3c 100644 --- a/lib/generators/loader-generator.js +++ b/lib/generators/loader-generator.js @@ -1,6 +1,6 @@ -var path = require("path"); -var _ = require("lodash"); -var webpackGenerator = require("./webpack-generator"); +const path = require("path"); +const _ = require("lodash"); +const webpackGenerator = require("./webpack-generator"); /** * Formats a string into webpack loader format @@ -25,7 +25,7 @@ function makeLoaderName(name) { * @class LoaderGenerator * @extends {Generator} */ -var LoaderGenerator = webpackGenerator( +const LoaderGenerator = webpackGenerator( [ { type: "input", diff --git a/lib/generators/loader-generator.test.js b/lib/generators/loader-generator.test.js index 232013598ed..c98660dd7d5 100644 --- a/lib/generators/loader-generator.test.js +++ b/lib/generators/loader-generator.test.js @@ -1,15 +1,15 @@ "use strict"; -var makeLoaderName = require("./loader-generator").makeLoaderName; +const makeLoaderName = require("./loader-generator").makeLoaderName; describe("makeLoaderName", () => { it("should kebab-case loader name and append '-loader'", () => { - var loaderName = makeLoaderName("This is a test"); + const loaderName = makeLoaderName("This is a test"); expect(loaderName).toEqual("this-is-a-test-loader"); }); it("should not modify a properly formatted loader name", () => { - var loaderName = makeLoaderName("properly-named-loader"); + const loaderName = makeLoaderName("properly-named-loader"); expect(loaderName).toEqual("properly-named-loader"); }); }); diff --git a/lib/generators/plugin-generator.js b/lib/generators/plugin-generator.js index 1d8a09e1e45..92e495766c8 100644 --- a/lib/generators/plugin-generator.js +++ b/lib/generators/plugin-generator.js @@ -1,6 +1,6 @@ -var path = require("path"); -var _ = require("lodash"); -var webpackGenerator = require("./webpack-generator"); +const path = require("path"); +const _ = require("lodash"); +const webpackGenerator = require("./webpack-generator"); /** * A yeoman generator class for creating a webpack @@ -10,7 +10,7 @@ var webpackGenerator = require("./webpack-generator"); * @class PluginGenerator * @extends {Generator} */ -var PluginGenerator = webpackGenerator( +const PluginGenerator = webpackGenerator( [ { type: "input", diff --git a/lib/generators/webpack-generator.js b/lib/generators/webpack-generator.js index 561f10931f7..8d2ab7f3c8e 100644 --- a/lib/generators/webpack-generator.js +++ b/lib/generators/webpack-generator.js @@ -1,7 +1,7 @@ -var path = require("path"); -var mkdirp = require("mkdirp"); -var Generator = require("yeoman-generator"); -var copyUtils = require("../utils/copy-utils"); +const path = require("path"); +const mkdirp = require("mkdirp"); +const Generator = require("yeoman-generator"); +const copyUtils = require("../utils/copy-utils"); /** * Creates a Yeoman Generator that generates a project conforming @@ -40,14 +40,14 @@ function webpackGenerator( } default() { - var currentDirName = path.basename(this.destinationPath()); + const currentDirName = path.basename(this.destinationPath()); if (currentDirName !== this.props.name) { this.log(` Your project must be inside a folder named ${this.props.name} I will create this folder for you. `); mkdirp(this.props.name); - var pathToProjectDir = this.destinationPath(this.props.name); + const pathToProjectDir = this.destinationPath(this.props.name); this.destinationRoot(pathToProjectDir); } } diff --git a/lib/init/transformations/top-scope/__snapshots__/top-scope.test.js.snap b/lib/init/transformations/top-scope/__snapshots__/top-scope.test.js.snap index 1030caa1998..dd59a02c372 100644 --- a/lib/init/transformations/top-scope/__snapshots__/top-scope.test.js.snap +++ b/lib/init/transformations/top-scope/__snapshots__/top-scope.test.js.snap @@ -1,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`top-scope transforms correctly using "top-scope-0" data 1`] = ` -"var test = 'me'; +"const test = 'me'; module.exports = {} " `; diff --git a/lib/init/transformations/top-scope/top-scope.test.js b/lib/init/transformations/top-scope/top-scope.test.js index 6522ee5e4c3..559a40e4e28 100644 --- a/lib/init/transformations/top-scope/top-scope.test.js +++ b/lib/init/transformations/top-scope/top-scope.test.js @@ -2,7 +2,7 @@ const defineTest = require("../../../utils/defineTest"); -defineTest(__dirname, "top-scope", "top-scope-0", ["var test = 'me';"], "init"); +defineTest(__dirname, "top-scope", "top-scope-0", ["const test = 'me';"], "init"); defineTest( __dirname, "top-scope", diff --git a/lib/migrate/__testfixtures__/failing.js b/lib/migrate/__testfixtures__/failing.js index a52f3ffe703..b01df5fa5e2 100644 --- a/lib/migrate/__testfixtures__/failing.js +++ b/lib/migrate/__testfixtures__/failing.js @@ -1,8 +1,8 @@ -var webpack = require("webpack"); -var nodeEnvironment = process.env.NODE_ENV; -var _ = require("lodash"); +const webpack = require("webpack"); +const nodeEnvironment = process.env.NODE_ENV; +const _ = require("lodash"); -var config = { +const config = { entry: { lib: "./app/index.js", email: "./app/email.js" diff --git a/lib/utils/ast-utils.js b/lib/utils/ast-utils.js index 626ec1810e4..8075c354b5c 100644 --- a/lib/utils/ast-utils.js +++ b/lib/utils/ast-utils.js @@ -234,7 +234,7 @@ function createOrUpdatePluginByName(j, rootNodePath, pluginName, options) { * @param {any} j — jscodeshift API * @param {Node} rootNode - `plugins: []` Root Node. See https://github.com/facebook/jscodeshift/wiki/jscodeshift-Documentation#nodepaths * @param {String} pluginPackageName - ex. `extract-text-plugin` - * @returns {String} variable name - ex. 'var s = require(s) gives "s"` + * @returns {String} variable name - ex. 'const s = require(s) gives "s"` */ function findVariableToPlugin(j, rootNode, pluginPackageName) { diff --git a/lib/utils/ast-utils.test.js b/lib/utils/ast-utils.test.js index 99f907b7432..7dfdf7eeeb9 100644 --- a/lib/utils/ast-utils.test.js +++ b/lib/utils/ast-utils.test.js @@ -70,7 +70,7 @@ describe("utils", () => { describe("findRootNodesByName", () => { it("should find plugins: [] nodes", () => { const ast = j(` -var a = { plugins: [], foo: { plugins: [] } } +const a = { plugins: [], foo: { plugins: [] } } `); const res = utils.findRootNodesByName(j, ast, "plugins"); expect(res.size()).toEqual(2); @@ -78,7 +78,7 @@ var a = { plugins: [], foo: { plugins: [] } } it("should not find plugins: [] nodes", () => { const ast = j(` -var a = { plugs: [] } +const a = { plugs: [] } `); const res = utils.findRootNodesByName(j, ast, "plugins"); expect(res.size()).toEqual(0); @@ -132,12 +132,12 @@ var a = { plugs: [] } describe("findVariableToPlugin", () => { it("should find the variable name of a plugin", () => { const ast = j(` - var packageName = require('package-name'); - var someOtherVar = somethingElse; - var otherPackage = require('other-package'); + const packageName = require('package-name'); + const someOtherconst = somethingElse; + const otherPackage = require('other-package'); `); - const foundVar = utils.findVariableToPlugin(j, ast, "other-package"); - expect(foundVar).toEqual("otherPackage"); + const found = utils.findVariableToPlugin(j, ast, "other-package"); + expect(found).toEqual("otherPackage"); }); }); @@ -166,7 +166,7 @@ var a = { plugs: [] } describe("findObjWithOneOfKeys", () => { it("should find keys", () => { const ast = j(` - var ab = { + const ab = { a: 1, b: 2 } diff --git a/lib/utils/copy-utils.js b/lib/utils/copy-utils.js index d161d38943d..8a673925c51 100644 --- a/lib/utils/copy-utils.js +++ b/lib/utils/copy-utils.js @@ -1,4 +1,4 @@ -var path = require("path"); +const path = require("path"); /** * Takes in a file path in the `./templates` directory. Copies that @@ -8,13 +8,13 @@ var path = require("path"); * @param {string} templateDir Absolute path to template directory * @returns {Function} A curried function that takes a file path and copies it */ -var generatorCopy = ( +const generatorCopy = ( generator, templateDir ) => /** @param {string} filePath */ filePath => { - var sourceParts = templateDir.split(path.delimiter); + const sourceParts = templateDir.split(path.delimiter); sourceParts.push.apply(sourceParts, filePath.split("/")); - var targetParts = path.dirname(filePath).split("/"); + const targetParts = path.dirname(filePath).split("/"); targetParts.push(path.basename(filePath, ".tpl")); generator.fs.copy( @@ -34,14 +34,14 @@ var generatorCopy = ( * the template files. * @returns {Function} A curried function that takes a file path and copies it */ -var generatorCopyTpl = ( +const generatorCopyTpl = ( generator, templateDir, templateData ) => /** @param {string} filePath */ filePath => { - var sourceParts = templateDir.split(path.delimiter); + const sourceParts = templateDir.split(path.delimiter); sourceParts.push.apply(sourceParts, filePath.split("/")); - var targetParts = path.dirname(filePath).split("/"); + const targetParts = path.dirname(filePath).split("/"); targetParts.push(path.basename(filePath, ".tpl").slice(1)); generator.fs.copyTpl( diff --git a/lib/utils/hashtable.js b/lib/utils/hashtable.js index 6b159632cb7..2073b3b935e 100644 --- a/lib/utils/hashtable.js +++ b/lib/utils/hashtable.js @@ -6,11 +6,11 @@ * @returns {Array} A sorted array with removed dupe elements */ module.exports = function hashtable(a) { - var prims = { boolean: {}, number: {}, string: {} }, + const prims = { boolean: {}, number: {}, string: {} }, objs = []; return a.filter(function(item) { - var type = typeof item; + const type = typeof item; if (type in prims) return prims[type].hasOwnProperty(item) ? false