-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsdoc-check-indentation.js
95 lines (83 loc) 路 2.91 KB
/
jsdoc-check-indentation.js
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
// Based on https://github.com/gajus/eslint-plugin-jsdoc/blob/main/src/rules/checkIndentation.js
const { default: iterateJsdoc } = require("eslint-plugin-jsdoc/dist/iterateJsdoc");
/**
* @param {string} str
* @param {string[]} excludeTags
* @returns {string}
*/
const maskExcludedContent = (str, excludeTags) => {
const regContent = new RegExp(`([ \\t]+\\*)[ \\t]@(?:${excludeTags.join('|')})(?=[ \\n])([\\w|\\W]*?\\n)(?=[ \\t]*\\*(?:[ \\t]*@\\w+\\s|\\/))`, 'gu');
return str.replace(regContent, (_match, margin, code) => {
return (margin + '\n').repeat(code.match(/\n/gu).length);
});
};
/**
* @param {string} str
* @returns {string}
*/
const maskCodeBlocks = (str) => {
const regContent = /([ \t]+\*)[ \t]```[^\n]*?([\w|\W]*?\n)(?=[ \t]*\*(?:[ \t]*(?:```|@\w+\s)|\/))/gu;
return str.replace(regContent, (_match, margin, code) => {
return (margin + '\n').repeat(code.match(/\n/gu).length);
});
};
const maskLineContinuations = (str) => {
// Match a line ending with a backslash (first capture group) and the \
// line following it (second capture group)
const regContent = /(\*\s.*\\)\n(\s*\*\s+.+)/gu;
return str.replace(regContent, (_match, lineBeforeContinuation, lineAfterContinuation) => {
// Normalize the line after the continuation to remove any extra spaces \
// after the '*'. The goal is to treat the continuation line as a \
// direct extension of the previous line.
const normalizedLine = lineAfterContinuation.replace(/(\*\s)(.+)/, "$1$2");
// Return the combined line (previous line + continuation line)
return lineBeforeContinuation + normalizedLine;
});
}
module.exports = iterateJsdoc(({
sourceCode,
jsdocNode,
report,
context,
}) => {
const options = context.options[0] || {};
const /** @type {{excludeTags: string[]}} */ {
excludeTags = [
'example',
],
} = options;
const reg = /^(?:\/?\**|[ \t]*)\*[ \t]{2}/gmu;
const textWithoutCodeBlocks = maskCodeBlocks(sourceCode.getText(jsdocNode));
const textWithoutExcluded = excludeTags.length ? maskExcludedContent(textWithoutCodeBlocks, excludeTags) : textWithoutCodeBlocks;
const text = maskLineContinuations(textWithoutExcluded);
if (reg.test(text)) {
const lineBreaks = text.slice(0, reg.lastIndex).match(/\n/gu) || [];
report('There must be no indentation.', null, {
line: lineBreaks.length,
});
}
}, {
iterateAllJsdocs: true,
meta: {
docs: {
description: 'Reports invalid padding inside JSDoc blocks.',
url: 'https://github.com/gajus/eslint-plugin-jsdoc/blob/main/docs/rules/check-indentation.md#repos-sticky-header',
},
schema: [
{
additionalProperties: false,
properties: {
excludeTags: {
items: {
pattern: '^\\S+$',
type: 'string',
},
type: 'array',
},
},
type: 'object',
},
],
type: 'layout',
},
});