-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
validTypes.js
83 lines (72 loc) · 2.1 KB
/
validTypes.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
import {parse} from 'jsdoctypeparser';
import iterateJsdoc from '../iterateJsdoc';
const asExpression = /as\s+/;
export default iterateJsdoc(({
jsdoc,
report,
utils
}) => {
if (!jsdoc.tags) {
return;
}
jsdoc.tags.forEach((tag) => {
const validTypeParsing = function (type, tagName) {
try {
parse(type);
} catch (err) {
let error = err;
if (tagName) {
if (['memberof', 'memberof!'].includes(tagName)) {
const endChar = type.slice(-1);
if (['#', '.', '~'].includes(endChar)) {
try {
parse(type.slice(0, -1));
error = {};
} catch (memberofError) {
// Use the original error for including the whole type
}
}
} else if (tagName === 'borrows') {
const startChar = type.charAt();
if (['#', '.', '~'].includes(startChar)) {
try {
parse(type.slice(1));
error = {};
} catch (memberofError) {
// Use the original error for including the whole type
}
}
}
}
if (error.name === 'SyntaxError') {
report('Syntax error in type: ' + type, null, tag);
return false;
}
}
return true;
};
if (tag.tag === 'borrows') {
const thisNamepath = tag.description.replace(asExpression, '');
if (!asExpression.test(tag.description) || !thisNamepath) {
report('@borrows must have an "as" expression. Found "' + tag.description + '"', null, tag);
return;
}
if (validTypeParsing(thisNamepath, 'borrows')) {
const thatNamepath = tag.name;
validTypeParsing(thatNamepath);
}
} else if (utils.isNamepathTag(tag.tag)) {
if (utils.passesEmptyNamepathCheck(tag)) {
return;
}
validTypeParsing(tag.name, tag.tag);
} else if (tag.type && utils.isTagWithType(tag.tag)) {
validTypeParsing(tag.type);
}
});
}, {
iterateAllJsdocs: true,
meta: {
type: 'suggestion'
}
});