@tai-kun/vp-lint-plugins 0.0.0 → 0.0.1

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 (107) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -43
  3. package/dist/src/index.d.ts +22 -0
  4. package/dist/src/index.d.ts.map +1 -0
  5. package/dist/src/index.js +21 -0
  6. package/dist/src/plugin.d.ts +3 -0
  7. package/dist/src/plugin.d.ts.map +1 -0
  8. package/dist/src/plugin.js +46 -0
  9. package/dist/src/rules/blank-line-after-control-block.d.ts +7 -0
  10. package/dist/src/rules/blank-line-after-control-block.d.ts.map +1 -0
  11. package/dist/src/rules/blank-line-after-control-block.js +54 -0
  12. package/dist/src/rules/blank-line-after-declaration.d.ts +7 -0
  13. package/dist/src/rules/blank-line-after-declaration.d.ts.map +1 -0
  14. package/dist/src/rules/blank-line-after-declaration.js +79 -0
  15. package/dist/src/rules/blank-line-after-variable-declaration.d.ts +8 -0
  16. package/dist/src/rules/blank-line-after-variable-declaration.d.ts.map +1 -0
  17. package/dist/src/rules/blank-line-after-variable-declaration.js +62 -0
  18. package/dist/src/rules/blank-line-before-return.d.ts +7 -0
  19. package/dist/src/rules/blank-line-before-return.d.ts.map +1 -0
  20. package/dist/src/rules/blank-line-before-return.js +81 -0
  21. package/dist/src/rules/blank-line-between-class-members.d.ts +8 -0
  22. package/dist/src/rules/blank-line-between-class-members.d.ts.map +1 -0
  23. package/dist/src/rules/blank-line-between-class-members.js +85 -0
  24. package/dist/src/rules/blank-line-between-switch-cases.d.ts +8 -0
  25. package/dist/src/rules/blank-line-between-switch-cases.d.ts.map +1 -0
  26. package/dist/src/rules/blank-line-between-switch-cases.js +45 -0
  27. package/dist/src/rules/blank-line.d.ts +14 -0
  28. package/dist/src/rules/blank-line.d.ts.map +1 -0
  29. package/dist/src/rules/blank-line.js +68 -0
  30. package/dist/src/rules/comment-japanese-spacing.d.ts +8 -0
  31. package/dist/src/rules/comment-japanese-spacing.d.ts.map +1 -0
  32. package/dist/src/rules/comment-japanese-spacing.js +195 -0
  33. package/dist/src/rules/comment-one-sentence-per-line.d.ts +9 -0
  34. package/dist/src/rules/comment-one-sentence-per-line.d.ts.map +1 -0
  35. package/dist/src/rules/comment-one-sentence-per-line.js +190 -0
  36. package/dist/src/rules/comment-utils.d.ts +30 -0
  37. package/dist/src/rules/comment-utils.d.ts.map +1 -0
  38. package/dist/src/rules/comment-utils.js +153 -0
  39. package/dist/src/rules/jsdoc-param-order.d.ts +6 -0
  40. package/dist/src/rules/jsdoc-param-order.d.ts.map +1 -0
  41. package/dist/src/rules/jsdoc-param-order.js +101 -0
  42. package/dist/src/rules/jsdoc-tag-lines.d.ts +6 -0
  43. package/dist/src/rules/jsdoc-tag-lines.d.ts.map +1 -0
  44. package/dist/src/rules/jsdoc-tag-lines.js +128 -0
  45. package/dist/src/rules/jsdoc-tag-order.d.ts +6 -0
  46. package/dist/src/rules/jsdoc-tag-order.d.ts.map +1 -0
  47. package/dist/src/rules/jsdoc-tag-order.js +62 -0
  48. package/dist/src/rules/jsdoc-utils.d.ts +26 -0
  49. package/dist/src/rules/jsdoc-utils.d.ts.map +1 -0
  50. package/dist/src/rules/jsdoc-utils.js +132 -0
  51. package/dist/src/rules/member-ordering.d.ts +6 -0
  52. package/dist/src/rules/member-ordering.d.ts.map +1 -0
  53. package/dist/src/rules/member-ordering.js +66 -0
  54. package/dist/src/rules/object-properties-newline.d.ts +9 -0
  55. package/dist/src/rules/object-properties-newline.d.ts.map +1 -0
  56. package/dist/src/rules/object-properties-newline.js +47 -0
  57. package/dist/src/rules/object-sort-utils.d.ts +32 -0
  58. package/dist/src/rules/object-sort-utils.d.ts.map +1 -0
  59. package/dist/src/rules/object-sort-utils.js +221 -0
  60. package/dist/src/rules/overload-signatures-adjacent.d.ts +6 -0
  61. package/dist/src/rules/overload-signatures-adjacent.d.ts.map +1 -0
  62. package/dist/src/rules/overload-signatures-adjacent.js +143 -0
  63. package/dist/src/rules/sort-import-export-specifiers.d.ts +8 -0
  64. package/dist/src/rules/sort-import-export-specifiers.d.ts.map +1 -0
  65. package/dist/src/rules/sort-import-export-specifiers.js +130 -0
  66. package/dist/src/rules/sort-jsx-props.d.ts +7 -0
  67. package/dist/src/rules/sort-jsx-props.d.ts.map +1 -0
  68. package/dist/src/rules/sort-jsx-props.js +102 -0
  69. package/dist/src/rules/sort-object-properties.d.ts +9 -0
  70. package/dist/src/rules/sort-object-properties.d.ts.map +1 -0
  71. package/dist/src/rules/sort-object-properties.js +54 -0
  72. package/dist/src/rules/sort-type-members.d.ts +8 -0
  73. package/dist/src/rules/sort-type-members.d.ts.map +1 -0
  74. package/dist/src/rules/sort-type-members.js +213 -0
  75. package/dist/src/rules/spaced-comment.d.ts +6 -0
  76. package/dist/src/rules/spaced-comment.d.ts.map +1 -0
  77. package/dist/src/rules/spaced-comment.js +105 -0
  78. package/dist/src/rules/type-literal-properties-newline.d.ts +8 -0
  79. package/dist/src/rules/type-literal-properties-newline.d.ts.map +1 -0
  80. package/dist/src/rules/type-literal-properties-newline.js +39 -0
  81. package/package.json +40 -8
  82. package/src/index.ts +22 -0
  83. package/src/plugin.ts +48 -0
  84. package/src/rules/blank-line-after-control-block.ts +82 -0
  85. package/src/rules/blank-line-after-declaration.ts +106 -0
  86. package/src/rules/blank-line-after-variable-declaration.ts +84 -0
  87. package/src/rules/blank-line-before-return.ts +101 -0
  88. package/src/rules/blank-line-between-class-members.ts +112 -0
  89. package/src/rules/blank-line-between-switch-cases.ts +53 -0
  90. package/src/rules/blank-line.ts +113 -0
  91. package/src/rules/comment-japanese-spacing.ts +271 -0
  92. package/src/rules/comment-one-sentence-per-line.ts +264 -0
  93. package/src/rules/comment-utils.ts +218 -0
  94. package/src/rules/jsdoc-param-order.ts +137 -0
  95. package/src/rules/jsdoc-tag-lines.ts +165 -0
  96. package/src/rules/jsdoc-tag-order.ts +75 -0
  97. package/src/rules/jsdoc-utils.ts +179 -0
  98. package/src/rules/member-ordering.ts +93 -0
  99. package/src/rules/object-properties-newline.ts +54 -0
  100. package/src/rules/object-sort-utils.ts +339 -0
  101. package/src/rules/overload-signatures-adjacent.ts +204 -0
  102. package/src/rules/sort-import-export-specifiers.ts +194 -0
  103. package/src/rules/sort-jsx-props.ts +149 -0
  104. package/src/rules/sort-object-properties.ts +62 -0
  105. package/src/rules/sort-type-members.ts +299 -0
  106. package/src/rules/spaced-comment.ts +129 -0
  107. package/src/rules/type-literal-properties-newline.ts +44 -0
