@croct/eslint-plugin 0.2.0 → 0.2.3

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.
@@ -74,6 +74,12 @@ export declare const configs: {
74
74
  };
75
75
  })[];
76
76
  };
77
+ overrides: {
78
+ files: string[];
79
+ rules: {
80
+ 'sort-keys-fix/sort-keys-fix': string;
81
+ };
82
+ }[];
77
83
  };
78
84
  typescript: {
79
85
  extends: string[];
@@ -142,6 +148,10 @@ export declare const configs: {
142
148
  };
143
149
  })[];
144
150
  'no-undef': string;
151
+ '@typescript-eslint/no-namespace': string;
152
+ '@typescript-eslint/no-redeclare': (string | {
153
+ ignoreDeclarationMerge: boolean;
154
+ })[];
145
155
  'no-new-object'?: undefined;
146
156
  };
147
157
  plugins?: undefined;
@@ -178,6 +188,8 @@ export declare const configs: {
178
188
  '@typescript-eslint/object-curly-spacing'?: undefined;
179
189
  '@typescript-eslint/member-delimiter-style'?: undefined;
180
190
  'no-undef'?: undefined;
191
+ '@typescript-eslint/no-namespace'?: undefined;
192
+ '@typescript-eslint/no-redeclare'?: undefined;
181
193
  };
182
194
  env: {
183
195
  jest: boolean;
@@ -192,6 +204,11 @@ export declare const configs: {
192
204
  '@croct/argument-spacing': string;
193
205
  '@croct/complex-expression-spacing': string;
194
206
  '@croct/newline-per-chained-call': string;
207
+ '@croct/min-chained-call-depth': string;
208
+ 'eslint-comments/disable-enable-pair': (string | {
209
+ allowWholeFile: boolean;
210
+ })[];
211
+ 'eslint-comments/require-description': string;
195
212
  'newline-per-chained-call': string;
196
213
  'no-plusplus': string;
197
214
  'array-bracket-newline': string[];
@@ -280,6 +297,7 @@ export declare const configs: {
280
297
  prev: string[];
281
298
  next: string;
282
299
  })[];
300
+ 'no-useless-escape': string;
283
301
  };
284
302
  overrides: {
285
303
  files: string[];
@@ -5,6 +5,11 @@ 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;
9
+ 'eslint-comments/disable-enable-pair': (string | {
10
+ allowWholeFile: boolean;
11
+ })[];
12
+ 'eslint-comments/require-description': string;
8
13
  'newline-per-chained-call': string;
9
14
  'no-plusplus': string;
10
15
  'array-bracket-newline': string[];
@@ -93,6 +98,7 @@ export declare const javascript: {
93
98
  prev: string[];
94
99
  next: string;
95
100
  })[];
101
+ 'no-useless-escape': string;
96
102
  };
97
103
  overrides: {
98
104
  files: string[];
@@ -5,6 +5,7 @@ exports.javascript = {
5
5
  extends: [
6
6
  'plugin:jest/recommended',
7
7
  'airbnb-base',
8
+ 'plugin:eslint-comments/recommended',
8
9
  ],
9
10
  plugins: [
10
11
  'jest',
@@ -17,6 +18,14 @@ exports.javascript = {
17
18
  '@croct/argument-spacing': 'error',
18
19
  '@croct/complex-expression-spacing': 'error',
19
20
  '@croct/newline-per-chained-call': 'error',
21
+ '@croct/min-chained-call-depth': 'error',
22
+ 'eslint-comments/disable-enable-pair': [
23
+ 'error',
24
+ {
25
+ allowWholeFile: true,
26
+ },
27
+ ],
28
+ 'eslint-comments/require-description': 'error',
20
29
  'newline-per-chained-call': 'off',
21
30
  'no-plusplus': 'off',
22
31
  'array-bracket-newline': [
@@ -204,6 +213,7 @@ exports.javascript = {
204
213
  ],
205
214
  },
206
215
  ],
216
+ 'no-useless-escape': 'error',
207
217
  },
208
218
  overrides: [
209
219
  {
@@ -60,4 +60,10 @@ export declare const react: {
60
60
  };
61
61
  })[];
62
62
  };
63
+ overrides: {
64
+ files: string[];
65
+ rules: {
66
+ 'sort-keys-fix/sort-keys-fix': string;
67
+ };
68
+ }[];
63
69
  };
