@siim-m/config 0.0.0-stage → 0.2.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.
Files changed (91) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +57 -2
  3. package/dist/anti-slop/LICENSE +21 -0
  4. package/dist/anti-slop/effect/baseline.d.ts +18 -0
  5. package/dist/anti-slop/effect/baseline.js +41 -0
  6. package/dist/anti-slop/effect/index.d.ts +3 -0
  7. package/dist/anti-slop/effect/index.js +16 -0
  8. package/dist/anti-slop/effect/rules/no-manual-effect-error-tag.d.ts +1 -0
  9. package/dist/anti-slop/effect/rules/no-manual-effect-error-tag.js +39 -0
  10. package/dist/anti-slop/effect/rules/no-service-constructor-imports.d.ts +2 -0
  11. package/dist/anti-slop/effect/rules/no-service-constructor-imports.js +44 -0
  12. package/dist/anti-slop/effect/rules/no-service-parameters.d.ts +2 -0
  13. package/dist/anti-slop/effect/rules/no-service-parameters.js +181 -0
  14. package/dist/anti-slop/effect/rules/prefer-effect-match.d.ts +1 -0
  15. package/dist/anti-slop/effect/rules/prefer-effect-match.js +48 -0
  16. package/dist/anti-slop/effect/shared/tagged-values.d.ts +8 -0
  17. package/dist/anti-slop/effect/shared/tagged-values.js +68 -0
  18. package/dist/anti-slop/index.d.ts +3 -0
  19. package/dist/anti-slop/index.js +42 -0
  20. package/dist/anti-slop/rules/no-chained-type-assertions.d.ts +2 -0
  21. package/dist/anti-slop/rules/no-chained-type-assertions.js +60 -0
  22. package/dist/anti-slop/rules/no-conditional-empty-object-spread.d.ts +2 -0
  23. package/dist/anti-slop/rules/no-conditional-empty-object-spread.js +40 -0
  24. package/dist/anti-slop/rules/no-known-value-widening.d.ts +2 -0
  25. package/dist/anti-slop/rules/no-known-value-widening.js +300 -0
  26. package/dist/anti-slop/rules/no-module-mocking.d.ts +2 -0
  27. package/dist/anti-slop/rules/no-module-mocking.js +69 -0
  28. package/dist/anti-slop/rules/no-object-parameters.d.ts +2 -0
  29. package/dist/anti-slop/rules/no-object-parameters.js +56 -0
  30. package/dist/anti-slop/rules/no-reduce-accumulator-copy.d.ts +2 -0
  31. package/dist/anti-slop/rules/no-reduce-accumulator-copy.js +99 -0
  32. package/dist/anti-slop/rules/no-reflect-apply.d.ts +2 -0
  33. package/dist/anti-slop/rules/no-reflect-apply.js +25 -0
  34. package/dist/anti-slop/rules/no-reflect-get.d.ts +2 -0
  35. package/dist/anti-slop/rules/no-reflect-get.js +25 -0
  36. package/dist/anti-slop/rules/no-runtime-typeof.d.ts +2 -0
  37. package/dist/anti-slop/rules/no-runtime-typeof.js +64 -0
  38. package/dist/anti-slop/rules/no-unknown-parameters.d.ts +2 -0
  39. package/dist/anti-slop/rules/no-unknown-parameters.js +51 -0
  40. package/dist/anti-slop/rules/no-unknown-returns.d.ts +2 -0
  41. package/dist/anti-slop/rules/no-unknown-returns.js +58 -0
  42. package/dist/anti-slop/rules/no-unknown-type-aliases.d.ts +2 -0
  43. package/dist/anti-slop/rules/no-unknown-type-aliases.js +40 -0
  44. package/dist/anti-slop/rules/no-unsafe-dictionary-type.d.ts +2 -0
  45. package/dist/anti-slop/rules/no-unsafe-dictionary-type.js +134 -0
  46. package/dist/anti-slop/rules/no-widen-then-assert.d.ts +2 -0
  47. package/dist/anti-slop/rules/no-widen-then-assert.js +270 -0
  48. package/dist/anti-slop/rules/require-pascal-case-namespace-imports.d.ts +8 -0
  49. package/dist/anti-slop/rules/require-pascal-case-namespace-imports.js +36 -0
  50. package/dist/anti-slop/rules/require-readable-spacing.d.ts +3 -0
  51. package/dist/anti-slop/rules/require-readable-spacing.js +65 -0
  52. package/dist/anti-slop/rules/require-safety-comment-for-type-assertion.d.ts +2 -0
  53. package/dist/anti-slop/rules/require-safety-comment-for-type-assertion.js +100 -0
  54. package/dist/anti-slop/shared/array-method.d.ts +12 -0
  55. package/dist/anti-slop/shared/array-method.js +84 -0
  56. package/dist/anti-slop/shared/dictionary-types.d.ts +20 -0
  57. package/dist/anti-slop/shared/dictionary-types.js +377 -0
  58. package/dist/anti-slop/shared/function-parameters.d.ts +8 -0
  59. package/dist/anti-slop/shared/function-parameters.js +40 -0
  60. package/dist/anti-slop/shared/lexical-type-parameters.d.ts +5 -0
  61. package/dist/anti-slop/shared/lexical-type-parameters.js +47 -0
  62. package/dist/anti-slop/shared/reflect-method.d.ts +3 -0
  63. package/dist/anti-slop/shared/reflect-method.js +20 -0
  64. package/dist/anti-slop/shared/scope.d.ts +3 -0
  65. package/dist/anti-slop/shared/scope.js +11 -0
  66. package/dist/anti-slop/shared/type-alias-resolution.d.ts +23 -0
  67. package/dist/anti-slop/shared/type-alias-resolution.js +162 -0
  68. package/dist/anti-slop/vendor/eslint-stylistic/LICENSE +22 -0
  69. package/dist/anti-slop/vendor/eslint-stylistic/padding-line-ast.d.ts +27 -0
  70. package/dist/anti-slop/vendor/eslint-stylistic/padding-line-ast.js +28 -0
  71. package/dist/anti-slop/vendor/eslint-stylistic/padding-line-between-statements.d.ts +37 -0
  72. package/dist/anti-slop/vendor/eslint-stylistic/padding-line-between-statements.js +703 -0
  73. package/dist/anti-slop/vendor/eslint-stylistic/padding-line-options.d.ts +87 -0
  74. package/dist/anti-slop-react/LICENSE +21 -0
  75. package/dist/anti-slop-react/index.d.ts +3 -0
  76. package/dist/anti-slop-react/index.js +14 -0
  77. package/dist/anti-slop-react/rules/no-hookless-use-function.d.ts +8 -0
  78. package/dist/anti-slop-react/rules/no-hookless-use-function.js +67 -0
  79. package/dist/anti-slop-react/rules/no-manual-memo.d.ts +7 -0
  80. package/dist/anti-slop-react/rules/no-manual-memo.js +58 -0
  81. package/dist/anti-slop-react/rules/require-sync-comment-for-effect.d.ts +8 -0
  82. package/dist/anti-slop-react/rules/require-sync-comment-for-effect.js +64 -0
  83. package/dist/oxfmt.d.ts +27 -0
  84. package/dist/oxfmt.js +19 -0
  85. package/dist/oxlint-react.d.ts +62 -0
  86. package/dist/oxlint-react.js +62 -0
  87. package/dist/oxlint.d.ts +44 -0
  88. package/dist/oxlint.js +44 -0
  89. package/package.json +63 -4
  90. package/reset.d.ts +4 -0
  91. package/tsconfig.base.json +148 -0
