From 6e4f8cea36faf553d2ccfcff29f2ff57702fe1b3 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Tue, 28 Jun 2016 11:03:34 -0700 Subject: [PATCH] Chore: First ES6 refactoring (refs #6407) First steps in taking advantage of ES6 features: 1. Remove es6-map dependency (no longer needed) 2. Use babelify to create browser bundle 3. Refactor a couple files into ES6 classes --- .eslintrc.yml | 4 +++ Makefile.js | 6 ++-- lib/config/config-ops.js | 44 +++++++++++------------ lib/config/environments.js | 30 ++++++++-------- lib/rules/no-unmodified-loop-condition.js | 3 +- lib/rules/prefer-const.js | 1 - package.json | 6 +++- 7 files changed, 51 insertions(+), 43 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index 84f6d55539c..0486c031a8f 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -2,6 +2,10 @@ root: true env: node: true + es6: true + +parserOptions: + ecmaVersion: 6 extends: "./packages/eslint-config-eslint/default.yml" diff --git a/Makefile.js b/Makefile.js index 64082c5076f..1defd3d98dd 100644 --- a/Makefile.js +++ b/Makefile.js @@ -688,13 +688,13 @@ target.browserify = function() { generateRulesIndex(TEMP_DIR); // 5. browserify the temp directory - nodeCLI.exec("browserify", "-x espree", TEMP_DIR + "eslint.js", "-o", BUILD_DIR + "eslint.js", "-s eslint"); + nodeCLI.exec("browserify", "-x espree", TEMP_DIR + "eslint.js", "-o", BUILD_DIR + "eslint.js", "-s eslint", "-t [ babelify --presets [ es2015 ] --plugins [ transform-runtime ] ]"); // 6. Browserify espree nodeCLI.exec("browserify", "-r espree", "-o", TEMP_DIR + "espree.js"); - // 7. Concatenate the two files together - cat(TEMP_DIR + "espree.js", BUILD_DIR + "eslint.js").to(BUILD_DIR + "eslint.js"); + // 7. Concatenate Babel polyfill, Espree, and ESLint files together + cat("./node_modules/babel-polyfill/dist/polyfill.js", TEMP_DIR + "espree.js", BUILD_DIR + "eslint.js").to(BUILD_DIR + "eslint.js"); // 8. remove temp directory rm("-r", TEMP_DIR); diff --git a/lib/config/config-ops.js b/lib/config/config-ops.js index d62169502b7..46960332faa 100644 --- a/lib/config/config-ops.js +++ b/lib/config/config-ops.js @@ -30,20 +30,20 @@ var RULE_SEVERITY_STRINGS = ["off", "warn", "error"], // Public Interface //------------------------------------------------------------------------------ -module.exports = { +module.exports = class ConfigOps { /** * Creates an empty configuration object suitable for merging as a base. * @returns {Object} A configuration object. */ - createEmptyConfig: function() { + static createEmptyConfig() { return { globals: {}, env: {}, rules: {}, parserOptions: {} }; - }, + } /** * Creates an environment config based on the specified environments. @@ -51,7 +51,7 @@ module.exports = { * @returns {Object} A configuration object with the appropriate rules and globals * set. */ - createEnvironmentConfig: function(env) { + static createEnvironmentConfig(env) { var envConfig = this.createEmptyConfig(); @@ -78,7 +78,7 @@ module.exports = { } return envConfig; - }, + } /** * Given a config with environment settings, applies the globals and @@ -86,14 +86,14 @@ module.exports = { * @param {Object} config The configuration information. * @returns {Object} The updated configuration information. */ - applyEnvironments: function(config) { + static applyEnvironments(config) { if (config.env && typeof config.env === "object") { debug("Apply environment settings to config"); return this.merge(this.createEnvironmentConfig(config.env), config); } return config; - }, + } /** * Merges two config objects. This will not only add missing keys, but will also modify values to match. @@ -103,7 +103,7 @@ module.exports = { * @param {boolean} [isRule] Whether its a rule * @returns {Object} merged config object. */ - merge: function deepmerge(target, src, combine, isRule) { + static merge(target, src, combine, isRule) { /* The MIT License (MIT) @@ -159,7 +159,7 @@ module.exports = { if (isRule) { dst[i] = e; } else { - dst[i] = deepmerge(target[i], e, combine, isRule); + dst[i] = ConfigOps.merge(target[i], e, combine, isRule); } } else { if (!combine) { @@ -179,17 +179,17 @@ module.exports = { } Object.keys(src).forEach(function(key) { if (Array.isArray(src[key]) || Array.isArray(target[key])) { - dst[key] = deepmerge(target[key], src[key], key === "plugins", isRule); + dst[key] = ConfigOps.merge(target[key], src[key], key === "plugins", isRule); } else if (typeof src[key] !== "object" || !src[key] || key === "exported" || key === "astGlobals") { dst[key] = src[key]; } else { - dst[key] = deepmerge(target[key] || {}, src[key], combine, key === "rules"); + dst[key] = ConfigOps.merge(target[key] || {}, src[key], combine, key === "rules"); } }); } return dst; - }, + } /** * Converts new-style severity settings (off, warn, error) into old-style @@ -198,7 +198,7 @@ module.exports = { * @param {Object} config The config object to normalize. * @returns {void} */ - normalize: function(config) { + static normalize(config) { if (config.rules) { Object.keys(config.rules).forEach(function(ruleId) { @@ -211,7 +211,7 @@ module.exports = { } }); } - }, + } /** * Converts old-style severity settings (0, 1, 2) into new-style @@ -220,7 +220,7 @@ module.exports = { * @param {Object} config The config object to normalize. * @returns {void} */ - normalizeToStrings: function(config) { + static normalizeToStrings(config) { if (config.rules) { Object.keys(config.rules).forEach(function(ruleId) { @@ -233,14 +233,14 @@ module.exports = { } }); } - }, + } /** * Determines if the severity for the given rule configuration represents an error. * @param {int|string|Array} ruleConfig The configuration for an individual rule. * @returns {boolean} True if the rule represents an error, false if not. */ - isErrorSeverity: function(ruleConfig) { + static isErrorSeverity(ruleConfig) { var severity = Array.isArray(ruleConfig) ? ruleConfig[0] : ruleConfig; @@ -249,28 +249,28 @@ module.exports = { } return (typeof severity === "number" && severity === 2); - }, + } /** * Checks whether a given config has valid severity or not. * @param {number|string|Array} ruleConfig - The configuration for an individual rule. * @returns {boolean} `true` if the configuration has valid severity. */ - isValidSeverity: function(ruleConfig) { + static isValidSeverity(ruleConfig) { var severity = Array.isArray(ruleConfig) ? ruleConfig[0] : ruleConfig; if (typeof severity === "string") { severity = severity.toLowerCase(); } return VALID_SEVERITIES.indexOf(severity) !== -1; - }, + } /** * Checks whether every rule of a given config has valid severity or not. - * @param {object} config - The configuration for rules. + * @param {Object} config - The configuration for rules. * @returns {boolean} `true` if the configuration has valid severity. */ - isEverySeverityValid: function(config) { + static isEverySeverityValid(config) { return Object.keys(config).every(function(ruleId) { return this.isValidSeverity(config[ruleId]); }, this); diff --git a/lib/config/environments.js b/lib/config/environments.js index 8daef864e31..ca25162a503 100644 --- a/lib/config/environments.js +++ b/lib/config/environments.js @@ -14,7 +14,7 @@ var envs = require("../../conf/environments"); // Private //------------------------------------------------------------------------------ -var environments = Object.create(null); +var environments = new Map(); /** * Loads the default environments. @@ -23,7 +23,7 @@ var environments = Object.create(null); */ function load() { Object.keys(envs).forEach(function(envName) { - environments[envName] = envs[envName]; + environments.set(envName, envs[envName]); }); } @@ -34,18 +34,20 @@ load(); // Public Interface //------------------------------------------------------------------------------ -module.exports = { +module.exports = class Environments { - load: load, + static load() { + load(); + } /** * Gets the environment with the given name. * @param {string} name The name of the environment to retrieve. * @returns {Object?} The environment object or null if not found. */ - get: function(name) { - return environments[name] || null; - }, + static get(name) { + return environments.get(name) || null; + } /** * Defines an environment. @@ -53,9 +55,9 @@ module.exports = { * @param {Object} env The environment settings. * @returns {void} */ - define: function(name, env) { - environments[name] = env; - }, + static define(name, env) { + environments.set(name, env); + } /** * Imports all environments from a plugin. @@ -63,20 +65,20 @@ module.exports = { * @param {string} pluginName The name of the plugin. * @returns {void} */ - importPlugin: function(plugin, pluginName) { + static importPlugin(plugin, pluginName) { if (plugin.environments) { Object.keys(plugin.environments).forEach(function(envName) { this.define(pluginName + "/" + envName, plugin.environments[envName]); }, this); } - }, + } /** * Resets all environments. Only use for tests! * @returns {void} */ - testReset: function() { - environments = Object.create(null); + static testReset() { + environments = new Map(); load(); } }; diff --git a/lib/rules/no-unmodified-loop-condition.js b/lib/rules/no-unmodified-loop-condition.js index ed49b5996ef..efb65434836 100644 --- a/lib/rules/no-unmodified-loop-condition.js +++ b/lib/rules/no-unmodified-loop-condition.js @@ -9,8 +9,7 @@ // Requirements //------------------------------------------------------------------------------ -var Map = require("es6-map"), - Traverser = require("../util/traverser"), +var Traverser = require("../util/traverser"), astUtils = require("../ast-utils"); //------------------------------------------------------------------------------ diff --git a/lib/rules/prefer-const.js b/lib/rules/prefer-const.js index 7b8ac425193..121021118c4 100644 --- a/lib/rules/prefer-const.js +++ b/lib/rules/prefer-const.js @@ -9,7 +9,6 @@ // Requirements //------------------------------------------------------------------------------ -var Map = require("es6-map"); var lodash = require("lodash"); //------------------------------------------------------------------------------ diff --git a/package.json b/package.json index abe440dcd62..9a7a2771859 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,6 @@ "concat-stream": "^1.4.6", "debug": "^2.1.1", "doctrine": "^1.2.2", - "es6-map": "^0.1.3", "escope": "^3.6.0", "espree": "^3.1.6", "estraverse": "^4.2.0", @@ -71,6 +70,11 @@ "user-home": "^2.0.0" }, "devDependencies": { + "babel-plugin-transform-runtime": "^6.9.0", + "babel-polyfill": "^6.9.1", + "babel-preset-es2015": "^6.9.0", + "babel-runtime": "^6.9.2", + "babelify": "^7.3.0", "beefy": "^2.0.0", "brfs": "0.0.9", "browserify": "^12.0.1",