iterateJsdoc.js
8.91 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.parseComment = void 0;
var _lodash = _interopRequireDefault(require("lodash"));
var _commentParser = _interopRequireDefault(require("comment-parser"));
var _jsdocUtils = _interopRequireDefault(require("./jsdocUtils"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var parseComment = function parseComment(commentNode, indent) {
// Preserve JSDoc block start/end indentation.
return (0, _commentParser.default)(indent + '/*' + commentNode.value + indent + '*/', {
// @see https://github.com/yavorskiy/comment-parser/issues/21
parsers: [_commentParser.default.PARSERS.parse_tag, _commentParser.default.PARSERS.parse_type, function (str, data) {
if (_lodash.default.includes(['return', 'returns', 'throws', 'exception'], data.tag)) {
return null;
}
return _commentParser.default.PARSERS.parse_name(str, data);
}, _commentParser.default.PARSERS.parse_description]
})[0] || {};
};
exports.parseComment = parseComment;
var curryUtils = function curryUtils(functionNode, jsdoc, tagNamePreference, exampleCodeRegex, rejectExampleCodeRegex, additionalTagNames, baseConfig, configFile, captionRequired, matchingFileName, eslintrcForExamples, allowInlineConfig, allowEmptyNamepaths, reportUnusedDisableDirectives, noDefaultExampleRules, allowOverrideWithoutParam, allowImplementsWithoutParam, allowAugmentsExtendsWithoutParam, checkSeesForNamepaths, ancestors, sourceCode) {
var utils = {};
utils.getFunctionParameterNames = function () {
return _jsdocUtils.default.getFunctionParameterNames(functionNode);
};
utils.getFunctionSourceCode = function () {
return sourceCode.getText(functionNode);
};
utils.isConstructor = function () {
return functionNode.parent && functionNode.parent.kind === 'constructor';
};
utils.getJsdocParameterNamesDeep = function () {
return _jsdocUtils.default.getJsdocParameterNamesDeep(jsdoc, utils.getPreferredTagName('param'));
};
utils.getJsdocParameterNames = function () {
return _jsdocUtils.default.getJsdocParameterNames(jsdoc, utils.getPreferredTagName('param'));
};
utils.getPreferredTagName = function (name) {
return _jsdocUtils.default.getPreferredTagName(name, tagNamePreference);
};
utils.getExampleCodeRegex = function () {
return exampleCodeRegex;
};
utils.getRejectExampleCodeRegex = function () {
return rejectExampleCodeRegex;
};
utils.getMatchingFileName = function () {
return matchingFileName;
};
utils.isValidTag = function (name) {
return _jsdocUtils.default.isValidTag(name, additionalTagNames);
};
utils.hasATag = function (name) {
return _jsdocUtils.default.hasATag(jsdoc, name);
};
utils.hasTag = function (name) {
return _jsdocUtils.default.hasTag(jsdoc, name);
};
utils.useEslintrcForExamples = function () {
return eslintrcForExamples;
};
utils.allowInlineConfig = function () {
return allowInlineConfig;
};
utils.reportUnusedDisableDirectives = function () {
return reportUnusedDisableDirectives;
};
utils.hasNoDefaultExampleRules = function () {
return noDefaultExampleRules;
};
utils.getBaseConfig = function () {
return baseConfig;
};
utils.getConfigFile = function () {
return configFile;
};
utils.isCaptionRequired = function () {
return captionRequired;
};
utils.isOverrideAllowedWithoutParam = function () {
return allowOverrideWithoutParam;
};
utils.isImplementsAllowedWithoutParam = function () {
return allowImplementsWithoutParam;
};
utils.isAugmentsExtendsAllowedWithoutParam = function () {
return allowAugmentsExtendsWithoutParam;
};
utils.isNamepathType = function (tagName) {
return _jsdocUtils.default.isNamepathType(tagName, checkSeesForNamepaths);
};
utils.passesEmptyNamepathCheck = function (tag) {
return !tag.name && allowEmptyNamepaths && _lodash.default.includes([// These may serve some minor purpose when empty
'callback', 'event', 'listens', 'fires', 'emits'], tag.tag);
};
utils.getClassJsdocNode = function () {
var greatGrandParent = ancestors.slice(-3)[0];
var greatGrandParentValue = greatGrandParent && sourceCode.getFirstToken(greatGrandParent).value;
if (greatGrandParentValue === 'class') {
var classJsdocNode = sourceCode.getJSDocComment(greatGrandParent);
return classJsdocNode;
}
return false;
};
utils.classHasTag = function (tagName) {
var classJsdocNode = utils.getClassJsdocNode();
if (classJsdocNode) {
var indent = _lodash.default.repeat(' ', classJsdocNode.loc.start.column);
var classJsdoc = parseComment(classJsdocNode, indent);
if (_jsdocUtils.default.hasTag(classJsdoc, tagName)) {
return true;
}
}
return false;
};
return utils;
};
var _default = function _default(iterator) {
return function (context) {
var sourceCode = context.getSourceCode();
var tagNamePreference = _lodash.default.get(context, 'settings.jsdoc.tagNamePreference') || {};
var exampleCodeRegex = _lodash.default.get(context, 'settings.jsdoc.exampleCodeRegex') || null;
var rejectExampleCodeRegex = _lodash.default.get(context, 'settings.jsdoc.rejectExampleCodeRegex') || null;
var matchingFileName = _lodash.default.get(context, 'settings.jsdoc.matchingFileName') || null;
var additionalTagNames = _lodash.default.get(context, 'settings.jsdoc.additionalTagNames') || {};
var baseConfig = _lodash.default.get(context, 'settings.jsdoc.baseConfig') || {};
var configFile = _lodash.default.get(context, 'settings.jsdoc.configFile');
var eslintrcForExamples = _lodash.default.get(context, 'settings.jsdoc.eslintrcForExamples') !== false;
var allowInlineConfig = _lodash.default.get(context, 'settings.jsdoc.allowInlineConfig') !== false;
var allowEmptyNamepaths = _lodash.default.get(context, 'settings.jsdoc.allowEmptyNamepaths') !== false;
var reportUnusedDisableDirectives = _lodash.default.get(context, 'settings.jsdoc.reportUnusedDisableDirectives') !== false;
var captionRequired = Boolean(_lodash.default.get(context, 'settings.jsdoc.captionRequired'));
var noDefaultExampleRules = Boolean(_lodash.default.get(context, 'settings.jsdoc.noDefaultExampleRules'));
var allowOverrideWithoutParam = Boolean(_lodash.default.get(context, 'settings.jsdoc.allowOverrideWithoutParam'));
var allowImplementsWithoutParam = Boolean(_lodash.default.get(context, 'settings.jsdoc.allowImplementsWithoutParam'));
var allowAugmentsExtendsWithoutParam = Boolean(_lodash.default.get(context, 'settings.jsdoc.allowAugmentsExtendsWithoutParam'));
var checkSeesForNamepaths = Boolean(_lodash.default.get(context, 'settings.jsdoc.checkSeesForNamepaths'));
var checkJsdoc = function checkJsdoc(functionNode) {
var jsdocNode = sourceCode.getJSDocComment(functionNode);
if (!jsdocNode) {
return;
}
var ancestors = context.getAncestors();
var indent = _lodash.default.repeat(' ', jsdocNode.loc.start.column);
var jsdoc = parseComment(jsdocNode, indent);
var report = function report(message) {
var fixer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
var jsdocLoc = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var loc;
if (jsdocLoc) {
var lineNumber = jsdocNode.loc.start.line + jsdocLoc.line;
loc = {
end: {
line: lineNumber
},
start: {
line: lineNumber
}
};
if (jsdocLoc.column) {
var colNumber = jsdocNode.loc.start.column + jsdocLoc.column;
loc.end.column = colNumber;
loc.start.column = colNumber;
}
}
if (fixer === null) {
context.report({
loc,
message,
node: jsdocNode
});
} else {
context.report({
fix: fixer,
loc,
message,
node: jsdocNode
});
}
};
var utils = curryUtils(functionNode, jsdoc, tagNamePreference, exampleCodeRegex, rejectExampleCodeRegex, additionalTagNames, baseConfig, configFile, captionRequired, matchingFileName, eslintrcForExamples, allowInlineConfig, allowEmptyNamepaths, reportUnusedDisableDirectives, noDefaultExampleRules, allowOverrideWithoutParam, allowImplementsWithoutParam, allowAugmentsExtendsWithoutParam, checkSeesForNamepaths, ancestors, sourceCode);
iterator({
context,
functionNode,
indent,
jsdoc,
jsdocNode,
report,
sourceCode,
utils
});
};
return {
ArrowFunctionExpression: checkJsdoc,
FunctionDeclaration: checkJsdoc,
FunctionExpression: checkJsdoc
};
};
};
exports.default = _default;
//# sourceMappingURL=iterateJsdoc.js.map