@kikiutils/eslint-config 12.0.4 → 12.0.5

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 (41) hide show
  1. package/README.md +3 -3
  2. package/dist/_internals/rules/comment-indent.d.ts.map +1 -1
  3. package/dist/_internals/rules/comment-indent.js +36 -27
  4. package/dist/_internals/rules/comment-indent.js.map +1 -1
  5. package/dist/_internals/rules/compact-iteration-layout.js +1 -0
  6. package/dist/_internals/rules/compact-iteration-layout.js.map +1 -1
  7. package/dist/_internals/rules/consistent-parameter-layout.js +23 -36
  8. package/dist/_internals/rules/consistent-parameter-layout.js.map +1 -1
  9. package/dist/_internals/rules/no-composable-in-head-getter.d.ts.map +1 -1
  10. package/dist/_internals/rules/no-composable-in-head-getter.js +20 -27
  11. package/dist/_internals/rules/no-composable-in-head-getter.js.map +1 -1
  12. package/dist/_internals/rules/no-silent-catch.js +2 -2
  13. package/dist/_internals/rules/no-silent-catch.js.map +1 -1
  14. package/dist/_internals/rules/no-thenable-zod-refinement.d.ts.map +1 -1
  15. package/dist/_internals/rules/no-thenable-zod-refinement.js.map +1 -1
  16. package/dist/_internals/rules/prefer-enhanced-date.d.ts.map +1 -1
  17. package/dist/_internals/rules/prefer-enhanced-date.js +38 -42
  18. package/dist/_internals/rules/prefer-enhanced-date.js.map +1 -1
  19. package/dist/_internals/rules/statement-padding.js +1 -1
  20. package/dist/_internals/rules/statement-padding.js.map +1 -1
  21. package/dist/_internals/rules/utils.d.ts +1 -1
  22. package/dist/_internals/rules/utils.js.map +1 -1
  23. package/dist/base.js.map +1 -1
  24. package/dist/nuxt.js +1 -4
  25. package/dist/nuxt.js.map +1 -1
  26. package/dist/vue.d.ts.map +1 -1
  27. package/dist/vue.js +3 -2
  28. package/dist/vue.js.map +1 -1
  29. package/package.json +4 -4
  30. package/src/_internals/rules/comment-indent.ts +44 -52
  31. package/src/_internals/rules/compact-iteration-layout.ts +1 -0
  32. package/src/_internals/rules/consistent-parameter-layout.ts +41 -66
  33. package/src/_internals/rules/no-composable-in-head-getter.ts +23 -28
  34. package/src/_internals/rules/no-silent-catch.ts +2 -3
  35. package/src/_internals/rules/no-thenable-zod-refinement.ts +1 -2
  36. package/src/_internals/rules/prefer-enhanced-date.ts +58 -88
  37. package/src/_internals/rules/statement-padding.ts +1 -1
  38. package/src/_internals/rules/utils.ts +1 -1
  39. package/src/base.ts +0 -2
  40. package/src/nuxt.ts +2 -6
  41. package/src/vue.ts +4 -4
@@ -1,9 +1,4 @@
1
- import type {
2
- AST,
3
- Rule,
4
- } from 'eslint';
5
-
6
- type Comment = ReturnType<Rule.RuleContext['sourceCode']['getAllComments']>[number];
1
+ import type { Rule } from 'eslint';
7
2
 