package/configs/react.js CHANGED
@@ -17,6 +17,7 @@ exports.react = {
17
17
  'testing-library',
18
18
  'jest-dom',
19
19
  '@croct',
20
+ 'sort-keys-fix',
20
21
  ],
21
22
  rules: {
22
23
  '@croct/jsx-attribute-spacing': 'error',
@@ -120,4 +121,12 @@ exports.react = {
120
121
  },
121
122
  ],
122
123
  },
124
+ overrides: [
125
+ {
126
+ files: ['**/styles.ts'],
127
+ rules: {
128
+ 'sort-keys-fix/sort-keys-fix': 'error',
129
+ },
130
+ },
131
+ ],
123
132
  };
@@ -65,6 +65,10 @@ export declare const typescript: {
65
65
  };
66
66
  })[];
67
67
  'no-undef': string;
68
+ '@typescript-eslint/no-namespace': string;
69
+ '@typescript-eslint/no-redeclare': (string | {
70
+ ignoreDeclarationMerge: boolean;
71
+ })[];
68
72
  'no-new-object'?: undefined;
69
73
  };
70
74
  plugins?: undefined;
@@ -101,6 +105,8 @@ export declare const typescript: {
101
105
  '@typescript-eslint/object-curly-spacing'?: undefined;
102
106
  '@typescript-eslint/member-delimiter-style'?: undefined;
103
107
  'no-undef'?: undefined;
108
+ '@typescript-eslint/no-namespace'?: undefined;
109
+ '@typescript-eslint/no-redeclare'?: undefined;
104
110
  };
105
111
  env: {
106
112
  jest: boolean;
@@ -77,6 +77,10 @@ exports.typescript = {
77
77
  },
78
78
  }],
79
79
  'no-undef': 'off',
80
+ '@typescript-eslint/no-namespace': 'off',
81
+ '@typescript-eslint/no-redeclare': ['error', {
82
+ ignoreDeclarationMerge: true,
83
+ }],
80
84
  },
81
85
  },
82
86
  {
package/index.d.ts CHANGED
@@ -17,6 +17,12 @@ 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
+ MemberExpression: (node: import("@typescript-eslint/types/dist/ast-spec").MemberExpression) => void;
25
+ }>;
20
26
  };
21
27
  configs: {
22
28
  cypress: {
@@ -94,6 +100,12 @@ declare const configuration: {
94
100
  };
95
101
  })[];
96
102
  };
103
+ overrides: {
104
+ files: string[];
105
+ rules: {
106
+ 'sort-keys-fix/sort-keys-fix': string;
107
+ };
108
+ }[];
97
109
  };
98
110
  typescript: {
99
111
  extends: string[];
@@ -162,6 +174,10 @@ declare const configuration: {
162
174
  };
163
175
  })[];
164
176
  'no-undef': string;
177
+ '@typescript-eslint/no-namespace': string;
178
+ '@typescript-eslint/no-redeclare': (string | {
179
+ ignoreDeclarationMerge: boolean;
180
+ })[];
165
181
  'no-new-object'?: undefined;
166
182
  };
167
183
  plugins?: undefined;
@@ -198,6 +214,8 @@ declare const configuration: {
198
214
  '@typescript-eslint/object-curly-spacing'?: undefined;
199
215
  '@typescript-eslint/member-delimiter-style'?: undefined;
200
216
  'no-undef'?: undefined;
217
+ '@typescript-eslint/no-namespace'?: undefined;
218
+ '@typescript-eslint/no-redeclare'?: undefined;
201
219
  };
