/
checkExamples.js
350 lines (303 loc) · 9.75 KB
/
checkExamples.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Todo: When peerDeps bump to ESLint 7, see about replacing `CLIEngine`
// with non-deprecated `ESLint` class:
// https://github.com/eslint/eslint/blob/master/docs/user-guide/migrating-to-7.0.0.md#-the-cliengine-class-has-been-deprecated
import {
CLIEngine,
} from 'eslint';
import iterateJsdoc from '../iterateJsdoc';
const zeroBasedLineIndexAdjust = -1;
const likelyNestedJSDocIndentSpace = 1;
const preTagSpaceLength = 1;
// If a space is present, we should ignore it
const firstLinePrefixLength = preTagSpaceLength;
const hasCaptionRegex = /^\s*<caption>(.*?)<\/caption>/u;
const escapeStringRegexp = (str) => {
return str.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&');
};
const countChars = (str, ch) => {
return (str.match(new RegExp(escapeStringRegexp(ch), 'gu')) || []).length;
};
const getRegexFromString = (regexString) => {
const match = regexString.match(/^\/(.*)\/([gimyus]*)$/u);
let flags = 'u';
let regex = regexString;
if (match) {
[, regex, flags] = match;
if (!flags) {
flags = 'u';
}
const uniqueFlags = [...new Set(flags)];
flags = uniqueFlags.join('');
}
return new RegExp(regex, flags);
};
export default iterateJsdoc(({
report,
utils,
context,
globalState,
}) => {
const tagName = utils.getPreferredTagName({tagName: 'example'});
if (!utils.hasTag(tagName)) {
return;
}
if (!globalState.has('checkExamples-matchingFileName')) {
globalState.set('checkExamples-matchingFileName', new Map());
}
const matchingFileNameMap = globalState.get('checkExamples-matchingFileName');
const options = context.options[0] || {};
let {
exampleCodeRegex = null,
rejectExampleCodeRegex = null,
} = options;
const {
noDefaultExampleRules = false,
checkEslintrc = true,
matchingFileName: filename = null,
paddedIndent = 0,
baseConfig = {},
configFile,
allowInlineConfig = true,
reportUnusedDisableDirectives = true,
captionRequired = false,
} = options;
let defaultFileName;
if (!filename) {
const jsFileName = context.getFilename();
if (typeof jsFileName === 'string' && jsFileName.includes('.')) {
defaultFileName = jsFileName.replace(/\..*?$/, '.md');
} else {
defaultFileName = 'dummy.md';
}
}
// Make this configurable?
const rulePaths = [];
const rules = noDefaultExampleRules ? undefined : {
// "always" newline rule at end unlikely in sample code
'eol-last': 0,
// Wouldn't generally expect example paths to resolve relative to JS file
'import/no-unresolved': 0,
// Snippets likely too short to always include import/export info
'import/unambiguous': 0,
// The end of a multiline comment would end the comment the example is in.
'jsdoc/require-jsdoc': 0,
// Unlikely to have inadvertent debugging within examples
'no-console': 0,
// Often wish to start `@example` code after newline; also may use
// empty lines for spacing
'no-multiple-empty-lines': 0,
// Many variables in examples will be `undefined`
'no-undef': 0,
// Common to define variables for clarity without always using them
'no-unused-vars': 0,
// See import/no-unresolved
'node/no-missing-import': 0,
'node/no-missing-require': 0,
// Can generally look nicer to pad a little even if code imposes more stringency
'padded-blocks': 0,
};
if (exampleCodeRegex) {
exampleCodeRegex = getRegexFromString(exampleCodeRegex);
}
if (rejectExampleCodeRegex) {
rejectExampleCodeRegex = getRegexFromString(rejectExampleCodeRegex);
}
utils.forEachPreferredTag('example', (tag, targetTagName) => {
let source = tag.description;
const match = source.match(hasCaptionRegex);
if (captionRequired && (!match || !match[1].trim())) {
report('Caption is expected for examples.', null, tag);
}
// If we allow newlines in hasCaptionRegex, we should add to line count
source = source.replace(hasCaptionRegex, '');
if (exampleCodeRegex && !exampleCodeRegex.test(source) ||
rejectExampleCodeRegex && rejectExampleCodeRegex.test(source)
) {
return;
}
const sources = [];
if (exampleCodeRegex) {
let nonJSPrefacingCols = 0;
let nonJSPrefacingLines = 0;
let startingIndex = 0;
let lastStringCount = 0;
let exampleCode;
exampleCodeRegex.lastIndex = 0;
while ((exampleCode = exampleCodeRegex.exec(source)) !== null) {
const {index, 0: n0, 1: n1} = exampleCode;
// Count anything preceding user regex match (can affect line numbering)
const preMatch = source.slice(startingIndex, index);
const preMatchLines = countChars(preMatch, '\n');
const colDelta = preMatchLines ?
preMatch.slice(preMatch.lastIndexOf('\n') + 1).length :
preMatch.length;
let nonJSPreface;
let nonJSPrefaceLineCount;
if (n1) {
const idx = n0.indexOf(n1);
nonJSPreface = n0.slice(0, idx);
nonJSPrefaceLineCount = countChars(nonJSPreface, '\n');
} else {
nonJSPreface = '';
nonJSPrefaceLineCount = 0;
}
nonJSPrefacingLines += lastStringCount + preMatchLines + nonJSPrefaceLineCount;
// Ignore `preMatch` delta if newlines here
if (nonJSPrefaceLineCount) {
const charsInLastLine = nonJSPreface.slice(nonJSPreface.lastIndexOf('\n') + 1).length;
nonJSPrefacingCols += charsInLastLine;
} else {
nonJSPrefacingCols += colDelta + nonJSPreface.length;
}
const string = n1 || n0;
sources.push({
nonJSPrefacingCols,
nonJSPrefacingLines,
string,
});
startingIndex = exampleCodeRegex.lastIndex;
lastStringCount = countChars(string, '\n');
if (!exampleCodeRegex.global) {
break;
}
}
} else {
sources.push({
nonJSPrefacingCols: 0,
nonJSPrefacingLines: 0,
string: source,
});
}
// Todo: Make fixable
// Todo: Fix whitespace indent
const checkRules = function ({
nonJSPrefacingCols,
nonJSPrefacingLines,
string,
}) {
const cliConfig = {
allowInlineConfig,
baseConfig,
configFile,
reportUnusedDisableDirectives,
rulePaths,
rules,
useEslintrc: checkEslintrc,
};
const cliConfigStr = JSON.stringify(cliConfig);
const src = paddedIndent ?
string.replace(new RegExp(`(^|\n) {${paddedIndent}}(?!$)`, 'gu'), '\n') :
string;
// Programmatic ESLint API: https://eslint.org/docs/developer-guide/nodejs-api
const fileNameMapKey = filename ?
'a' + cliConfigStr + filename :
'b' + cliConfigStr + defaultFileName;
const file = filename || defaultFileName;
let cliFile;
if (matchingFileNameMap.has(fileNameMapKey)) {
cliFile = matchingFileNameMap.get(fileNameMapKey);
} else {
const cli = new CLIEngine(cliConfig);
let config;
if (filename || checkEslintrc) {
config = cli.getConfigForFile(file);
}
// We need a new instance to ensure that the rules that may only
// be available to `file` (if it has its own `.eslintrc`),
// will be defined.
cliFile = new CLIEngine({
allowInlineConfig,
baseConfig: {
...baseConfig,
...config,
},
configFile,
reportUnusedDisableDirectives,
rulePaths,
rules,
useEslintrc: false,
});
matchingFileNameMap.set(fileNameMapKey, cliFile);
}
const {results: [{messages}]} =
cliFile.executeOnText(src);
// NOTE: `tag.line` can be 0 if of form `/** @tag ... */`
const codeStartLine = tag.line + nonJSPrefacingLines;
const codeStartCol = likelyNestedJSDocIndentSpace;
messages.forEach(({message, line, column, severity, ruleId}) => {
const startLine = codeStartLine + line + zeroBasedLineIndexAdjust;
const startCol = codeStartCol + (
// This might not work for line 0, but line 0 is unlikely for examples
line <= 1 ? nonJSPrefacingCols + firstLinePrefixLength : preTagSpaceLength
) + column;
report(
'@' + targetTagName + ' ' + (severity === 2 ? 'error' : 'warning') +
(ruleId ? ' (' + ruleId + ')' : '') + ': ' +
message,
null,
{
column: startCol,
line: startLine,
},
);
});
};
sources.forEach(checkRules);
});
}, {
iterateAllJsdocs: true,
meta: {
docs: {
description: 'Ensures that (JavaScript) examples within JSDoc adhere to ESLint rules.',
},
schema: [
{
additionalProperties: false,
properties: {
allowInlineConfig: {
default: true,
type: 'boolean',
},
baseConfig: {
type: 'object',
},
captionRequired: {
default: false,
type: 'boolean',
},
checkEslintrc: {
default: true,
type: 'boolean',
},
configFile: {
type: 'string',
},
exampleCodeRegex: {
type: 'string',
},
matchingFileName: {
type: 'string',
},
noDefaultExampleRules: {
default: false,
type: 'boolean',
},
paddedIndent: {
default: 0,
type: 'integer',
},
rejectExampleCodeRegex: {
type: 'string',
},
reportUnusedDisableDirectives: {
default: true,
type: 'boolean',
},
},
type: 'object',
},
],
type: 'suggestion',
},
noTrim: true,
});