@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
@@ -0,0 +1,66 @@
1
+ import { defineRule } from "vite-plus/lint/plugins";
2
+ const MEMBER_GROUP_ORDER = {
3
+ constructor: 3,
4
+ "static-block": 1,
5
+ "static-field": 0,
6
+ "static-method": 4,
7
+ "instance-field": 2,
8
+ "instance-method": 5,
9
+ };
10
+ function getMemberGroup(member) {
11
+ if (member.type === "StaticBlock") {
12
+ return "static-block";
13
+ }
14
+ if (member.type === "TSIndexSignature") {
15
+ return null;
16
+ }
17
+ if (member.type === "PropertyDefinition" ||
18
+ member.type === "TSAbstractPropertyDefinition" ||
19
+ member.type === "AccessorProperty" ||
20
+ member.type === "TSAbstractAccessorProperty") {
21
+ return member.static ? "static-field" : "instance-field";
22
+ }
23
+ if (member.type === "MethodDefinition" || member.type === "TSAbstractMethodDefinition") {
24
+ if (member.kind === "constructor") {
25
+ return "constructor";
26
+ }
27
+ return member.static ? "static-method" : "instance-method";
28
+ }
29
+ return null;
30
+ }
31
+ /**
32
+ * クラスメンバーを「static フィールド → static ブロック → インスタンスフィールド → constructor → static メソッド → インスタンスメソッド」の順に要求します。
33
+ */
34
+ const memberOrdering = defineRule({
35
+ meta: {
36
+ docs: {
37
+ description: "Require class members to be ordered as static fields, static blocks, instance fields, constructor, static methods, and instance methods.",
38
+ },
39
+ type: "suggestion",
40
+ messages: {
41
+ order: "Class members must be ordered as static fields, static blocks, instance fields, constructor, static methods, and instance methods.",
42
+ },
43
+ },
44
+ create(context) {
45
+ return {
46
+ ClassBody(node) {
47
+ let previousRank = null;
48
+ for (const member of node.body) {
49
+ const group = getMemberGroup(member);
50
+ if (group === null) {
51
+ continue;
52
+ }
53
+ const rank = MEMBER_GROUP_ORDER[group];
54
+ if (previousRank !== null && rank < previousRank) {
55
+ context.report({
56
+ node: member,
57
+ messageId: "order",
58
+ });
59
+ }
60
+ previousRank = rank;
61
+ }
62
+ },
63
+ };
64
+ },
65
+ });
66
+ export default memberOrdering;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * プロパティーが 2 つ以上あるオブジェクトリテラルを複数行にします。
3
+ *
4
+ * Oxfmt の `objectWrap` には「プロパティー数で改行を強制する」オプションがないため、改行を挿入する最小限の fix を提供します。
5
+ * `objectWrap: "preserve"` と組み合わせることで、後続の Oxfmt がプロパティーごとの改行とインデントを整えます。
6
+ */
7
+ declare const objectPropertiesNewline: import("vite-plus/lint/plugins").Rule;
8
+ export default objectPropertiesNewline;
9
+ //# sourceMappingURL=object-properties-newline.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"object-properties-newline.d.ts","sourceRoot":"","sources":["../../../src/rules/object-properties-newline.ts"],"names":[],"mappings":"AAIA;;;;;GAKG;AACH,QAAA,MAAM,uBAAuB,uCAyC3B,CAAC;eAEY,uBAAuB"}
@@ -0,0 +1,47 @@
1
+ import { defineRule } from "vite-plus/lint/plugins";
2
+ import { getSortPlan } from "./object-sort-utils.js";
3
+ /**
4
+ * プロパティーが 2 つ以上あるオブジェクトリテラルを複数行にします。
5
+ *
6
+ * Oxfmt の `objectWrap` には「プロパティー数で改行を強制する」オプションがないため、改行を挿入する最小限の fix を提供します。
7
+ * `objectWrap: "preserve"` と組み合わせることで、後続の Oxfmt がプロパティーごとの改行とインデントを整えます。
8
+ */
9
+ const objectPropertiesNewline = defineRule({
10
+ meta: {
11
+ docs: {
12
+ description: "Enforce multiline object literals when they have two or more properties.",
13
+ },
14
+ type: "layout",
15
+ fixable: "whitespace",
16
+ messages: {
17
+ multiline: "Objects with 2 or more properties must be multiline.",
18
+ },
19
+ },
20
+ create(context) {
21
+ return {
22
+ ObjectExpression(node) {
23
+ if (node.properties.length < 2) {
24
+ return;
25
+ }
26
+ // 並び替え可能なオブジェクトは sort-object-properties が複数行化も担当します。
27
+ if (getSortPlan(node, context.sourceCode) !== null) {
28
+ return;
29
+ }
30
+ // Oxfmt は `{` の直後に改行があるかどうかで複数行を維持するため、最初のプロパティーが `{` と同じ行にある場合だけを対象にします。
31
+ const firstProperty = node.properties[0];
32
+ if (firstProperty === undefined || firstProperty.loc.start.line !== node.loc.start.line) {
33
+ return;
34
+ }
35
+ context.report({
36
+ fix(fixer) {
37
+ // Oxfmt は `{` の直後の改行を複数行オブジェクトの目印にするため、ここでは改行を 1 つ挿入するだけで十分です。
38
+ return fixer.insertTextAfterRange([node.range[0], node.range[0] + 1], "\n");
39
+ },
40
+ node,
41
+ messageId: "multiline",
42
+ });
43
+ },
44
+ };
45
+ },
46
+ });
47
+ export default objectPropertiesNewline;
@@ -0,0 +1,32 @@
1
+ import type { ESTree, SourceCode } from "vite-plus/lint/plugins";
2
+ type SortableObject = ESTree.ObjectExpression | ESTree.ObjectPattern;
3
+ interface SortPlanOptions {
4
+ /**
5
+ * 末尾の RestElement を許可するかどうかです。
6
+ */
7
+ allowRest?: boolean;
8
+ /**
9
+ * 1 行のオブジェクトを複数行に整形するかどうかです。
10
+ */
11
+ forceMultiline?: boolean;
12
+ }
13
+ type SortPlan = {
14
+ needsFix: false;
15
+ replacement: null;
16
+ } | {
17
+ needsFix: true;
18
+ replacement: string;
19
+ };
20
+ declare function compareNames(left: string, right: string): number;
21
+ /**
22
+ * オブジェクトリテラル/オブジェクトパターンの並び替え計画を返します。
23
+ *
24
+ * - プロパティー名の長さが短い順、同じ長さなら名前順
25
+ * - 空行で区切られたグループ内だけで並び替え
26
+ * - プロパティー直上のコメントはプロパティーと一緒に移動
27
+ *
28
+ * 並び替えが安全でない場合は null を返します。
29
+ */
30
+ declare function getSortPlan(node: SortableObject, sourceCode: SourceCode, options?: SortPlanOptions): SortPlan | null;
31
+ export { getSortPlan, compareNames };
32
+ //# sourceMappingURL=object-sort-utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"object-sort-utils.d.ts","sourceRoot":"","sources":["../../../src/rules/object-sort-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAW,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAE1E,KAAK,cAAc,GAAG,MAAM,CAAC,gBAAgB,GAAG,MAAM,CAAC,aAAa,CAAC;AAIrE,UAAU,eAAe;IACvB;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB;;OAEG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAYD,KAAK,QAAQ,GACT;IACE,QAAQ,EAAE,KAAK,CAAC;IAChB,WAAW,EAAE,IAAI,CAAC;CACnB,GACD;IACE,QAAQ,EAAE,IAAI,CAAC;IACf,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAuEN,iBAAS,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAiBzD;AAeD;;;;;;;;GAQG;AACH,iBAAS,WAAW,CAClB,IAAI,EAAE,cAAc,EACpB,UAAU,EAAE,UAAU,EACtB,OAAO,GAAE,eAAoB,GAC5B,QAAQ,GAAG,IAAI,CAwLjB;AAED,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,CAAC"}
@@ -0,0 +1,221 @@
1
+ function getLineStarts(text) {
2
+ const starts = [0];
3
+ for (let index = 0; index < text.length; index += 1) {
4
+ if (text[index] === "\n") {
5
+ starts.push(index + 1);
6
+ }
7
+ }
8
+ return starts;
9
+ }
10
+ function getLineIndent(text, lineStarts, line) {
11
+ const start = lineStarts[line - 1] ?? 0;
12
+ const match = /^[ \t]*/u.exec(text.slice(start));
13
+ return match?.[0] ?? "";
14
+ }
15
+ function getPropertyName(property) {
16
+ if (property.type !== "Property" || property.computed || property.kind !== "init") {
17
+ return null;
18
+ }
19
+ const key = property.key;
20
+ let name;
21
+ if (key.type === "Identifier") {
22
+ name = key.name;
23
+ }
24
+ else if (key.type === "Literal" &&
25
+ (typeof key.value === "string" || typeof key.value === "number")) {
26
+ name = String(key.value);
27
+ }
28
+ else {
29
+ return null;
30
+ }
31
+ return name === "__proto__" ? null : name;
32
+ }
33
+ function isSortableObject(node, allowRest) {
34
+ if (node.properties.length < 2) {
35
+ return false;
36
+ }
37
+ const names = new Set();
38
+ for (const [index, property] of node.properties.entries()) {
39
+ if (property.type === "RestElement") {
40
+ if (!allowRest || index !== node.properties.length - 1) {
41
+ return false;
42
+ }
43
+ continue;
44
+ }
45
+ const name = getPropertyName(property);
46
+ if (name === null || names.has(name)) {
47
+ return false;
48
+ }
49
+ names.add(name);
50
+ }
51
+ return true;
52
+ }
53
+ function compareNames(left, right) {
54
+ if (left.length !== right.length) {
55
+ return left.length - right.length;
56
+ }
57
+ const leftLower = left.toLowerCase();
58
+ const rightLower = right.toLowerCase();
59
+ if (leftLower !== rightLower) {
60
+ return leftLower < rightLower ? -1 : 1;
61
+ }
62
+ if (left === right) {
63
+ return 0;
64
+ }
65
+ return left < right ? -1 : 1;
66
+ }
67
+ function compareUnits(left, right) {
68
+ return compareNames(left.name, right.name);
69
+ }
70
+ function sortGroup(group) {
71
+ const sortable = group.filter((unit) => unit.name !== null);
72
+ sortable.sort(compareUnits);
73
+ const rest = group.filter((unit) => unit.name === null);
74
+ return [...sortable, ...rest];
75
+ }
76
+ /**
77
+ * オブジェクトリテラル/オブジェクトパターンの並び替え計画を返します。
78
+ *
79
+ * - プロパティー名の長さが短い順、同じ長さなら名前順
80
+ * - 空行で区切られたグループ内だけで並び替え
81
+ * - プロパティー直上のコメントはプロパティーと一緒に移動
82
+ *
83
+ * 並び替えが安全でない場合は null を返します。
84
+ */
85
+ function getSortPlan(node, sourceCode, options = {}) {
86
+ const { allowRest = false, forceMultiline = true } = options;
87
+ if (!isSortableObject(node, allowRest)) {
88
+ return null;
89
+ }
90
+ const source = sourceCode.text;
91
+ const lineStarts = getLineStarts(source);
92
+ const lineIndent = getLineIndent(source, lineStarts, node.loc.start.line);
93
+ const comments = sourceCode
94
+ .getAllComments()
95
+ .filter((comment) => comment.range[0] >= node.range[0] && comment.range[1] <= node.range[1]);
96
+ const firstProperty = node.properties[0];
97
+ if (firstProperty === undefined) {
98
+ return null;
99
+ }
100
+ const inlineStart = firstProperty.loc.start.line === node.loc.start.line;
101
+ const singleLine = node.loc.start.line === node.loc.end.line;
102
+ const units = [];
103
+ if (inlineStart) {
104
+ if (comments.length > 0) {
105
+ return null;
106
+ }
107
+ for (const property of node.properties) {
108
+ const isRest = property.type === "RestElement";
109
+ units.push({
110
+ name: isRest ? null : getPropertyName(property),
111
+ text: source.slice(property.range[0], property.range[1]),
112
+ endLine: property.loc.end.line,
113
+ property,
114
+ startLine: property.loc.start.line,
115
+ });
116
+ }
117
+ }
118
+ else {
119
+ let previousProperty = null;
120
+ for (const property of node.properties) {
121
+ const previous = previousProperty;
122
+ const boundary = previous === null ? node.range[0] + 1 : previous.range[1];
123
+ const candidates = comments.filter((comment) => comment.range[0] >= boundary && comment.range[1] <= property.range[0]);
124
+ if (previous !== null &&
125
+ comments.some((comment) => comment.loc.start.line === previous.loc.end.line &&
126
+ comment.range[0] >= previous.range[1] &&
127
+ comment.range[1] <= property.range[0])) {
128
+ return null;
129
+ }
130
+ const attached = [];
131
+ let expectedLine = property.loc.start.line;
132
+ for (let index = candidates.length - 1; index >= 0; index -= 1) {
133
+ const comment = candidates[index];
134
+ if (comment === undefined) {
135
+ continue;
136
+ }
137
+ if (comment.loc.end.line !== expectedLine && comment.loc.end.line !== expectedLine - 1) {
138
+ return null;
139
+ }
140
+ if (comment.loc.start.line === expectedLine) {
141
+ return null;
142
+ }
143
+ if (comment.loc.start.line === node.loc.start.line) {
144
+ return null;
145
+ }
146
+ attached.unshift(comment);
147
+ expectedLine = comment.loc.start.line;
148
+ }
149
+ if (attached.length !== candidates.length) {
150
+ return null;
151
+ }
152
+ const first = attached[0] ?? property;
153
+ const startLine = first.loc.start.line;
154
+ const unitStart = lineStarts[startLine - 1] ?? 0;
155
+ const isRest = property.type === "RestElement";
156
+ units.push({
157
+ name: isRest ? null : getPropertyName(property),
158
+ text: source.slice(unitStart, property.range[1]),
159
+ endLine: property.loc.end.line,
160
+ property,
161
+ startLine,
162
+ });
163
+ previousProperty = property;
164
+ }
165
+ const lastProperty = node.properties.at(-1);
166
+ if (lastProperty === undefined) {
167
+ return null;
168
+ }
169
+ if (comments.some((comment) => comment.range[0] >= lastProperty.range[1] && comment.range[1] <= node.range[1] - 1)) {
170
+ return null;
171
+ }
172
+ }
173
+ const groups = [];
174
+ let currentGroup = [];
175
+ let previousEndLine = null;
176
+ for (const unit of units) {
177
+ if (previousEndLine !== null && unit.startLine > previousEndLine + 1) {
178
+ groups.push(currentGroup);
179
+ currentGroup = [];
180
+ }
181
+ currentGroup.push(unit);
182
+ previousEndLine = unit.endLine;
183
+ }
184
+ if (currentGroup.length > 0) {
185
+ groups.push(currentGroup);
186
+ }
187
+ let needsSort = false;
188
+ for (const group of groups) {
189
+ const sorted = sortGroup(group);
190
+ if (sorted.some((unit, index) => unit !== group[index])) {
191
+ needsSort = true;
192
+ break;
193
+ }
194
+ }
195
+ const needsFormat = forceMultiline && inlineStart;
196
+ if (!needsSort && !needsFormat) {
197
+ return {
198
+ needsFix: false,
199
+ replacement: null,
200
+ };
201
+ }
202
+ if (singleLine && !forceMultiline) {
203
+ const sortedUnits = sortGroup(units);
204
+ return {
205
+ needsFix: true,
206
+ replacement: `{ ${sortedUnits.map((unit) => unit.text).join(", ")} }`,
207
+ };
208
+ }
209
+ const groupsText = groups
210
+ .map((group) => sortGroup(group)
211
+ .map((unit) => unit.text)
212
+ .join(",\n"))
213
+ .join(",\n\n");
214
+ const lastUnit = groups.flatMap((group) => sortGroup(group)).at(-1);
215
+ const trailingComma = lastUnit?.property.type === "RestElement" ? "" : ",";
216
+ return {
217
+ needsFix: true,
218
+ replacement: `{\n${groupsText}${trailingComma}\n${lineIndent}}`,
219
+ };
220
+ }
221
+ export { getSortPlan, compareNames };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * オーバーロード署名と実装の間に空行を入れないようにします。
3
+ */
4
+ declare const overloadSignaturesAdjacent: import("vite-plus/lint/plugins").Rule;
5
+ export default overloadSignaturesAdjacent;
6
+ //# sourceMappingURL=overload-signatures-adjacent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"overload-signatures-adjacent.d.ts","sourceRoot":"","sources":["../../../src/rules/overload-signatures-adjacent.ts"],"names":[],"mappings":"AAiGA;;GAEG;AACH,QAAA,MAAM,0BAA0B,uCAqG9B,CAAC;eAEY,0BAA0B"}
@@ -0,0 +1,143 @@
1
+ import { defineRule } from "vite-plus/lint/plugins";
2
+ import { getNextStatement } from "./blank-line.js";
3
+ function getFunction(node) {
4
+ if (node.type === "FunctionDeclaration" || node.type === "TSDeclareFunction") {
5
+ return node;
6
+ }
7
+ if (node.type === "ExportNamedDeclaration" && node.declaration !== null) {
8
+ const declaration = node.declaration;
9
+ if (declaration.type === "FunctionDeclaration" || declaration.type === "TSDeclareFunction") {
10
+ return declaration;
11
+ }
12
+ }
13
+ return null;
14
+ }
15
+ function getKeyName(node) {
16
+ if (node.computed) {
17
+ return null;
18
+ }
19
+ const key = node.key;
20
+ if (key.type === "Identifier") {
21
+ return key.name;
22
+ }
23
+ if (key.type === "Literal" && (typeof key.value === "string" || typeof key.value === "number")) {
24
+ return String(key.value);
25
+ }
26
+ return null;
27
+ }
28
+ function getRemoveBlankLinesFix(fixer, sourceCode, previous, next) {
29
+ if (next.loc.start.line <= previous.loc.end.line + 1) {
30
+ return null;
31
+ }
32
+ const fromLineEnd = sourceCode.text.indexOf("\n", previous.range[1]);
33
+ if (fromLineEnd === -1) {
34
+ return null;
35
+ }
36
+ const nextLineStart = sourceCode.text.lastIndexOf("\n", next.range[0]) + 1;
37
+ const indent = sourceCode.text.slice(nextLineStart, next.range[0]);
38
+ if (indent.trim() !== "") {
39
+ return null;
40
+ }
41
+ return fixer.replaceTextRange([fromLineEnd, next.range[0]], `\n${indent}`);
42
+ }
43
+ function hasCommentBetween(sourceCode, previous, next) {
44
+ return sourceCode
45
+ .getAllComments()
46
+ .some((comment) => comment.range[0] >= previous.range[1] && comment.range[1] <= next.range[0]);
47
+ }
48
+ function reportIfSeparated(context, sourceCode, previous, next) {
49
+ if (next.loc.start.line <= previous.loc.end.line + 1 ||
50
+ hasCommentBetween(sourceCode, previous, next)) {
51
+ return;
52
+ }
53
+ context.report({
54
+ fix(fixer) {
55
+ return getRemoveBlankLinesFix(fixer, sourceCode, previous, next);
56
+ },
57
+ node: next,
58
+ messageId: "adjacent",
59
+ });
60
+ }
61
+ /**
62
+ * オーバーロード署名と実装の間に空行を入れないようにします。
63
+ */
64
+ const overloadSignaturesAdjacent = defineRule({
65
+ meta: {
66
+ docs: {
67
+ description: "Require overload signatures and their implementation to be adjacent.",
68
+ },
69
+ type: "layout",
70
+ fixable: "whitespace",
71
+ messages: {
72
+ adjacent: "Overload signatures must be adjacent to each other and to the implementation.",
73
+ },
74
+ },
75
+ create(context) {
76
+ const sourceCode = context.sourceCode;
77
+ return {
78
+ ClassBody(node) {
79
+ for (let index = 1; index < node.body.length; index += 1) {
80
+ const previous = node.body[index - 1];
81
+ const next = node.body[index];
82
+ if (previous === undefined ||
83
+ next === undefined ||
84
+ previous.type !== "MethodDefinition" ||
85
+ previous.value.body !== null ||
86
+ next.type !== "MethodDefinition") {
87
+ continue;
88
+ }
89
+ const previousName = getKeyName(previous);
90
+ const nextName = getKeyName(next);
91
+ if (previousName === null || previousName !== nextName) {
92
+ continue;
93
+ }
94
+ reportIfSeparated(context, sourceCode, previous, next);
95
+ }
96
+ },
97
+ TSDeclareFunction(node) {
98
+ if (node.body !== null) {
99
+ return;
100
+ }
101
+ const nextStatement = getNextStatement(node);
102
+ if (nextStatement === null) {
103
+ return;
104
+ }
105
+ const nextFunction = getFunction(nextStatement);
106
+ if (nextFunction === null || nextFunction.id?.name !== node.id?.name) {
107
+ return;
108
+ }
109
+ reportIfSeparated(context, sourceCode, node, nextStatement);
110
+ },
111
+ FunctionDeclaration(node) {
112
+ if (node.body !== null) {
113
+ return;
114
+ }
115
+ const nextStatement = getNextStatement(node);
116
+ if (nextStatement === null) {
117
+ return;
118
+ }
119
+ const nextFunction = getFunction(nextStatement);
120
+ if (nextFunction === null || nextFunction.id?.name !== node.id?.name) {
121
+ return;
122
+ }
123
+ reportIfSeparated(context, sourceCode, node, nextStatement);
124
+ },
125
+ ExportNamedDeclaration(node) {
126
+ const currentFunction = getFunction(node);
127
+ if (currentFunction === null || currentFunction.body !== null) {
128
+ return;
129
+ }
130
+ const nextStatement = getNextStatement(node);
131
+ if (nextStatement === null) {
132
+ return;
133
+ }
134
+ const nextFunction = getFunction(nextStatement);
135
+ if (nextFunction === null || nextFunction.id?.name !== currentFunction.id?.name) {
136
+ return;
137
+ }
138
+ reportIfSeparated(context, sourceCode, node, nextStatement);
139
+ },
140
+ };
141
+ },
142
+ });
143
+ export default overloadSignaturesAdjacent;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * import / export の波括弧内の specifier を並べます。
3
+ *
4
+ * 「型 → 変数など → default」のグループ順で、各グループ内は名前の長さが短い順、同じ長さなら名前順です。
5
+ */
6
+ declare const sortImportExportSpecifiers: import("vite-plus/lint/plugins").Rule;
7
+ export default sortImportExportSpecifiers;
8
+ //# sourceMappingURL=sort-import-export-specifiers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sort-import-export-specifiers.d.ts","sourceRoot":"","sources":["../../../src/rules/sort-import-export-specifiers.ts"],"names":[],"mappings":"AAwEA;;;;GAIG;AACH,QAAA,MAAM,0BAA0B,uCAkH9B,CAAC;eAEY,0BAA0B"}
@@ -0,0 +1,130 @@
1
+ import { defineRule } from "vite-plus/lint/plugins";
2
+ import { compareNames } from "./object-sort-utils.js";
3
+ function getSpecifierName(node) {
4
+ if (node.type === "Identifier") {
5
+ return node.name;
6
+ }
7
+ if (node.type === "Literal") {
8
+ return node.value;
9
+ }
10
+ return null;
11
+ }
12
+ function getLineIndent(source, offset) {
13
+ const lineStart = source.lastIndexOf("\n", offset) + 1;
14
+ const match = /^[ \t]*/u.exec(source.slice(lineStart, offset));
15
+ return match?.[0] ?? "";
16
+ }
17
+ function hasUnsafeComment(node, specifiers, sourceCode) {
18
+ const comments = sourceCode
19
+ .getAllComments()
20
+ .filter((comment) => comment.range[0] >= node.range[0] && comment.range[1] <= node.range[1]);
21
+ return comments.some((comment) => !specifiers.some((specifier) => comment.range[0] >= specifier.range[0] && comment.range[1] <= specifier.range[1]));
22
+ }
23
+ function buildReplacement(units, sourceCode, first, last) {
24
+ const sorted = [...units].sort((left, right) => {
25
+ if (left.group !== right.group) {
26
+ return left.group - right.group;
27
+ }
28
+ return compareNames(left.name, right.name);
29
+ });
30
+ if (sorted.every((unit, index) => unit === units[index])) {
31
+ return null;
32
+ }
33
+ const multiline = first.loc.start.line !== last.loc.end.line;
34
+ const separator = multiline ? `,\n${getLineIndent(sourceCode.text, first.range[0])}` : ", ";
35
+ return sorted.map((unit) => unit.text).join(separator);
36
+ }
37
+ /**
38
+ * import / export の波括弧内の specifier を並べます。
39
+ *
40
+ * 「型 → 変数など → default」のグループ順で、各グループ内は名前の長さが短い順、同じ長さなら名前順です。
41
+ */
42
+ const sortImportExportSpecifiers = defineRule({
43
+ meta: {
44
+ docs: {
45
+ description: "Sort import and export specifiers by group (type, value, default), then by name length and name.",
46
+ },
47
+ type: "layout",
48
+ fixable: "whitespace",
49
+ messages: {
50
+ sort: "Import/export specifiers must be sorted by group, name length, then name.",
51
+ },
52
+ },
53
+ create(context) {
54
+ const sourceCode = context.sourceCode;
55
+ function reportSort(node, specifiers, units) {
56
+ const first = specifiers[0];
57
+ const last = specifiers.at(-1);
58
+ if (first === undefined || last === undefined) {
59
+ return;
60
+ }
61
+ const replacement = buildReplacement(units, sourceCode, first, last);
62
+ if (replacement === null) {
63
+ return;
64
+ }
65
+ context.report({
66
+ fix(fixer) {
67
+ return fixer.replaceTextRange([first.range[0], last.range[1]], replacement);
68
+ },
69
+ node,
70
+ messageId: "sort",
71
+ });
72
+ }
73
+ return {
74
+ ImportDeclaration(node) {
75
+ const specifiers = node.specifiers.filter((specifier) => specifier.type === "ImportSpecifier");
76
+ if (specifiers.length < 2 || hasUnsafeComment(node, specifiers, sourceCode)) {
77
+ return;
78
+ }
79
+ const units = [];
80
+ for (const specifier of specifiers) {
81
+ const name = getSpecifierName(specifier.imported);
82
+ if (name === null) {
83
+ return;
84
+ }
85
+ let group = 1;
86
+ if (node.importKind === "type" || specifier.importKind === "type") {
87
+ group = 0;
88
+ }
89
+ else if (name === "default") {
90
+ group = 2;
91
+ }
92
+ units.push({
93
+ name,
94
+ text: sourceCode.text.slice(specifier.range[0], specifier.range[1]),
95
+ group,
96
+ });
97
+ }
98
+ reportSort(node, specifiers, units);
99
+ },
100
+ ExportNamedDeclaration(node) {
101
+ const { specifiers } = node;
102
+ if (specifiers.length < 2 || hasUnsafeComment(node, specifiers, sourceCode)) {
103
+ return;
104
+ }
105
+ const units = [];
106
+ for (const specifier of specifiers) {
107
+ const localName = getSpecifierName(specifier.local);
108
+ const exportedName = getSpecifierName(specifier.exported);
109
+ if (localName === null || exportedName === null) {
110
+ return;
111
+ }
112
+ let group = 1;
113
+ if (node.exportKind === "type" || specifier.exportKind === "type") {
114
+ group = 0;
115
+ }
116
+ else if (localName === "default" || exportedName === "default") {
117
+ group = 2;
118
+ }
119
+ units.push({
120
+ name: localName,
121
+ text: sourceCode.text.slice(specifier.range[0], specifier.range[1]),
122
+ group,
123
+ });
124
+ }
125
+ reportSort(node, specifiers, units);
126
+ },
127
+ };
128
+ },
129
+ });
130
+ export default sortImportExportSpecifiers;