-
-
Notifications
You must be signed in to change notification settings - Fork 153
/
noMissingSyntax.js
129 lines (118 loc) · 3.1 KB
/
noMissingSyntax.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
import iterateJsdoc from '../iterateJsdoc';
const setDefaults = (state) => {
if (!state.selectorMap) {
state.selectorMap = {};
}
};
const incrementSelector = (state, selector, comment) => {
if (!state.selectorMap[selector]) {
state.selectorMap[selector] = {};
}
if (!state.selectorMap[selector][comment]) {
state.selectorMap[selector][comment] = 0;
}
state.selectorMap[selector][comment]++;
};
export default iterateJsdoc(({
info: {selector, comment},
state,
}) => {
setDefaults(state);
incrementSelector(state, selector, comment);
}, {
contextSelected: true,
exit ({
context, state,
}) {
if (!context.options.length) {
context.report({
loc: {
start: {
column: 1,
line: 1,
},
},
message: 'Rule `no-restricted-syntax` is missing a `context` option.',
});
return;
}
setDefaults(state);
const {
contexts,
} = context.options[0];
// Report when MISSING
contexts.some((cntxt) => {
const contextStr = typeof cntxt === 'object' ? cntxt.context : cntxt;
const comment = cntxt?.comment ?? '';
const contextKey = contextStr === 'any' ? undefined : contextStr;
if (
(!state.selectorMap[contextKey] ||
!state.selectorMap[contextKey][comment] ||
state.selectorMap[contextKey][comment] < (cntxt?.minimum ?? 1)) &&
(contextStr !== 'any' || Object.values(state.selectorMap).every((cmmnt) => {
return !cmmnt[comment] || cmmnt[comment] < (cntxt?.minimum ?? 1);
}))
) {
const message = cntxt?.message ?? 'Syntax is required: {{context}}' +
(comment ? ' with {{comment}}' : '');
context.report({
data: {
comment,
context: contextStr,
},
loc: {
end: {line: 1},
start: {line: 1},
},
message,
});
return true;
}
return false;
});
},
meta: {
docs: {
description: 'Reports when certain comment structures are always expected.',
url: 'https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-no-missing-syntax',
},
fixable: 'code',
schema: [
{
additionalProperties: false,
properties: {
contexts: {
items: {
anyOf: [
{
type: 'string',
},
{
additionalProperties: false,
properties: {
comment: {
type: 'string',
},
context: {
type: 'string',
},
message: {
type: 'string',
},
minimum: {
type: 'integer',
},
},
type: 'object',
},
],
},
type: 'array',
},
},
type: 'object',
},
],
type: 'suggestion',
},
});