202
220
  env: {
203
221
  jest: boolean;
@@ -212,6 +230,11 @@ declare const configuration: {
212
230
  '@croct/argument-spacing': string;
213
231
  '@croct/complex-expression-spacing': string;
214
232
  '@croct/newline-per-chained-call': string;
233
+ '@croct/min-chained-call-depth': string;
234
+ 'eslint-comments/disable-enable-pair': (string | {
235
+ allowWholeFile: boolean;
236
+ })[];
237
+ 'eslint-comments/require-description': string;
215
238
  'newline-per-chained-call': string;
216
239
  'no-plusplus': string;
217
240
  'array-bracket-newline': string[];
@@ -300,6 +323,7 @@ declare const configuration: {
300
323
  prev: string[];
301
324
  next: string;
302
325
  })[];
326
+ 'no-useless-escape': string;
303
327
  };
304
328
  overrides: {
305
329
  files: string[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@croct/eslint-plugin",
3
- "version": "0.2.0",
3
+ "version": "0.2.3",
4
4
  "description": "ESLint rules and presets applied to all Croct JavaScript projects.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -42,6 +42,7 @@
42
42
  "eslint-config-airbnb": "^19.0",
43
43
  "eslint-config-airbnb-base": "^15.0",
44
44
  "eslint-plugin-cypress": "^2.12",
45
+ "eslint-plugin-eslint-comments": "^3.2.0",
45
46
  "eslint-plugin-import": "^2.25",
46
47
  "eslint-plugin-import-newlines": "^1.1",
47
48
  "eslint-plugin-jest": "^26.1",
@@ -61,6 +62,7 @@
61
62
  "eslint": "^8.8",
62
63
  "eslint-plugin-eslint-plugin": "^4.1.0",
63
64
  "eslint-plugin-self": "^1.2.1",
65
+ "eslint-plugin-sort-keys-fix": "^1.1.2",
64
66
  "jest": "^27.5",
65
67
  "ts-jest": "^27.1",
66
68
  "typescript": "^4.5"
package/rules/index.d.ts CHANGED
@@ -16,4 +16,10 @@ 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
+ MemberExpression: (node: import("@typescript-eslint/types/dist/ast-spec").MemberExpression) => void;
24
+ }>;
19
25
  };
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,7 @@
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
+ MemberExpression: (node: TSESTree.MemberExpression) => void;
7
+ }>;
@@ -0,0 +1,131 @@
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
+ depth += 1;
47
+ }
48
+ else if (currentNode.type === 'CallExpression') {
49
+ currentNode = currentNode.callee;
50
+ }
51
+ }
52
+ return depth;
53
+ }
54
+ function check(node) {
55
+ var _a, _b;
56
+ // If the node is a member expression inside a call expression skip, this is to ensure
57
+ // that we consider the correct line length of the result.
58
+ //
59
+ // Example:
60
+ // ```ts
61
+ // foo
62
+ // .bar();
63
+ // ```
64
+ // The replacement of this input should be `foo.bar();`, which has 10 character.
65
+ // Without this check it would consider the length up to `r`, which is 7.
66
+ if (node.type === 'MemberExpression' && ((_a = node.parent) === null || _a === void 0 ? void 0 : _a.type) === 'CallExpression') {
67
+ return;
68
+ }
69
+ // If the node is a call expression we need to validate it's callee as a member
70
+ // expression.
71
+ // If the node itself is already a member expression, like the
72
+ // `property` in `this.property.function()`, we validate the node directly.
73
+ const callee = node.type === 'CallExpression' ? node.callee : node;
74
+ if (
75
+ // If the callee is not a member expression, we can skip.
76
+ // For example, root level calls like `foo();`.
77
+ callee.type !== 'MemberExpression'
78
+ // If the callee is a computed member expression, like `foo[bar]()`, we can skip.
79
+ || callee.computed
80
+ // If the callee is already in the same line as it's object, we can skip.
81
+ || callee.object.loc.end.line === callee.property.loc.start.line) {
82
+ return;
83
+ }
84
+ // We only inline the first level of chained calls.
85
+ // If the current call is nested inside another call, we can skip.
86
+ if (getDepth(callee) > 1) {
87
+ return;
88
+ }
89
+ const { maxLineLength = 100 } = (_b = context.options[0]) !== null && _b !== void 0 ? _b : {};
90
+ const { property } = callee;
91
+ const lastToken = sourceCode.getLastToken(node, {
92
+ filter: token => token.loc.end.line === property.loc.start.line,
93
+ });
94
+ const semicolon = sourceCode.getLastToken(node.parent, {
95
+ filter: token => (token.loc.start.line === property.loc.start.line
96
+ && token.type === 'Punctuator'
97
+ && token.value === ';'),
98
+ });
99
+ const lineLength = callee.object.loc.end.column
100
+ + lastToken.loc.end.column - property.loc.start.column
101
+ + 1
102
+ + (semicolon !== null ? 1 : 0);
103
+ if (maxLineLength !== null && lineLength > maxLineLength) {
104
+ return;
105
+ }
106
+ const punctuator = sourceCode.getTokenBefore(callee.property);
107
+ const previousToken = sourceCode.getTokenBefore(punctuator, { includeComments: true });
108
+ const nextToken = sourceCode.getTokenAfter(punctuator, { includeComments: true });
109
+ if ((0, ast_utils_1.isCommentToken)(previousToken) || (0, ast_utils_1.isCommentToken)(nextToken)) {
110
+ return;
111
+ }
112
+ context.report({
113
+ node: node,
114
+ loc: {
115
+ start: callee.object.loc.end,
116
+ end: callee.property.loc.start,
117
+ },
118
+ messageId: 'unexpectedLineBreak',
119
+ fix: fixer => fixer.replaceTextRange([previousToken.range[1], nextToken.range[0]], punctuator.value),
120
+ });
121
+ }
122
+ return {
123
+ CallExpression: (node) => {
124
+ check(node);
125
+ },
126
+ MemberExpression: (node) => {
127
+ check(node);
128
+ },
129
+ };
130
+ },
131
+ });
@@ -42,8 +42,7 @@ exports.newlinePerChainedCall = (0, createRule_1.createRule)({
42
42
  const sourceCode = context.getSourceCode();
43
43
  function getPropertyText(node) {
44
44
  const prefix = '.';
45
- const lines = sourceCode.getText(node.property)
46
- .split(LINEBREAK_MATCHER);
45
+ const lines = sourceCode.getText(node.property).split(LINEBREAK_MATCHER);
47
46
  return prefix + lines[0];
48
47
  }
49
48
  function hasObjectAndPropertyOnSameLine(node) {
@@ -53,6 +52,7 @@ exports.newlinePerChainedCall = (0, createRule_1.createRule)({
53
52
  return token.value !== ')' || token.type !== 'Punctuator';
54
53
  }
55
54
  function validateCallExpressionIgnoreDepth(node) {
55
+ var _a, _b;
56
56
  let hasCallExpression = false;
57
57
  if (node.type === 'CallExpression') {
58
58
  hasCallExpression = true;
@@ -80,9 +80,17 @@ exports.newlinePerChainedCall = (0, createRule_1.createRule)({
80
80
  if (memberExpressions.length > ignoreChainWithDepth
81
81
  && hasCallExpression
82
82
  && memberExpressions.some(hasObjectAndPropertyOnSameLine)) {
83
- memberExpressions
84
- .filter(hasObjectAndPropertyOnSameLine)
85
- .forEach(memberExpression => {
83
+ const expressionsOnSameLine = memberExpressions
84
+ .filter(hasObjectAndPropertyOnSameLine);
85
+ const rootNode = expressionsOnSameLine[expressionsOnSameLine.length - 1];
86
+ if (rootNode.type === 'MemberExpression'
87
+ && (((_a = rootNode.parent) === null || _a === void 0 ? void 0 : _a.type) === 'CallExpression'
88
+ || ((_b = rootNode.parent) === null || _b === void 0 ? void 0 : _b.type) === 'MemberExpression')
89
+ && (rootNode.object.type === 'ThisExpression'
90
+ || rootNode.object.type === 'Identifier')) {
91
+ expressionsOnSameLine.pop();
92
+ }
93
+ expressionsOnSameLine.forEach(memberExpression => {
86
94
  context.report({
87
95
  node: memberExpression.property,
88
96
  loc: memberExpression.property.loc.start,