Skip to content

Commit

Permalink
Chore: First ES6 refactoring (refs #6407)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nzakas committed Jun 30, 2016
1 parent e2b2030 commit d2bdf4d
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 41 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.yml
Expand Up @@ -2,6 +2,10 @@ root: true

env:
node: true
es6: true

parserOptions:
ecmaVersion: 6

extends:
"./packages/eslint-config-eslint/default.yml"
2 changes: 1 addition & 1 deletion Makefile.js
Expand Up @@ -688,7 +688,7 @@ 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 ] ]");

// 6. Browserify espree
nodeCLI.exec("browserify", "-r espree", "-o", TEMP_DIR + "espree.js");
Expand Down
44 changes: 22 additions & 22 deletions lib/config/config-ops.js
Expand Up @@ -30,28 +30,28 @@ 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.
* @param {Object<string,boolean>} env The environment settings.
* @returns {Object} A configuration object with the appropriate rules and globals
* set.
*/
createEnvironmentConfig: function(env) {
static createEnvironmentConfig(env) {

var envConfig = this.createEmptyConfig();

Expand All @@ -78,22 +78,22 @@ module.exports = {
}

return envConfig;
},
}

/**
* Given a config with environment settings, applies the globals and
* ecmaFeatures to the configuration and returns the result.
* @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.
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -211,7 +211,7 @@ module.exports = {
}
});
}
},
}

/**
* Converts old-style severity settings (0, 1, 2) into new-style
Expand All @@ -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) {
Expand All @@ -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;

Expand All @@ -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);
Expand Down
30 changes: 16 additions & 14 deletions lib/config/environments.js
Expand Up @@ -14,7 +14,7 @@ var envs = require("../../conf/environments");
// Private
//------------------------------------------------------------------------------

var environments = Object.create(null);
var environments = new Map();

/**
* Loads the default environments.
Expand All @@ -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]);
});
}

Expand All @@ -34,49 +34,51 @@ 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.
* @param {string} name The name of the environment.
* @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.
* @param {Object} plugin The plugin object.
* @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();
}
};
3 changes: 1 addition & 2 deletions lib/rules/no-unmodified-loop-condition.js
Expand Up @@ -9,8 +9,7 @@
// Requirements
//------------------------------------------------------------------------------

var Map = require("es6-map"),
Traverser = require("../util/traverser"),
var Traverser = require("../util/traverser"),
astUtils = require("../ast-utils");

//------------------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion lib/rules/prefer-const.js
Expand Up @@ -9,7 +9,6 @@
// Requirements
//------------------------------------------------------------------------------

var Map = require("es6-map");
var lodash = require("lodash");

//------------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down Expand Up @@ -71,6 +70,8 @@
"user-home": "^2.0.0"
},
"devDependencies": {
"babel-preset-es2015": "^6.9.0",
"babelify": "^7.3.0",
"beefy": "^2.0.0",
"brfs": "0.0.9",
"browserify": "^12.0.1",
Expand Down

0 comments on commit d2bdf4d

Please sign in to comment.