@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,204 @@
1
+ import { defineRule } from "vite-plus/lint/plugins";
2
+ import type { Fix, Fixer, ESTree, Context, SourceCode } from "vite-plus/lint/plugins";
3
+
4
+ import { getNextStatement } from "./blank-line.js";
5
+
6
+ function getFunction(node: ESTree.Node): ESTree.Function | null {
7
+ if (node.type === "FunctionDeclaration" || node.type === "TSDeclareFunction") {
8
+ return node;
9
+ }
10
+
11
+ if (node.type === "ExportNamedDeclaration" && node.declaration !== null) {
12
+ const declaration = node.declaration;
13
+
14
+ if (declaration.type === "FunctionDeclaration" || declaration.type === "TSDeclareFunction") {
15
+ return declaration;
16
+ }
17
+ }
18
+
19
+ return null;
20
+ }
21
+
22
+ function getKeyName(node: ESTree.MethodDefinition): string | null {
23
+ if (node.computed) {
24
+ return null;
25
+ }
26
+
27
+ const key = node.key;
28
+
29
+ if (key.type === "Identifier") {
30
+ return key.name;
31
+ }
32
+
33
+ if (key.type === "Literal" && (typeof key.value === "string" || typeof key.value === "number")) {
34
+ return String(key.value);
35
+ }
36
+
37
+ return null;
38
+ }
39
+
40
+ function getRemoveBlankLinesFix(
41
+ fixer: Fixer,
42
+ sourceCode: SourceCode,
43
+ previous: ESTree.Node,
44
+ next: ESTree.Node,
45
+ ): Fix | null {
46
+ if (next.loc.start.line <= previous.loc.end.line + 1) {
47
+ return null;
48
+ }
49
+
50
+ const fromLineEnd = sourceCode.text.indexOf("\n", previous.range[1]);
51
+
52
+ if (fromLineEnd === -1) {
53
+ return null;
54
+ }
55
+
56
+ const nextLineStart = sourceCode.text.lastIndexOf("\n", next.range[0]) + 1;
57
+ const indent = sourceCode.text.slice(nextLineStart, next.range[0]);
58
+
59
+ if (indent.trim() !== "") {
60
+ return null;
61
+ }
62
+
63
+ return fixer.replaceTextRange([fromLineEnd, next.range[0]], `\n${indent}`);
64
+ }
65
+
66
+ function hasCommentBetween(
67
+ sourceCode: SourceCode,
68
+ previous: ESTree.Node,
69
+ next: ESTree.Node,
70
+ ): boolean {
71
+ return sourceCode
72
+ .getAllComments()
73
+ .some((comment) => comment.range[0] >= previous.range[1] && comment.range[1] <= next.range[0]);
74
+ }
75
+
76
+ function reportIfSeparated(
77
+ context: Context,
78
+ sourceCode: SourceCode,
79
+ previous: ESTree.Node,
80
+ next: ESTree.Node,
81
+ ): void {
82
+ if (
83
+ next.loc.start.line <= previous.loc.end.line + 1 ||
84
+ hasCommentBetween(sourceCode, previous, next)
85
+ ) {
86
+ return;
87
+ }
88
+
89
+ context.report({
90
+ fix(fixer) {
91
+ return getRemoveBlankLinesFix(fixer, sourceCode, previous, next);
92
+ },
93
+ node: next,
94
+ messageId: "adjacent",
95
+ });
96
+ }
97
+
98
+ /**
99
+ * オーバーロード署名と実装の間に空行を入れないようにします。
100
+ */
101
+ const overloadSignaturesAdjacent = defineRule({
102
+ meta: {
103
+ docs: {
104
+ description: "Require overload signatures and their implementation to be adjacent.",
105
+ },
106
+ type: "layout",
107
+ fixable: "whitespace",
108
+ messages: {
109
+ adjacent: "Overload signatures must be adjacent to each other and to the implementation.",
110
+ },
111
+ },
112
+ create(context) {
113
+ const sourceCode = context.sourceCode;
114
+
115
+ return {
116
+ ClassBody(node): void {
117
+ for (let index = 1; index < node.body.length; index += 1) {
118
+ const previous = node.body[index - 1];
119
+ const next = node.body[index];
120
+
121
+ if (
122
+ previous === undefined ||
123
+ next === undefined ||
124
+ previous.type !== "MethodDefinition" ||
125
+ previous.value.body !== null ||
126
+ next.type !== "MethodDefinition"
127
+ ) {
128
+ continue;
129
+ }
130
+
131
+ const previousName = getKeyName(previous);
132
+ const nextName = getKeyName(next);
133
+
134
+ if (previousName === null || previousName !== nextName) {
135
+ continue;
136
+ }
137
+
138
+ reportIfSeparated(context, sourceCode, previous, next);
139
+ }
140
+ },
141
+ TSDeclareFunction(node): void {
142
+ if (node.body !== null) {
143
+ return;
144
+ }
145
+
146
+ const nextStatement = getNextStatement(node);
147
+
148
+ if (nextStatement === null) {
149
+ return;
150
+ }
151
+
152
+ const nextFunction = getFunction(nextStatement);
153
+
154
+ if (nextFunction === null || nextFunction.id?.name !== node.id?.name) {
155
+ return;
156
+ }
157
+
158
+ reportIfSeparated(context, sourceCode, node, nextStatement);
159
+ },
160
+ FunctionDeclaration(node): void {
161
+ if (node.body !== null) {
162
+ return;
163
+ }
164
+
165
+ const nextStatement = getNextStatement(node);
166
+
167
+ if (nextStatement === null) {
168
+ return;
169
+ }
170
+
171
+ const nextFunction = getFunction(nextStatement);
172
+
173
+ if (nextFunction === null || nextFunction.id?.name !== node.id?.name) {
174
+ return;
175
+ }
176
+
177
+ reportIfSeparated(context, sourceCode, node, nextStatement);
178
+ },
179
+ ExportNamedDeclaration(node): void {
180
+ const currentFunction = getFunction(node);
181
+
182
+ if (currentFunction === null || currentFunction.body !== null) {
183
+ return;
184
+ }
185
+
186
+ const nextStatement = getNextStatement(node);
187
+
188
+ if (nextStatement === null) {
189
+ return;
190
+ }
191
+
192
+ const nextFunction = getFunction(nextStatement);
193
+
194
+ if (nextFunction === null || nextFunction.id?.name !== currentFunction.id?.name) {
195
+ return;
196
+ }
197
+
198
+ reportIfSeparated(context, sourceCode, node, nextStatement);
199
+ },
200
+ };
201
+ },
202
+ });
203
+
204
+ export default overloadSignaturesAdjacent;
@@ -0,0 +1,194 @@
1
+ import { defineRule } from "vite-plus/lint/plugins";
2
+ import type { ESTree, SourceCode } from "vite-plus/lint/plugins";
3
+
4
+ import { compareNames } from "./object-sort-utils.js";
5
+
6
+ interface SpecifierUnit {
7
+ group: number;
8
+ name: string;
9
+ text: string;
10
+ }
11
+
12
+ function getSpecifierName(node: ESTree.ModuleExportName): string | null {
13
+ if (node.type === "Identifier") {
14
+ return node.name;
15
+ }
16
+
17
+ if (node.type === "Literal") {
18
+ return node.value;
19
+ }
20
+
21
+ return null;
22
+ }
23
+
24
+ function getLineIndent(source: string, offset: number): string {
25
+ const lineStart = source.lastIndexOf("\n", offset) + 1;
26
+ const match = /^[ \t]*/u.exec(source.slice(lineStart, offset));
27
+
28
+ return match?.[0] ?? "";
29
+ }
30
+
31
+ function hasUnsafeComment(
32
+ node: ESTree.Node,
33
+ specifiers: readonly ESTree.Node[],
34
+ sourceCode: SourceCode,
35
+ ): boolean {
36
+ const comments = sourceCode
37
+ .getAllComments()
38
+ .filter((comment) => comment.range[0] >= node.range[0] && comment.range[1] <= node.range[1]);
39
+
40
+ return comments.some(
41
+ (comment) =>
42
+ !specifiers.some(
43
+ (specifier) =>
44
+ comment.range[0] >= specifier.range[0] && comment.range[1] <= specifier.range[1],
45
+ ),
46
+ );
47
+ }
48
+
49
+ function buildReplacement(
50
+ units: readonly SpecifierUnit[],
51
+ sourceCode: SourceCode,
52
+ first: ESTree.Node,
53
+ last: ESTree.Node,
54
+ ): string | null {
55
+ const sorted = [...units].sort((left, right) => {
56
+ if (left.group !== right.group) {
57
+ return left.group - right.group;
58
+ }
59
+
60
+ return compareNames(left.name, right.name);
61
+ });
62
+
63
+ if (sorted.every((unit, index) => unit === units[index])) {
64
+ return null;
65
+ }
66
+
67
+ const multiline = first.loc.start.line !== last.loc.end.line;
68
+ const separator = multiline ? `,\n${getLineIndent(sourceCode.text, first.range[0])}` : ", ";
69
+
70
+ return sorted.map((unit) => unit.text).join(separator);
71
+ }
72
+
73
+ /**
74
+ * import / export の波括弧内の specifier を並べます。
75
+ *
76
+ * 「型 → 変数など → default」のグループ順で、各グループ内は名前の長さが短い順、同じ長さなら名前順です。
77
+ */
78
+ const sortImportExportSpecifiers = defineRule({
79
+ meta: {
80
+ docs: {
81
+ description:
82
+ "Sort import and export specifiers by group (type, value, default), then by name length and name.",
83
+ },
84
+ type: "layout",
85
+ fixable: "whitespace",
86
+ messages: {
87
+ sort: "Import/export specifiers must be sorted by group, name length, then name.",
88
+ },
89
+ },
90
+ create(context) {
91
+ const sourceCode = context.sourceCode;
92
+
93
+ function reportSort(
94
+ node: ESTree.Node,
95
+ specifiers: readonly ESTree.Node[],
96
+ units: readonly SpecifierUnit[],
97
+ ): void {
98
+ const first = specifiers[0];
99
+ const last = specifiers.at(-1);
100
+
101
+ if (first === undefined || last === undefined) {
102
+ return;
103
+ }
104
+
105
+ const replacement = buildReplacement(units, sourceCode, first, last);
106
+
107
+ if (replacement === null) {
108
+ return;
109
+ }
110
+
111
+ context.report({
112
+ fix(fixer) {
113
+ return fixer.replaceTextRange([first.range[0], last.range[1]], replacement);
114
+ },
115
+ node,
116
+ messageId: "sort",
117
+ });
118
+ }
119
+
120
+ return {
121
+ ImportDeclaration(node): void {
122
+ const specifiers = node.specifiers.filter(
123
+ (specifier): specifier is ESTree.ImportSpecifier => specifier.type === "ImportSpecifier",
124
+ );
125
+
126
+ if (specifiers.length < 2 || hasUnsafeComment(node, specifiers, sourceCode)) {
127
+ return;
128
+ }
129
+
130
+ const units: SpecifierUnit[] = [];
131
+
132
+ for (const specifier of specifiers) {
133
+ const name = getSpecifierName(specifier.imported);
134
+
135
+ if (name === null) {
136
+ return;
137
+ }
138
+
139
+ let group = 1;
140
+
141
+ if (node.importKind === "type" || specifier.importKind === "type") {
142
+ group = 0;
143
+ } else if (name === "default") {
144
+ group = 2;
145
+ }
146
+
147
+ units.push({
148
+ name,
149
+ text: sourceCode.text.slice(specifier.range[0], specifier.range[1]),
150
+ group,
151
+ });
152
+ }
153
+
154
+ reportSort(node, specifiers, units);
155
+ },
156
+ ExportNamedDeclaration(node): void {
157
+ const { specifiers } = node;
158
+
159
+ if (specifiers.length < 2 || hasUnsafeComment(node, specifiers, sourceCode)) {
160
+ return;
161
+ }
162
+
163
+ const units: SpecifierUnit[] = [];
164
+
165
+ for (const specifier of specifiers) {
166
+ const localName = getSpecifierName(specifier.local);
167
+ const exportedName = getSpecifierName(specifier.exported);
168
+
169
+ if (localName === null || exportedName === null) {
170
+ return;
171
+ }
172
+
173
+ let group = 1;
174
+
175
+ if (node.exportKind === "type" || specifier.exportKind === "type") {
176
+ group = 0;
177
+ } else if (localName === "default" || exportedName === "default") {
178
+ group = 2;
179
+ }
180
+
181
+ units.push({
182
+ name: localName,
183
+ text: sourceCode.text.slice(specifier.range[0], specifier.range[1]),
184
+ group,
185
+ });
186
+ }
187
+
188
+ reportSort(node, specifiers, units);
189
+ },
190
+ };
191
+ },
192
+ });
193
+
194
+ export default sortImportExportSpecifiers;
@@ -0,0 +1,149 @@
1
+ import { defineRule } from "vite-plus/lint/plugins";
2
+ import type { ESTree, SourceCode } from "vite-plus/lint/plugins";
3
+
4
+ import { compareNames } from "./object-sort-utils.js";
5
+
6
+ interface AttributeUnit {
7
+ name: string;
8
+ text: string;
9
+ }
10
+
11
+ type NamedAttribute = ESTree.JSXAttribute & { name: ESTree.JSXIdentifier };
12
+
13
+ function isNamedAttribute(attribute: ESTree.JSXAttributeItem): attribute is NamedAttribute {
14
+ return attribute.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier";
15
+ }
16
+
17
+ function getAttributeGroup(name: string): number {
18
+ if (/^on[A-Z].*/u.test(name)) {
19
+ return 1;
20
+ }
21
+
22
+ if (/^aria-.+/u.test(name)) {
23
+ return 2;
24
+ }
25
+
26
+ if (/^data-.+/u.test(name)) {
27
+ return 3;
28
+ }
29
+
30
+ return 0;
31
+ }
32
+
33
+ function compareAttributes(left: AttributeUnit, right: AttributeUnit): number {
34
+ const leftGroup = getAttributeGroup(left.name);
35
+ const rightGroup = getAttributeGroup(right.name);
36
+
37
+ if (leftGroup !== rightGroup) {
38
+ return leftGroup - rightGroup;
39
+ }
40
+
41
+ return compareNames(left.name, right.name);
42
+ }
43
+
44
+ function getLineIndent(source: string, offset: number): string {
45
+ const lineStart = source.lastIndexOf("\n", offset) + 1;
46
+ const match = /^[ \t]*/u.exec(source.slice(lineStart, offset));
47
+
48
+ return match?.[0] ?? "";
49
+ }
50
+
51
+ function hasCommentBetweenAttributes(
52
+ attributes: readonly NamedAttribute[],
53
+ sourceCode: SourceCode,
54
+ ): boolean {
55
+ const comments = sourceCode.getAllComments();
56
+
57
+ return attributes.some((attribute, index) => {
58
+ const nextAttribute = attributes[index + 1];
59
+
60
+ if (nextAttribute === undefined) {
61
+ return false;
62
+ }
63
+
64
+ return comments.some(
65
+ (comment) =>
66
+ comment.range[0] >= attribute.range[1] && comment.range[1] <= nextAttribute.range[0],
67
+ );
68
+ });
69
+ }
70
+
71
+ /**
72
+ * JSX の props を「一般 → on* → aria-* → data-*」のグループ順に並べます。
73
+ * 各グループ内ではプロパティー名の長さが短い順、同じ長さなら名前順です。
74
+ */
75
+ const sortJsxProps = defineRule({
76
+ meta: {
77
+ docs: {
78
+ description:
79
+ "Sort JSX props by group (general, on*, aria-*, data-*), then by name length and name.",
80
+ },
81
+ type: "layout",
82
+ fixable: "whitespace",
83
+ messages: {
84
+ sort: "JSX props must be sorted by group, name length, then name.",
85
+ },
86
+ },
87
+ create(context) {
88
+ const sourceCode = context.sourceCode;
89
+
90
+ return {
91
+ JSXOpeningElement(node): void {
92
+ const attributes = node.attributes.filter(isNamedAttribute);
93
+
94
+ if (attributes.length < 2 || attributes.length !== node.attributes.length) {
95
+ return;
96
+ }
97
+
98
+ const names = new Set<string>();
99
+
100
+ for (const attribute of attributes) {
101
+ if (names.has(attribute.name.name)) {
102
+ return;
103
+ }
104
+
105
+ names.add(attribute.name.name);
106
+ }
107
+
108
+ if (hasCommentBetweenAttributes(attributes, sourceCode)) {
109
+ return;
110
+ }
111
+
112
+ const units: AttributeUnit[] = attributes.map((attribute) => ({
113
+ name: attribute.name.name,
114
+ text: sourceCode.text.slice(attribute.range[0], attribute.range[1]),
115
+ }));
116
+ const sorted = [...units].sort(compareAttributes);
117
+
118
+ if (sorted.every((unit, index) => unit === units[index])) {
119
+ return;
120
+ }
121
+
122
+ const firstAttribute = attributes[0];
123
+ const lastAttribute = attributes.at(-1);
124
+
125
+ if (firstAttribute === undefined || lastAttribute === undefined) {
126
+ return;
127
+ }
128
+
129
+ const multiline = firstAttribute.loc.start.line !== lastAttribute.loc.end.line;
130
+ const separator = multiline
131
+ ? `\n${getLineIndent(sourceCode.text, firstAttribute.range[0])}`
132
+ : " ";
133
+
134
+ context.report({
135
+ fix(fixer) {
136
+ return fixer.replaceTextRange(
137
+ [firstAttribute.range[0], lastAttribute.range[1]],
138
+ sorted.map((unit) => unit.text).join(separator),
139
+ );
140
+ },
141
+ node,
142
+ messageId: "sort",
143
+ });
144
+ },
145
+ };
146
+ },
147
+ });
148
+
149
+ export default sortJsxProps;
@@ -0,0 +1,62 @@
1
+ import { defineRule } from "vite-plus/lint/plugins";
2
+
3
+ import { getSortPlan } from "./object-sort-utils.js";
4
+
5
+ /**
6
+ * オブジェクトリテラルのプロパティーを、名前の長さが短い順、同じ長さなら名前順に並べます。
7
+ *
8
+ * 空行で区切られたグループは意図的なまとまりとみなし、グループ内だけで並べ替えます。
9
+ * プロパティー直上のコメントはプロパティーと一緒に移動します。
10
+ */
11
+ const sortObjectProperties = defineRule({
12
+ meta: {
13
+ docs: {
14
+ description:
15
+ "Sort object properties by name length and name, preserving comment groups separated by blank lines.",
16
+ },
17
+ type: "layout",
18
+ fixable: "whitespace",
19
+ messages: {
20
+ sort: "Object properties must be sorted by name length, then by name.",
21
+ },
22
+ },
23
+ create(context) {
24
+ return {
25
+ ObjectPattern(node): void {
26
+ const plan = getSortPlan(node, context.sourceCode, {
27
+ allowRest: true,
28
+ forceMultiline: false,
29
+ });
30
+
31
+ if (plan === null || !plan.needsFix) {
32
+ return;
33
+ }
34
+
35
+ context.report({
36
+ fix(fixer) {
37
+ return fixer.replaceText(node, plan.replacement);
38
+ },
39
+ node,
40
+ messageId: "sort",
41
+ });
42
+ },
43
+ ObjectExpression(node): void {
44
+ const plan = getSortPlan(node, context.sourceCode);
45
+
46
+ if (plan === null || !plan.needsFix) {
47
+ return;
48
+ }
49
+
50
+ context.report({
51
+ fix(fixer) {
52
+ return fixer.replaceText(node, plan.replacement);
53
+ },
54
+ node,
55
+ messageId: "sort",
56
+ });
57
+ },
58
+ };
59
+ },
60
+ });
61
+
62
+ export default sortObjectProperties;