@@ -0,0 +1,703 @@
1
+ import { isClosingBraceToken, isFunction, isNotSemicolonToken, isParenthesized, isSemicolonToken, isSingleLine, isTokenOnSameLine, isTopLevelExpressionStatement, LINEBREAKS, skipChainExpression, } from './padding-line-ast.js';
2
+ const CJS_EXPORT = /^(?:module\s*\.\s*)?exports(?:\s*\.|\s*\[|$)/u;
3
+ const CJS_IMPORT = /^require\(/u;
4
+ const LT = `[${Array.from(LINEBREAKS).join('')}]`;
5
+ const PADDING_LINE_SEQUENCE = new RegExp(String.raw `^(\s*?${LT})\s*${LT}(\s*;?)$`, 'u');
6
+ function isSelectorOption(option) {
7
+ return typeof option === 'object' && !Array.isArray(option);
8
+ }
9
+ /**
10
+ * Creates tester which check if a node starts with specific keyword with the
11
+ * appropriate AST_NODE_TYPES.
12
+ * @param keyword The keyword to test.
13
+ * @returns the created tester.
14
+ * @private
15
+ */
16
+ function newKeywordTester(type, keyword) {
17
+ return {
18
+ test(node, sourceCode) {
19
+ const isSameKeyword = sourceCode.getFirstToken(node)?.value === keyword;
20
+ const isSameType = Array.isArray(type)
21
+ ? type.includes(node.type)
22
+ : type === node.type;
23
+ return isSameKeyword && isSameType;
24
+ },
25
+ };
26
+ }
27
+ /**
28
+ * Creates tester which check if a node is specific type.
29
+ * @param type The node type to test.
30
+ * @returns the created tester.
31
+ * @private
32
+ */
33
+ function newNodeTypeTester(type) {
34
+ return {
35
+ test: (node) => node.type === type,
36
+ };
37
+ }
38
+ /**
39
+ * Checks the given node is an expression statement of IIFE.
40
+ * @param node The node to check.
41
+ * @returns `true` if the node is an expression statement of IIFE.
42
+ * @private
43
+ */
44
+ function isIIFEStatement(node) {
45
+ if (node.type === 'ExpressionStatement') {
46
+ let expression = skipChainExpression(node.expression);
47
+ if (expression.type === 'UnaryExpression')
48
+ expression = skipChainExpression(expression.argument);
49
+ if (expression.type === 'CallExpression') {
50
+ let node = expression.callee;
51
+ while (node.type === 'SequenceExpression') {
52
+ const lastExpression = node.expressions.at(-1);
53
+ if (lastExpression === undefined)
54
+ throw new Error('Padding rule invariant: sequence expression is empty');
55
+ node = lastExpression;
56
+ }
57
+ return isFunction(node);
58
+ }
59
+ }
60
+ return false;
61
+ }
62
+ /**
63
+ * Checks the given node is a CommonJS require statement
64
+ * @param node The node to check.
65
+ * @returns `true` if the node is a CommonJS require statement.
66
+ * @private
67
+ */
68
+ function isCJSRequire(node) {
69
+ if (node.type === 'VariableDeclaration') {
70
+ const declaration = node.declarations[0];
71
+ if (declaration?.init) {
72
+ let call = declaration?.init;
73
+ while (call.type === 'MemberExpression')
74
+ call = call.object;
75
+ if (call.type === 'CallExpression'
76
+ && call.callee.type === 'Identifier') {
77
+ return call.callee.name === 'require';
78
+ }
79
+ }
80
+ }
81
+ return false;
82
+ }
83
+ /**
84
+ * Checks whether the given node is a block-like statement.
85
+ * This checks the last token of the node is the closing brace of a block.
86
+ * @param sourceCode The source code to get tokens.
87
+ * @param node The node to check.
88
+ * @returns `true` if the node is a block-like statement.
89
+ * @private
90
+ */
91
+ function isBlockLikeStatement(node, sourceCode) {
92
+ // do-while with a block is a block-like statement.
93
+ if (node.type === 'DoWhileStatement'
94
+ && node.body.type === 'BlockStatement') {
95
+ return true;
96
+ }
97
+ /**
98
+ * IIFE is a block-like statement specially from
99
+ * JSCS#disallowPaddingNewLinesAfterBlocks.
100
+ */
101
+ if (isIIFEStatement(node))
102
+ return true;
103
+ // Checks the last token is a closing brace of blocks.
104
+ const lastToken = sourceCode.getLastToken(node, isNotSemicolonToken);
105
+ const belongingNode = lastToken && isClosingBraceToken(lastToken)
106
+ ? sourceCode.getNodeByRangeIndex(lastToken.range[0])
107
+ : null;
108
+ return (!!belongingNode
109
+ && (belongingNode.type === 'BlockStatement'
110
+ || belongingNode.type === 'SwitchStatement'));
111
+ }
112
+ /**
113
+ * Check whether the given node is a directive or not.
114
+ * @param node The node to check.
115
+ * @param sourceCode The source code object to get tokens.
116
+ * @returns `true` if the node is a directive.
117
+ */
118
+ function isDirective(node, sourceCode) {
119
+ return (isTopLevelExpressionStatement(node)
120
+ && node.expression.type === 'Literal'
121
+ && typeof node.expression.value === 'string'
122
+ && !isParenthesized(node.expression, sourceCode));
123
+ }
124
+ /**
125
+ * Check whether the given node is a part of directive prologue or not.
126
+ * @param node The node to check.
127
+ * @param sourceCode The source code object to get tokens.
128
+ * @returns `true` if the node is a part of directive prologue.
129
+ */
130
+ function isDirectivePrologue(node, sourceCode) {
131
+ if (isDirective(node, sourceCode)
132
+ && node.parent
133
+ && 'body' in node.parent
134
+ && Array.isArray(node.parent.body)) {
135
+ for (const sibling of node.parent.body) {
136
+ if (sibling === node)
137
+ break;
138
+ if (!isDirective(sibling, sourceCode))
139
+ return false;
140
+ }
141
+ return true;
142
+ }
143
+ return false;
144
+ }
145
+ /**
146
+ * Checks the given node is a CommonJS export statement
147
+ * @param node The node to check.
148
+ * @returns `true` if the node is a CommonJS export statement.
149
+ * @private
150
+ */
151
+ function isCJSExport(node) {
152
+ if (node.type === 'ExpressionStatement') {
153
+ const expression = node.expression;
154
+ if (expression.type === 'AssignmentExpression') {
155
+ let left = expression.left;
156
+ if (left.type === 'MemberExpression') {
157
+ while (left.object.type === 'MemberExpression')
158
+ left = left.object;
159
+ return (left.object.type === 'Identifier'
160
+ && (left.object.name === 'exports'
161
+ || (left.object.name === 'module'
162
+ && left.property.type === 'Identifier'
163
+ && left.property.name === 'exports')));
164
+ }
165
+ }
166
+ }
167
+ return false;
168
+ }
169
+ /**
170
+ * Check whether the given node is an expression
171
+ * @param node The node to check.
172
+ * @param sourceCode The source code object to get tokens.
173
+ * @returns `true` if the node is an expression
174
+ */
175
+ function isExpression(node, sourceCode) {
176
+ return (node.type === 'ExpressionStatement'
177
+ && !isDirectivePrologue(node, sourceCode));
178
+ }
179
+ /**
180
+ * Gets the actual last token.
181
+ *
182
+ * If a semicolon is semicolon-less style's semicolon, this ignores it.
183
+ * For example:
184
+ *
185
+ * foo()
186
+ * ;[1, 2, 3].forEach(bar)
187
+ * @param sourceCode The source code to get tokens.
188
+ * @param node The node to get.
189
+ * @returns The actual last token.
190
+ * @private
191
+ */
192
+ function getActualLastToken(node, sourceCode) {
193
+ const semiToken = sourceCode.getLastToken(node);
194
+ const prevToken = sourceCode.getTokenBefore(semiToken);
195
+ const nextToken = sourceCode.getTokenAfter(semiToken);
196
+ const isSemicolonLessStyle = prevToken
197
+ && nextToken
198
+ && prevToken.range[0] >= node.range[0]
199
+ && isSemicolonToken(semiToken)
200
+ && !isTokenOnSameLine(prevToken, semiToken)
201
+ && isTokenOnSameLine(semiToken, nextToken);
202
+ return isSemicolonLessStyle ? prevToken : semiToken;
203
+ }
204
+ /**
205
+ * This returns the concatenation of the first 2 captured strings.
206
+ * @param _ Unused. Whole matched string.
207
+ * @param trailingSpaces The trailing spaces of the first line.
208
+ * @param indentSpaces The indentation spaces of the last line.
209
+ * @returns The concatenation of trailingSpaces and indentSpaces.
210
+ * @private
211
+ */
212
+ function replacerToRemovePaddingLines(_, trailingSpaces, indentSpaces) {
213
+ return trailingSpaces + indentSpaces;
214
+ }
215
+ function getReportLoc(node, sourceCode) {
216
+ if (isSingleLine(node))
217
+ return node.loc;
218
+ const line = node.loc.start.line;
219
+ const sourceLine = sourceCode.lines[line - 1];
220
+ if (sourceLine === undefined)
221
+ throw new Error('Padding rule invariant: statement source line is missing');
222
+ return {
223
+ start: node.loc.start,
224
+ end: {
225
+ line,
226
+ column: sourceLine.length,
227
+ },
228
+ };
229
+ }
230
+ /**
231
+ * Check and report statements for `any` configuration.
232
+ * It does nothing.
233
+ *
234
+ * @private
235
+ */
236
+ function verifyForAny() {
237
+ // Empty
238
+ }
239
+ /**
240
+ * Check and report statements for `never` configuration.
241
+ * This autofix removes blank lines between the given 2 statements.
242
+ * However, if comments exist between 2 blank lines, it does not remove those
243
+ * blank lines automatically.
244
+ * @param context The rule context to report.
245
+ * @param _ Unused. The previous node to check.
246
+ * @param nextNode The next node to check.
247
+ * @param paddingLines The array of token pairs that blank
248
+ * lines exist between the pair.
249
+ *
250
+ * @private
251
+ */
252
+ function verifyForNever(context, _, nextNode, paddingLines) {
253
+ if (paddingLines.length === 0)
254
+ return;
255
+ context.report({
256
+ node: nextNode,
257
+ messageId: 'unexpectedBlankLine',
258
+ loc: getReportLoc(nextNode, context.sourceCode),
259
+ fix(fixer) {
260
+ if (paddingLines.length >= 2)
261
+ return null;
262
+ const paddingPair = paddingLines[0];
263
+ if (paddingPair === undefined)
264
+ throw new Error('Padding rule invariant: reported padding pair is missing');
265
+ const [prevToken, nextToken] = paddingPair;
266
+ const start = prevToken.range[1];
267
+ const end = nextToken.range[0];
268
+ const text = context
269
+ .sourceCode
270
+ .text
271
+ .slice(start, end)
272
+ .replace(PADDING_LINE_SEQUENCE, replacerToRemovePaddingLines);
273
+ return fixer.replaceTextRange([start, end], text);
274
+ },
275
+ });
276
+ }
277
+ /**
278
+ * Check and report statements for `always` configuration.
279
+ * This autofix inserts a blank line between the given 2 statements.
280
+ * If the `prevNode` has trailing comments, it inserts a blank line after the
281
+ * trailing comments.
282
+ * @param context The rule context to report.
283
+ * @param prevNode The previous node to check.
284
+ * @param nextNode The next node to check.
285
+ * @param paddingLines The array of token pairs that blank
286
+ * lines exist between the pair.
287
+ *
288
+ * @private
289
+ */
290
+ function verifyForAlways(context, prevNode, nextNode, paddingLines) {
291
+ if (paddingLines.length > 0)
292
+ return;
293
+ context.report({
294
+ node: nextNode,
295
+ messageId: 'expectedBlankLine',
296
+ loc: getReportLoc(nextNode, context.sourceCode),
297
+ fix(fixer) {
298
+ const sourceCode = context.sourceCode;
299
+ let prevToken = getActualLastToken(prevNode, sourceCode);
300
+ const nextToken = sourceCode.getFirstTokenBetween(prevToken, nextNode, {
301
+ includeComments: true,
302
+ /**
303
+ * Skip the trailing comments of the previous node.
304
+ * This inserts a blank line after the last trailing comment.
305
+ *
306
+ * For example:
307
+ *
308
+ * foo(); // trailing comment.
309
+ * // comment.
310
+ * bar();
311
+ *
312
+ * Get fixed to:
313
+ *
314
+ * foo(); // trailing comment.
315
+ *
316
+ * // comment.
317
+ * bar();
318
+ * @param token The token to check.
319
+ * @returns `true` if the token is not a trailing comment.
320
+ * @private
321
+ */
322
+ filter(token) {
323
+ if (isTokenOnSameLine(prevToken, token)) {
324
+ prevToken = token;
325
+ return false;
326
+ }
327
+ return true;
328
+ },
329
+ }) || nextNode;
330
+ const insertText = isTokenOnSameLine(prevToken, nextToken)
331
+ ? '\n\n'
332
+ : '\n';
333
+ return fixer.insertTextAfter(prevToken, insertText);
334
+ },
335
+ });
336
+ }
337
+ /**
338
+ * Types of blank lines.
339
+ * `any`, `never`, and `always` are defined.
340
+ * Those have `verify` method to check and report statements.
341
+ * @private
342
+ */
343
+ const PaddingTypes = {
344
+ any: { verify: verifyForAny },
345
+ never: { verify: verifyForNever },
346
+ always: { verify: verifyForAlways },
347
+ };
348
+ const MaybeMultilineStatementType = {
349
+ 'block-like': { test: isBlockLikeStatement },
350
+ 'expression': { test: isExpression },
351
+ 'return': newKeywordTester('ReturnStatement', 'return'),
352
+ 'export': newKeywordTester([
353
+ 'ExportAllDeclaration',
354
+ 'ExportDefaultDeclaration',
355
+ 'ExportNamedDeclaration',
356
+ ], 'export'),
357
+ 'var': newKeywordTester('VariableDeclaration', 'var'),
358
+ 'let': newKeywordTester('VariableDeclaration', 'let'),
359
+ 'const': newKeywordTester('VariableDeclaration', 'const'),
360
+ 'using': {
361
+ test: node => node.type === 'VariableDeclaration'
362
+ && (node.kind === 'using' || node.kind === 'await using'),
363
+ },
364
+ 'type': newKeywordTester('TSTypeAliasDeclaration', 'type'),
365
+ };
366
+ /**
367
+ * Types of statements.
368
+ * Those have `test` method to check it matches to the given statement.
369
+ * @private
370
+ */
371
+ const StatementTypes = {
372
+ '*': { test: () => true },
373
+ 'exports': { test: isCJSExport },
374
+ 'require': { test: isCJSRequire },
375
+ 'directive': { test: isDirectivePrologue },
376
+ 'iife': { test: isIIFEStatement },
377
+ 'block': newNodeTypeTester('BlockStatement'),
378
+ 'empty': newNodeTypeTester('EmptyStatement'),
379
+ 'function': newNodeTypeTester('FunctionDeclaration'),
380
+ 'ts-method': newNodeTypeTester('TSMethodSignature'),
381
+ 'break': newKeywordTester('BreakStatement', 'break'),
382
+ 'case': newKeywordTester('SwitchCase', 'case'),
383
+ 'class': newKeywordTester('ClassDeclaration', 'class'),
384
+ 'continue': newKeywordTester('ContinueStatement', 'continue'),
385
+ 'debugger': newKeywordTester('DebuggerStatement', 'debugger'),
386
+ 'default': newKeywordTester(['SwitchCase', 'ExportDefaultDeclaration'], 'default'),
387
+ 'do': newKeywordTester('DoWhileStatement', 'do'),
388
+ 'for': newKeywordTester([
389
+ 'ForStatement',
390
+ 'ForInStatement',
391
+ 'ForOfStatement',
392
+ ], 'for'),
393
+ 'if': newKeywordTester('IfStatement', 'if'),
394
+ 'import': newKeywordTester('ImportDeclaration', 'import'),
395
+ 'switch': newKeywordTester('SwitchStatement', 'switch'),
396
+ 'throw': newKeywordTester('ThrowStatement', 'throw'),
397
+ 'try': newKeywordTester('TryStatement', 'try'),
398
+ 'while': newKeywordTester(['WhileStatement', 'DoWhileStatement'], 'while'),
399
+ 'with': newKeywordTester('WithStatement', 'with'),
400
+ 'cjs-export': {
401
+ test: (node, sourceCode) => node.type === 'ExpressionStatement'
402
+ && node.expression.type === 'AssignmentExpression'
403
+ && CJS_EXPORT.test(sourceCode.getText(node.expression.left)),
404
+ },
405
+ 'cjs-import': {
406
+ test: (node, sourceCode) => node.type === 'VariableDeclaration'
407
+ && node.declarations.length > 0
408
+ && node.declarations[0]?.init != null
409
+ && CJS_IMPORT.test(sourceCode.getText(node.declarations[0].init)),
410
+ },
411
+ 'enum': newKeywordTester('TSEnumDeclaration', 'enum'),
412
+ 'interface': newKeywordTester('TSInterfaceDeclaration', 'interface'),
413
+ 'function-overload': newNodeTypeTester('TSDeclareFunction'),
414
+ ...Object.fromEntries(Object.entries(MaybeMultilineStatementType)
415
+ .flatMap(([key, value]) => [
416
+ [key, value],
417
+ [
418
+ `singleline-${key}`,
419
+ {
420
+ ...value,
421
+ test: (node, sourceCode) => value.test(node, sourceCode) && isSingleLine(node),
422
+ },
423
+ ],
424
+ [
425
+ `multiline-${key}`,
426
+ {
427
+ ...value,
428
+ test: (node, sourceCode) => value.test(node, sourceCode) && !isSingleLine(node),
429
+ },
430
+ ],
431
+ ])),
432
+ };
433
+ /**
434
+ * Build the vendored padding rule with caller-owned, typed policy options.
435
+ * `statementTypes` registers caller-defined statement types, looked up before
436
+ * the upstream table, for syntax the upstream matchers do not distinguish.
437
+ */
438
+ export default function createPaddingLineRule(options, statementTypes) {
439
+ const customStatementTypes = statementTypes ?? {};
440
+ return {
441
+ meta: {
442
+ type: 'layout',
443
+ docs: {
444
+ description: 'Require or disallow padding lines between statements',
445
+ },
446
+ fixable: 'whitespace',
447
+ hasSuggestions: false,
448
+ // This is intentionally an array schema as you can pass 0..n config objects
449
+ schema: {
450
+ $defs: {
451
+ paddingType: {
452
+ type: 'string',
453
+ enum: Object.keys(PaddingTypes),
454
+ },
455
+ statementType: {
456
+ type: 'string',
457
+ enum: Object.keys(StatementTypes),
458
+ },
459
+ selectorOption: {
460
+ type: 'object',
461
+ properties: {
462
+ selector: {
463
+ type: 'string',
464
+ },
465
+ lineMode: {
466
+ type: 'string',
467
+ enum: ['any', 'singleline', 'multiline'],
468
+ },
469
+ },
470
+ required: ['selector'],
471
+ additionalProperties: false,
472
+ },
473
+ statementMatcher: {
474
+ anyOf: [
475
+ { $ref: '#/$defs/statementType' },
476
+ { $ref: '#/$defs/selectorOption' },
477
+ ],
478
+ },
479
+ statementOption: {
480
+ anyOf: [
481
+ { $ref: '#/$defs/statementMatcher' },
482
+ {
483
+ type: 'array',
484
+ items: { $ref: '#/$defs/statementMatcher' },
485
+ minItems: 1,
486
+ uniqueItems: true,
487
+ additionalItems: false,
488
+ },
489
+ ],
490
+ },
491
+ },
492
+ type: 'array',
493
+ additionalItems: false,
494
+ items: {
495
+ type: 'object',
496
+ properties: {
497
+ blankLine: { $ref: '#/$defs/paddingType' },
498
+ prev: { $ref: '#/$defs/statementOption' },
499
+ next: { $ref: '#/$defs/statementOption' },
500
+ },
501
+ additionalProperties: false,
502
+ required: ['blankLine', 'prev', 'next'],
503
+ },
504
+ },
505
+ messages: {
506
+ unexpectedBlankLine: 'Unexpected blank line before this statement.',
507
+ expectedBlankLine: 'Expected blank line before this statement.',
508
+ },
509
+ },
510
+ create(context) {
511
+ const sourceCode = context.sourceCode;
512
+ const selectorMatchedNodes = new Map();
513
+ const pendingPairs = [];
514
+ function collectSelectorOption(option) {
515
+ if (Array.isArray(option)) {
516
+ for (const item of option)
517
+ collectSelectorOption(item);
518
+ return;
519
+ }
520
+ if (!isSelectorOption(option))
521
+ return;
522
+ selectorMatchedNodes.set(option.selector, new Set());
523
+ }
524
+ for (const configure of options) {
525
+ collectSelectorOption(configure.prev);
526
+ collectSelectorOption(configure.next);
527
+ }
528
+ let scopeInfo = null;
529
+ /**
530
+ * Processes to enter to new scope.
531
+ * This manages the current previous statement.
532
+ *
533
+ * @private
534
+ */
535
+ function enterScope() {
536
+ scopeInfo = {
537
+ upper: scopeInfo,
538
+ prevNode: null,
539
+ };
540
+ }
541
+ /**
542
+ * Processes to exit from the current scope.
543
+ *
544
+ * @private
545
+ */
546
+ function exitScope() {
547
+ if (scopeInfo)
548
+ scopeInfo = scopeInfo.upper;
549
+ }
550
+ /**
551
+ * Checks whether the given node matches the given type.
552
+ * @param node The statement node to check.
553
+ * @param type The statement type to check.
554
+ * @returns `true` if the statement node matched the type.
555
+ * @private
556
+ */
557
+ function match(node, type) {
558
+ let innerStatementNode = node;
559
+ while (innerStatementNode.type === 'LabeledStatement')
560
+ innerStatementNode = innerStatementNode.body;
561
+ if (Array.isArray(type))
562
+ return type.some(match.bind(null, innerStatementNode));
563
+ if (isSelectorOption(type)) {
564
+ const matchedNodes = selectorMatchedNodes.get(type.selector);
565
+ if (!matchedNodes?.has(innerStatementNode))
566
+ return false;
567
+ const lineMode = type.lineMode;
568
+ if (lineMode === 'singleline')
569
+ return isSingleLine(innerStatementNode);
570
+ else if (lineMode === 'multiline')
571
+ return !isSingleLine(innerStatementNode);
572
+ return true;
573
+ }
574
+ else {
575
+ const statementType = customStatementTypes[type] ?? StatementTypes[type];
576
+ if (statementType === undefined)
577
+ throw new Error(`Padding rule invariant: unsupported statement type ${type}`);
578
+ return statementType.test(innerStatementNode, sourceCode);
579
+ }
580
+ }
581
+ /**
582
+ * Finds the last matched configure from options.
583
+ * @param prevNode The previous statement to match.
584
+ * @param nextNode The current statement to match.
585
+ * @returns The tester of the last matched configure.
586
+ * @private
587
+ */
588
+ function getPaddingType(prevNode, nextNode) {
589
+ for (let i = options.length - 1; i >= 0; --i) {
590
+ const configure = options[i];
591
+ if (configure === undefined)
592
+ throw new Error('Padding rule invariant: configuration entry is missing');
593
+ if (match(prevNode, configure.prev)
594
+ && match(nextNode, configure.next)) {
595
+ return PaddingTypes[configure.blankLine];
596
+ }
597
+ }
598
+ return PaddingTypes.any;
599
+ }
600
+ /**
601
+ * Gets padding line sequences between the given 2 statements.
602
+ * Comments are separators of the padding line sequences.
603
+ * @param prevNode The previous statement to count.
604
+ * @param nextNode The current statement to count.
605
+ * @returns The array of token pairs.
606
+ * @private
607
+ */
608
+ function getPaddingLineSequences(prevNode, nextNode) {
609
+ const pairs = [];
610
+ let prevToken = getActualLastToken(prevNode, sourceCode);
611
+ if (nextNode.loc.start.line - prevToken.loc.end.line >= 2) {
612
+ do {
613
+ const token = sourceCode.getTokenAfter(prevToken, {
614
+ includeComments: true,
615
+ });
616
+ if (token.loc.start.line - prevToken.loc.end.line >= 2)
617
+ pairs.push([prevToken, token]);
618
+ prevToken = token;
619
+ } while (prevToken.range[0] < nextNode.range[0]);
620
+ }
621
+ return pairs;
622
+ }
623
+ /**
624
+ * Verify padding lines between the given node and the previous node.
625
+ * @param node The node to verify.
626
+ *
627
+ * @private
628
+ */
629
+ function verify(node) {
630
+ if (!node.parent
631
+ || ![
632
+ 'BlockStatement',
633
+ 'Program',
634
+ 'StaticBlock',
635
+ 'SwitchCase',
636
+ 'SwitchStatement',
637
+ 'TSInterfaceBody',
638
+ 'TSModuleBlock',
639
+ 'TSTypeLiteral',
640
+ ].includes(node.parent.type)) {
641
+ return;
642
+ }
643
+ // Save this node as the current previous statement.
644
+ const prevNode = scopeInfo.prevNode;
645
+ // Verify.
646
+ if (prevNode)
647
+ pendingPairs.push({ prevNode, nextNode: node });
648
+ scopeInfo.prevNode = node;
649
+ }
650
+ function verifyPendingPairs() {
651
+ for (const { prevNode, nextNode } of pendingPairs) {
652
+ const type = getPaddingType(prevNode, nextNode);
653
+ const paddingLines = getPaddingLineSequences(prevNode, nextNode);
654
+ type.verify(context, prevNode, nextNode, paddingLines);
655
+ }
656
+ }
657
+ /**
658
+ * Verify padding lines between the given node and the previous node.
659
+ * Then process to enter to new scope.
660
+ * @param node The node to verify.
661
+ *
662
+ * @private
663
+ */
664
+ function verifyThenEnterScope(node) {
665
+ verify(node);
666
+ enterScope();
667
+ }
668
+ const selectorMatchListeners = Object.fromEntries(Array.from(selectorMatchedNodes.keys(), selector => [
669
+ selector,
670
+ (node) => {
671
+ selectorMatchedNodes.get(selector)?.add(node);
672
+ },
673
+ ]));
674
+ return {
675
+ 'Program': enterScope,
676
+ 'Program:exit': () => {
677
+ verifyPendingPairs();
678
+ exitScope();
679
+ },
680
+ 'BlockStatement': enterScope,
681
+ 'BlockStatement:exit': exitScope,
682
+ 'SwitchStatement': enterScope,
683
+ 'SwitchStatement:exit': exitScope,
684
+ 'SwitchCase': verifyThenEnterScope,
685
+ 'SwitchCase:exit': exitScope,
686
+ 'StaticBlock': enterScope,
687
+ 'StaticBlock:exit': exitScope,
688
+ 'TSInterfaceBody': enterScope,
689
+ 'TSInterfaceBody:exit': exitScope,
690
+ 'TSModuleBlock': enterScope,
691
+ 'TSModuleBlock:exit': exitScope,
692
+ 'TSTypeLiteral': enterScope,
693
+ 'TSTypeLiteral:exit': exitScope,
694
+ 'TSDeclareFunction': verifyThenEnterScope,
695
+ 'TSDeclareFunction:exit': exitScope,
696
+ 'TSMethodSignature': verifyThenEnterScope,
697
+ 'TSMethodSignature:exit': exitScope,
698
+ ':statement': verify,
699
+ ...selectorMatchListeners,
700
+ };
701
+ },
702
+ };
703
+ }