@tai-kun/vp-lint-plugins 0.0.4 → 0.0.6

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 (79) 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/{comment-utils.d.ts → _comment-utils.d.ts} +1 -1
  9. package/dist/src/rules/_comment-utils.d.ts.map +1 -0
  10. package/dist/src/rules/{comment-utils.js → _comment-utils.js} +2 -1
  11. package/dist/src/rules/{jsdoc-utils.d.ts → _jsdoc-utils.d.ts} +1 -1
  12. package/dist/src/rules/_jsdoc-utils.d.ts.map +1 -0
  13. package/dist/src/rules/{jsdoc-utils.js → _jsdoc-utils.js} +1 -1
  14. package/dist/src/rules/{object-sort-utils.d.ts → _object-sort-utils.d.ts} +1 -1
  15. package/dist/src/rules/_object-sort-utils.d.ts.map +1 -0
  16. package/dist/src/rules/blank-line.d.ts +5 -14
  17. package/dist/src/rules/blank-line.d.ts.map +1 -1
  18. package/dist/src/rules/blank-line.js +426 -66
  19. package/dist/src/rules/comment-japanese-spacing.js +1 -1
  20. package/dist/src/rules/comment-one-sentence-per-line.d.ts +1 -1
  21. package/dist/src/rules/comment-one-sentence-per-line.js +2 -2
  22. package/dist/src/rules/jsdoc-param-order.js +1 -1
  23. package/dist/src/rules/jsdoc-tag-lines.js +1 -1
  24. package/dist/src/rules/jsdoc-tag-order.js +1 -1
  25. package/dist/src/rules/object-properties-newline.d.ts +2 -1
  26. package/dist/src/rules/object-properties-newline.d.ts.map +1 -1
  27. package/dist/src/rules/object-properties-newline.js +39 -8
  28. package/dist/src/rules/overload-signatures-adjacent.js +1 -1
  29. package/dist/src/rules/sort-import-export-specifiers.js +1 -1
  30. package/dist/src/rules/sort-jsx-props.js +1 -1
  31. package/dist/src/rules/sort-object-properties.js +1 -1
  32. package/dist/src/rules/sort-type-members.js +1 -1
  33. package/package.json +1 -1
  34. package/src/index.ts +1 -6
  35. package/src/plugin.ts +2 -12
  36. package/src/rules/_blank-line-utils.ts +165 -0
  37. package/src/rules/{comment-utils.ts → _comment-utils.ts} +2 -1
  38. package/src/rules/{jsdoc-utils.ts → _jsdoc-utils.ts} +1 -1
  39. package/src/rules/blank-line.ts +555 -97
  40. package/src/rules/comment-japanese-spacing.ts +2 -2
  41. package/src/rules/comment-one-sentence-per-line.ts +3 -3
  42. package/src/rules/jsdoc-param-order.ts +1 -1
  43. package/src/rules/jsdoc-tag-lines.ts +2 -2
  44. package/src/rules/jsdoc-tag-order.ts +1 -1
  45. package/src/rules/object-properties-newline.ts +93 -10
  46. package/src/rules/overload-signatures-adjacent.ts +1 -1
  47. package/src/rules/sort-import-export-specifiers.ts +1 -1
  48. package/src/rules/sort-jsx-props.ts +1 -1
  49. package/src/rules/sort-object-properties.ts +1 -1
  50. package/src/rules/sort-type-members.ts +1 -1
  51. package/dist/src/rules/blank-line-after-control-block.d.ts +0 -7
  52. package/dist/src/rules/blank-line-after-control-block.d.ts.map +0 -1
  53. package/dist/src/rules/blank-line-after-control-block.js +0 -54
  54. package/dist/src/rules/blank-line-after-declaration.d.ts +0 -7
  55. package/dist/src/rules/blank-line-after-declaration.d.ts.map +0 -1
  56. package/dist/src/rules/blank-line-after-declaration.js +0 -79
  57. package/dist/src/rules/blank-line-after-variable-declaration.d.ts +0 -9
  58. package/dist/src/rules/blank-line-after-variable-declaration.d.ts.map +0 -1
  59. package/dist/src/rules/blank-line-after-variable-declaration.js +0 -118
  60. package/dist/src/rules/blank-line-before-return.d.ts +0 -7
  61. package/dist/src/rules/blank-line-before-return.d.ts.map +0 -1
  62. package/dist/src/rules/blank-line-before-return.js +0 -81
  63. package/dist/src/rules/blank-line-between-class-members.d.ts +0 -8
  64. package/dist/src/rules/blank-line-between-class-members.d.ts.map +0 -1
  65. package/dist/src/rules/blank-line-between-class-members.js +0 -85
  66. package/dist/src/rules/blank-line-between-switch-cases.d.ts +0 -8
  67. package/dist/src/rules/blank-line-between-switch-cases.d.ts.map +0 -1
  68. package/dist/src/rules/blank-line-between-switch-cases.js +0 -45
  69. package/dist/src/rules/comment-utils.d.ts.map +0 -1
  70. package/dist/src/rules/jsdoc-utils.d.ts.map +0 -1
  71. package/dist/src/rules/object-sort-utils.d.ts.map +0 -1
  72. package/src/rules/blank-line-after-control-block.ts +0 -82
  73. package/src/rules/blank-line-after-declaration.ts +0 -106
  74. package/src/rules/blank-line-after-variable-declaration.ts +0 -165
  75. package/src/rules/blank-line-before-return.ts +0 -101
  76. package/src/rules/blank-line-between-class-members.ts +0 -112
  77. package/src/rules/blank-line-between-switch-cases.ts +0 -53
  78. /package/dist/src/rules/{object-sort-utils.js → _object-sort-utils.js} +0 -0
  79. /package/src/rules/{object-sort-utils.ts → _object-sort-utils.ts} +0 -0