8
3
  const closingPunctuators = new Set([
9
4
  ')',
@@ -21,58 +16,55 @@ export const commentIndent: Rule.RuleModule = {
21
16
  create(context) {
22
17
  const { sourceCode } = context;
23
18
  const getIndent = (line: number) => /^[\t ]*/.exec(sourceCode.lines[line - 1]!)![0];
24
- const startsLine = (token: AST.Token | Comment) => {
25
- const previous = sourceCode.getTokenBefore(token, { includeComments: true });
26
- return !previous || previous.loc!.end.line < token.loc!.start.line;
27
- };
28
19
 
29
20
  return {
30
21
  Program() {
31
- for (const comment of sourceCode.getAllComments()) {
32
- if (!startsLine(comment)) continue;
33
- // style/indent lets a comment align with the opening bracket before it, which leaves comments
34
- // at the outer depth after a fixer nests the bracket's contents one level deeper.
35
- let anchor = sourceCode.getTokenBefore(comment, { includeComments: true });
36
- while ((anchor?.type === 'Block' || anchor?.type === 'Line') && startsLine(anchor)) {
37
- anchor = sourceCode.getTokenBefore(anchor, { includeComments: true });
38
- }
22
+ const comments = sourceCode.getAllComments();
23
+ for (let index = 0; index < comments.length;) {
24
+ const firstIndex = index++;
25
+ const first = comments[firstIndex]!;
26
+ const opening = sourceCode.getTokenBefore(first, { includeComments: true });
27
+ if (opening?.type !== 'Punctuator' || !openingPunctuators.has(opening.value)) continue;
28
+ const next = sourceCode.getTokenAfter(first);
29
+ if (!next) break;
30
+ while (index < comments.length && comments[index]!.range![0] < next.range[0]) index++;
31
+ if (closingPunctuators.has(next.value) || comments[index - 1]!.loc!.end.line === next.loc.start.line) continue;
39
32
 
40
- if (anchor?.type !== 'Punctuator' || !openingPunctuators.has(anchor.value)) continue;
41
- const next = sourceCode.getTokenAfter(comment);
42
- if (!next || closingPunctuators.has(next.value) || !startsLine(next)) continue;
43
- const { end, start } = comment.loc!;
44
- // style/indent only lets a comment follow the next code when no blank line separates them.
45
- const gapLines = sourceCode.lines.slice(end.line, next.loc.start.line - 1);
46
- if (gapLines.some((text) => !text.trim())) continue;
47
- const indent = getIndent(start.line);
33
+ // Only comments after the last blank line can take the following code's indentation.
34
+ let lastBlank = next.loc.start.line - 1;
35
+ while (lastBlank > first.loc!.end.line && sourceCode.lines[lastBlank - 1]!.trim()) lastBlank--;
48
36
  const expected = getIndent(next.loc.start.line);
49
- if (indent === expected) continue;
50
- const commentLines = Array.from(
51
- { length: end.line - start.line + 1 },
52
- (_, index) => start.line + index,
53
- );
37
+ let previousEnd = opening.loc.end.line;
38
+ for (let position = firstIndex; position < index; position++) {
39
+ const comment = comments[position]!;
40
+ const { end, start } = comment.loc!;
41
+ if (start.line === previousEnd) break;
42
+ previousEnd = end.line;
43
+ if (end.line < lastBlank) continue;
44
+ const indent = getIndent(start.line);
45
+ if (indent === expected) continue;
46
+ context.report({
47
+ * fix(fixer) {
48
+ for (let line = start.line; line <= end.line; line++) {
49
+ if (!sourceCode.lines[line - 1]!.startsWith(indent)) continue;
50
+ const offset = sourceCode.getIndexFromLoc({
51
+ column: 0,
52
+ line,
53
+ });
54
54
 
55
- context.report({
56
- // Shift a multiline block comment as a whole so its continuation lines stay aligned.
57
- fix: (fixer) => commentLines
58
- .filter((line) => sourceCode.lines[line - 1]!.startsWith(indent))
59
- .map((line) => {
60
- const offset = sourceCode.getIndexFromLoc({
61
- column: 0,
62
- line,
63
- });
64
-
65
- return fixer.replaceTextRange(
66
- [
67
- offset,
68
- offset + indent.length,
69
- ],
70
- expected,
71
- );
72
- }),
73
- loc: comment.loc!,
74
- messageId: 'align',
75
- });
55
+ yield fixer.replaceTextRange(
56
+ [
57
+ offset,
58
+ offset + indent.length,
59
+ ],
60
+ expected,
61
+ );
62
+ }
63
+ },
64
+ loc: comment.loc!,
65
+ messageId: 'align',
66
+ });
67
+ }
76
68
  }
77
69
  },
78
70
  };
@@ -7,6 +7,7 @@ export const compactIterationLayout: Rule.RuleModule = {
7
7
  const { sourceCode } = context;
8
8
 
9
9
  function checkParentheses(node: IterationNode): void {
10
+ if (node.loc!.start.line === node.body.loc!.start.line) return;
10
11
  const openingParen = sourceCode.getFirstToken(node, (token) => token.value === '(')!;
11
12
  const closingParen = sourceCode.getTokenBefore(node.body)!;
12
13
  const next = sourceCode.getTokenAfter(openingParen)!;
@@ -43,66 +43,48 @@ export const consistentParameterLayout: Rule.RuleModule = {
43
43
  function checkTestCallLayout(node: CallNode): void {
44
44
  const items = node.arguments;
45
45
  if (!items.length) return;
46
+ if (
47
+ node.loc!.start.line === items[0]!.loc!.start.line
48
+ && items.every((item, index) => item.loc!.end.line === (items[index + 1]?.loc!.start.line ?? node.loc!.end.line))
49
+ ) return;
50
+
46
51
  const openingParen = getCallOpeningParen(node)!;
47
52
 
48
53
  const closingParen = tokenStore.getLastToken(node)!;
49
54
  const previous = tokenStore.getTokenBefore(closingParen)!;
50
- const gaps: Array<[AST.Token, AST.Token, string]> = [
51
- [
52
- openingParen,
53
- tokenStore.getTokenAfter(openingParen)!,
54
- '',
55
- ],
56
- [
57
- previous,
58
- closingParen,
59
- '',
60
- ],
61
- ];
55
+ const fixes: Rule.Fix[] = [];
56
+
57
+ function collectGap(left: AST.Token, right: AST.Token, text: string): boolean {
58
+ // A boundary comment keeps the whole list in its original layout.
59
+ if (tokenStore.commentsExistBetween(left, right)) return false;
60
+ if (left.loc.end.line !== right.loc.start.line) {
61
+ fixes.push({
62
+ range: [
63
+ left.range[1],
64
+ right.range[0],
65
+ ],
66
+ text,
67
+ });
68
+ }
62
69
 
63
- for (let index = 1; index < items.length; index++) {
64
- const comma = tokenStore.getTokensBetween(items[index - 1]!, items[index]!)
65
- .find((token) => token.type === 'Punctuator' && token.value === ',')!;
66
-
67
- gaps.push(
68
- [
69
- tokenStore.getTokenBefore(comma)!,
70
- comma,
71
- '',
72
- ],
73
- [
74
- comma,
75
- tokenStore.getTokenAfter(comma)!,
76
- ' ',
77
- ],
78
- );
70
+ return true;
79
71
  }
80
72
 
81
- if (previous.value === ',') {
82
- gaps.push([
83
- tokenStore.getTokenBefore(previous)!,
84
- previous,
85
- '',
86
- ]);
87
- }
73
+ if (!collectGap(openingParen, tokenStore.getTokenAfter(openingParen)!, '') || !collectGap(previous, closingParen, '')) return;
74
+ for (let index = 1; index < items.length; index++) {
75
+ const comma = tokenStore.getFirstTokenBetween(
76
+ items[index - 1]!,
77
+ items[index]!,
78
+ (token) => token.type === 'Punctuator' && token.value === ',',
79
+ )!;
88
80
 
89
- // Keep the whole list's layout when a boundary comment prevents safe compaction.
90
- if (gaps.some(([left, right]) => tokenStore.commentsExistBetween(left, right))) return;
91
- const multilineGaps = gaps.filter(([left, right]) => left.loc.end.line !== right.loc.start.line);
92
- if (!multilineGaps.length) return;
81
+ if (!collectGap(tokenStore.getTokenBefore(comma)!, comma, '') || !collectGap(comma, tokenStore.getTokenAfter(comma)!, ' ')) return;
82
+ }
93
83
 
84
+ if (previous.value === ',' && !collectGap(tokenStore.getTokenBefore(previous)!, previous, '')) return;
85
+ if (!fixes.length) return;
94
86
  context.report({
95
- fix: (fixer) => multilineGaps.map(([
96
- left,
97
- right,
98
- text,
99
- ]) => fixer.replaceTextRange(
100
- [
101
- left.range[1],
102
- right.range[0],
103
- ],
104
- text,
105
- )),
87
+ fix: () => fixes,
106
88
  messageId: 'compactTestCall',
107
89
  node,
108
90
  });
@@ -113,7 +95,7 @@ export const consistentParameterLayout: Rule.RuleModule = {
113
95
  items: readonly ListItem[],
114
96
  ): void {
115
97
  const first = items[0];
116
- if (!first) return;
98
+ if (!first || node.loc!.start.line === node.loc!.end.line) return;
117
99
  const isSingleParameter = items.length === 1;
118
100
 
119
101
  // JSX argument layout belongs to the JSX formatting rules.
@@ -133,23 +115,19 @@ export const consistentParameterLayout: Rule.RuleModule = {
133
115
  ? tokenStore.getLastToken(node)
134
116
  : tokenStore.getTokenAfter(items.at(-1)!, (token) => token.value === ')');
135
117
 
118
+ if (!closingParen || openingParen.loc.start.line === closingParen.loc.end.line) return;
136
119
  const next = tokenStore.getTokenAfter(openingParen)!;
137
-
138
120
  if (isSingleParameter) {
139
- if (!closingParen || closingParen.value !== ')') return;
140
- let previous = tokenStore.getTokenBefore(closingParen)!;
141
- if (previous.value === ',') previous = tokenStore.getTokenBefore(previous)!;
121
+ if (closingParen.value !== ')') return;
122
+ const previous = tokenStore.getTokenBefore(closingParen)!;
123
+ const hasOpeningNewline = openingParen.loc.end.line !== next.loc.start.line;
124
+ const hasClosingNewline = previous.loc.end.line !== closingParen.loc.start.line;
125
+ if (hasOpeningNewline === hasClosingNewline) return;
142
126
  if (
143
127
  tokenStore.commentsExistBetween(openingParen, next)
144
- || tokenStore.commentsExistBetween(previous, closingParen)
128
+ || tokenStore.commentsExistBetween(previous.value === ',' ? tokenStore.getTokenBefore(previous)! : previous, closingParen)
145
129
  ) return;
146
130
 
147
- const hasOpeningNewline = openingParen.loc.end.line !== next.loc.start.line;
148
- const hasClosingNewline = tokenStore.getTokenBefore(closingParen)!.loc.end.line
149
- !== closingParen.loc.start.line;
150
-
151
- if (hasOpeningNewline === hasClosingNewline) return;
152
-
153
131
  context.report({
154
132
  fix: (fixer) => hasOpeningNewline
155
133
  ? fixer.insertTextBefore(closingParen, '\n')
@@ -161,16 +139,13 @@ export const consistentParameterLayout: Rule.RuleModule = {
161
139
  return;
162
140
  }
163
141
 
164
- if (!closingParen || openingParen.loc.start.line === closingParen.loc.end.line) return;
165
-
166
142
  if (isTemplate) {
167
143
  // Script list formatters do not traverse Vue template expressions.
168
144
  const newlineOffsets: number[] = [];
169
145
  if (openingParen.loc.end.line === next.loc.start.line) newlineOffsets.push(openingParen.range[1]);
170
146
  for (let index = 1; index < items.length; index++) {
171
147
  const item = items[index]!;
172
- const comma = tokenStore.getTokensBetween(items[index - 1]!, item)
173
- .find((token) => token.value === ',');
148
+ const comma = tokenStore.getFirstTokenBetween(items[index - 1]!, item, (token) => token.value === ',');
174
149
 
175
150
  if (comma && comma.loc.end.line === tokenStore.getTokenAfter(comma)!.loc.start.line) {
176
151
  newlineOffsets.push(comma.range[1]);
@@ -17,22 +17,13 @@ const headComposables = new Set([
17
17
  ]);
18
18
 
19
19
  // Functions nested in the body are skipped: calling the outer function does not run them.
20
- function findCalledComposable(bodies: readonly ts.Node[]): string | undefined {
21
- let composable: string | undefined;
22
- const visit = (node: ts.Node): void => {
23
- if (composable || ts.isFunctionLike(node)) return;
24
- if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && composablePattern.test(node.expression.text)) {
25
- composable = node.expression.text;
26
- return;
27
- }
28
-
29
- ts.forEachChild(node, visit);
30
- };
31
-
32
- for (const body of bodies) {
33
- visit(body);
34
- if (composable) return composable;
20
+ function findCalledComposable(node: ts.Node): string | undefined {
21
+ if (ts.isFunctionLike(node)) return;
22
+ if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && composablePattern.test(node.expression.text)) {
23
+ return node.expression.text;
35
24
  }
25
+
26
+ return ts.forEachChild(node, findCalledComposable);
36
27
  }
37
28
 
38
29
  function getBody(node: ts.Node | undefined): ts.Node | undefined {
@@ -52,7 +43,7 @@ function getImplementationBodies(checker: ts.TypeChecker, call: ts.CallExpressio
52
43
 
53
44
  if (values.some(Boolean)) {
54
45
  return values
55
- .map((value) => getBody(value))
46
+ .map(getBody)
56
47
  .filter((body) => body !== undefined);
57
48
  }
58
49
 
@@ -62,7 +53,7 @@ function getImplementationBodies(checker: ts.TypeChecker, call: ts.CallExpressio
62
53
  const name = declaration && ts.isFunctionDeclaration(declaration) ? declaration.name : undefined;
63
54
  const implementations = name ? checker.getSymbolAtLocation(name)?.declarations ?? [] : [declaration];
64
55
  return implementations
65
- .map((implementation) => getBody(implementation))
56
+ .map(getBody)
66
57
  .filter((body) => body !== undefined);
67
58
  }
68
59
 
@@ -113,17 +104,21 @@ export const noComposableInHeadGetter: Rule.RuleModule = {
113
104
  }
114
105
 
115
106
  if (!checker || !nodeMap) return;
116
- const composable = findCalledComposable(getImplementationBodies(checker, nodeMap.get(node) as ts.CallExpression));
117
- if (!composable) return;
118
- context.report({
119
- data: {
120
- composable,
121
- function: context.sourceCode.getText(callee),
122
- head,
123
- },
124
- messageId: 'indirectGetter',
125
- node,
126
- });
107
+ for (const body of getImplementationBodies(checker, nodeMap.get(node) as ts.CallExpression)) {
108
+ const composable = findCalledComposable(body);
109
+ if (!composable) continue;
110
+ context.report({
111
+ data: {
112
+ composable,
113
+ function: context.sourceCode.getText(callee),
114
+ head,
115
+ },
116
+ messageId: 'indirectGetter',
117
+ node,
118
+ });
119
+
120
+ return;
121
+ }
127
122
  }
128
123
 
129
124
  return {
@@ -16,8 +16,8 @@ function isSilent(callback: CallArgument): boolean {
16
16
  if (callback.type !== 'ArrowFunctionExpression' && callback.type !== 'FunctionExpression') return false;
17
17
  const { body } = callback;
18
18
  if (body.type !== 'BlockStatement') return isNothing(body);
19
- const [statement, ...rest] = body.body;
20
- return !statement || (!rest.length && statement.type === 'ReturnStatement' && isNothing(statement.argument));
19
+ const [statement] = body.body;
20
+ return !statement || (body.body.length === 1 && statement.type === 'ReturnStatement' && isNothing(statement.argument));
21
21
  }
22
22
 
23
23
  export const noSilentCatch: Rule.RuleModule = {
@@ -32,7 +32,6 @@ export const noSilentCatch: Rule.RuleModule = {
32
32
  || !isSilent(callback)
33
33
  ) return;
34
34
 
35
- // A comment right before or inside the callback is the reason the rejection can be ignored.
36
35
  const { sourceCode } = context;
37
36
  if (sourceCode.getCommentsBefore(callback).length || sourceCode.getCommentsInside(callback).length) return;
38
37
 
@@ -19,8 +19,7 @@ function isNonPromiseThenable(checker: TypeChecker, type: Type): boolean {
19
19
  );
20
20
  }
21
21
 
22
- // Zod awaits a refinement only when it returns a native Promise (`instanceof Promise`, so subtypes and branded
23
- // intersections such as `SafePromise` count); any other thenable counts as a truthy result.
22
+ // Zod uses `instanceof Promise`, which also accepts subclasses and branded Promise values.
24
23
  function isPromise(checker: TypeChecker, type: Type): boolean {
25
24
  if (type.isIntersection()) return type.types.some((part) => isPromise(checker, part));
26
25
  if (type.getSymbol()?.getName() === 'Promise') return true;