From 075ec252fc20eda35fcff934677ee2abb9c86646 Mon Sep 17 00:00:00 2001 From: Claire Dranginis Date: Wed, 11 Jan 2017 11:17:02 -0500 Subject: [PATCH] Chore: update to use ES6 classes (refs #7849) (#7891) * Chore: update to ES6 classes (refs #7849) * Chore: refactor ruleFixer to be plain object (refs #7849) * Chore: make suggested changes (refs #7849) --- lib/config/autoconfig.js | 33 ++++++++++++++--------------- lib/config/config-rule.js | 31 +++++++++++++-------------- lib/rule-context.js | 4 ++-- lib/util/comment-event-generator.js | 33 +++++++++++++++-------------- lib/util/rule-fixer.js | 11 +++------- tests/lib/util/rule-fixer.js | 24 ++++++++------------- 6 files changed, 62 insertions(+), 74 deletions(-) diff --git a/lib/config/autoconfig.js b/lib/config/autoconfig.js index 23fdbe69803..4c005c0538d 100644 --- a/lib/config/autoconfig.js +++ b/lib/config/autoconfig.js @@ -65,17 +65,16 @@ function makeRegistryItems(rulesConfig) { * Unless a rulesConfig is provided at construction, the registry will not contain * any rules, only methods. This will be useful for building up registries manually. * -* @constructor -* @class Registry -* @param {rulesConfig} [rulesConfig] Hash of rule names and arrays of possible configurations +* Registry class */ -function Registry(rulesConfig) { - this.rules = (rulesConfig) ? makeRegistryItems(rulesConfig) : {}; -} - -Registry.prototype = { +class Registry { - constructor: Registry, + /** + * @param {rulesConfig} [rulesConfig] Hash of rule names and arrays of possible configurations + */ + constructor(rulesConfig) { + this.rules = (rulesConfig) ? makeRegistryItems(rulesConfig) : {}; + } /** * Populate the registry with core rule configs. @@ -89,7 +88,7 @@ Registry.prototype = { const rulesConfig = configRule.createCoreRuleConfigs(); this.rules = makeRegistryItems(rulesConfig); - }, + } /** * Creates sets of rule configurations which can be used for linting @@ -156,7 +155,7 @@ Registry.prototype = { } return ruleSets; - }, + } /** * Remove all items from the registry with a non-zero number of errors @@ -182,7 +181,7 @@ Registry.prototype = { }); return newRegistry; - }, + } /** * Removes rule configurations which were not included in a ruleSet @@ -199,7 +198,7 @@ Registry.prototype = { }); return newRegistry; - }, + } /** * Creates a registry of rules which had no error-free configs. @@ -221,7 +220,7 @@ Registry.prototype = { }); return failingRegistry; - }, + } /** * Create an eslint config for any rules which only have one configuration @@ -240,7 +239,7 @@ Registry.prototype = { }); return config; - }, + } /** * Return a cloned registry containing only configs with a desired specificity @@ -258,7 +257,7 @@ Registry.prototype = { }); return newRegistry; - }, + } /** * Lint SourceCodes against all configurations in the registry, and record results @@ -316,7 +315,7 @@ Registry.prototype = { return lintedRegistry; } -}; +} /** * Extract rule configuration into eslint:recommended where possible. diff --git a/lib/config/config-rule.js b/lib/config/config-rule.js index d495198aed4..bd7aa13b92c 100644 --- a/lib/config/config-rule.js +++ b/lib/config/config-rule.js @@ -176,22 +176,21 @@ function combinePropertyObjects(objArr1, objArr2) { * * ruleConfigSet.ruleConfigs // -> [[2], [2, "always"], [2, "never"]] * - * @param {ruleConfig[]} configs Valid rule configurations - * @constructor + * Rule configuration set class */ -function RuleConfigSet(configs) { +class RuleConfigSet { /** - * Stored valid rule configurations for this instance - * @type {array} - */ - this.ruleConfigs = configs || []; - -} - -RuleConfigSet.prototype = { - - constructor: RuleConfigSet, + * @param {ruleConfig[]} configs Valid rule configurations + */ + constructor(configs) { + + /** + * Stored valid rule configurations for this instance + * @type {array} + */ + this.ruleConfigs = configs || []; + } /** * Add a severity level to the front of all configs in the instance. @@ -210,7 +209,7 @@ RuleConfigSet.prototype = { // Add a single config at the beginning consisting of only the severity this.ruleConfigs.unshift(severity); - }, + } /** * Add rule configs from an array of strings (schema enums) @@ -219,7 +218,7 @@ RuleConfigSet.prototype = { */ addEnums(enums) { this.ruleConfigs = this.ruleConfigs.concat(combineArrays(this.ruleConfigs, enums)); - }, + } /** * Add rule configurations from a schema object @@ -261,7 +260,7 @@ RuleConfigSet.prototype = { this.ruleConfigs = this.ruleConfigs.concat(combineArrays(this.ruleConfigs, objectConfigSet.objectConfigs)); } } -}; +} /** * Generate valid rule configurations based on a schema object diff --git a/lib/rule-context.js b/lib/rule-context.js index 9c80d2e1a31..99221666af8 100644 --- a/lib/rule-context.js +++ b/lib/rule-context.js @@ -8,7 +8,7 @@ // Requirements //------------------------------------------------------------------------------ -const RuleFixer = require("./util/rule-fixer"); +const ruleFixer = require("./util/rule-fixer"); //------------------------------------------------------------------------------ // Constants @@ -124,7 +124,7 @@ class RuleContext { // if there's a fix specified, get it if (typeof descriptor.fix === "function") { - fix = descriptor.fix(new RuleFixer()); + fix = descriptor.fix(ruleFixer); } this.eslint.report( diff --git a/lib/util/comment-event-generator.js b/lib/util/comment-event-generator.js index dfa7132ff85..239a9834d0e 100644 --- a/lib/util/comment-event-generator.js +++ b/lib/util/comment-event-generator.js @@ -70,46 +70,47 @@ function emitCommentsExit(generator, comments) { * This is the decorator pattern. * This generates events of comments before/after events which are generated the original generator. * - * @param {EventGenerator} originalEventGenerator - An event generator which is the decoration target. - * @param {SourceCode} sourceCode - A source code which has comments. - * @returns {CommentEventGenerator} new instance. + * Comment event generator class */ -function CommentEventGenerator(originalEventGenerator, sourceCode) { - this.original = originalEventGenerator; - this.emitter = originalEventGenerator.emitter; - this.sourceCode = sourceCode; - this.commentLocsEnter = []; - this.commentLocsExit = []; -} +class CommentEventGenerator { -CommentEventGenerator.prototype = { - constructor: CommentEventGenerator, + /** + * @param {EventGenerator} originalEventGenerator - An event generator which is the decoration target. + * @param {SourceCode} sourceCode - A source code which has comments. + */ + constructor(originalEventGenerator, sourceCode) { + this.original = originalEventGenerator; + this.emitter = originalEventGenerator.emitter; + this.sourceCode = sourceCode; + this.commentLocsEnter = []; + this.commentLocsExit = []; + } /** * Emits an event of entering comments. * @param {ASTNode} node - A node which was entered. * @returns {void} */ - enterNode: function enterNode(node) { + enterNode(node) { const comments = this.sourceCode.getComments(node); emitCommentsEnter(this, comments.leading); this.original.enterNode(node); emitCommentsEnter(this, comments.trailing); - }, + } /** * Emits an event of leaving comments. * @param {ASTNode} node - A node which was left. * @returns {void} */ - leaveNode: function leaveNode(node) { + leaveNode(node) { const comments = this.sourceCode.getComments(node); emitCommentsExit(this, comments.trailing); this.original.leaveNode(node); emitCommentsExit(this, comments.leading); } -}; +} module.exports = CommentEventGenerator; diff --git a/lib/util/rule-fixer.js b/lib/util/rule-fixer.js index e6afe85f502..bdd80d13b16 100644 --- a/lib/util/rule-fixer.js +++ b/lib/util/rule-fixer.js @@ -34,14 +34,9 @@ function insertTextAt(index, text) { /** * Creates code fixing commands for rules. - * @constructor */ -function RuleFixer() { - Object.freeze(this); -} -RuleFixer.prototype = { - constructor: RuleFixer, +const ruleFixer = Object.freeze({ /** * Creates a fix command that inserts text after the given node or token. @@ -139,7 +134,7 @@ RuleFixer.prototype = { }; } -}; +}); -module.exports = RuleFixer; +module.exports = ruleFixer; diff --git a/tests/lib/util/rule-fixer.js b/tests/lib/util/rule-fixer.js index ff9142d077a..f3376c0663a 100644 --- a/tests/lib/util/rule-fixer.js +++ b/tests/lib/util/rule-fixer.js @@ -9,7 +9,7 @@ //------------------------------------------------------------------------------ const assert = require("chai").assert, - RuleFixer = require("../../../lib/util/rule-fixer"); + ruleFixer = require("../../../lib/util/rule-fixer"); //------------------------------------------------------------------------------ // Tests @@ -17,17 +17,11 @@ const assert = require("chai").assert, describe("RuleFixer", () => { - let fixer; - - beforeEach(() => { - fixer = new RuleFixer(); - }); - describe("insertTextBefore", () => { it("should return an object with the correct information when called", () => { - const result = fixer.insertTextBefore({ range: [0, 1] }, "Hi"); + const result = ruleFixer.insertTextBefore({ range: [0, 1] }, "Hi"); assert.deepEqual(result, { range: [0, 0], @@ -42,7 +36,7 @@ describe("RuleFixer", () => { it("should return an object with the correct information when called", () => { - const result = fixer.insertTextBeforeRange([0, 1], "Hi"); + const result = ruleFixer.insertTextBeforeRange([0, 1], "Hi"); assert.deepEqual(result, { range: [0, 0], @@ -57,7 +51,7 @@ describe("RuleFixer", () => { it("should return an object with the correct information when called", () => { - const result = fixer.insertTextAfter({ range: [0, 1] }, "Hi"); + const result = ruleFixer.insertTextAfter({ range: [0, 1] }, "Hi"); assert.deepEqual(result, { range: [1, 1], @@ -72,7 +66,7 @@ describe("RuleFixer", () => { it("should return an object with the correct information when called", () => { - const result = fixer.insertTextAfterRange([0, 1], "Hi"); + const result = ruleFixer.insertTextAfterRange([0, 1], "Hi"); assert.deepEqual(result, { range: [1, 1], @@ -87,7 +81,7 @@ describe("RuleFixer", () => { it("should return an object with the correct information when called", () => { - const result = fixer.remove({ range: [0, 1] }); + const result = ruleFixer.remove({ range: [0, 1] }); assert.deepEqual(result, { range: [0, 1], @@ -102,7 +96,7 @@ describe("RuleFixer", () => { it("should return an object with the correct information when called", () => { - const result = fixer.removeRange([0, 1]); + const result = ruleFixer.removeRange([0, 1]); assert.deepEqual(result, { range: [0, 1], @@ -118,7 +112,7 @@ describe("RuleFixer", () => { it("should return an object with the correct information when called", () => { - const result = fixer.replaceText({ range: [0, 1] }, "Hi"); + const result = ruleFixer.replaceText({ range: [0, 1] }, "Hi"); assert.deepEqual(result, { range: [0, 1], @@ -133,7 +127,7 @@ describe("RuleFixer", () => { it("should return an object with the correct information when called", () => { - const result = fixer.replaceTextRange([0, 1], "Hi"); + const result = ruleFixer.replaceTextRange([0, 1], "Hi"); assert.deepEqual(result, { range: [0, 1],