@croct/eslint-plugin 0.2.1 → 0.2.2
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.
- package/configs/index.d.ts +1 -0
- package/configs/javascript.d.ts +1 -0
- package/configs/javascript.js +1 -0
- package/index.d.ts +6 -0
- package/package.json +1 -1
- package/rules/index.d.ts +5 -0
- package/rules/index.js +2 -0
- package/rules/min-chained-call-depth/index.d.ts +6 -0
- package/rules/min-chained-call-depth/index.js +105 -0
- package/rules/newline-per-chained-call/index.js +5 -3
package/configs/index.d.ts
CHANGED
|
@@ -192,6 +192,7 @@ export declare const configs: {
|
|
|
192
192
|
'@croct/argument-spacing': string;
|
|
193
193
|
'@croct/complex-expression-spacing': string;
|
|
194
194
|
'@croct/newline-per-chained-call': string;
|
|
195
|
+
'@croct/min-chained-call-depth': string;
|
|
195
196
|
'newline-per-chained-call': string;
|
|
196
197
|
'no-plusplus': string;
|
|
197
198
|
'array-bracket-newline': string[];
|
package/configs/javascript.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export declare const javascript: {
|
|
|
5
5
|
'@croct/argument-spacing': string;
|
|
6
6
|
'@croct/complex-expression-spacing': string;
|
|
7
7
|
'@croct/newline-per-chained-call': string;
|
|
8
|
+
'@croct/min-chained-call-depth': string;
|
|
8
9
|
'newline-per-chained-call': string;
|
|
9
10
|
'no-plusplus': string;
|
|
10
11
|
'array-bracket-newline': string[];
|
package/configs/javascript.js
CHANGED
|
@@ -17,6 +17,7 @@ exports.javascript = {
|
|
|
17
17
|
'@croct/argument-spacing': 'error',
|
|
18
18
|
'@croct/complex-expression-spacing': 'error',
|
|
19
19
|
'@croct/newline-per-chained-call': 'error',
|
|
20
|
+
'@croct/min-chained-call-depth': 'error',
|
|
20
21
|
'newline-per-chained-call': 'off',
|
|
21
22
|
'no-plusplus': 'off',
|
|
22
23
|
'array-bracket-newline': [
|
package/index.d.ts
CHANGED
|
@@ -17,6 +17,11 @@ declare const configuration: {
|
|
|
17
17
|
CallExpression: (node: import("@typescript-eslint/types/dist/ast-spec").CallExpression) => void;
|
|
18
18
|
MemberExpression: (node: import("@typescript-eslint/types/dist/ast-spec").MemberExpression) => void;
|
|
19
19
|
}>;
|
|
20
|
+
'min-chained-call-depth': import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleModule<"unexpectedLineBreak", {
|
|
21
|
+
maxLineLength: number;
|
|
22
|
+
}[], {
|
|
23
|
+
CallExpression: (node: import("@typescript-eslint/types/dist/ast-spec").CallExpression) => void;
|
|
24
|
+
}>;
|
|
20
25
|
};
|
|
21
26
|
configs: {
|
|
22
27
|
cypress: {
|
|
@@ -212,6 +217,7 @@ declare const configuration: {
|
|
|
212
217
|
'@croct/argument-spacing': string;
|
|
213
218
|
'@croct/complex-expression-spacing': string;
|
|
214
219
|
'@croct/newline-per-chained-call': string;
|
|
220
|
+
'@croct/min-chained-call-depth': string;
|
|
215
221
|
'newline-per-chained-call': string;
|
|
216
222
|
'no-plusplus': string;
|
|
217
223
|
'array-bracket-newline': string[];
|
package/package.json
CHANGED
package/rules/index.d.ts
CHANGED
|
@@ -16,4 +16,9 @@ export declare const rules: {
|
|
|
16
16
|
CallExpression: (node: import("@typescript-eslint/types/dist/ast-spec").CallExpression) => void;
|
|
17
17
|
MemberExpression: (node: import("@typescript-eslint/types/dist/ast-spec").MemberExpression) => void;
|
|
18
18
|
}>;
|
|
19
|
+
'min-chained-call-depth': import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleModule<"unexpectedLineBreak", {
|
|
20
|
+
maxLineLength: number;
|
|
21
|
+
}[], {
|
|
22
|
+
CallExpression: (node: import("@typescript-eslint/types/dist/ast-spec").CallExpression) => void;
|
|
23
|
+
}>;
|
|
19
24
|
};
|
package/rules/index.js
CHANGED
|
@@ -5,9 +5,11 @@ const argument_spacing_1 = require("./argument-spacing");
|
|
|
5
5
|
const jsx_attribute_spacing_1 = require("./jsx-attribute-spacing");
|
|
6
6
|
const complex_expression_spacing_1 = require("./complex-expression-spacing");
|
|
7
7
|
const newline_per_chained_call_1 = require("./newline-per-chained-call");
|
|
8
|
+
const min_chained_call_depth_1 = require("./min-chained-call-depth");
|
|
8
9
|
exports.rules = {
|
|
9
10
|
'argument-spacing': argument_spacing_1.argumentSpacing,
|
|
10
11
|
'jsx-attribute-spacing': jsx_attribute_spacing_1.jsxAttributeSpacing,
|
|
11
12
|
'complex-expression-spacing': complex_expression_spacing_1.complexExpressionSpacing,
|
|
12
13
|
'newline-per-chained-call': newline_per_chained_call_1.newlinePerChainedCall,
|
|
14
|
+
'min-chained-call-depth': min_chained_call_depth_1.minChainedCallDepth,
|
|
13
15
|
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { TSESTree } from '@typescript-eslint/experimental-utils';
|
|
2
|
+
export declare const minChainedCallDepth: import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleModule<"unexpectedLineBreak", {
|
|
3
|
+
maxLineLength: number;
|
|
4
|
+
}[], {
|
|
5
|
+
CallExpression: (node: TSESTree.CallExpression) => void;
|
|
6
|
+
}>;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.minChainedCallDepth = void 0;
|
|
4
|
+
const ast_utils_1 = require("@typescript-eslint/utils/dist/ast-utils");
|
|
5
|
+
const createRule_1 = require("../createRule");
|
|
6
|
+
exports.minChainedCallDepth = (0, createRule_1.createRule)({
|
|
7
|
+
name: 'min-chained-call-depth',
|
|
8
|
+
meta: {
|
|
9
|
+
type: 'layout',
|
|
10
|
+
docs: {
|
|
11
|
+
description: 'Enforces a minimum depth for multiline chained calls.',
|
|
12
|
+
recommended: 'error',
|
|
13
|
+
},
|
|
14
|
+
fixable: 'whitespace',
|
|
15
|
+
schema: [
|
|
16
|
+
{
|
|
17
|
+
type: 'object',
|
|
18
|
+
properties: {
|
|
19
|
+
maxLineLength: {
|
|
20
|
+
type: 'integer',
|
|
21
|
+
minimum: 1,
|
|
22
|
+
default: 100,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
additionalProperties: false,
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
messages: {
|
|
29
|
+
unexpectedLineBreak: 'Unexpected line break.',
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
defaultOptions: [
|
|
33
|
+
{
|
|
34
|
+
maxLineLength: 100,
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
create: context => {
|
|
38
|
+
const sourceCode = context.getSourceCode();
|
|
39
|
+
function getDepth(node) {
|
|
40
|
+
let depth = 0;
|
|
41
|
+
let currentNode = node;
|
|
42
|
+
while (currentNode.type === 'CallExpression'
|
|
43
|
+
|| currentNode.type === 'MemberExpression') {
|
|
44
|
+
if (currentNode.type === 'MemberExpression') {
|
|
45
|
+
currentNode = currentNode.object;
|
|
46
|
+
}
|
|
47
|
+
else if (currentNode.type === 'CallExpression') {
|
|
48
|
+
currentNode = currentNode.callee;
|
|
49
|
+
}
|
|
50
|
+
depth += 1;
|
|
51
|
+
}
|
|
52
|
+
return depth;
|
|
53
|
+
}
|
|
54
|
+
function check(node) {
|
|
55
|
+
var _a, _b, _c;
|
|
56
|
+
if (((_a = node.parent) === null || _a === void 0 ? void 0 : _a.type) === 'MemberExpression'
|
|
57
|
+
|| ((_b = node.parent) === null || _b === void 0 ? void 0 : _b.type) === 'CallExpression'
|
|
58
|
+
|| node.callee.type !== 'MemberExpression'
|
|
59
|
+
|| node.callee.computed
|
|
60
|
+
|| node.callee.object.loc.end.line === node.callee.property.loc.start.line) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (getDepth(node) > 3) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const { maxLineLength = 100 } = (_c = context.options[0]) !== null && _c !== void 0 ? _c : {};
|
|
67
|
+
const { property } = node.callee;
|
|
68
|
+
const lastToken = sourceCode.getLastToken(node, {
|
|
69
|
+
filter: token => token.loc.end.line === property.loc.start.line,
|
|
70
|
+
});
|
|
71
|
+
const semicolon = sourceCode.getLastToken(node.parent, {
|
|
72
|
+
filter: token => (token.loc.start.line === property.loc.start.line
|
|
73
|
+
&& token.type === 'Punctuator'
|
|
74
|
+
&& token.value === ';'),
|
|
75
|
+
});
|
|
76
|
+
const lineLength = node.callee.object.loc.end.column
|
|
77
|
+
+ lastToken.loc.end.column - property.loc.start.column
|
|
78
|
+
+ 1
|
|
79
|
+
+ (semicolon !== null ? 1 : 0);
|
|
80
|
+
if (maxLineLength !== null && lineLength > maxLineLength) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const punctuator = sourceCode.getTokenBefore(node.callee.property);
|
|
84
|
+
const previousToken = sourceCode.getTokenBefore(punctuator, { includeComments: true });
|
|
85
|
+
const nextToken = sourceCode.getTokenAfter(punctuator, { includeComments: true });
|
|
86
|
+
if ((0, ast_utils_1.isCommentToken)(previousToken) || (0, ast_utils_1.isCommentToken)(nextToken)) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
context.report({
|
|
90
|
+
node: node,
|
|
91
|
+
loc: {
|
|
92
|
+
start: node.callee.object.loc.end,
|
|
93
|
+
end: node.callee.property.loc.start,
|
|
94
|
+
},
|
|
95
|
+
messageId: 'unexpectedLineBreak',
|
|
96
|
+
fix: fixer => fixer.replaceTextRange([previousToken.range[1], nextToken.range[0]], punctuator.value),
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
CallExpression: (node) => {
|
|
101
|
+
check(node);
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
},
|
|
105
|
+
});
|
|
@@ -53,6 +53,7 @@ exports.newlinePerChainedCall = (0, createRule_1.createRule)({
|
|
|
53
53
|
return token.value !== ')' || token.type !== 'Punctuator';
|
|
54
54
|
}
|
|
55
55
|
function validateCallExpressionIgnoreDepth(node) {
|
|
56
|
+
var _a;
|
|
56
57
|
let hasCallExpression = false;
|
|
57
58
|
if (node.type === 'CallExpression') {
|
|
58
59
|
hasCallExpression = true;
|
|
@@ -84,11 +85,12 @@ exports.newlinePerChainedCall = (0, createRule_1.createRule)({
|
|
|
84
85
|
.filter(hasObjectAndPropertyOnSameLine);
|
|
85
86
|
const rootNode = expressionsOnSameLine[expressionsOnSameLine.length - 1];
|
|
86
87
|
if (rootNode.type === 'MemberExpression'
|
|
87
|
-
&& rootNode.
|
|
88
|
+
&& ((_a = rootNode.parent) === null || _a === void 0 ? void 0 : _a.type) === 'CallExpression'
|
|
89
|
+
&& (rootNode.object.type === 'ThisExpression'
|
|
90
|
+
|| rootNode.object.type === 'Identifier')) {
|
|
88
91
|
expressionsOnSameLine.pop();
|
|
89
92
|
}
|
|
90
|
-
expressionsOnSameLine
|
|
91
|
-
.forEach(memberExpression => {
|
|
93
|
+
expressionsOnSameLine.forEach(memberExpression => {
|
|
92
94
|
context.report({
|
|
93
95
|
node: memberExpression.property,
|
|
94
96
|
loc: memberExpression.property.loc.start,
|