package/src/plugin.ts ADDED
@@ -0,0 +1,48 @@
1
+ import { definePlugin } from "vite-plus/lint/plugins";
2
+
3
+ import blankLineAfterControlBlock from "./rules/blank-line-after-control-block.js";
4
+ import blankLineAfterDeclaration from "./rules/blank-line-after-declaration.js";
5
+ import blankLineAfterVariableDeclaration from "./rules/blank-line-after-variable-declaration.js";
6
+ import blankLineBeforeReturn from "./rules/blank-line-before-return.js";
7
+ import blankLineBetweenClassMembers from "./rules/blank-line-between-class-members.js";
8
+ import blankLineBetweenSwitchCases from "./rules/blank-line-between-switch-cases.js";
9
+ import commentJapaneseSpacing from "./rules/comment-japanese-spacing.js";
10
+ import commentOneSentencePerLine from "./rules/comment-one-sentence-per-line.js";
11
+ import jsdocParamOrder from "./rules/jsdoc-param-order.js";
12
+ import jsdocTagLines from "./rules/jsdoc-tag-lines.js";
13
+ import jsdocTagOrder from "./rules/jsdoc-tag-order.js";
14
+ import memberOrdering from "./rules/member-ordering.js";
15
+ import objectPropertiesNewline from "./rules/object-properties-newline.js";
16
+ import overloadSignaturesAdjacent from "./rules/overload-signatures-adjacent.js";
17
+ import sortImportExportSpecifiers from "./rules/sort-import-export-specifiers.js";
18
+ import sortJsxProps from "./rules/sort-jsx-props.js";
19
+ import sortObjectProperties from "./rules/sort-object-properties.js";
20
+ import sortTypeMembers from "./rules/sort-type-members.js";
21
+ import spacedComment from "./rules/spaced-comment.js";
22
+ import typeLiteralPropertiesNewline from "./rules/type-literal-properties-newline.js";
23
+
24
+ export default definePlugin({
25
+ meta: { name: "tai-kun" },
26
+ rules: {
27
+ "sort-jsx-props": sortJsxProps,
28
+ "spaced-comment": spacedComment,
29
+ "jsdoc-tag-lines": jsdocTagLines,
30
+ "jsdoc-tag-order": jsdocTagOrder,
31
+ "member-ordering": memberOrdering,
32
+ "jsdoc-param-order": jsdocParamOrder,
33
+ "sort-type-members": sortTypeMembers,
34
+ "sort-object-properties": sortObjectProperties,
35
+ "blank-line-before-return": blankLineBeforeReturn,
36
+ "comment-japanese-spacing": commentJapaneseSpacing,
37
+ "object-properties-newline": objectPropertiesNewline,
38
+ "blank-line-after-declaration": blankLineAfterDeclaration,
39
+ "overload-signatures-adjacent": overloadSignaturesAdjacent,
40
+ "comment-one-sentence-per-line": commentOneSentencePerLine,
41
+ "sort-import-export-specifiers": sortImportExportSpecifiers,
42
+ "blank-line-after-control-block": blankLineAfterControlBlock,
43
+ "blank-line-between-switch-cases": blankLineBetweenSwitchCases,
44
+ "type-literal-properties-newline": typeLiteralPropertiesNewline,
45
+ "blank-line-between-class-members": blankLineBetweenClassMembers,
46
+ "blank-line-after-variable-declaration": blankLineAfterVariableDeclaration,
47
+ },
48
+ });
@@ -0,0 +1,82 @@
1
+ import { defineRule } from "vite-plus/lint/plugins";
2
+ import type { ESTree, Visitor } from "vite-plus/lint/plugins";
3
+
4
+ import {
5
+ hasBlankLine,
6
+ getBlankLineFix,
7
+ getNextStatement,
8
+ getInsertionTarget,
9
+ } from "./blank-line.js";
10
+
11
+ type ControlStatement =
12
+ | ESTree.DoWhileStatement
13
+ | ESTree.ForInStatement
14
+ | ESTree.ForOfStatement
15
+ | ESTree.ForStatement
16
+ | ESTree.IfStatement
17
+ | ESTree.SwitchStatement
18
+ | ESTree.TryStatement
19
+ | ESTree.WhileStatement;
20
+
21
+ const CONTROL_STATEMENT_TYPES: ReadonlySet<ControlStatement["type"]> = new Set([
22
+ "DoWhileStatement",
23
+ "ForInStatement",
24
+ "ForOfStatement",
25
+ "ForStatement",
26
+ "IfStatement",
27
+ "SwitchStatement",
28
+ "TryStatement",
29
+ "WhileStatement",
30
+ ]);
31
+
32
+ /**
33
+ * 制御ブロックの直後に次の処理がある場合、空行を 1 行以上確保します。
34
+ * 余分な空行は Oxfmt が 1 行に畳みます。
35
+ */
36
+ const blankLineAfterControlBlock = defineRule({
37
+ meta: {
38
+ docs: {
39
+ description: "Require a blank line after a control block when another statement follows.",
40
+ },
41
+ type: "layout",
42
+ fixable: "whitespace",
43
+ messages: {
44
+ blankLine: "Expected a blank line after a control block when another statement follows.",
45
+ },
46
+ },
47
+ create(context) {
48
+ const sourceCode = context.sourceCode;
49
+
50
+ function checkNode(node: ESTree.Node): void {
51
+ const nextStatement = getNextStatement(node);
52
+
53
+ if (nextStatement === null || nextStatement.type === "EmptyStatement") {
54
+ return;
55
+ }
56
+
57
+ const target = getInsertionTarget(node, nextStatement, sourceCode);
58
+
59
+ if (hasBlankLine(node, target)) {
60
+ return;
61
+ }
62
+
63
+ context.report({
64
+ fix(fixer) {
65
+ return getBlankLineFix(fixer, sourceCode, node, target);
66
+ },
67
+ node,
68
+ messageId: "blankLine",
69
+ });
70
+ }
71
+
72
+ const visitors: Visitor = {};
73
+
74
+ for (const type of CONTROL_STATEMENT_TYPES) {
75
+ visitors[type] = checkNode;
76
+ }
77
+
78
+ return visitors;
79
+ },
80
+ });
81
+
82
+ export default blankLineAfterControlBlock;
@@ -0,0 +1,106 @@
1
+ import { defineRule } from "vite-plus/lint/plugins";
2
+ import type { ESTree, Visitor } from "vite-plus/lint/plugins";
3
+
4
+ import {
5
+ hasBlankLine,
6
+ getBlankLineFix,
7
+ getNextStatement,
8
+ getInsertionTarget,
9
+ } from "./blank-line.js";
10
+
11
+ const DECLARATION_TYPES: ReadonlySet<string> = new Set([
12
+ "ClassDeclaration",
13
+ "FunctionDeclaration",
14
+ "TSDeclareFunction",
15
+ "TSEnumDeclaration",
16
+ "TSInterfaceDeclaration",
17
+ "TSModuleDeclaration",
18
+ "TSTypeAliasDeclaration",
19
+ ]);
20
+
21
+ function getDeclaration(node: ESTree.Node): ESTree.Node | null {
22
+ if (DECLARATION_TYPES.has(node.type)) {
23
+ return node;
24
+ }
25
+
26
+ if (
27
+ (node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration") &&
28
+ node.declaration !== null &&
29
+ DECLARATION_TYPES.has(node.declaration.type)
30
+ ) {
31
+ return node.declaration;
32
+ }
33
+
34
+ return null;
35
+ }
36
+
37
+ function isOverload(declaration: ESTree.Node): boolean {
38
+ return (
39
+ (declaration.type === "FunctionDeclaration" || declaration.type === "TSDeclareFunction") &&
40
+ declaration.body === null
41
+ );
42
+ }
43
+
44
+ /**
45
+ * 関数・クラス・型などの宣言の後に次の文がある場合、空行を 1 行以上確保します。
46
+ * 余分な空行は Oxfmt が 1 行に畳みます。
47
+ */
48
+ const blankLineAfterDeclaration = defineRule({
49
+ meta: {
50
+ docs: {
51
+ description: "Require a blank line after a declaration when another statement follows.",
52
+ },
53
+ type: "layout",
54
+ fixable: "whitespace",
55
+ messages: {
56
+ blankLine: "Expected a blank line after a declaration when another statement follows.",
57
+ },
58
+ },
59
+ create(context) {
60
+ const sourceCode = context.sourceCode;
61
+
62
+ function checkNode(node: ESTree.Node): void {
63
+ const declaration = getDeclaration(node);
64
+
65
+ if (declaration === null || isOverload(declaration)) {
66
+ return;
67
+ }
68
+
69
+ const nextStatement = getNextStatement(node);
70
+
71
+ if (nextStatement === null || nextStatement.type === "EmptyStatement") {
72
+ return;
73
+ }
74
+
75
+ const target = getInsertionTarget(node, nextStatement, sourceCode);
76
+
77
+ if (hasBlankLine(node, target)) {
78
+ return;
79
+ }
80
+
81
+ context.report({
82
+ fix(fixer) {
83
+ return getBlankLineFix(fixer, sourceCode, node, target);
84
+ },
85
+ node,
86
+ messageId: "blankLine",
87
+ });
88
+ }
89
+
90
+ const visitors: Visitor = {
91
+ ClassDeclaration: checkNode,
92
+ TSDeclareFunction: checkNode,
93
+ TSEnumDeclaration: checkNode,
94
+ FunctionDeclaration: checkNode,
95
+ TSModuleDeclaration: checkNode,
96
+ ExportNamedDeclaration: checkNode,
97
+ TSInterfaceDeclaration: checkNode,
98
+ TSTypeAliasDeclaration: checkNode,
99
+ ExportDefaultDeclaration: checkNode,
100
+ };
101
+
102
+ return visitors;
103
+ },
104
+ });
105
+
106
+ export default blankLineAfterDeclaration;
@@ -0,0 +1,84 @@
1
+ import { defineRule } from "vite-plus/lint/plugins";
2
+ import type { ESTree } from "vite-plus/lint/plugins";
3
+
4
+ import {
5
+ hasBlankLine,
6
+ getBlankLineFix,
7
+ getNextStatement,
8
+ getInsertionTarget,
9
+ } from "./blank-line.js";
10
+
11
+ function isVariableDeclarationStatement(node: ESTree.Node): boolean {
12
+ if (node.type === "VariableDeclaration") {
13
+ return true;
14
+ }
15
+
16
+ return node.type === "ExportNamedDeclaration" && node.declaration?.type === "VariableDeclaration";
17
+ }
18
+
19
+ /**
20
+ * 連続する変数宣言の後に別の種類の文が続く場合、空行を 1 行以上確保します。
21
+ *
22
+ * return の直前は blank-line-before-return が担当するため対象外にします。
23
+ */
24
+ const blankLineAfterVariableDeclaration = defineRule({
25
+ meta: {
26
+ docs: {
27
+ description:
28
+ "Require a blank line after a variable declaration group when another statement follows.",
29
+ },
30
+ type: "layout",
31
+ fixable: "whitespace",
32
+ messages: {
33
+ blankLine:
34
+ "Expected a blank line after a variable declaration group when another statement follows.",
35
+ },
36
+ },
37
+ create(context) {
38
+ const sourceCode = context.sourceCode;
39
+
40
+ function checkStatement(node: ESTree.Node): void {
41
+ if (node.type === "VariableDeclaration" && node.parent.type === "ExportNamedDeclaration") {
42
+ return;
43
+ }
44
+
45
+ const nextStatement = getNextStatement(node);
46
+
47
+ if (
48
+ nextStatement === null ||
49
+ nextStatement.type === "EmptyStatement" ||
50
+ nextStatement.type === "ReturnStatement" ||
51
+ isVariableDeclarationStatement(nextStatement)
52
+ ) {
53
+ return;
54
+ }
55
+
56
+ const target = getInsertionTarget(node, nextStatement, sourceCode);
57
+
58
+ if (hasBlankLine(node, target)) {
59
+ return;
60
+ }
61
+
62
+ context.report({
63
+ fix(fixer) {
64
+ return getBlankLineFix(fixer, sourceCode, node, target);
65
+ },
66
+ node,
67
+ messageId: "blankLine",
68
+ });
69
+ }
70
+
71
+ return {
72
+ VariableDeclaration(node): void {
73
+ checkStatement(node);
74
+ },
75
+ ExportNamedDeclaration(node): void {
76
+ if (node.declaration?.type === "VariableDeclaration") {
77
+ checkStatement(node);
78
+ }
79
+ },
80
+ };
81
+ },
82
+ });
83
+
84
+ export default blankLineAfterVariableDeclaration;
@@ -0,0 +1,101 @@
1
+ import { defineRule } from "vite-plus/lint/plugins";
2
+
3
+ import {
4
+ hasBlankLine,
5
+ getBlankLineFix,
6
+ getInsertionTarget,
7
+ getPreviousStatement,
8
+ } from "./blank-line.js";
9
+
10
+ const SKIP_PREVIOUS_TYPES: ReadonlySet<string> = new Set([
11
+ // 制御ブロックは blank-line-after-control-block が担当します。
12
+ "DoWhileStatement",
13
+ "ForInStatement",
14
+ "ForOfStatement",
15
+ "ForStatement",
16
+ "IfStatement",
17
+ "SwitchStatement",
18
+ "TryStatement",
19
+ "WhileStatement",
20
+ // 宣言は blank-line-after-declaration が担当します。
21
+ "ClassDeclaration",
22
+ "ExportAllDeclaration",
23
+ "ExportDefaultDeclaration",
24
+ "ExportNamedDeclaration",
25
+ "FunctionDeclaration",
26
+ "ImportDeclaration",
27
+ "TSDeclareFunction",
28
+ "TSEnumDeclaration",
29
+ "TSInterfaceDeclaration",
30
+ "TSModuleDeclaration",
31
+ "TSTypeAliasDeclaration",
32
+ // 途中で終わる文や空文の後は対象外にします。
33
+ "BlockStatement",
34
+ "BreakStatement",
35
+ "ContinueStatement",
36
+ "DebuggerStatement",
37
+ "EmptyStatement",
38
+ "LabeledStatement",
39
+ "ReturnStatement",
40
+ "ThrowStatement",
41
+ "WithStatement",
42
+ ]);
43
+
44
+ /**
45
+ * return の直前に処理がある場合、空行を 1 行以上確保します。
46
+ * 余分な空行は Oxfmt が 1 行に畳みます。
47
+ */
48
+ const blankLineBeforeReturn = defineRule({
49
+ meta: {
50
+ docs: {
51
+ description:
52
+ "Require a blank line before a return statement when another statement precedes it.",
53
+ },
54
+ type: "layout",
55
+ fixable: "whitespace",
56
+ messages: {
57
+ blankLine:
58
+ "Expected a blank line before a return statement when another statement precedes it.",
59
+ },
60
+ },
61
+ create(context) {
62
+ const sourceCode = context.sourceCode;
63
+
64
+ return {
65
+ ReturnStatement(node): void {
66
+ const previousStatement = getPreviousStatement(node);
67
+
68
+ if (previousStatement === null) {
69
+ return;
70
+ }
71
+
72
+ if (SKIP_PREVIOUS_TYPES.has(previousStatement.type)) {
73
+ return;
74
+ }
75
+
76
+ if (
77
+ previousStatement.type === "ExpressionStatement" &&
78
+ typeof previousStatement.directive === "string"
79
+ ) {
80
+ return;
81
+ }
82
+
83
+ const target = getInsertionTarget(previousStatement, node, sourceCode);
84
+
85
+ if (hasBlankLine(previousStatement, target)) {
86
+ return;
87
+ }
88
+
89
+ context.report({
90
+ fix(fixer) {
91
+ return getBlankLineFix(fixer, sourceCode, previousStatement, target);
92
+ },
93
+ node,
94
+ messageId: "blankLine",
95
+ });
96
+ },
97
+ };
98
+ },
99
+ });
100
+
101
+ export default blankLineBeforeReturn;
@@ -0,0 +1,112 @@
1
+ import { defineRule } from "vite-plus/lint/plugins";
2
+ import type { ESTree } from "vite-plus/lint/plugins";
3
+
4
+ import { hasBlankLine, getBlankLineFix, getInsertionTarget } from "./blank-line.js";
5
+
6
+ function getKeyName(member: ESTree.ClassElement): string | null {
7
+ if (member.type === "TSIndexSignature" || member.type === "StaticBlock") {
8
+ return null;
9
+ }
10
+
11
+ if (member.computed) {
12
+ return null;
13
+ }
14
+
15
+ const key = member.key;
16
+
17
+ if (key.type === "Identifier") {
18
+ return key.name;
19
+ }
20
+
21
+ if (key.type === "Literal" && (typeof key.value === "string" || typeof key.value === "number")) {
22
+ return String(key.value);
23
+ }
24
+
25
+ return null;
26
+ }
27
+
28
+ function isOverload(member: ESTree.ClassElement): boolean {
29
+ return member.type === "MethodDefinition" && member.value.body === null;
30
+ }
31
+
32
+ function isGetterSetterPair(previous: ESTree.ClassElement, next: ESTree.ClassElement): boolean {
33
+ if (previous.type !== "MethodDefinition" || next.type !== "MethodDefinition") {
34
+ return false;
35
+ }
36
+
37
+ if (
38
+ !(
39
+ (previous.kind === "get" && next.kind === "set") ||
40
+ (previous.kind === "set" && next.kind === "get")
41
+ )
42
+ ) {
43
+ return false;
44
+ }
45
+
46
+ const previousName = getKeyName(previous);
47
+ const nextName = getKeyName(next);
48
+
49
+ return previousName !== null && previousName === nextName;
50
+ }
51
+
52
+ function getMemberStart(member: ESTree.ClassElement): ESTree.ClassElement | ESTree.Decorator {
53
+ if (member.type === "TSIndexSignature" || member.type === "StaticBlock") {
54
+ return member;
55
+ }
56
+
57
+ return member.decorators[0] ?? member;
58
+ }
59
+
60
+ /**
61
+ * クラスメンバーの間に空行を 1 行以上確保します。
62
+ *
63
+ * オーバーロード署名と getter/setter のペアは連続させます。
64
+ */
65
+ const blankLineBetweenClassMembers = defineRule({
66
+ meta: {
67
+ docs: {
68
+ description: "Require a blank line between class members.",
69
+ },
70
+ type: "layout",
71
+ fixable: "whitespace",
72
+ messages: {
73
+ blankLine: "Expected a blank line between class members.",
74
+ },
75
+ },
76
+ create(context) {
77
+ const sourceCode = context.sourceCode;
78
+
79
+ return {
80
+ ClassBody(node): void {
81
+ for (let index = 1; index < node.body.length; index += 1) {
82
+ const previous = node.body[index - 1];
83
+ const next = node.body[index];
84
+
85
+ if (previous === undefined || next === undefined) {
86
+ continue;
87
+ }
88
+
89
+ if (isOverload(previous) || isGetterSetterPair(previous, next)) {
90
+ continue;
91
+ }
92
+
93
+ const target = getInsertionTarget(previous, getMemberStart(next), sourceCode);
94
+
95
+ if (hasBlankLine(previous, target)) {
96
+ continue;
97
+ }
98
+
99
+ context.report({
100
+ fix(fixer) {
101
+ return getBlankLineFix(fixer, sourceCode, previous, target);
102
+ },
103
+ node: next,
104
+ messageId: "blankLine",
105
+ });
106
+ }
107
+ },
108
+ };
109
+ },
110
+ });
111
+
112
+ export default blankLineBetweenClassMembers;
@@ -0,0 +1,53 @@
1
+ import { defineRule } from "vite-plus/lint/plugins";
2
+
3
+ import { hasBlankLine, getBlankLineFix, getInsertionTarget } from "./blank-line.js";
4
+
5
+ /**
6
+ * 空でない switch の case の間に空行を 1 行以上確保します。
7
+ *
8
+ * 連続する case はフォールスルーとみなし、対象外にします。
9
+ */
10
+ const blankLineBetweenSwitchCases = defineRule({
11
+ meta: {
12
+ docs: {
13
+ description: "Require a blank line between non-empty switch cases.",
14
+ },
15
+ type: "layout",
16
+ fixable: "whitespace",
17
+ messages: {
18
+ blankLine: "Expected a blank line between switch cases.",
19
+ },
20
+ },
21
+ create(context) {
22
+ const sourceCode = context.sourceCode;
23
+
24
+ return {
25
+ SwitchStatement(node): void {
26
+ for (let index = 1; index < node.cases.length; index += 1) {
27
+ const previous = node.cases[index - 1];
28
+ const next = node.cases[index];
29
+
30
+ if (previous === undefined || next === undefined || previous.consequent.length === 0) {
31
+ continue;
32
+ }
33
+
34
+ const target = getInsertionTarget(previous, next, sourceCode);
35
+
36
+ if (hasBlankLine(previous, target)) {
37
+ continue;
38
+ }
39
+
40
+ context.report({
41
+ fix(fixer) {
42
+ return getBlankLineFix(fixer, sourceCode, previous, target);
43
+ },
44
+ node: next,
45
+ messageId: "blankLine",
46
+ });
47
+ }
48
+ },
49
+ };
50
+ },
51
+ });
52
+
53
+ export default blankLineBetweenSwitchCases;
@@ -0,0 +1,113 @@
1
+ import type { Fix, Fixer, ESTree, Comment, SourceCode } from "vite-plus/lint/plugins";
2
+
3
+ /**
4
+ * 文の配列を持つ親から、その文が属する配列を取得します。
5
+ */
6
+ function getStatementBody(node: ESTree.Node): readonly ESTree.Node[] | null {
7
+ const { parent } = node;
8
+
9
+ if (parent === null) {
10
+ return null;
11
+ }
12
+
13
+ if (
14
+ parent.type === "Program" ||
15
+ parent.type === "BlockStatement" ||
16
+ parent.type === "StaticBlock" ||
17
+ parent.type === "TSModuleBlock"
18
+ ) {
19
+ return parent.body;
20
+ }
21
+
22
+ if (parent.type === "SwitchCase") {
23
+ return parent.consequent;
24
+ }
25
+
26
+ return null;
27
+ }
28
+
29
+ function getNextStatement(node: ESTree.Node): ESTree.Node | null {
30
+ const body = getStatementBody(node);
31
+
32
+ if (body === null) {
33
+ return null;
34
+ }
35
+
36
+ const index = body.indexOf(node);
37
+
38
+ if (index === -1 || index === body.length - 1) {
39
+ return null;
40
+ }
41
+
42
+ return body[index + 1] ?? null;
43
+ }
44
+
45
+ function getPreviousStatement(node: ESTree.Node): ESTree.Node | null {
46
+ const body = getStatementBody(node);
47
+
48
+ if (body === null) {
49
+ return null;
50
+ }
51
+
52
+ const index = body.indexOf(node);
53
+
54
+ if (index <= 0) {
55
+ return null;
56
+ }
57
+
58
+ return body[index - 1] ?? null;
59
+ }
60
+
61
+ /**
62
+ * 空行の挿入位置を返します。
63
+ *
64
+ * 次の文に付くコメントがある場合は、そのコメントの前を挿入位置にします。
65
+ * 同じ行の末尾コメントは対象外です。
66
+ */
67
+ function getInsertionTarget(
68
+ node: ESTree.Node,
69
+ nextStatement: ESTree.Node,
70
+ sourceCode: SourceCode,
71
+ ): ESTree.Node | Comment {
72
+ return (
73
+ sourceCode
74
+ .getAllComments()
75
+ .find(
76
+ (comment) =>
77
+ comment.range[0] >= node.range[1] &&
78
+ comment.range[1] <= nextStatement.range[0] &&
79
+ comment.loc.start.line > node.loc.end.line,
80
+ ) ?? nextStatement
81
+ );
82
+ }
83
+
84
+ function hasBlankLine(node: ESTree.Node, target: ESTree.Node | Comment): boolean {
85
+ return target.loc.start.line > node.loc.end.line + 1;
86
+ }
87
+
88
+ function getBlankLineFix(
89
+ fixer: Fixer,
90
+ sourceCode: SourceCode,
91
+ node: ESTree.Node,
92
+ target: ESTree.Node | Comment,
93
+ ): Fix | null {
94
+ if (target.loc.start.line === node.loc.end.line) {
95
+ return fixer.insertTextAfterRange([node.range[1], node.range[1]], "\n\n");
96
+ }
97
+
98
+ const lineEnd = sourceCode.text.indexOf("\n", node.range[1]);
99
+
100
+ if (lineEnd === -1) {
101
+ return null;
102
+ }
103
+
104
+ return fixer.insertTextBeforeRange([lineEnd, lineEnd + 1], "\n");
105
+ }
106
+
107
+ export {
108
+ hasBlankLine,
109
+ getBlankLineFix,
110
+ getNextStatement,
111
+ getInsertionTarget,
112
+ getPreviousStatement,
113
+ };