lint-rules-alvin 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,398 @@
1
+ import eslintTs from 'typescript-eslint';
2
+
3
+ export const typescript = {
4
+ name: 'typescript',
5
+ extends: [ eslintTs.configs.recommendedTypeChecked ],
6
+ languageOptions: { parserOptions: { projectService: true } },
7
+ rules: {
8
+ '@typescript-eslint/array-type': [
9
+ 'error',
10
+ {
11
+ default: 'array',
12
+ readonly: 'array'
13
+ }
14
+ ],
15
+ '@typescript-eslint/class-literal-property-style': [
16
+ 'error',
17
+ 'fields'
18
+ ],
19
+ '@typescript-eslint/consistent-indexed-object-style': [
20
+ 'error',
21
+ 'record'
22
+ ],
23
+ '@typescript-eslint/consistent-type-exports': 'error',
24
+ '@typescript-eslint/consistent-type-imports': 'off', // Prefer import/ rule over this
25
+ '@typescript-eslint/consistent-type-assertions': [
26
+ 'error',
27
+ {
28
+ assertionStyle: 'as',
29
+ arrayLiteralTypeAssertions: 'allow',
30
+ objectLiteralTypeAssertions: 'allow'
31
+ }
32
+ ],
33
+ 'dot-notation': 'off',
34
+ '@typescript-eslint/dot-notation': [
35
+ 'error',
36
+ {
37
+ allowPrivateClassPropertyAccess: false,
38
+ allowProtectedClassPropertyAccess: false,
39
+ allowIndexSignaturePropertyAccess: false
40
+ }
41
+ ],
42
+ '@typescript-eslint/explicit-function-return-type': 'off',
43
+ 'max-params': 'off',
44
+ '@typescript-eslint/max-params': [
45
+ 'warn',
46
+ {
47
+ max: 3,
48
+ countVoidThis: false
49
+ }
50
+ ],
51
+ '@typescript-eslint/member-ordering': [
52
+ 'error',
53
+ {
54
+ default: {
55
+ memberTypes: [
56
+ // Index signature
57
+ 'signature',
58
+ 'call-signature',
59
+
60
+ // Fields
61
+ 'public-static-field',
62
+ 'protected-static-field',
63
+ 'private-static-field',
64
+ '#private-static-field',
65
+
66
+ 'public-decorated-field',
67
+ 'protected-decorated-field',
68
+ 'private-decorated-field',
69
+
70
+ 'public-instance-field',
71
+ 'protected-instance-field',
72
+ 'private-instance-field',
73
+ '#private-instance-field',
74
+
75
+ 'public-abstract-field',
76
+ 'protected-abstract-field',
77
+
78
+ 'public-field',
79
+ 'protected-field',
80
+ 'private-field',
81
+ '#private-field',
82
+
83
+ 'static-field',
84
+ 'instance-field',
85
+ 'abstract-field',
86
+
87
+ 'decorated-field',
88
+
89
+ 'field',
90
+
91
+ // Static initialization
92
+ 'static-initialization',
93
+
94
+ // Constructors
95
+ 'public-constructor',
96
+ 'protected-constructor',
97
+ 'private-constructor',
98
+
99
+ 'constructor',
100
+
101
+ // Accessors
102
+ 'public-static-accessor',
103
+ 'protected-static-accessor',
104
+ 'private-static-accessor',
105
+ '#private-static-accessor',
106
+
107
+ 'public-decorated-accessor',
108
+ 'protected-decorated-accessor',
109
+ 'private-decorated-accessor',
110
+
111
+ 'public-instance-accessor',
112
+ 'protected-instance-accessor',
113
+ 'private-instance-accessor',
114
+ '#private-instance-accessor',
115
+
116
+ 'public-abstract-accessor',
117
+ 'protected-abstract-accessor',
118
+
119
+ 'public-accessor',
120
+ 'protected-accessor',
121
+ 'private-accessor',
122
+ '#private-accessor',
123
+
124
+ 'static-accessor',
125
+ 'instance-accessor',
126
+ 'abstract-accessor',
127
+
128
+ 'decorated-accessor',
129
+
130
+ 'accessor',
131
+
132
+ // Getters
133
+ 'public-static-get',
134
+ 'protected-static-get',
135
+ 'private-static-get',
136
+ '#private-static-get',
137
+
138
+ 'public-decorated-get',
139
+ 'protected-decorated-get',
140
+ 'private-decorated-get',
141
+
142
+ 'public-instance-get',
143
+ 'protected-instance-get',
144
+ 'private-instance-get',
145
+ '#private-instance-get',
146
+
147
+ 'public-abstract-get',
148
+ 'protected-abstract-get',
149
+
150
+ 'public-get',
151
+ 'protected-get',
152
+ 'private-get',
153
+ '#private-get',
154
+
155
+ 'static-get',
156
+ 'instance-get',
157
+ 'abstract-get',
158
+
159
+ 'decorated-get',
160
+
161
+ 'get',
162
+
163
+ // Setters
164
+ 'public-static-set',
165
+ 'protected-static-set',
166
+ 'private-static-set',
167
+ '#private-static-set',
168
+
169
+ 'public-decorated-set',
170
+ 'protected-decorated-set',
171
+ 'private-decorated-set',
172
+
173
+ 'public-instance-set',
174
+ 'protected-instance-set',
175
+ 'private-instance-set',
176
+ '#private-instance-set',
177
+
178
+ 'public-abstract-set',
179
+ 'protected-abstract-set',
180
+
181
+ 'public-set',
182
+ 'protected-set',
183
+ 'private-set',
184
+ '#private-set',
185
+
186
+ 'static-set',
187
+ 'instance-set',
188
+ 'abstract-set',
189
+
190
+ 'decorated-set',
191
+
192
+ 'set',
193
+
194
+ // Methods
195
+ 'public-static-method',
196
+ 'protected-static-method',
197
+ 'private-static-method',
198
+ '#private-static-method',
199
+
200
+ 'public-decorated-method',
201
+ 'protected-decorated-method',
202
+ 'private-decorated-method',
203
+
204
+ 'public-instance-method',
205
+ 'protected-instance-method',
206
+ 'private-instance-method',
207
+ '#private-instance-method',
208
+
209
+ 'public-abstract-method',
210
+ 'protected-abstract-method',
211
+
212
+ 'public-method',
213
+ 'protected-method',
214
+ 'private-method',
215
+ '#private-method',
216
+
217
+ 'static-method',
218
+ 'instance-method',
219
+ 'abstract-method',
220
+
221
+ 'decorated-method',
222
+
223
+ 'method'
224
+ ]
225
+ }
226
+ }
227
+ ],
228
+ '@typescript-eslint/method-signature-style': [
229
+ 'error',
230
+ 'property'
231
+ ],
232
+ '@typescript-eslint/naming-convention': [
233
+ 'error',
234
+ {
235
+ selector: 'default',
236
+ format: [
237
+ 'camelCase',
238
+ 'PascalCase',
239
+ 'UPPER_CASE',
240
+ 'snake_case'
241
+ ],
242
+ leadingUnderscore: 'forbid',
243
+ trailingUnderscore: 'forbid'
244
+ },
245
+ {
246
+ selector: 'memberLike', // classicAccessor, autoAccessor, enumMember, method, parameterProperty, property
247
+ format: [
248
+ 'camelCase',
249
+ 'PascalCase',
250
+ 'UPPER_CASE',
251
+ 'snake_case'
252
+ ],
253
+ leadingUnderscore: 'forbid',
254
+ trailingUnderscore: 'forbid'
255
+ },
256
+ {
257
+ selector: 'accessor', // classicAccessor, autoAccessor
258
+ format: [
259
+ 'camelCase',
260
+ 'PascalCase',
261
+ 'UPPER_CASE',
262
+ 'snake_case'
263
+ ],
264
+ leadingUnderscore: 'forbid',
265
+ trailingUnderscore: 'forbid'
266
+ },
267
+ {
268
+ selector: 'method', // classMethod, objectLiteralMethod, typeMethod
269
+ format: [
270
+ 'camelCase',
271
+ 'PascalCase',
272
+ 'snake_case'
273
+ ],
274
+ leadingUnderscore: 'forbid',
275
+ trailingUnderscore: 'forbid'
276
+ },
277
+ {
278
+ selector: 'property', // classProperty, objectLiteralProperty, typeProperty
279
+ format: [
280
+ 'camelCase',
281
+ 'UPPER_CASE'
282
+ ],
283
+ leadingUnderscore: 'allow',
284
+ trailingUnderscore: 'forbid'
285
+ },
286
+ {
287
+ selector: 'typeLike', // class, enum, interface, typeAlias, typeParameter
288
+ format: [ 'PascalCase' ],
289
+ leadingUnderscore: 'allow',
290
+ trailingUnderscore: 'allow'
291
+ },
292
+ {
293
+ selector: 'variableLike', // function, parameter, variable
294
+ format: [],
295
+ leadingUnderscore: 'allow',
296
+ trailingUnderscore: 'allow'
297
+ },
298
+ {
299
+ selector: 'import',
300
+ format: [
301
+ 'camelCase',
302
+ 'PascalCase'
303
+ ],
304
+ leadingUnderscore: 'forbid',
305
+ trailingUnderscore: 'forbid'
306
+ },
307
+ {
308
+ selector: 'objectLiteralProperty',
309
+ format: [],
310
+ leadingUnderscore: 'allow',
311
+ trailingUnderscore: 'allow'
312
+ }
313
+ ],
314
+ 'no-array-constructor': 'off',
315
+ '@typescript-eslint/no-array-constructor': 'error',
316
+ '@typescript-eslint/no-array-delete': 'warn',
317
+ '@typescript-eslint/no-base-to-string': 'error',
318
+ '@typescript-eslint/no-confusing-non-null-assertion': 'error',
319
+ '@typescript-eslint/no-confusing-void-expression': 'error',
320
+ '@typescript-eslint/no-deprecated': 'error',
321
+ '@typescript-eslint/no-dupe-class-members': 'error',
322
+ '@typescript-eslint/no-duplicate-enum-values': 'error',
323
+ '@typescript-eslint/no-duplicate-type-constituents': 'error',
324
+ '@typescript-eslint/no-dynamic-delete': 'error',
325
+ '@typescript-eslint/no-empty-function': 'warn',
326
+ '@typescript-eslint/no-empty-interface': 'warn',
327
+ '@typescript-eslint/no-empty-object-type': 'warn',
328
+ '@typescript-eslint/no-explicit-any': 'off',
329
+ '@typescript-eslint/no-extra-non-null-assertion': 'error',
330
+ '@typescript-eslint/no-extraneous-class': 'error',
331
+ '@typescript-eslint/no-floating-promises': [
332
+ 'error',
333
+ {
334
+ checkThenables: false,
335
+ ignoreIIFE: false,
336
+ ignoreVoid: true
337
+ }
338
+ ],
339
+ '@typescript-eslint/no-for-in-array': 'error',
340
+ 'no-implied-eval': 'off',
341
+ '@typescript-eslint/no-implied-eval': 'error',
342
+ '@typescript-eslint/no-invalid-void-type': [
343
+ 'error',
344
+ {
345
+ allowInGenericTypeArguments: true,
346
+ allowAsThisParameter: false
347
+ }
348
+ ],
349
+ 'no-loop-func': 'off',
350
+ '@typescript-eslint/no-loop-func': 'error',
351
+ 'no-magic-numbers': 'off',
352
+ '@typescript-eslint/no-magic-numbers': 'off',
353
+ '@typescript-eslint/no-meaningless-void-operator': [
354
+ 'error',
355
+ { checkNever: true }
356
+ ],
357
+ '@typescript-eslint/no-misused-new': 'error',
358
+ '@typescript-eslint/no-misused-promises': [
359
+ 'error',
360
+ {
361
+ checksConditionals: true,
362
+ checksVoidReturn: true,
363
+ checksSpreads: true
364
+ }
365
+ ],
366
+ '@typescript-eslint/no-misused-spread': 'error',
367
+ '@typescript-eslint/no-mixed-enums': 'error',
368
+ '@typescript-eslint/no-namespace': [
369
+ 'error',
370
+ {
371
+ allowDeclarations: false,
372
+ allowDefinitionFiles: true
373
+ }
374
+ ],
375
+ 'no-shadow': 'off',
376
+ '@typescript-eslint/no-shadow': [
377
+ 'error',
378
+ {
379
+ builtinGlobals: true,
380
+ hoist: 'functions',
381
+ ignoreOnInitialization: false,
382
+ ignoreTypeValueShadow: true,
383
+ ignoreFunctionTypeParameterNameValueShadow: false
384
+ }
385
+ ],
386
+ '@typescript-eslint/no-unnecessary-boolean-literal-compare': 'error',
387
+ '@typescript-eslint/no-unnecessary-condition': 'error',
388
+ '@typescript-eslint/no-unnecessary-parameter-property-assignment': 'error',
389
+ '@typescript-eslint/no-unnecessary-qualifier': 'error',
390
+ '@typescript-eslint/no-unnecessary-template-expression': 'error',
391
+ '@typescript-eslint/no-unnecessary-type-arguments': 'error',
392
+ '@typescript-eslint/no-unnecessary-type-assertion': 'error',
393
+ '@typescript-eslint/no-unnecessary-type-constraint': 'error',
394
+ '@typescript-eslint/no-unnecessary-type-conversion': 'error',
395
+ '@typescript-eslint/no-unnecessary-type-parameters': 'error'
396
+
397
+ }
398
+ };
@@ -0,0 +1,174 @@
1
+ /**
2
+ * @type {import('eslint').Rule.RuleModule}
3
+ */
4
+ export const chainFirstOnNewlineRule = {
5
+ meta: {
6
+ type: 'layout',
7
+ docs: {
8
+ description: 'Enforce or forbid a newline before the first element in a chained expression.',
9
+ category: 'Stylistic Issues',
10
+ recommended: false
11
+ },
12
+ fixable: 'whitespace',
13
+ schema: [
14
+ {
15
+ enum: [
16
+ 'require',
17
+ 'forbid'
18
+ ]
19
+ }
20
+ ],
21
+ messages: {
22
+ require: 'Expected a newline before the first element of the chain.',
23
+ forbid: 'Unexpected newline before the first element of the chain.'
24
+ }
25
+ },
26
+
27
+ create(context) {
28
+
29
+ const sourceCode = context.sourceCode;
30
+ const option = context.options[0] || 'require';
31
+
32
+ function isChainable(node) {
33
+
34
+ if (!node)
35
+ return false;
36
+ return node.type === 'MemberExpression' || node.type === 'CallExpression';
37
+
38
+ }
39
+
40
+ function check(node) { // node is the end of the chain
41
+
42
+ let firstMemberExpr = null;
43
+ let current = node;
44
+
45
+ // Traverse down to find the root and the first member expression
46
+ while (isChainable(current)) {
47
+
48
+ if (current.type === 'MemberExpression') {
49
+
50
+ firstMemberExpr = current;
51
+ current = current.object;
52
+
53
+ } else { // CallExpression
54
+
55
+ current = current.callee;
56
+
57
+ }
58
+
59
+ }
60
+ const root = current;
61
+
62
+ if (!firstMemberExpr) {
63
+
64
+ return;
65
+
66
+ }
67
+
68
+ const endOfChainNode = node.type === 'CallExpression'
69
+ ? node.callee
70
+ : node;
71
+
72
+ // Ignore single-line chains.
73
+ if (root.loc.start.line === endOfChainNode.loc.end.line) {
74
+
75
+ return;
76
+
77
+ }
78
+
79
+ const object = firstMemberExpr.object;
80
+ const property = firstMemberExpr.property;
81
+
82
+ const dotToken = sourceCode.getTokenBefore(property);
83
+
84
+ const objectEndLine = object.loc.end.line;
85
+ const propertyStartLine = dotToken.loc.start.line;
86
+
87
+ const isSameLine = objectEndLine === propertyStartLine;
88
+
89
+ if (option === 'require') {
90
+
91
+ if (isSameLine) {
92
+
93
+ context
94
+ .report({
95
+ node: property,
96
+ messageId: 'require',
97
+ loc: dotToken.loc,
98
+ fix(fixer) {
99
+
100
+ return fixer
101
+ .insertTextBefore(
102
+ dotToken,
103
+ '\n'
104
+ );
105
+
106
+ }
107
+ });
108
+
109
+ }
110
+
111
+ } else { // 'forbid'
112
+
113
+ if (!isSameLine) {
114
+
115
+ context
116
+ .report({
117
+ node: property,
118
+ messageId: 'forbid',
119
+ loc: dotToken.loc,
120
+ fix(fixer) {
121
+
122
+ const rangeToRemove = [
123
+ object.range[1],
124
+ dotToken.range[0]
125
+ ];
126
+ return fixer.removeRange(rangeToRemove);
127
+
128
+ }
129
+ });
130
+
131
+ }
132
+
133
+ }
134
+
135
+ }
136
+
137
+ return {
138
+ 'MemberExpression:exit'(node) {
139
+
140
+ if (node.parent.type === 'MemberExpression' && node.parent.object === node)
141
+ return;
142
+ if (node.parent.type === 'CallExpression' && node.parent.callee === node)
143
+ return;
144
+ if (node.parent.type === 'CallExpression' && node
145
+ .parent
146
+ .arguments
147
+ .includes(node))
148
+ return;
149
+ check(node);
150
+
151
+ },
152
+ 'CallExpression:exit'(node) {
153
+
154
+ if (node.parent.type === 'MemberExpression' && node.parent.object === node)
155
+ return;
156
+ if (node.parent.type === 'CallExpression' && node.parent.callee === node)
157
+ return;
158
+ if (node.parent.type === 'CallExpression'
159
+ && node
160
+ .parent
161
+ .arguments
162
+ .includes(node))
163
+ return;
164
+ if (isChainable(node.callee)) {
165
+
166
+ check(node);
167
+
168
+ }
169
+
170
+ }
171
+ };
172
+
173
+ }
174
+ };
@@ -0,0 +1,99 @@
1
+ /**
2
+ * @type {import('eslint').Rule.RuleModule}
3
+ */
4
+ export const jsxMultilinePropNewlineRule = {
5
+ meta: {
6
+ type: 'layout',
7
+ docs: {
8
+ description: 'Enforces a new line for the first prop if any prop\'s value is multiline.',
9
+ category: 'Stylistic Issues',
10
+ recommended: false
11
+ },
12
+ fixable: 'code',
13
+ schema: [],
14
+ messages: { error: 'The first property should be on a new line if any property\'s value is multiline.' }
15
+ },
16
+
17
+ create(context) {
18
+
19
+ const sourceCode = context.sourceCode;
20
+
21
+ function isMultiline(node) {
22
+
23
+ return node && node.loc && node.loc.start.line < node.loc.end.line;
24
+
25
+ }
26
+
27
+ return {
28
+ JSXOpeningElement(node) {
29
+
30
+ if (node.attributes.length === 0) {
31
+
32
+ return;
33
+
34
+ }
35
+
36
+ const hasMultilineProp = node.attributes.some(
37
+ (attr) => {
38
+
39
+ // Check for JSXAttribute (e.g., href={...})
40
+ if (attr.type === 'JSXAttribute') {
41
+
42
+ return isMultiline(attr.value);
43
+
44
+ }
45
+
46
+ // Check for JSXSpreadAttribute (e.g., {...props})
47
+ if (attr.type === 'JSXSpreadAttribute') {
48
+
49
+ return isMultiline(attr);
50
+
51
+ }
52
+ return false;
53
+
54
+ }
55
+ );
56
+
57
+ if (!hasMultilineProp) {
58
+
59
+ return;
60
+
61
+ }
62
+
63
+ const firstProp = node.attributes[0];
64
+
65
+ // Check if the tag name and the first prop are on the same line.
66
+ if (node.name.loc.end.line === firstProp.loc.start.line) {
67
+
68
+ context.report({
69
+ node: firstProp,
70
+ messageId: 'error',
71
+ fix(fixer) {
72
+
73
+ // Get indentation of the line with the opening tag.
74
+ const line = sourceCode
75
+ .getLines()
76
+ [node.loc.start.line - 1];
77
+ const baseIndentMatch = line.match(/^\s*/);
78
+ const baseIndent = baseIndentMatch
79
+ ? baseIndentMatch[0]
80
+ : '';
81
+
82
+ // Assume an indent of 2 spaces, a common practice.
83
+ const indent = baseIndent + ' ';
84
+
85
+ return fixer.insertTextBefore(
86
+ firstProp,
87
+ `\n${indent}`
88
+ );
89
+
90
+ }
91
+ });
92
+
93
+ }
94
+
95
+ }
96
+ };
97
+
98
+ }
99
+ };