@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.
- package/LICENSE +21 -0
- package/README.md +1 -43
- package/dist/src/index.d.ts +22 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +21 -0
- package/dist/src/plugin.d.ts +3 -0
- package/dist/src/plugin.d.ts.map +1 -0
- package/dist/src/plugin.js +46 -0
- package/dist/src/rules/blank-line-after-control-block.d.ts +7 -0
- package/dist/src/rules/blank-line-after-control-block.d.ts.map +1 -0
- package/dist/src/rules/blank-line-after-control-block.js +54 -0
- package/dist/src/rules/blank-line-after-declaration.d.ts +7 -0
- package/dist/src/rules/blank-line-after-declaration.d.ts.map +1 -0
- package/dist/src/rules/blank-line-after-declaration.js +79 -0
- package/dist/src/rules/blank-line-after-variable-declaration.d.ts +8 -0
- package/dist/src/rules/blank-line-after-variable-declaration.d.ts.map +1 -0
- package/dist/src/rules/blank-line-after-variable-declaration.js +62 -0
- package/dist/src/rules/blank-line-before-return.d.ts +7 -0
- package/dist/src/rules/blank-line-before-return.d.ts.map +1 -0
- package/dist/src/rules/blank-line-before-return.js +81 -0
- package/dist/src/rules/blank-line-between-class-members.d.ts +8 -0
- package/dist/src/rules/blank-line-between-class-members.d.ts.map +1 -0
- package/dist/src/rules/blank-line-between-class-members.js +85 -0
- package/dist/src/rules/blank-line-between-switch-cases.d.ts +8 -0
- package/dist/src/rules/blank-line-between-switch-cases.d.ts.map +1 -0
- package/dist/src/rules/blank-line-between-switch-cases.js +45 -0
- package/dist/src/rules/blank-line.d.ts +14 -0
- package/dist/src/rules/blank-line.d.ts.map +1 -0
- package/dist/src/rules/blank-line.js +68 -0
- package/dist/src/rules/comment-japanese-spacing.d.ts +8 -0
- package/dist/src/rules/comment-japanese-spacing.d.ts.map +1 -0
- package/dist/src/rules/comment-japanese-spacing.js +195 -0
- package/dist/src/rules/comment-one-sentence-per-line.d.ts +9 -0
- package/dist/src/rules/comment-one-sentence-per-line.d.ts.map +1 -0
- package/dist/src/rules/comment-one-sentence-per-line.js +190 -0
- package/dist/src/rules/comment-utils.d.ts +30 -0
- package/dist/src/rules/comment-utils.d.ts.map +1 -0
- package/dist/src/rules/comment-utils.js +153 -0
- package/dist/src/rules/jsdoc-param-order.d.ts +6 -0
- package/dist/src/rules/jsdoc-param-order.d.ts.map +1 -0
- package/dist/src/rules/jsdoc-param-order.js +101 -0
- package/dist/src/rules/jsdoc-tag-lines.d.ts +6 -0
- package/dist/src/rules/jsdoc-tag-lines.d.ts.map +1 -0
- package/dist/src/rules/jsdoc-tag-lines.js +128 -0
- package/dist/src/rules/jsdoc-tag-order.d.ts +6 -0
- package/dist/src/rules/jsdoc-tag-order.d.ts.map +1 -0
- package/dist/src/rules/jsdoc-tag-order.js +62 -0
- package/dist/src/rules/jsdoc-utils.d.ts +26 -0
- package/dist/src/rules/jsdoc-utils.d.ts.map +1 -0
- package/dist/src/rules/jsdoc-utils.js +132 -0
- package/dist/src/rules/member-ordering.d.ts +6 -0
- package/dist/src/rules/member-ordering.d.ts.map +1 -0
- package/dist/src/rules/member-ordering.js +66 -0
- package/dist/src/rules/object-properties-newline.d.ts +9 -0
- package/dist/src/rules/object-properties-newline.d.ts.map +1 -0
- package/dist/src/rules/object-properties-newline.js +47 -0
- package/dist/src/rules/object-sort-utils.d.ts +32 -0
- package/dist/src/rules/object-sort-utils.d.ts.map +1 -0
- package/dist/src/rules/object-sort-utils.js +221 -0
- package/dist/src/rules/overload-signatures-adjacent.d.ts +6 -0
- package/dist/src/rules/overload-signatures-adjacent.d.ts.map +1 -0
- package/dist/src/rules/overload-signatures-adjacent.js +143 -0
- package/dist/src/rules/sort-import-export-specifiers.d.ts +8 -0
- package/dist/src/rules/sort-import-export-specifiers.d.ts.map +1 -0
- package/dist/src/rules/sort-import-export-specifiers.js +130 -0
- package/dist/src/rules/sort-jsx-props.d.ts +7 -0
- package/dist/src/rules/sort-jsx-props.d.ts.map +1 -0
- package/dist/src/rules/sort-jsx-props.js +102 -0
- package/dist/src/rules/sort-object-properties.d.ts +9 -0
- package/dist/src/rules/sort-object-properties.d.ts.map +1 -0
- package/dist/src/rules/sort-object-properties.js +54 -0
- package/dist/src/rules/sort-type-members.d.ts +8 -0
- package/dist/src/rules/sort-type-members.d.ts.map +1 -0
- package/dist/src/rules/sort-type-members.js +213 -0
- package/dist/src/rules/spaced-comment.d.ts +6 -0
- package/dist/src/rules/spaced-comment.d.ts.map +1 -0
- package/dist/src/rules/spaced-comment.js +105 -0
- package/dist/src/rules/type-literal-properties-newline.d.ts +8 -0
- package/dist/src/rules/type-literal-properties-newline.d.ts.map +1 -0
- package/dist/src/rules/type-literal-properties-newline.js +39 -0
- package/package.json +40 -8
- package/src/index.ts +22 -0
- package/src/plugin.ts +48 -0
- package/src/rules/blank-line-after-control-block.ts +82 -0
- package/src/rules/blank-line-after-declaration.ts +106 -0
- package/src/rules/blank-line-after-variable-declaration.ts +84 -0
- package/src/rules/blank-line-before-return.ts +101 -0
- package/src/rules/blank-line-between-class-members.ts +112 -0
- package/src/rules/blank-line-between-switch-cases.ts +53 -0
- package/src/rules/blank-line.ts +113 -0
- package/src/rules/comment-japanese-spacing.ts +271 -0
- package/src/rules/comment-one-sentence-per-line.ts +264 -0
- package/src/rules/comment-utils.ts +218 -0
- package/src/rules/jsdoc-param-order.ts +137 -0
- package/src/rules/jsdoc-tag-lines.ts +165 -0
- package/src/rules/jsdoc-tag-order.ts +75 -0
- package/src/rules/jsdoc-utils.ts +179 -0
- package/src/rules/member-ordering.ts +93 -0
- package/src/rules/object-properties-newline.ts +54 -0
- package/src/rules/object-sort-utils.ts +339 -0
- package/src/rules/overload-signatures-adjacent.ts +204 -0
- package/src/rules/sort-import-export-specifiers.ts +194 -0
- package/src/rules/sort-jsx-props.ts +149 -0
- package/src/rules/sort-object-properties.ts +62 -0
- package/src/rules/sort-type-members.ts +299 -0
- package/src/rules/spaced-comment.ts +129 -0
- package/src/rules/type-literal-properties-newline.ts +44 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
const SENTENCE_END = /[。..!?!?]$/u;
|
|
2
|
+
const TRAILING_CLOSERS = /[」』】)》)〕〉]}"'”’]+$/u;
|
|
3
|
+
const DIRECTIVE_PREFIXES = [
|
|
4
|
+
"#!",
|
|
5
|
+
"///",
|
|
6
|
+
"oxlint-",
|
|
7
|
+
"eslint-",
|
|
8
|
+
"prettier-",
|
|
9
|
+
"@ts-",
|
|
10
|
+
"@type",
|
|
11
|
+
"@see",
|
|
12
|
+
"@link",
|
|
13
|
+
"TODO",
|
|
14
|
+
"FIXME",
|
|
15
|
+
"NOTE",
|
|
16
|
+
"HACK",
|
|
17
|
+
"XXX",
|
|
18
|
+
"istanbul",
|
|
19
|
+
"c8 ",
|
|
20
|
+
"v8 ",
|
|
21
|
+
"#region",
|
|
22
|
+
"#endregion",
|
|
23
|
+
"biome-",
|
|
24
|
+
"deno-",
|
|
25
|
+
];
|
|
26
|
+
function isBlank(text) {
|
|
27
|
+
return text.trim() === "";
|
|
28
|
+
}
|
|
29
|
+
function isDirective(text) {
|
|
30
|
+
const value = text.trimStart();
|
|
31
|
+
return DIRECTIVE_PREFIXES.some((prefix) => value.indexOf(prefix) === 0);
|
|
32
|
+
}
|
|
33
|
+
function isLabelLine(text) {
|
|
34
|
+
const value = text.trim();
|
|
35
|
+
return /^[^\s::]{1,20}[::]\s/u.test(value) || /[::]$/u.test(value);
|
|
36
|
+
}
|
|
37
|
+
function isTag(text) {
|
|
38
|
+
return /^@[A-Za-z]/u.test(text.trimStart());
|
|
39
|
+
}
|
|
40
|
+
function isListOrHeading(text) {
|
|
41
|
+
return /^(?:[-*+]\s|\d+[.)]\s|#{1,6}\s|\|)/u.test(text.trimStart());
|
|
42
|
+
}
|
|
43
|
+
function isCodeLike(text) {
|
|
44
|
+
const value = text.trimStart();
|
|
45
|
+
return (/^(?:```|~~~|const |let |var |function |return |if \(|for \(|while \(|switch \(|class |import |export |type |interface |\{|\}|\$ |> |pnpm |npm |npx |node |git |mise |yarn |bun |deno |curl |cd |echo |sudo |https?:\/\/)/u.test(value) || /=>|;\s*$/u.test(value));
|
|
46
|
+
}
|
|
47
|
+
function isFence(text) {
|
|
48
|
+
const value = text.trimStart();
|
|
49
|
+
return value.slice(0, 3) === "```" || value.slice(0, 3) === "~~~";
|
|
50
|
+
}
|
|
51
|
+
function isSentenceEnd(text) {
|
|
52
|
+
const value = text.trimEnd().replace(TRAILING_CLOSERS, "");
|
|
53
|
+
return SENTENCE_END.test(value);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 散文として扱わない行かどうかを返します。
|
|
57
|
+
*/
|
|
58
|
+
function isSkippableProseLine(text) {
|
|
59
|
+
const trimmed = text.trim();
|
|
60
|
+
if (trimmed === "" || isFence(trimmed)) {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
return (isDirective(trimmed) ||
|
|
64
|
+
isLabelLine(trimmed) ||
|
|
65
|
+
isTag(trimmed) ||
|
|
66
|
+
isListOrHeading(trimmed) ||
|
|
67
|
+
isCodeLike(trimmed));
|
|
68
|
+
}
|
|
69
|
+
function getBlockLineInfos(comment, sourceCode) {
|
|
70
|
+
const raw = sourceCode.getText(comment);
|
|
71
|
+
if (!raw.includes("\n")) {
|
|
72
|
+
return [];
|
|
73
|
+
}
|
|
74
|
+
const lines = raw.split("\n");
|
|
75
|
+
const infos = [];
|
|
76
|
+
let offset = comment.range[0];
|
|
77
|
+
for (const [index, line] of lines.entries()) {
|
|
78
|
+
let contentStart;
|
|
79
|
+
let contentEnd = line.length;
|
|
80
|
+
if (index === 0) {
|
|
81
|
+
contentStart = raw.slice(0, 3) === "/**" ? 3 : 2;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
const match = /^(?<prefix>\s*\*[ \t]?).*$/u.exec(line);
|
|
85
|
+
if (match === null) {
|
|
86
|
+
infos.push(null);
|
|
87
|
+
offset += line.length + 1;
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
contentStart = match.groups?.["prefix"]?.length ?? 0;
|
|
91
|
+
const closeIndex = line.indexOf("*/");
|
|
92
|
+
if (closeIndex !== -1) {
|
|
93
|
+
contentEnd = Math.max(contentStart, closeIndex);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
infos.push({
|
|
97
|
+
end: offset + contentEnd,
|
|
98
|
+
line: comment.loc.start.line + index,
|
|
99
|
+
start: offset + contentStart,
|
|
100
|
+
column: index === 0 ? comment.loc.start.column + contentStart : contentStart,
|
|
101
|
+
content: line.slice(contentStart, contentEnd),
|
|
102
|
+
});
|
|
103
|
+
offset += line.length + 1;
|
|
104
|
+
}
|
|
105
|
+
return infos;
|
|
106
|
+
}
|
|
107
|
+
function getLineGroupInfos(group, sourceCode) {
|
|
108
|
+
return group.map((comment) => {
|
|
109
|
+
const raw = sourceCode.getText(comment);
|
|
110
|
+
const commentPrefix = raw.slice(0, 2) === "//" ? 2 : 0;
|
|
111
|
+
const spacePrefix = raw[commentPrefix] === " " ? 1 : 0;
|
|
112
|
+
const contentStart = commentPrefix + spacePrefix;
|
|
113
|
+
return {
|
|
114
|
+
end: comment.range[1],
|
|
115
|
+
line: comment.loc.start.line,
|
|
116
|
+
start: comment.range[0] + contentStart,
|
|
117
|
+
column: comment.loc.start.column + contentStart,
|
|
118
|
+
content: raw.slice(contentStart),
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
function getSingleLineBlockInfo(comment, sourceCode) {
|
|
123
|
+
const raw = sourceCode.getText(comment);
|
|
124
|
+
const prefixLength = raw.startsWith("/**") ? 3 : 2;
|
|
125
|
+
const closeIndex = raw.lastIndexOf("*/");
|
|
126
|
+
const contentEnd = closeIndex === -1 ? raw.length : closeIndex;
|
|
127
|
+
return {
|
|
128
|
+
end: comment.range[0] + contentEnd,
|
|
129
|
+
line: comment.loc.start.line,
|
|
130
|
+
start: comment.range[0] + prefixLength,
|
|
131
|
+
column: comment.loc.start.column + prefixLength,
|
|
132
|
+
content: raw.slice(prefixLength, contentEnd),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* コメントの内容行を返します。
|
|
137
|
+
*
|
|
138
|
+
* ブロックコメントで `*` の付かない行がある場合は、その行を null として返します。
|
|
139
|
+
*/
|
|
140
|
+
function getCommentLineInfos(comment, sourceCode) {
|
|
141
|
+
if (comment.type === "Line") {
|
|
142
|
+
return getLineGroupInfos([comment], sourceCode);
|
|
143
|
+
}
|
|
144
|
+
if (comment.type === "Shebang") {
|
|
145
|
+
return [];
|
|
146
|
+
}
|
|
147
|
+
const raw = sourceCode.getText(comment);
|
|
148
|
+
if (!raw.includes("\n")) {
|
|
149
|
+
return [getSingleLineBlockInfo(comment, sourceCode)];
|
|
150
|
+
}
|
|
151
|
+
return getBlockLineInfos(comment, sourceCode);
|
|
152
|
+
}
|
|
153
|
+
export { isTag, isBlank, isFence, isCodeLike, isDirective, isLabelLine, isSentenceEnd, isListOrHeading, getLineGroupInfos, getCommentLineInfos, isSkippableProseLine, };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsdoc-param-order.d.ts","sourceRoot":"","sources":["../../../src/rules/jsdoc-param-order.ts"],"names":[],"mappings":"AA6FA;;GAEG;AACH,QAAA,MAAM,eAAe,uCAsCnB,CAAC;eAEY,eAAe"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
2
|
+
import { getNodeJSDoc, getParamName, getJSDocLines, hasExampleOrFence } from "./jsdoc-utils.js";
|
|
3
|
+
function getParamNameFromPattern(param) {
|
|
4
|
+
if (param.type === "Identifier") {
|
|
5
|
+
return param.name;
|
|
6
|
+
}
|
|
7
|
+
if (param.type === "AssignmentPattern") {
|
|
8
|
+
return param.left.type === "Identifier" ? param.left.name : null;
|
|
9
|
+
}
|
|
10
|
+
if (param.type === "RestElement") {
|
|
11
|
+
return param.argument.type === "Identifier" ? param.argument.name : null;
|
|
12
|
+
}
|
|
13
|
+
if (param.type === "TSParameterProperty") {
|
|
14
|
+
return getParamNameFromPattern(param.parameter);
|
|
15
|
+
}
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
function getExpectedParams(params) {
|
|
19
|
+
const names = [];
|
|
20
|
+
for (const param of params) {
|
|
21
|
+
const name = getParamNameFromPattern(param);
|
|
22
|
+
if (name === null) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
names.push(name);
|
|
26
|
+
}
|
|
27
|
+
return names;
|
|
28
|
+
}
|
|
29
|
+
function checkFunction(context, sourceCode, node, params) {
|
|
30
|
+
const comment = getNodeJSDoc(node, sourceCode);
|
|
31
|
+
if (comment === null) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const lines = getJSDocLines(comment, sourceCode);
|
|
35
|
+
if (lines === null || hasExampleOrFence(lines)) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const tagNames = lines
|
|
39
|
+
.filter((line) => line.tagName === "param")
|
|
40
|
+
.map((line) => getParamName(line.content));
|
|
41
|
+
if (tagNames.length === 0 || tagNames.some((name) => name === null)) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const expected = getExpectedParams(params);
|
|
45
|
+
if (expected === null || expected.length !== tagNames.length) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (new Set(expected).size !== expected.length ||
|
|
49
|
+
new Set(tagNames).size !== tagNames.length ||
|
|
50
|
+
tagNames.some((name) => name?.includes(".") === true)) {
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
for (const [index, name] of tagNames.entries()) {
|
|
54
|
+
if (name !== expected[index]) {
|
|
55
|
+
context.report({
|
|
56
|
+
loc: comment.loc,
|
|
57
|
+
messageId: "paramOrder",
|
|
58
|
+
});
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* `@param` タグが関数の引数の順序に従っていることを要求します。
|
|
65
|
+
*/
|
|
66
|
+
const jsdocParamOrder = defineRule({
|
|
67
|
+
meta: {
|
|
68
|
+
docs: {
|
|
69
|
+
description: "Require @param tags to follow the order of function parameters.",
|
|
70
|
+
},
|
|
71
|
+
type: "suggestion",
|
|
72
|
+
messages: {
|
|
73
|
+
paramOrder: "@param tags must follow the order of function parameters.",
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
create(context) {
|
|
77
|
+
const sourceCode = context.sourceCode;
|
|
78
|
+
return {
|
|
79
|
+
MethodDefinition(node) {
|
|
80
|
+
checkFunction(context, sourceCode, node, node.value.params);
|
|
81
|
+
},
|
|
82
|
+
TSDeclareFunction(node) {
|
|
83
|
+
checkFunction(context, sourceCode, node, node.params);
|
|
84
|
+
},
|
|
85
|
+
FunctionDeclaration(node) {
|
|
86
|
+
checkFunction(context, sourceCode, node, node.params);
|
|
87
|
+
},
|
|
88
|
+
ExportNamedDeclaration(node) {
|
|
89
|
+
const declaration = node.declaration;
|
|
90
|
+
if (declaration !== null &&
|
|
91
|
+
(declaration.type === "FunctionDeclaration" || declaration.type === "TSDeclareFunction")) {
|
|
92
|
+
checkFunction(context, sourceCode, node, declaration.params);
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
TSAbstractMethodDefinition(node) {
|
|
96
|
+
checkFunction(context, sourceCode, node, node.value.params);
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
export default jsdocParamOrder;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsdoc-tag-lines.d.ts","sourceRoot":"","sources":["../../../src/rules/jsdoc-tag-lines.ts"],"names":[],"mappings":"AAeA;;GAEG;AACH,QAAA,MAAM,aAAa,uCAgJjB,CAAC;eAEY,aAAa"}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
2
|
+
import { getJSDocLines, hasExampleOrFence } from "./jsdoc-utils.js";
|
|
3
|
+
function getBlankPrefix(line) {
|
|
4
|
+
return line.prefix.trimEnd();
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* description とタグ、タググループの間の空行を揃えます。
|
|
8
|
+
*/
|
|
9
|
+
const jsdocTagLines = defineRule({
|
|
10
|
+
meta: {
|
|
11
|
+
docs: {
|
|
12
|
+
description: "Require consistent blank lines between JSDoc tag groups.",
|
|
13
|
+
},
|
|
14
|
+
type: "layout",
|
|
15
|
+
fixable: "whitespace",
|
|
16
|
+
messages: {
|
|
17
|
+
missingBlankLine: "Expected a blank line between JSDoc tag groups.",
|
|
18
|
+
unexpectedBlankLine: "Unexpected blank line in a JSDoc tag group.",
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
create(context) {
|
|
22
|
+
const sourceCode = context.sourceCode;
|
|
23
|
+
return {
|
|
24
|
+
"Program:exit"() {
|
|
25
|
+
for (const comment of sourceCode.getAllComments()) {
|
|
26
|
+
if (comment.type !== "Block") {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
const lines = getJSDocLines(comment, sourceCode);
|
|
30
|
+
if (lines === null || lines.length < 3 || hasExampleOrFence(lines)) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
const opening = lines[0];
|
|
34
|
+
const closing = lines.at(-1);
|
|
35
|
+
if (opening === undefined ||
|
|
36
|
+
closing === undefined ||
|
|
37
|
+
opening.content.trim() !== "" ||
|
|
38
|
+
closing.content.trim() !== "") {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
const contentLines = lines.slice(1, -1);
|
|
42
|
+
const entries = [];
|
|
43
|
+
for (const [index, line] of contentLines.entries()) {
|
|
44
|
+
if (line.content.trim() !== "") {
|
|
45
|
+
entries.push({
|
|
46
|
+
line,
|
|
47
|
+
groupId: -1,
|
|
48
|
+
lineIndex: index,
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (entries.length === 0 || !entries.some((entry) => entry.line.tagName !== null)) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
let firstTagIndex = -1;
|
|
56
|
+
let currentGroup = -1;
|
|
57
|
+
let currentTag = null;
|
|
58
|
+
for (const [index, entry] of entries.entries()) {
|
|
59
|
+
if (entry.line.tagName === null) {
|
|
60
|
+
if (currentTag === null) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
entry.groupId = currentGroup;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (firstTagIndex === -1) {
|
|
67
|
+
firstTagIndex = index;
|
|
68
|
+
}
|
|
69
|
+
if (currentTag === null || entry.line.tagName !== currentTag) {
|
|
70
|
+
currentGroup += 1;
|
|
71
|
+
currentTag = entry.line.tagName;
|
|
72
|
+
}
|
|
73
|
+
entry.groupId = currentGroup;
|
|
74
|
+
}
|
|
75
|
+
if (firstTagIndex === -1) {
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const descriptionExists = firstTagIndex > 0;
|
|
79
|
+
const result = [opening.raw];
|
|
80
|
+
let previousEntry = null;
|
|
81
|
+
let missingBlankLine = false;
|
|
82
|
+
for (const entry of entries) {
|
|
83
|
+
let desiredBlankLines;
|
|
84
|
+
let originalBlankLines;
|
|
85
|
+
if (previousEntry === null) {
|
|
86
|
+
originalBlankLines = entry.lineIndex;
|
|
87
|
+
desiredBlankLines = 0;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
originalBlankLines = entry.lineIndex - previousEntry.lineIndex - 1;
|
|
91
|
+
if (previousEntry.groupId === -1 && entry.groupId === -1) {
|
|
92
|
+
desiredBlankLines = originalBlankLines;
|
|
93
|
+
}
|
|
94
|
+
else if (previousEntry.groupId === -1) {
|
|
95
|
+
desiredBlankLines = descriptionExists ? 1 : 0;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
desiredBlankLines = previousEntry.groupId === entry.groupId ? 0 : 1;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (desiredBlankLines > originalBlankLines) {
|
|
102
|
+
missingBlankLine = true;
|
|
103
|
+
}
|
|
104
|
+
const blankPrefix = getBlankPrefix(entry.line);
|
|
105
|
+
for (let count = 0; count < desiredBlankLines; count += 1) {
|
|
106
|
+
result.push(blankPrefix);
|
|
107
|
+
}
|
|
108
|
+
result.push(entry.line.raw);
|
|
109
|
+
previousEntry = entry;
|
|
110
|
+
}
|
|
111
|
+
result.push(closing.raw);
|
|
112
|
+
const replacement = result.join("\n");
|
|
113
|
+
if (replacement === sourceCode.getText(comment)) {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
context.report({
|
|
117
|
+
fix(fixer) {
|
|
118
|
+
return fixer.replaceText(comment, replacement);
|
|
119
|
+
},
|
|
120
|
+
loc: comment.loc,
|
|
121
|
+
messageId: missingBlankLine ? "missingBlankLine" : "unexpectedBlankLine",
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
export default jsdocTagLines;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsdoc-tag-order.d.ts","sourceRoot":"","sources":["../../../src/rules/jsdoc-tag-order.ts"],"names":[],"mappings":"AAmBA;;GAEG;AACH,QAAA,MAAM,aAAa,uCAkDjB,CAAC;eAEY,aAAa"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
2
|
+
import { getJSDocLines, hasExampleOrFence } from "./jsdoc-utils.js";
|
|
3
|
+
const TAG_ORDER = {
|
|
4
|
+
see: 70,
|
|
5
|
+
beta: 65,
|
|
6
|
+
note: 50,
|
|
7
|
+
param: 10,
|
|
8
|
+
throws: 40,
|
|
9
|
+
default: 20,
|
|
10
|
+
returns: 30,
|
|
11
|
+
internal: 65,
|
|
12
|
+
template: 5,
|
|
13
|
+
typeParam: 5,
|
|
14
|
+
deprecated: 60,
|
|
15
|
+
};
|
|
16
|
+
const UNKNOWN_TAG_ORDER = 100;
|
|
17
|
+
/**
|
|
18
|
+
* JSDoc のタグの順序を統一します。
|
|
19
|
+
*/
|
|
20
|
+
const jsdocTagOrder = defineRule({
|
|
21
|
+
meta: {
|
|
22
|
+
docs: {
|
|
23
|
+
description: "Enforce a consistent order of JSDoc tags.",
|
|
24
|
+
},
|
|
25
|
+
type: "suggestion",
|
|
26
|
+
messages: {
|
|
27
|
+
order: "JSDoc tags must be ordered as follows: @param, @default, @returns, @throws, @note, @deprecated, @see.",
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
create(context) {
|
|
31
|
+
const sourceCode = context.sourceCode;
|
|
32
|
+
return {
|
|
33
|
+
"Program:exit"() {
|
|
34
|
+
for (const comment of sourceCode.getAllComments()) {
|
|
35
|
+
if (comment.type !== "Block") {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
const lines = getJSDocLines(comment, sourceCode);
|
|
39
|
+
if (lines === null || hasExampleOrFence(lines)) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
let previousOrder = null;
|
|
43
|
+
for (const line of lines) {
|
|
44
|
+
if (line.tagName === null) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
const order = TAG_ORDER[line.tagName] ?? UNKNOWN_TAG_ORDER;
|
|
48
|
+
if (previousOrder !== null && order < previousOrder) {
|
|
49
|
+
context.report({
|
|
50
|
+
loc: comment.loc,
|
|
51
|
+
messageId: "order",
|
|
52
|
+
});
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
previousOrder = order;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
export default jsdocTagOrder;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ESTree, Comment, SourceCode } from "vite-plus/lint/plugins";
|
|
2
|
+
interface JSDocLine {
|
|
3
|
+
content: string;
|
|
4
|
+
prefix: string;
|
|
5
|
+
raw: string;
|
|
6
|
+
tagName: string | null;
|
|
7
|
+
}
|
|
8
|
+
declare function getTagName(text: string): string | null;
|
|
9
|
+
/**
|
|
10
|
+
* `@param` 行からパラメーター名を取得します。
|
|
11
|
+
*/
|
|
12
|
+
declare function getParamName(content: string): string | null;
|
|
13
|
+
/**
|
|
14
|
+
* JSDoc コメントの各行を返します。
|
|
15
|
+
*
|
|
16
|
+
* JSDoc でない場合や、`*` の付かない行がある場合は null を返します。
|
|
17
|
+
*/
|
|
18
|
+
declare function getJSDocLines(comment: Comment, sourceCode: SourceCode): JSDocLine[] | null;
|
|
19
|
+
declare function hasExampleOrFence(lines: readonly JSDocLine[]): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* ノードに付いている JSDoc コメントを返します。
|
|
22
|
+
*/
|
|
23
|
+
declare function getNodeJSDoc(node: ESTree.Node, sourceCode: SourceCode): Comment | null;
|
|
24
|
+
export type { JSDocLine };
|
|
25
|
+
export { getTagName, getNodeJSDoc, getParamName, getJSDocLines, hasExampleOrFence };
|
|
26
|
+
//# sourceMappingURL=jsdoc-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsdoc-utils.d.ts","sourceRoot":"","sources":["../../../src/rules/jsdoc-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAI1E,UAAU,SAAS;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CACxB;AAED,iBAAS,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAI/C;AAyBD;;GAEG;AACH,iBAAS,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAqBpD;AAED;;;;GAIG;AACH,iBAAS,aAAa,CAAC,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,GAAG,SAAS,EAAE,GAAG,IAAI,CA+DnF;AAED,iBAAS,iBAAiB,CAAC,KAAK,EAAE,SAAS,SAAS,EAAE,GAAG,OAAO,CAE/D;AAcD;;GAEG;AACH,iBAAS,YAAY,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,UAAU,EAAE,UAAU,GAAG,OAAO,GAAG,IAAI,CAmB/E;AAED,YAAY,EAAE,SAAS,EAAE,CAAC;AAE1B,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,YAAY,EAAE,aAAa,EAAE,iBAAiB,EAAE,CAAC"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { isFence } from "./comment-utils.js";
|
|
2
|
+
function getTagName(text) {
|
|
3
|
+
const match = /^@(?<tag>[A-Za-z][\w-]*)/u.exec(text.trimStart());
|
|
4
|
+
return match?.groups?.["tag"] ?? null;
|
|
5
|
+
}
|
|
6
|
+
function stripType(value) {
|
|
7
|
+
if (!value.startsWith("{")) {
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
let depth = 0;
|
|
11
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
12
|
+
const character = value[index];
|
|
13
|
+
if (character === "{") {
|
|
14
|
+
depth += 1;
|
|
15
|
+
}
|
|
16
|
+
else if (character === "}") {
|
|
17
|
+
depth -= 1;
|
|
18
|
+
if (depth === 0) {
|
|
19
|
+
return value.slice(index + 1).trimStart();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* `@param` 行からパラメーター名を取得します。
|
|
27
|
+
*/
|
|
28
|
+
function getParamName(content) {
|
|
29
|
+
let value = content.trimStart();
|
|
30
|
+
if (getTagName(value) !== "param") {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
value = stripType(value.slice("@param".length).trimStart());
|
|
34
|
+
const match = /^(?<name>\S+)/u.exec(value);
|
|
35
|
+
const name = match?.groups?.["name"] ?? null;
|
|
36
|
+
if (name === null) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
return name
|
|
40
|
+
.replace(/^\[/u, "")
|
|
41
|
+
.replace(/\]$/u, "")
|
|
42
|
+
.replace(/^\.\.\./u, "")
|
|
43
|
+
.replace(/=.*$/u, "");
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* JSDoc コメントの各行を返します。
|
|
47
|
+
*
|
|
48
|
+
* JSDoc でない場合や、`*` の付かない行がある場合は null を返します。
|
|
49
|
+
*/
|
|
50
|
+
function getJSDocLines(comment, sourceCode) {
|
|
51
|
+
const raw = sourceCode.getText(comment);
|
|
52
|
+
if (!raw.startsWith("/**") || comment.type !== "Block") {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
const rawLines = raw.split("\n");
|
|
56
|
+
if (rawLines.length === 1) {
|
|
57
|
+
const contentStart = 3;
|
|
58
|
+
const contentEnd = raw.lastIndexOf("*/");
|
|
59
|
+
const content = raw.slice(contentStart, contentEnd);
|
|
60
|
+
return [
|
|
61
|
+
{
|
|
62
|
+
raw,
|
|
63
|
+
prefix: raw.slice(0, contentStart),
|
|
64
|
+
content,
|
|
65
|
+
tagName: getTagName(content),
|
|
66
|
+
},
|
|
67
|
+
];
|
|
68
|
+
}
|
|
69
|
+
const lines = [];
|
|
70
|
+
for (const [index, line] of rawLines.entries()) {
|
|
71
|
+
if (index === 0) {
|
|
72
|
+
const contentStart = 3;
|
|
73
|
+
const closeIndex = line.lastIndexOf("*/");
|
|
74
|
+
const contentEnd = closeIndex === -1 ? line.length : Math.max(contentStart, closeIndex);
|
|
75
|
+
const content = line.slice(contentStart, contentEnd);
|
|
76
|
+
lines.push({
|
|
77
|
+
raw: line,
|
|
78
|
+
prefix: line.slice(0, contentStart),
|
|
79
|
+
content,
|
|
80
|
+
tagName: getTagName(content),
|
|
81
|
+
});
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const match = /^(?<prefix>\s*\*[ \t]?).*$/u.exec(line);
|
|
85
|
+
if (match === null) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
const prefix = match.groups?.["prefix"] ?? "";
|
|
89
|
+
const contentStart = prefix.length;
|
|
90
|
+
const closeIndex = line.lastIndexOf("*/");
|
|
91
|
+
const contentEnd = closeIndex === -1 ? line.length : Math.max(contentStart, closeIndex);
|
|
92
|
+
const content = line.slice(contentStart, contentEnd);
|
|
93
|
+
lines.push({
|
|
94
|
+
raw: line,
|
|
95
|
+
prefix,
|
|
96
|
+
content,
|
|
97
|
+
tagName: getTagName(content),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return lines;
|
|
101
|
+
}
|
|
102
|
+
function hasExampleOrFence(lines) {
|
|
103
|
+
return lines.some((line) => line.tagName === "example" || isFence(line.content));
|
|
104
|
+
}
|
|
105
|
+
function getNodeStart(node) {
|
|
106
|
+
if ("decorators" in node) {
|
|
107
|
+
const decorator = node.decorators?.[0];
|
|
108
|
+
if (decorator !== undefined) {
|
|
109
|
+
return decorator.range[0];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return node.range[0];
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* ノードに付いている JSDoc コメントを返します。
|
|
116
|
+
*/
|
|
117
|
+
function getNodeJSDoc(node, sourceCode) {
|
|
118
|
+
const start = getNodeStart(node);
|
|
119
|
+
const comment = sourceCode.getAllComments().findLast((candidate) => candidate.range[1] <= start);
|
|
120
|
+
if (comment === null || comment === undefined || comment.type !== "Block") {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
if (!sourceCode.getText(comment).startsWith("/**")) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
if (sourceCode.text.slice(comment.range[1], start).trim() !== "") {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
const startLine = sourceCode.getLocFromIndex(start).line;
|
|
130
|
+
return comment.loc.end.line === startLine - 1 ? comment : null;
|
|
131
|
+
}
|
|
132
|
+
export { getTagName, getNodeJSDoc, getParamName, getJSDocLines, hasExampleOrFence };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"member-ordering.d.ts","sourceRoot":"","sources":["../../../src/rules/member-ordering.ts"],"names":[],"mappings":"AAiDA;;GAEG;AACH,QAAA,MAAM,cAAc,uCAsClB,CAAC;eAEY,cAAc"}
|