Skip to content

Commit 075ec25

Browse files
qlaireilyavolodin
authored andcommitted
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)
1 parent 55f0cb6 commit 075ec25

File tree

6 files changed

+62
-74
lines changed

6 files changed

+62
-74
lines changed

lib/config/autoconfig.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,16 @@ function makeRegistryItems(rulesConfig) {
6565
* Unless a rulesConfig is provided at construction, the registry will not contain
6666
* any rules, only methods. This will be useful for building up registries manually.
6767
*
68-
* @constructor
69-
* @class Registry
70-
* @param {rulesConfig} [rulesConfig] Hash of rule names and arrays of possible configurations
68+
* Registry class
7169
*/
72-
function Registry(rulesConfig) {
73-
this.rules = (rulesConfig) ? makeRegistryItems(rulesConfig) : {};
74-
}
75-
76-
Registry.prototype = {
70+
class Registry {
7771

78-
constructor: Registry,
72+
/**
73+
* @param {rulesConfig} [rulesConfig] Hash of rule names and arrays of possible configurations
74+
*/
75+
constructor(rulesConfig) {
76+
this.rules = (rulesConfig) ? makeRegistryItems(rulesConfig) : {};
77+
}
7978

8079
/**
8180
* Populate the registry with core rule configs.
@@ -89,7 +88,7 @@ Registry.prototype = {
8988
const rulesConfig = configRule.createCoreRuleConfigs();
9089

9190
this.rules = makeRegistryItems(rulesConfig);
92-
},
91+
}
9392

9493
/**
9594
* Creates sets of rule configurations which can be used for linting
@@ -156,7 +155,7 @@ Registry.prototype = {
156155
}
157156

158157
return ruleSets;
159-
},
158+
}
160159

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

184183
return newRegistry;
185-
},
184+
}
186185

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

201200
return newRegistry;
202-
},
201+
}
203202

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

223222
return failingRegistry;
224-
},
223+
}
225224

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

242241
return config;
243-
},
242+
}
244243

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

260259
return newRegistry;
261-
},
260+
}
262261

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

317316
return lintedRegistry;
318317
}
319-
};
318+
}
320319

321320
/**
322321
* Extract rule configuration into eslint:recommended where possible.

lib/config/config-rule.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -176,22 +176,21 @@ function combinePropertyObjects(objArr1, objArr2) {
176176
*
177177
* ruleConfigSet.ruleConfigs // -> [[2], [2, "always"], [2, "never"]]
178178
*
179-
* @param {ruleConfig[]} configs Valid rule configurations
180-
* @constructor
179+
* Rule configuration set class
181180
*/
182-
function RuleConfigSet(configs) {
181+
class RuleConfigSet {
183182

184183
/**
185-
* Stored valid rule configurations for this instance
186-
* @type {array}
187-
*/
188-
this.ruleConfigs = configs || [];
189-
190-
}
191-
192-
RuleConfigSet.prototype = {
193-
194-
constructor: RuleConfigSet,
184+
* @param {ruleConfig[]} configs Valid rule configurations
185+
*/
186+
constructor(configs) {
187+
188+
/**
189+
* Stored valid rule configurations for this instance
190+
* @type {array}
191+
*/
192+
this.ruleConfigs = configs || [];
193+
}
195194

196195
/**
197196
* Add a severity level to the front of all configs in the instance.
@@ -210,7 +209,7 @@ RuleConfigSet.prototype = {
210209

211210
// Add a single config at the beginning consisting of only the severity
212211
this.ruleConfigs.unshift(severity);
213-
},
212+
}
214213

215214
/**
216215
* Add rule configs from an array of strings (schema enums)
@@ -219,7 +218,7 @@ RuleConfigSet.prototype = {
219218
*/
220219
addEnums(enums) {
221220
this.ruleConfigs = this.ruleConfigs.concat(combineArrays(this.ruleConfigs, enums));
222-
},
221+
}
223222

224223
/**
225224
* Add rule configurations from a schema object
@@ -261,7 +260,7 @@ RuleConfigSet.prototype = {
261260
this.ruleConfigs = this.ruleConfigs.concat(combineArrays(this.ruleConfigs, objectConfigSet.objectConfigs));
262261
}
263262
}
264-
};
263+
}
265264

266265
/**
267266
* Generate valid rule configurations based on a schema object

lib/rule-context.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// Requirements
99
//------------------------------------------------------------------------------
1010

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

1313
//------------------------------------------------------------------------------
1414
// Constants
@@ -124,7 +124,7 @@ class RuleContext {
124124

125125
// if there's a fix specified, get it
126126
if (typeof descriptor.fix === "function") {
127-
fix = descriptor.fix(new RuleFixer());
127+
fix = descriptor.fix(ruleFixer);
128128
}
129129

130130
this.eslint.report(

lib/util/comment-event-generator.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,46 +70,47 @@ function emitCommentsExit(generator, comments) {
7070
* This is the decorator pattern.
7171
* This generates events of comments before/after events which are generated the original generator.
7272
*
73-
* @param {EventGenerator} originalEventGenerator - An event generator which is the decoration target.
74-
* @param {SourceCode} sourceCode - A source code which has comments.
75-
* @returns {CommentEventGenerator} new instance.
73+
* Comment event generator class
7674
*/
77-
function CommentEventGenerator(originalEventGenerator, sourceCode) {
78-
this.original = originalEventGenerator;
79-
this.emitter = originalEventGenerator.emitter;
80-
this.sourceCode = sourceCode;
81-
this.commentLocsEnter = [];
82-
this.commentLocsExit = [];
83-
}
75+
class CommentEventGenerator {
8476

85-
CommentEventGenerator.prototype = {
86-
constructor: CommentEventGenerator,
77+
/**
78+
* @param {EventGenerator} originalEventGenerator - An event generator which is the decoration target.
79+
* @param {SourceCode} sourceCode - A source code which has comments.
80+
*/
81+
constructor(originalEventGenerator, sourceCode) {
82+
this.original = originalEventGenerator;
83+
this.emitter = originalEventGenerator.emitter;
84+
this.sourceCode = sourceCode;
85+
this.commentLocsEnter = [];
86+
this.commentLocsExit = [];
87+
}
8788

8889
/**
8990
* Emits an event of entering comments.
9091
* @param {ASTNode} node - A node which was entered.
9192
* @returns {void}
9293
*/
93-
enterNode: function enterNode(node) {
94+
enterNode(node) {
9495
const comments = this.sourceCode.getComments(node);
9596

9697
emitCommentsEnter(this, comments.leading);
9798
this.original.enterNode(node);
9899
emitCommentsEnter(this, comments.trailing);
99-
},
100+
}
100101

101102
/**
102103
* Emits an event of leaving comments.
103104
* @param {ASTNode} node - A node which was left.
104105
* @returns {void}
105106
*/
106-
leaveNode: function leaveNode(node) {
107+
leaveNode(node) {
107108
const comments = this.sourceCode.getComments(node);
108109

109110
emitCommentsExit(this, comments.trailing);
110111
this.original.leaveNode(node);
111112
emitCommentsExit(this, comments.leading);
112113
}
113-
};
114+
}
114115

115116
module.exports = CommentEventGenerator;

lib/util/rule-fixer.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,9 @@ function insertTextAt(index, text) {
3434

3535
/**
3636
* Creates code fixing commands for rules.
37-
* @constructor
3837
*/
39-
function RuleFixer() {
40-
Object.freeze(this);
41-
}
4238

43-
RuleFixer.prototype = {
44-
constructor: RuleFixer,
39+
const ruleFixer = Object.freeze({
4540

4641
/**
4742
* Creates a fix command that inserts text after the given node or token.
@@ -139,7 +134,7 @@ RuleFixer.prototype = {
139134
};
140135
}
141136

142-
};
137+
});
143138

144139

145-
module.exports = RuleFixer;
140+
module.exports = ruleFixer;

tests/lib/util/rule-fixer.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,19 @@
99
//------------------------------------------------------------------------------
1010

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

1414
//------------------------------------------------------------------------------
1515
// Tests
1616
//------------------------------------------------------------------------------
1717

1818
describe("RuleFixer", () => {
1919

20-
let fixer;
21-
22-
beforeEach(() => {
23-
fixer = new RuleFixer();
24-
});
25-
2620
describe("insertTextBefore", () => {
2721

2822
it("should return an object with the correct information when called", () => {
2923

30-
const result = fixer.insertTextBefore({ range: [0, 1] }, "Hi");
24+
const result = ruleFixer.insertTextBefore({ range: [0, 1] }, "Hi");
3125

3226
assert.deepEqual(result, {
3327
range: [0, 0],
@@ -42,7 +36,7 @@ describe("RuleFixer", () => {
4236

4337
it("should return an object with the correct information when called", () => {
4438

45-
const result = fixer.insertTextBeforeRange([0, 1], "Hi");
39+
const result = ruleFixer.insertTextBeforeRange([0, 1], "Hi");
4640

4741
assert.deepEqual(result, {
4842
range: [0, 0],
@@ -57,7 +51,7 @@ describe("RuleFixer", () => {
5751

5852
it("should return an object with the correct information when called", () => {
5953

60-
const result = fixer.insertTextAfter({ range: [0, 1] }, "Hi");
54+
const result = ruleFixer.insertTextAfter({ range: [0, 1] }, "Hi");
6155

6256
assert.deepEqual(result, {
6357
range: [1, 1],
@@ -72,7 +66,7 @@ describe("RuleFixer", () => {
7266

7367
it("should return an object with the correct information when called", () => {
7468

75-
const result = fixer.insertTextAfterRange([0, 1], "Hi");
69+
const result = ruleFixer.insertTextAfterRange([0, 1], "Hi");
7670

7771
assert.deepEqual(result, {
7872
range: [1, 1],
@@ -87,7 +81,7 @@ describe("RuleFixer", () => {
8781

8882
it("should return an object with the correct information when called", () => {
8983

90-
const result = fixer.remove({ range: [0, 1] });
84+
const result = ruleFixer.remove({ range: [0, 1] });
9185

9286
assert.deepEqual(result, {
9387
range: [0, 1],
@@ -102,7 +96,7 @@ describe("RuleFixer", () => {
10296

10397
it("should return an object with the correct information when called", () => {
10498

105-
const result = fixer.removeRange([0, 1]);
99+
const result = ruleFixer.removeRange([0, 1]);
106100

107101
assert.deepEqual(result, {
108102
range: [0, 1],
@@ -118,7 +112,7 @@ describe("RuleFixer", () => {
118112

119113
it("should return an object with the correct information when called", () => {
120114

121-
const result = fixer.replaceText({ range: [0, 1] }, "Hi");
115+
const result = ruleFixer.replaceText({ range: [0, 1] }, "Hi");
122116

123117
assert.deepEqual(result, {
124118
range: [0, 1],
@@ -133,7 +127,7 @@ describe("RuleFixer", () => {
133127

134128
it("should return an object with the correct information when called", () => {
135129

136-
const result = fixer.replaceTextRange([0, 1], "Hi");
130+
const result = ruleFixer.replaceTextRange([0, 1], "Hi");
137131

138132
assert.deepEqual(result, {
139133
range: [0, 1],

0 commit comments

Comments
 (0)