forked from fardad-dev/prettier-plugin-jsdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringify.ts
More file actions
executable file
·182 lines (158 loc) · 5.04 KB
/
Copy pathstringify.ts
File metadata and controls
executable file
·182 lines (158 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { Spec } from "comment-parser";
import {
formatDescription,
descriptionEndLine,
} from "./descriptionFormatter.js";
import {
DESCRIPTION,
EXAMPLE,
PRIVATE_REMARKS,
REMARKS,
SPACE_TAG_DATA,
} from "./tags.js";
import {
TAGS_ORDER,
TAGS_PEV_FORMATE_DESCRIPTION,
TAGS_VERTICALLY_ALIGN_ABLE,
} from "./roles.js";
import { AllOptions } from "./types.js";
import { formatCode, isDefaultTag } from "./utils.js";
const stringify = async (
{ name, description, type, tag }: Spec,
tagIndex: number,
finalTagsArray: Spec[],
options: AllOptions,
maxTagTitleLength: number,
maxTagTypeNameLength: number,
maxTagNameLength: number,
): Promise<string> => {
let tagString = "\n";
if (tag === SPACE_TAG_DATA.tag) {
return tagString;
}
const {
printWidth,
jsdocSpaces,
jsdocVerticalAlignment,
jsdocDescriptionTag,
tsdoc,
useTabs,
tabWidth,
jsdocSeparateTagGroups,
} = options;
const gap = " ".repeat(jsdocSpaces);
let tagTitleGapAdj = 0;
let tagTypeGapAdj = 0;
let tagNameGapAdj = 0;
let descGapAdj = 0;
if (jsdocVerticalAlignment && TAGS_VERTICALLY_ALIGN_ABLE.includes(tag)) {
if (tag) tagTitleGapAdj += maxTagTitleLength - tag.length;
else if (maxTagTitleLength) descGapAdj += maxTagTitleLength + gap.length;
if (type) tagTypeGapAdj += maxTagTypeNameLength - type.length;
else if (maxTagTypeNameLength)
descGapAdj += maxTagTypeNameLength + gap.length;
if (name) tagNameGapAdj += maxTagNameLength - name.length;
else if (maxTagNameLength) descGapAdj = maxTagNameLength + gap.length;
}
const useTagTitle = tag !== DESCRIPTION || jsdocDescriptionTag;
if (useTagTitle) {
tagString += `@${tag}${" ".repeat(tagTitleGapAdj || 0)}`;
}
if (type) {
const getUpdatedType = () => {
if (!isDefaultTag(tag)) {
return `{${type}}`;
}
// The space is to improve readability in non-monospace fonts
if (type === "[]") return "[ ]";
if (type === "{}") return "{ }";
const isAnObject = (value: string): boolean =>
/^{.*[A-z0-9_]+ ?:.*}$/.test(value);
const fixObjectCommas = (objWithBrokenCommas: string): string =>
objWithBrokenCommas.replace(/; ([A-z0-9_])/g, ", $1");
if (isAnObject(type)) {
return fixObjectCommas(type);
}
return type;
};
const updatedType = getUpdatedType();
tagString += gap + updatedType + " ".repeat(tagTypeGapAdj);
}
if (name) tagString += `${gap}${name}${" ".repeat(tagNameGapAdj)}`;
// Try to use prettier on @example tag description
if (tag === EXAMPLE && !tsdoc) {
const exampleCaption = description.match(/<caption>([\s\S]*?)<\/caption>/i);
if (exampleCaption) {
description = description.replace(exampleCaption[0], "");
tagString = `${tagString} ${exampleCaption[0]}`;
}
const beginningSpace = useTabs ? "\t" : " ".repeat(tabWidth);
const formattedExample = await formatCode(
description,
beginningSpace,
options,
);
tagString += formattedExample
.replace(
new RegExp(
`^\\n${beginningSpace
.replace(/[\t]/g, "[\\t]")
.replace(/[^S\r\n]/g, "[^S\\r\\n]")}\\n`,
),
"",
)
.trimEnd();
} // Add description (complicated because of text wrap)
else if (description) {
let descriptionString = "";
if (useTagTitle) tagString += gap + " ".repeat(descGapAdj);
if (
TAGS_PEV_FORMATE_DESCRIPTION.includes(tag) ||
TAGS_ORDER[tag as keyof typeof TAGS_ORDER] === undefined
) {
// Avoid wrapping
descriptionString = description;
} else {
const [, firstWord] = /^\s*(\S+)/.exec(description) || ["", ""];
// Wrap tag description
const beginningSpace =
tag === DESCRIPTION ||
([EXAMPLE, REMARKS, PRIVATE_REMARKS].includes(tag) && tsdoc)
? ""
: " "; // google style guide space
if (
(tag !== DESCRIPTION &&
tagString.length + firstWord.length > printWidth) ||
// tsdoc tags
[REMARKS, PRIVATE_REMARKS].includes(tag)
) {
// the tag is already longer than we are allowed to, so let's start at a new line
descriptionString =
`\n${beginningSpace}` +
(await formatDescription(tag, description, options, {
beginningSpace,
}));
} else {
// append the description to the tag
descriptionString = await formatDescription(tag, description, options, {
// 1 is `\n` which added to tagString
tagStringLength: tagString.length - 1,
beginningSpace,
});
}
}
if (jsdocSeparateTagGroups) {
descriptionString = descriptionString.trimEnd();
}
tagString += descriptionString.startsWith("\n")
? descriptionString.replace(/^\n[\s]+\n/g, "\n")
: descriptionString.trimStart();
}
// Add empty line after some tags if there is something below
tagString += descriptionEndLine({
tag,
isEndTag: tagIndex === finalTagsArray.length - 1,
});
return tagString;
};
export { stringify };