@tai-kun/vp-lint-plugins 0.0.4 → 0.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/dist/src/index.d.ts +1 -6
  2. package/dist/src/index.d.ts.map +1 -1
  3. package/dist/src/index.js +1 -6
  4. package/dist/src/plugin.js +2 -12
  5. package/dist/src/rules/blank-line-utils.d.ts +30 -0
  6. package/dist/src/rules/blank-line-utils.d.ts.map +1 -0
  7. package/dist/src/rules/blank-line-utils.js +103 -0
  8. package/dist/src/rules/blank-line.d.ts +5 -14
  9. package/dist/src/rules/blank-line.d.ts.map +1 -1
  10. package/dist/src/rules/blank-line.js +426 -66
  11. package/dist/src/rules/overload-signatures-adjacent.js +1 -1
  12. package/package.json +1 -1
  13. package/src/index.ts +1 -6
  14. package/src/plugin.ts +2 -12
  15. package/src/rules/blank-line-utils.ts +165 -0
  16. package/src/rules/blank-line.ts +555 -97
  17. package/src/rules/overload-signatures-adjacent.ts +1 -1
  18. package/dist/src/rules/blank-line-after-control-block.d.ts +0 -7
  19. package/dist/src/rules/blank-line-after-control-block.d.ts.map +0 -1
  20. package/dist/src/rules/blank-line-after-control-block.js +0 -54
  21. package/dist/src/rules/blank-line-after-declaration.d.ts +0 -7
  22. package/dist/src/rules/blank-line-after-declaration.d.ts.map +0 -1
  23. package/dist/src/rules/blank-line-after-declaration.js +0 -79
  24. package/dist/src/rules/blank-line-after-variable-declaration.d.ts +0 -9
  25. package/dist/src/rules/blank-line-after-variable-declaration.d.ts.map +0 -1
  26. package/dist/src/rules/blank-line-after-variable-declaration.js +0 -118
  27. package/dist/src/rules/blank-line-before-return.d.ts +0 -7
  28. package/dist/src/rules/blank-line-before-return.d.ts.map +0 -1
  29. package/dist/src/rules/blank-line-before-return.js +0 -81
  30. package/dist/src/rules/blank-line-between-class-members.d.ts +0 -8
  31. package/dist/src/rules/blank-line-between-class-members.d.ts.map +0 -1
  32. package/dist/src/rules/blank-line-between-class-members.js +0 -85
  33. package/dist/src/rules/blank-line-between-switch-cases.d.ts +0 -8
  34. package/dist/src/rules/blank-line-between-switch-cases.d.ts.map +0 -1
  35. package/dist/src/rules/blank-line-between-switch-cases.js +0 -45
  36. package/src/rules/blank-line-after-control-block.ts +0 -82
  37. package/src/rules/blank-line-after-declaration.ts +0 -106
  38. package/src/rules/blank-line-after-variable-declaration.ts +0 -165
  39. package/src/rules/blank-line-before-return.ts +0 -101
  40. package/src/rules/blank-line-between-class-members.ts +0 -112
  41. package/src/rules/blank-line-between-switch-cases.ts +0 -53
@@ -0,0 +1,165 @@
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
+ /**
89
+ * 挿入対象がコメントかどうかを返します。
90
+ *
91
+ * コメントを挟む場合、前後の空行は任意とします。
92
+ */
93
+ function isComment(target: ESTree.Node | Comment): target is Comment {
94
+ const { type } = target as { type: string };
95
+
96
+ return type === "Line" || type === "Block" || type === "Shebang";
97
+ }
98
+
99
+ /**
100
+ * JSDoc 形式のブロックコメント(`/**` で始まる)かどうかを返します。
101
+ *
102
+ * JSDoc 形式のブロックコメントの 1 行上は空行にします。
103
+ */
104
+ function isDocComment(target: ESTree.Node | Comment): target is Comment {
105
+ return isComment(target) && target.type === "Block" && target.value.startsWith("*");
106
+ }
107
+
108
+ function getBlankLineFix(
109
+ fixer: Fixer,
110
+ sourceCode: SourceCode,
111
+ node: ESTree.Node,
112
+ target: ESTree.Node | Comment,
113
+ ): Fix | null {
114
+ if (target.loc.start.line === node.loc.end.line) {
115
+ return fixer.insertTextAfterRange([node.range[1], node.range[1]], "\n\n");
116
+ }
117
+
118
+ const lineEnd = sourceCode.text.indexOf("\n", node.range[1]);
119
+
120
+ if (lineEnd === -1) {
121
+ return null;
122
+ }
123
+
124
+ return fixer.insertTextBeforeRange([lineEnd, lineEnd + 1], "\n");
125
+ }
126
+
127
+ /**
128
+ * 指定したノードと対象の間の空行を削除します。
129
+ */
130
+ function getRemoveBlankLineFix(
131
+ fixer: Fixer,
132
+ sourceCode: SourceCode,
133
+ node: ESTree.Node,
134
+ target: ESTree.Node | Comment,
135
+ ): Fix | null {
136
+ if (target.loc.start.line <= node.loc.end.line + 1) {
137
+ return null;
138
+ }
139
+
140
+ const fromLineEnd = sourceCode.text.indexOf("\n", node.range[1]);
141
+
142
+ if (fromLineEnd === -1) {
143
+ return null;
144
+ }
145
+
146
+ const targetLineStart = sourceCode.text.lastIndexOf("\n", target.range[0]) + 1;
147
+ const indent = sourceCode.text.slice(targetLineStart, target.range[0]);
148
+
149
+ if (indent.trim() !== "") {
150
+ return null;
151
+ }
152
+
153
+ return fixer.replaceTextRange([fromLineEnd, target.range[0]], `\n${indent}`);
154
+ }
155
+
156
+ export {
157
+ isComment,
158
+ hasBlankLine,
159
+ isDocComment,
160
+ getBlankLineFix,
161
+ getNextStatement,
162
+ getInsertionTarget,
163
+ getPreviousStatement,
164
+ getRemoveBlankLineFix,
165
+ };