-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
matchName.js
147 lines (136 loc) · 3.38 KB
/
matchName.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
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
import iterateJsdoc from '../iterateJsdoc';
// eslint-disable-next-line complexity
export default iterateJsdoc(({
context,
jsdoc,
report,
info: {
lastIndex,
},
utils,
}) => {
const {
match,
} = context.options[0] || {};
if (!match) {
report('Rule `no-restricted-syntax` is missing a `match` option.');
return;
}
const {
allowName,
disallowName,
replacement,
tags = [
'*',
],
} = match[lastIndex];
const allowNameRegex = allowName && utils.getRegexFromString(allowName);
const disallowNameRegex = disallowName && utils.getRegexFromString(disallowName);
let applicableTags = jsdoc.tags;
if (!tags.includes('*')) {
applicableTags = utils.getPresentTags(tags);
}
let reported = false;
for (const tag of applicableTags) {
const allowed = !allowNameRegex || allowNameRegex.test(tag.name);
const disallowed = disallowNameRegex && disallowNameRegex.test(tag.name);
const hasRegex = allowNameRegex || disallowNameRegex;
if (hasRegex && allowed && !disallowed) {
continue;
}
if (!hasRegex && reported) {
continue;
}
const fixer = () => {
for (const src of tag.source) {
if (src.tokens.name) {
src.tokens.name = src.tokens.name.replace(
disallowNameRegex, replacement,
);
break;
}
}
};
let {
message,
} = match[lastIndex];
if (!message) {
if (hasRegex) {
message = disallowed ?
`Only allowing names not matching \`${disallowNameRegex}\` but found "${tag.name}".` :
`Only allowing names matching \`${allowNameRegex}\` but found "${tag.name}".`;
} else {
message = `Prohibited context for "${tag.name}".`;
}
}
utils.reportJSDoc(
message,
hasRegex ? tag : null,
// We could match up
disallowNameRegex && replacement !== undefined ?
fixer :
null,
false,
{
// Could also supply `context`, `comment`, `tags`
allowName,
disallowName,
name: tag.name,
},
);
if (!hasRegex) {
reported = true;
}
}
}, {
matchContext: true,
meta: {
docs: {
description: 'Reports the name portion of a JSDoc tag if matching or not matching a given regular expression.',
url: 'https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-match-name',
},
fixable: 'code',
schema: [
{
additionalProperies: false,
properties: {
match: {
additionalProperies: false,
items: {
properties: {
allowName: {
type: 'string',
},
comment: {
type: 'string',
},
context: {
type: 'string',
},
disallowName: {
type: 'string',
},
message: {
type: 'string',
},
tags: {
items: {
type: 'string',
},
type: 'array',
},
},
type: 'object',
},
type: 'array',
},
},
required: [
'match',
],
type: 'object',
},
],
type: 'suggestion',
},
});