memberOrderingRule.js
14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var ts = require("typescript");
var Lint = require("../lint");
var OPTION_VARIABLES_BEFORE_FUNCTIONS = "variables-before-functions";
var OPTION_STATIC_BEFORE_INSTANCE = "static-before-instance";
var OPTION_PUBLIC_BEFORE_PRIVATE = "public-before-private";
var OPTION_ORDER = "order";
var PRESET_ORDERS = {
"fields-first": [
"public-static-field",
"protected-static-field",
"private-static-field",
"public-instance-field",
"protected-instance-field",
"private-instance-field",
"constructor",
"public-static-method",
"protected-static-method",
"private-static-method",
"public-instance-method",
"protected-instance-method",
"private-instance-method",
],
"statics-first": [
"public-static-field",
"public-static-method",
"protected-static-field",
"protected-static-method",
"private-static-field",
"private-static-method",
"public-instance-field",
"protected-instance-field",
"private-instance-field",
"constructor",
"public-instance-method",
"protected-instance-method",
"private-instance-method",
],
"instance-sandwich": [
"public-static-field",
"protected-static-field",
"private-static-field",
"public-instance-field",
"protected-instance-field",
"private-instance-field",
"constructor",
"public-instance-method",
"protected-instance-method",
"private-instance-method",
"public-static-method",
"protected-static-method",
"private-static-method",
],
};
var Rule = (function (_super) {
__extends(Rule, _super);
function Rule() {
_super.apply(this, arguments);
}
Rule.prototype.apply = function (sourceFile) {
return this.applyWithWalker(new MemberOrderingWalker(sourceFile, this.getOptions()));
};
Rule.metadata = {
ruleName: "member-ordering",
description: "Enforces member ordering.",
rationale: "A consistent ordering for class members can make classes easier to read, navigate, and edit.",
optionsDescription: (_a = ["\n One argument, which is an object, must be provided. It should contain an `order` property.\n The `order` property should have a value of one of the following strings:\n\n * `fields-first`\n * `statics-first`\n * `instance-sandwich`\n\n Alternatively, the value for `order` maybe be an array consisting of the following strings:\n\n * `public-static-field`\n * `protected-static-field`\n * `private-static-field`\n * `public-instance-field`\n * `protected-instance-field`\n * `private-instance-field`\n * `constructor`\n * `public-static-method`\n * `protected-static-method`\n * `private-static-method`\n * `public-instance-method`\n * `protected-instance-method`\n * `private-instance-method`\n\n This is useful if one of the preset orders does not meet your needs."], _a.raw = ["\n One argument, which is an object, must be provided. It should contain an \\`order\\` property.\n The \\`order\\` property should have a value of one of the following strings:\n\n * \\`fields-first\\`\n * \\`statics-first\\`\n * \\`instance-sandwich\\`\n\n Alternatively, the value for \\`order\\` maybe be an array consisting of the following strings:\n\n * \\`public-static-field\\`\n * \\`protected-static-field\\`\n * \\`private-static-field\\`\n * \\`public-instance-field\\`\n * \\`protected-instance-field\\`\n * \\`private-instance-field\\`\n * \\`constructor\\`\n * \\`public-static-method\\`\n * \\`protected-static-method\\`\n * \\`private-static-method\\`\n * \\`public-instance-method\\`\n * \\`protected-instance-method\\`\n * \\`private-instance-method\\`\n\n This is useful if one of the preset orders does not meet your needs."], Lint.Utils.dedent(_a)),
options: {
type: "object",
properties: {
order: {
oneOf: [{
type: "string",
enum: ["fields-first", "statics-first", "instance-sandwich"],
}, {
type: "array",
items: {
type: "string",
enum: PRESET_ORDERS["statics-first"],
},
maxLength: 13,
}],
},
},
additionalProperties: false,
},
optionExamples: ['[true, { "order": "fields-first" }]'],
type: "typescript",
};
return Rule;
var _a;
}(Lint.Rules.AbstractRule));
exports.Rule = Rule;
function getModifiers(isMethod, modifiers) {
return {
isInstance: !Lint.hasModifier(modifiers, ts.SyntaxKind.StaticKeyword),
isMethod: isMethod,
isPrivate: Lint.hasModifier(modifiers, ts.SyntaxKind.PrivateKeyword),
};
}
function toString(modifiers) {
return [
modifiers.isPrivate ? "private" : "public",
modifiers.isInstance ? "instance" : "static",
"member",
modifiers.isMethod ? "function" : "variable",
].join(" ");
}
var AccessLevel;
(function (AccessLevel) {
AccessLevel[AccessLevel["PRIVATE"] = 0] = "PRIVATE";
AccessLevel[AccessLevel["PROTECTED"] = 1] = "PROTECTED";
AccessLevel[AccessLevel["PUBLIC"] = 2] = "PUBLIC";
})(AccessLevel || (AccessLevel = {}));
var Membership;
(function (Membership) {
Membership[Membership["INSTANCE"] = 0] = "INSTANCE";
Membership[Membership["STATIC"] = 1] = "STATIC";
})(Membership || (Membership = {}));
var Kind;
(function (Kind) {
Kind[Kind["FIELD"] = 0] = "FIELD";
Kind[Kind["METHOD"] = 1] = "METHOD";
})(Kind || (Kind = {}));
function getNodeAndModifiers(node, isMethod, isConstructor) {
if (isConstructor === void 0) { isConstructor = false; }
var modifiers = node.modifiers;
var accessLevel = Lint.hasModifier(modifiers, ts.SyntaxKind.PrivateKeyword) ? AccessLevel.PRIVATE
: Lint.hasModifier(modifiers, ts.SyntaxKind.ProtectedKeyword) ? AccessLevel.PROTECTED
: AccessLevel.PUBLIC;
var kind = isMethod ? Kind.METHOD : Kind.FIELD;
var membership = Lint.hasModifier(modifiers, ts.SyntaxKind.StaticKeyword) ? Membership.STATIC : Membership.INSTANCE;
return {
accessLevel: accessLevel,
isConstructor: isConstructor,
kind: kind,
membership: membership,
node: node,
};
}
function getNodeOption(_a) {
var accessLevel = _a.accessLevel, isConstructor = _a.isConstructor, kind = _a.kind, membership = _a.membership;
if (isConstructor) {
return "constructor";
}
return [
AccessLevel[accessLevel].toLowerCase(),
Membership[membership].toLowerCase(),
Kind[kind].toLowerCase(),
].join("-");
}
var MemberOrderingWalker = (function (_super) {
__extends(MemberOrderingWalker, _super);
function MemberOrderingWalker() {
_super.apply(this, arguments);
this.memberStack = [];
this.hasOrderOption = this.getHasOrderOption();
}
MemberOrderingWalker.prototype.visitClassDeclaration = function (node) {
this.resetPreviousModifiers();
this.newMemberList();
_super.prototype.visitClassDeclaration.call(this, node);
this.checkMemberOrder();
};
MemberOrderingWalker.prototype.visitClassExpression = function (node) {
this.resetPreviousModifiers();
this.newMemberList();
_super.prototype.visitClassExpression.call(this, node);
this.checkMemberOrder();
};
MemberOrderingWalker.prototype.visitInterfaceDeclaration = function (node) {
this.resetPreviousModifiers();
this.newMemberList();
_super.prototype.visitInterfaceDeclaration.call(this, node);
this.checkMemberOrder();
};
MemberOrderingWalker.prototype.visitMethodDeclaration = function (node) {
this.checkModifiersAndSetPrevious(node, getModifiers(true, node.modifiers));
this.pushMember(getNodeAndModifiers(node, true));
_super.prototype.visitMethodDeclaration.call(this, node);
};
MemberOrderingWalker.prototype.visitMethodSignature = function (node) {
this.checkModifiersAndSetPrevious(node, getModifiers(true, node.modifiers));
this.pushMember(getNodeAndModifiers(node, true));
_super.prototype.visitMethodSignature.call(this, node);
};
MemberOrderingWalker.prototype.visitConstructorDeclaration = function (node) {
this.checkModifiersAndSetPrevious(node, getModifiers(true, node.modifiers));
this.pushMember(getNodeAndModifiers(node, true, true));
_super.prototype.visitConstructorDeclaration.call(this, node);
};
MemberOrderingWalker.prototype.visitPropertyDeclaration = function (node) {
var initializer = node.initializer;
var isFunction = initializer != null
&& (initializer.kind === ts.SyntaxKind.ArrowFunction || initializer.kind === ts.SyntaxKind.FunctionExpression);
this.checkModifiersAndSetPrevious(node, getModifiers(isFunction, node.modifiers));
this.pushMember(getNodeAndModifiers(node, isFunction));
_super.prototype.visitPropertyDeclaration.call(this, node);
};
MemberOrderingWalker.prototype.visitPropertySignature = function (node) {
this.checkModifiersAndSetPrevious(node, getModifiers(false, node.modifiers));
this.pushMember(getNodeAndModifiers(node, false));
_super.prototype.visitPropertySignature.call(this, node);
};
MemberOrderingWalker.prototype.visitTypeLiteral = function (node) {
};
MemberOrderingWalker.prototype.visitObjectLiteralExpression = function (node) {
};
MemberOrderingWalker.prototype.resetPreviousModifiers = function () {
this.previousMember = {
isInstance: false,
isMethod: false,
isPrivate: false,
};
};
MemberOrderingWalker.prototype.checkModifiersAndSetPrevious = function (node, currentMember) {
if (!this.canAppearAfter(this.previousMember, currentMember)) {
var failure = this.createFailure(node.getStart(), node.getWidth(), "Declaration of " + toString(currentMember) + " not allowed to appear after declaration of " + toString(this.previousMember));
this.addFailure(failure);
}
this.previousMember = currentMember;
};
MemberOrderingWalker.prototype.canAppearAfter = function (previousMember, currentMember) {
if (previousMember == null || currentMember == null) {
return true;
}
if (this.hasOption(OPTION_VARIABLES_BEFORE_FUNCTIONS) && previousMember.isMethod !== currentMember.isMethod) {
return Number(previousMember.isMethod) < Number(currentMember.isMethod);
}
if (this.hasOption(OPTION_STATIC_BEFORE_INSTANCE) && previousMember.isInstance !== currentMember.isInstance) {
return Number(previousMember.isInstance) < Number(currentMember.isInstance);
}
if (this.hasOption(OPTION_PUBLIC_BEFORE_PRIVATE) && previousMember.isPrivate !== currentMember.isPrivate) {
return Number(previousMember.isPrivate) < Number(currentMember.isPrivate);
}
return true;
};
MemberOrderingWalker.prototype.newMemberList = function () {
if (this.hasOrderOption) {
this.memberStack.push([]);
}
};
MemberOrderingWalker.prototype.pushMember = function (node) {
if (this.hasOrderOption) {
this.memberStack[this.memberStack.length - 1].push(node);
}
};
MemberOrderingWalker.prototype.checkMemberOrder = function () {
var _this = this;
if (this.hasOrderOption) {
var memberList_1 = this.memberStack.pop();
var order_1 = this.getOrder();
var memberRank_1 = memberList_1.map(function (n) { return order_1.indexOf(getNodeOption(n)); });
var prevRank_1 = -1;
memberRank_1.forEach(function (rank, i) {
if (rank === -1) {
return;
}
if (rank < prevRank_1) {
var node = memberList_1[i].node;
var nodeType = order_1[rank].split("-").join(" ");
var prevNodeType = order_1[prevRank_1].split("-").join(" ");
var lowerRanks = memberRank_1.filter(function (r) { return r < rank && r !== -1; }).sort();
var locationHint = lowerRanks.length > 0
? "after " + order_1[lowerRanks[lowerRanks.length - 1]].split("-").join(" ") + "s"
: "at the beginning of the class/interface";
var errorLine1 = ("Declaration of " + nodeType + " not allowed after declaration of " + prevNodeType + ". ") +
("Instead, this should come " + locationHint + ".");
_this.addFailure(_this.createFailure(node.getStart(), node.getWidth(), errorLine1));
}
else {
prevRank_1 = rank;
}
});
}
};
MemberOrderingWalker.prototype.getHasOrderOption = function () {
var allOptions = this.getOptions();
if (allOptions == null || allOptions.length === 0) {
return false;
}
var firstOption = allOptions[0];
return firstOption != null && typeof firstOption === "object" && firstOption[OPTION_ORDER] != null;
};
MemberOrderingWalker.prototype.getOrder = function () {
var orderOption = this.getOptions()[0][OPTION_ORDER];
if (Array.isArray(orderOption)) {
return orderOption;
}
else if (typeof orderOption === "string") {
return PRESET_ORDERS[orderOption] || PRESET_ORDERS["default"];
}
};
return MemberOrderingWalker;
}(Lint.RuleWalker));
exports.MemberOrderingWalker = MemberOrderingWalker;