-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlint.ts
139 lines (116 loc) · 3.75 KB
/
lint.ts
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
import { parse } from "npm:comment-parser@1.4.1";
import type { Block } from "npm:comment-parser@1.4.1";
function findIndexFromLineSplitted(
source: string[],
line: number,
ending?: boolean,
rawSource?: string,
): number | undefined {
if (line >= source.length) { return undefined }
const srcLine = source[line];
const src = rawSource ?? source.join("\n");
const index = src.indexOf(srcLine);
if (ending) {
return index + srcLine.length;
}
return index;
}
function removeBraces(str: string): string {
return str.slice(1, -1);
}
function generateReportsForTag(context: Deno.lint.RuleContext, ast: Block[], tagName: string) {
const src = context.sourceCode;
const sources = ast.filter((v) => {
return v.tags.find((t) => t.tag === tagName);
});
const sourceLines = src.text.split("\n");
const tagMap = sources.map((block) => {
// nearest declaration line
const tnTag = block.tags.find((t) => t.tag === tagName)!;
const verboseTnTag = block.source.find((s) =>
s.tokens.tag === tagName
);
const tnTagSrcMeta = tnTag.source[0]
// find source code location
const lastLineNumber = block.source[block.source.length - 1].number;
const nearestLine = lastLineNumber + 1;
const charIndex = findIndexFromLineSplitted(sourceLines, nearestLine, false, src.text);
return {
type: tagName,
name: tnTag.description.startsWith('-') ? tnTag.name : undefined,
description: tnTag.description.startsWith('-') ? tnTag.description.slice(2) : tnTag.name + " " + tnTag.description,
source: tnTag.source[0].tokens.type !== "" && tnTag.source[0].tokens.type.length >= 2 ? removeBraces(tnTag.source[0].tokens.type) : undefined,
tagLine: verboseTnTag?.number,
elementLine: nearestLine,
elementStartIndex: charIndex,
tag: {
// the start info for the tag
start: {
// the start line for the tag
line: tnTagSrcMeta.number,
fullIndex: findIndexFromLineSplitted(sourceLines, tnTagSrcMeta.number, false, src.text)!
},
end: {
// the end line for the tag
line: tnTagSrcMeta.number,
fullIndex: findIndexFromLineSplitted(sourceLines, tnTagSrcMeta.number, true, src.text)!
}
},
meta: {
tag: tnTag,
},
verbose: {
tag: verboseTnTag,
},
};
});
// return error
tagMap.forEach((tagBlock) => {
// find node in source
if (tagBlock.elementStartIndex) {
const node = src.ast.body.find((st) =>
st.range[0] === tagBlock.elementStartIndex
);
context.report({
message: tagBlock.name ? `${tagBlock.name}: ${tagBlock.description}` : tagBlock.description,
hint: tagBlock.source ? `See ${tagBlock.source} for more info` : undefined,
node,
});
} else {
context.report({
message: tagBlock.name ? `${tagBlock.name}: ${tagBlock.description}` : tagBlock.description,
hint: tagBlock.source ? `See ${tagBlock.source} for more info` : undefined,
range: [tagBlock.tag.start.fullIndex, tagBlock.tag.end.fullIndex]
});
}
});
}
/**
* The Doclint plugin
*/
const plugin: Deno.lint.Plugin = {
name: "doclint",
rules: {
"todo": {
create(context) {
// parse docs from source
const src = context.sourceCode;
// parsing...
const jsdocAst = parse(src.text);
generateReportsForTag(context, jsdocAst, 'todo');
return {};
},
},
"fixme": {
create(context) {
// parse docs from source
const src = context.sourceCode;
// parsing...
const jsdocAst = parse(src.text);
generateReportsForTag(context, jsdocAst, 'fixme');
return {};
},
}
},
};
export default plugin;