@@ -1,5 +1,5 @@
1
1
  import { defineRule } from "vite-plus/lint/plugins";
2
- import { getNextStatement, getRemoveBlankLineFix } from "./blank-line.js";
2
+ import { getNextStatement, getRemoveBlankLineFix } from "./_blank-line-utils.js";
3
3
  function getFunction(node) {
4
4
  if (node.type === "FunctionDeclaration" || node.type === "TSDeclareFunction") {
5
5
  return node;
@@ -1,5 +1,5 @@
1
1
  import { defineRule } from "vite-plus/lint/plugins";
2
- import { compareNames } from "./object-sort-utils.js";
2
+ import { compareNames } from "./_object-sort-utils.js";
3
3
  function getSpecifierName(node) {
4
4
  if (node.type === "Identifier") {
5
5
  return node.name;
@@ -1,5 +1,5 @@
1
1
  import { defineRule } from "vite-plus/lint/plugins";
2
- import { compareNames } from "./object-sort-utils.js";
2
+ import { compareNames } from "./_object-sort-utils.js";
3
3
  function isNamedAttribute(attribute) {
4
4
  return attribute.type === "JSXAttribute" && attribute.name.type === "JSXIdentifier";
5
5
  }
@@ -1,5 +1,5 @@
1
1
  import { defineRule } from "vite-plus/lint/plugins";
2
- import { getSortPlan } from "./object-sort-utils.js";
2
+ import { getSortPlan } from "./_object-sort-utils.js";
3
3
  /**
4
4
  * オブジェクトリテラルのプロパティーを、名前の長さが短い順、同じ長さなら名前順に並べます。
5
5
  *
@@ -1,5 +1,5 @@
1
1
  import { defineRule } from "vite-plus/lint/plugins";
2
- import { compareNames } from "./object-sort-utils.js";
2
+ import { compareNames } from "./_object-sort-utils.js";
3
3
  function getLineStarts(text) {
4
4
  const starts = [0];
5
5
  for (let index = 0; index < text.length; index += 1) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tai-kun/vp-lint-plugins",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "",
5
5
  "homepage": "https://tai-kun.github.io/vp-lint-plugins/",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -1,9 +1,4 @@
1
- export { default as blankLineAfterControlBlock } from "./rules/blank-line-after-control-block.js";
2
- export { default as blankLineAfterDeclaration } from "./rules/blank-line-after-declaration.js";
3
- export { default as blankLineAfterVariableDeclaration } from "./rules/blank-line-after-variable-declaration.js";
4
- export { default as blankLineBeforeReturn } from "./rules/blank-line-before-return.js";
5
- export { default as blankLineBetweenClassMembers } from "./rules/blank-line-between-class-members.js";
6
- export { default as blankLineBetweenSwitchCases } from "./rules/blank-line-between-switch-cases.js";
1
+ export { default as blankLine } from "./rules/blank-line.js";
7
2
  export { default as commentJapaneseSpacing } from "./rules/comment-japanese-spacing.js";
8
3
  export { default as commentOneSentencePerLine } from "./rules/comment-one-sentence-per-line.js";
9
4
  export { default as jsdocParamOrder } from "./rules/jsdoc-param-order.js";
package/src/plugin.ts CHANGED
@@ -1,11 +1,6 @@
1
1
  import { definePlugin } from "vite-plus/lint/plugins";
2
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";
3
+ import blankLine from "./rules/blank-line.js";
9
4
  import commentJapaneseSpacing from "./rules/comment-japanese-spacing.js";
10
5
  import commentOneSentencePerLine from "./rules/comment-one-sentence-per-line.js";
11
6
  import jsdocParamOrder from "./rules/jsdoc-param-order.js";
@@ -25,6 +20,7 @@ import typeLiteralPropertiesNewline from "./rules/type-literal-properties-newlin
25
20
  export default definePlugin({
26
21
  meta: { name: "tai-kun" },
27
22
  rules: {
23
+ "blank-line": blankLine,
28
24
  "merge-imports": mergeImports,
29
25
  "sort-jsx-props": sortJsxProps,
30
26
  "spaced-comment": spacedComment,
@@ -34,17 +30,11 @@ export default definePlugin({
34
30
  "jsdoc-param-order": jsdocParamOrder,
35
31
  "sort-type-members": sortTypeMembers,
36
32
  "sort-object-properties": sortObjectProperties,
37
- "blank-line-before-return": blankLineBeforeReturn,
38
33
  "comment-japanese-spacing": commentJapaneseSpacing,
39
34
  "object-properties-newline": objectPropertiesNewline,
40
- "blank-line-after-declaration": blankLineAfterDeclaration,
41
35
  "overload-signatures-adjacent": overloadSignaturesAdjacent,
42
36
  "comment-one-sentence-per-line": commentOneSentencePerLine,
43
37
  "sort-import-export-specifiers": sortImportExportSpecifiers,
44
- "blank-line-after-control-block": blankLineAfterControlBlock,
45
- "blank-line-between-switch-cases": blankLineBetweenSwitchCases,
46
38
  "type-literal-properties-newline": typeLiteralPropertiesNewline,
47
- "blank-line-between-class-members": blankLineBetweenClassMembers,
48
- "blank-line-after-variable-declaration": blankLineAfterVariableDeclaration,
49
39
  },
50
40
  });
@@ -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
+ };
@@ -8,7 +8,8 @@ interface CommentLine {
8
8
  start: number;
9
9
  }
10
10
 
11
- const SENTENCE_END = /[。..!?!?]$/u;
11
+ // ラベル行とは別に、閉じ括弧付きのコロン終わりも文末とします。
12
+ const SENTENCE_END = /[。..!?!?::]$/u;
12
13
  const TRAILING_CLOSERS = /[」』】)》)〕〉]}"'”’]+$/u;
13
14
  const DIRECTIVE_PREFIXES = [
14
15
  "#!",
@@ -1,6 +1,6 @@
1
1
  import type { ESTree, Comment, SourceCode } from "vite-plus/lint/plugins";
2
2
 
3
- import { isFence } from "./comment-utils.js";
3
+ import { isFence } from "./_comment-utils.js";
4
4
 
5
5
  interface JSDocLine {
6
6
  content: string;