forked from SVG-Edit/svgedit
-
Notifications
You must be signed in to change notification settings - Fork 9
/
jsdoc-check-overly-generic-types.js
103 lines (100 loc) · 3.12 KB
/
jsdoc-check-overly-generic-types.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
/* eslint-env node */
/**
* @todo Fork find-in-files to get ignore pattern support
*/
import fif from 'find-in-files';
(async () => {
/**
* @typedef {PlainObject} FileResult
* @property {string[]} matches
* @property {Integer} count
* @property {string[]} line
*/
const fileMatchPatterns = ['editor'];
/**
* Keys are file name strings
* @type {Object.<string, FileResult>}
*/
let results = await Promise.all(fileMatchPatterns.map((fileMatchPattern) => {
return fif.find(
{
// We grab to the end of the line as the `line` result for `find-in-files`
// only grabs from the beginning of the file to the end of the match.
term: `(@[^{\\n]*{[^}\\n]*(\\bobject|\\barray\\b|[^.]function|\\bnumber|\\*)[^}\\n]*}|@.*{} ).*`,
flags: 'gi'
},
fileMatchPattern,
'([^n]|[^i]n|[^m]in|[^.]min).js$'
);
}));
results = Object.assign(...results);
let total = 0;
let output = '';
Object.entries(results).forEach(([file, res]) => {
reduceFalseMatches(file, res);
if (!res.line.length) {
return;
}
output += `\nFound ${res.count} potentially overly generic JSDoc expression${res.count === 1 ? '' : 's'} in file ${file}:\n`;
res.line.forEach((line) => {
output += line + '\n';
});
total += res.line.length;
/*
res.matches.forEach((match) => {
console.log(match);
});
*/
});
console.log(`${output}\nTotal failures found: ${total}.\n`); // eslint-disable-line no-console
/**
* @external FindInFilesResult
* @type {PlainObject}
* @property {string[]} matches The matched strings
* @property {Integer} count The number of matches
* @property {string[]} line The lines that were matched. The docs mistakenly indicate the property is named `lines`; see {@link https://github.com/kaesetoast/find-in-files/pull/19}.
*/
/**
* Eliminates known false matches against overly generic types.
* @param {string} file
* @param {external:FindInFilesResult} res
* @returns {void}
*/
function reduceFalseMatches (file, res) {
switch (file) {
case 'editor/external/jamilih/jml-es.js':
case 'editor/xdomain-svgedit-config-iife.js': // Ignore
res.line = [];
break;
case 'editor/embedapi.js':
res.line = res.line.filter((line) => {
return ![
'* @param {...*} args Signature dependent on the function'
].includes(line);
});
break;
case 'editor/external/dynamic-import-polyfill/importModule.js':
res.line = res.line.filter((line) => {
return ![
'* @returns {Promise<*>} The value to which it resolves depends on the export of the targeted module.',
'* @returns {Promise<*>} Resolves to value of loading module or rejects with'
].includes(line);
});
break;
case 'editor/typedefs.js':
res.line = res.line.filter((line) => {
return ![
'* @typedef {number} Float',
'* @typedef {*} ArbitraryCallbackResult',
'* @typedef {Object} ArbitraryObject',
'* @typedef {Object} ArbitraryModule',
'* @typedef {Array} GenericArray',
'* @typedef {*} Any',
'* @param {...*} args Signature dependent on the function'
].includes(line);
});
break;
}
res.count = res.line.length;
}
})();