@croct/eslint-plugin 0.5.0 → 0.6.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.
package/index.d.ts CHANGED
@@ -12,14 +12,18 @@ declare const configuration: {
12
12
  IfStatement: (node: import("@typescript-eslint/types/dist/generated/ast-spec").IfStatement) => void;
13
13
  }>;
14
14
  'newline-per-chained-call': import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleModule<"expectedLineBreak", {
15
- ignoreChainWithDepth: number;
15
+ ignoreChainDeeperThan: number;
16
16
  }[], {
17
17
  CallExpression: (node: import("@typescript-eslint/types/dist/generated/ast-spec").CallExpression) => void;
18
18
  MemberExpression: (node: import("@typescript-eslint/types/dist/generated/ast-spec").MemberExpression) => void;
19
19
  }>;
20
- 'min-chained-call-depth': import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleModule<"unexpectedLineBreak", {
20
+ 'min-chained-call-depth': import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleModule<"unexpectedLineBreak", ({
21
21
  maxLineLength: number;
22
- }[], {
22
+ ignoreChainDeeperThan?: undefined;
23
+ } | {
24
+ ignoreChainDeeperThan: number;
25
+ maxLineLength?: undefined;
26
+ })[], {
23
27
  CallExpression: (node: import("@typescript-eslint/types/dist/generated/ast-spec").CallExpression) => void;
24
28
  MemberExpression: (node: import("@typescript-eslint/types/dist/generated/ast-spec").MemberExpression) => void;
25
29
  }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@croct/eslint-plugin",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "ESLint rules and presets applied to all Croct JavaScript projects.",
5
5
  "license": "MIT",
6
6
  "author": {
package/rules/index.d.ts CHANGED
@@ -11,14 +11,18 @@ export declare const rules: {
11
11
  IfStatement: (node: import("@typescript-eslint/types/dist/generated/ast-spec").IfStatement) => void;
12
12
  }>;
13
13
  'newline-per-chained-call': import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleModule<"expectedLineBreak", {
14
- ignoreChainWithDepth: number;
14
+ ignoreChainDeeperThan: number;
15
15
  }[], {
16
16
  CallExpression: (node: import("@typescript-eslint/types/dist/generated/ast-spec").CallExpression) => void;
17
17
  MemberExpression: (node: import("@typescript-eslint/types/dist/generated/ast-spec").MemberExpression) => void;
18
18
  }>;
19
- 'min-chained-call-depth': import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleModule<"unexpectedLineBreak", {
19
+ 'min-chained-call-depth': import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleModule<"unexpectedLineBreak", ({
20
20
  maxLineLength: number;
21
- }[], {
21
+ ignoreChainDeeperThan?: undefined;
22
+ } | {
23
+ ignoreChainDeeperThan: number;
24
+ maxLineLength?: undefined;
25
+ })[], {
22
26
  CallExpression: (node: import("@typescript-eslint/types/dist/generated/ast-spec").CallExpression) => void;
23
27
  MemberExpression: (node: import("@typescript-eslint/types/dist/generated/ast-spec").MemberExpression) => void;
24
28
  }>;
@@ -1,7 +1,11 @@
1
1
  import { TSESTree } from '@typescript-eslint/experimental-utils';
2
- export declare const minChainedCallDepth: import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleModule<"unexpectedLineBreak", {
2
+ export declare const minChainedCallDepth: import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleModule<"unexpectedLineBreak", ({
3
3
  maxLineLength: number;
4
- }[], {
4
+ ignoreChainDeeperThan?: undefined;
5
+ } | {
6
+ ignoreChainDeeperThan: number;
7
+ maxLineLength?: undefined;
8
+ })[], {
5
9
  CallExpression: (node: TSESTree.CallExpression) => void;
6
10
  MemberExpression: (node: TSESTree.MemberExpression) => void;
7
11
  }>;
@@ -27,6 +27,12 @@ exports.minChainedCallDepth = (0, createRule_1.createRule)({
27
27
  minimum: 1,
28
28
  default: 100,
29
29
  },
30
+ ignoreChainDeeperThan: {
31
+ type: 'integer',
32
+ minimum: 1,
33
+ maximum: 10,
34
+ default: 2,
35
+ },
30
36
  },
31
37
  additionalProperties: false,
32
38
  },
@@ -39,9 +45,13 @@ exports.minChainedCallDepth = (0, createRule_1.createRule)({
39
45
  {
40
46
  maxLineLength: 100,
41
47
  },
48
+ {
49
+ ignoreChainDeeperThan: 3,
50
+ },
42
51
  ],
43
52
  create: context => {
44
53
  const sourceCode = context.getSourceCode();
54
+ let maxDepth = 0;
45
55
  function getDepth(node) {
46
56
  let depth = 0;
47
57
  let currentNode = node;
@@ -81,26 +91,42 @@ exports.minChainedCallDepth = (0, createRule_1.createRule)({
81
91
  ? node.callee
82
92
  : node;
83
93
  if (
84
- // If the callee is not a member expression, we can skip.
94
+ // If the callee is not a member expression, skip.
85
95
  // For example, root level calls like `foo();`.
86
96
  callee.type !== experimental_utils_1.AST_NODE_TYPES.MemberExpression
87
- // If the callee is a computed member expression, like `foo[bar]()`, we can skip.
97
+ // If the callee is a computed member expression, like `foo[bar]()`, skip.
88
98
  || callee.computed
89
99
  /* eslint-disable-next-line @typescript-eslint/ban-ts-comment --
90
- * NewExpression is a possible callee object type
91
- */
100
+ * NewExpression is a possible callee object type
101
+ */
92
102
  // @ts-ignore
93
103
  || callee.object.type === experimental_utils_1.AST_NODE_TYPES.NewExpression
94
- // If the callee is already in the same line as it's object, we can skip.
104
+ // If the callee is already in the same line as it's object, skip.
95
105
  || callee.object.loc.end.line === callee.property.loc.start.line) {
96
106
  return;
97
107
  }
98
- // We only inline the first level of chained calls.
99
- // If the current call is nested inside another call, we can skip.
100
- if (getDepth(callee) > 1) {
108
+ const currentDepth = getDepth(callee);
109
+ maxDepth = Math.max(maxDepth, currentDepth);
110
+ // Only affect the root level as the total depth is must be known.
111
+ // If the current call is nested inside another call, skip.
112
+ if (currentDepth > 1) {
113
+ return;
114
+ }
115
+ const { maxLineLength = 100, ignoreChainDeeperThan = 2 } = (_b = context.options[0]) !== null && _b !== void 0 ? _b : {};
116
+ // If the max depth is greater than ignore threshold, skip
117
+ //
118
+ // Example:
119
+ // ```ts
120
+ // Array(10)
121
+ // .fill(0)
122
+ // .map(x => x + 1)
123
+ // .slice(0, 5);
124
+ // ```
125
+ // In this case the depth is 3, and the default value of ignoreChainDeeperThan is 2.
126
+ // So the check can be skipped.
127
+ if (maxDepth > ignoreChainDeeperThan) {
101
128
  return;
102
129
  }
103
- const { maxLineLength = 100 } = (_b = context.options[0]) !== null && _b !== void 0 ? _b : {};
104
130
  const { property } = callee;
105
131
  const lastToken = sourceCode.getLastToken(node, {
106
132
  filter: token => token.loc.end.line === property.loc.start.line,
@@ -1,6 +1,6 @@
1
1
  import { TSESTree } from '@typescript-eslint/experimental-utils';
2
2
  export declare const newlinePerChainedCall: import("@typescript-eslint/utils/dist/ts-eslint/Rule").RuleModule<"expectedLineBreak", {
3
- ignoreChainWithDepth: number;
3
+ ignoreChainDeeperThan: number;
4
4
  }[], {
5
5
  CallExpression: (node: TSESTree.CallExpression) => void;
6
6
  MemberExpression: (node: TSESTree.MemberExpression) => void;
@@ -16,7 +16,7 @@ exports.newlinePerChainedCall = (0, createRule_1.createRule)({
16
16
  {
17
17
  type: 'object',
18
18
  properties: {
19
- ignoreChainWithDepth: {
19
+ ignoreChainDeeperThan: {
20
20
  type: 'integer',
21
21
  minimum: 1,
22
22
  maximum: 10,
@@ -32,13 +32,13 @@ exports.newlinePerChainedCall = (0, createRule_1.createRule)({
32
32
  },
33
33
  defaultOptions: [
34
34
  {
35
- ignoreChainWithDepth: 2,
35
+ ignoreChainDeeperThan: 2,
36
36
  },
37
37
  ],
38
38
  create: context => {
39
39
  var _a, _b;
40
40
  const options = (_a = context.options[0]) !== null && _a !== void 0 ? _a : {};
41
- const ignoreChainWithDepth = (_b = options.ignoreChainWithDepth) !== null && _b !== void 0 ? _b : 2;
41
+ const ignoreChainWithDepth = (_b = options.ignoreChainDeeperThan) !== null && _b !== void 0 ? _b : 2;
42
42
  const sourceCode = context.getSourceCode();
43
43
  function getPropertyText(node) {
44
44
  const prefix = '.';