Skip to content

Commit

Permalink
Chore: update to use ES6 classes (refs #7849) (#7891)
Browse files Browse the repository at this point in the history
* Chore: update to ES6 classes (refs #7849)

* Chore: refactor ruleFixer to be plain object (refs #7849)

* Chore: make suggested changes (refs #7849)
  • Loading branch information
qlaire authored and ilyavolodin committed Jan 11, 2017
1 parent 55f0cb6 commit 075ec25
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 74 deletions.
33 changes: 16 additions & 17 deletions lib/config/autoconfig.js
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -156,7 +155,7 @@ Registry.prototype = {
}

return ruleSets;
},
}

/**
* Remove all items from the registry with a non-zero number of errors
Expand All @@ -182,7 +181,7 @@ Registry.prototype = {
});

return newRegistry;
},
}

/**
* Removes rule configurations which were not included in a ruleSet
Expand All @@ -199,7 +198,7 @@ Registry.prototype = {
});

return newRegistry;
},
}

/**
* Creates a registry of rules which had no error-free configs.
Expand All @@ -221,7 +220,7 @@ Registry.prototype = {
});

return failingRegistry;
},
}

/**
* Create an eslint config for any rules which only have one configuration
Expand All @@ -240,7 +239,7 @@ Registry.prototype = {
});

return config;
},
}

/**
* Return a cloned registry containing only configs with a desired specificity
Expand All @@ -258,7 +257,7 @@ Registry.prototype = {
});

return newRegistry;
},
}

/**
* Lint SourceCodes against all configurations in the registry, and record results
Expand Down Expand Up @@ -316,7 +315,7 @@ Registry.prototype = {

return lintedRegistry;
}
};
}

/**
* Extract rule configuration into eslint:recommended where possible.
Expand Down
31 changes: 15 additions & 16 deletions lib/config/config-rule.js
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -219,7 +218,7 @@ RuleConfigSet.prototype = {
*/
addEnums(enums) {
this.ruleConfigs = this.ruleConfigs.concat(combineArrays(this.ruleConfigs, enums));
},
}

/**
* Add rule configurations from a schema object
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/rule-context.js
Expand Up @@ -8,7 +8,7 @@
// Requirements
//------------------------------------------------------------------------------

const RuleFixer = require("./util/rule-fixer");
const ruleFixer = require("./util/rule-fixer");

//------------------------------------------------------------------------------
// Constants
Expand Down Expand Up @@ -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(
Expand Down
33 changes: 17 additions & 16 deletions lib/util/comment-event-generator.js
Expand Up @@ -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;
11 changes: 3 additions & 8 deletions lib/util/rule-fixer.js
Expand Up @@ -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.
Expand Down Expand Up @@ -139,7 +134,7 @@ RuleFixer.prototype = {
};
}

};
});


module.exports = RuleFixer;
module.exports = ruleFixer;
24 changes: 9 additions & 15 deletions tests/lib/util/rule-fixer.js
Expand Up @@ -9,25 +9,19 @@
//------------------------------------------------------------------------------

const assert = require("chai").assert,
RuleFixer = require("../../../lib/util/rule-fixer");
ruleFixer = require("../../../lib/util/rule-fixer");

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

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],
Expand All @@ -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],
Expand All @@ -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],
Expand All @@ -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],
Expand All @@ -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],
Expand All @@ -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],
Expand All @@ -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],
Expand All @@ -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],
Expand Down

0 comments on commit 075ec25

Please sign in to comment.