Skip to content

Commit b79f380

Browse files
authored
Merge pull request #220 from brettz9/rule-format-3
feat: update to later ESLint rule format
2 parents 3a42f20 + 373252c commit b79f380

File tree

3 files changed

+162
-152
lines changed

3 files changed

+162
-152
lines changed

Diff for: src/iterateJsdoc.js

+120-102
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const parseComment = (commentNode, indent) => {
2222
};
2323

2424
const curryUtils = (
25-
functionNode,
25+
node,
2626
jsdoc,
2727
tagNamePreference,
2828
exampleCodeRegex,
@@ -47,15 +47,15 @@ const curryUtils = (
4747
const utils = {};
4848

4949
utils.getFunctionParameterNames = () => {
50-
return jsdocUtils.getFunctionParameterNames(functionNode);
50+
return jsdocUtils.getFunctionParameterNames(node);
5151
};
5252

5353
utils.getFunctionSourceCode = () => {
54-
return sourceCode.getText(functionNode);
54+
return sourceCode.getText(node);
5555
};
5656

5757
utils.isConstructor = () => {
58-
return functionNode.parent && functionNode.parent.kind === 'constructor';
58+
return node.parent && node.parent.kind === 'constructor';
5959
};
6060

6161
utils.getJsdocParameterNamesDeep = () => {
@@ -178,113 +178,131 @@ export {
178178
parseComment
179179
};
180180

181-
export default (iterator) => {
182-
return (context) => {
183-
const sourceCode = context.getSourceCode();
184-
const tagNamePreference = _.get(context, 'settings.jsdoc.tagNamePreference') || {};
185-
const exampleCodeRegex = _.get(context, 'settings.jsdoc.exampleCodeRegex') || null;
186-
const rejectExampleCodeRegex = _.get(context, 'settings.jsdoc.rejectExampleCodeRegex') || null;
187-
const matchingFileName = _.get(context, 'settings.jsdoc.matchingFileName') || null;
188-
const additionalTagNames = _.get(context, 'settings.jsdoc.additionalTagNames') || {};
189-
const baseConfig = _.get(context, 'settings.jsdoc.baseConfig') || {};
190-
const configFile = _.get(context, 'settings.jsdoc.configFile');
191-
const eslintrcForExamples = _.get(context, 'settings.jsdoc.eslintrcForExamples') !== false;
192-
const allowInlineConfig = _.get(context, 'settings.jsdoc.allowInlineConfig') !== false;
193-
const allowEmptyNamepaths = _.get(context, 'settings.jsdoc.allowEmptyNamepaths') !== false;
194-
const reportUnusedDisableDirectives = _.get(context, 'settings.jsdoc.reportUnusedDisableDirectives') !== false;
195-
const captionRequired = Boolean(_.get(context, 'settings.jsdoc.captionRequired'));
196-
const noDefaultExampleRules = Boolean(_.get(context, 'settings.jsdoc.noDefaultExampleRules'));
197-
const allowOverrideWithoutParam = Boolean(_.get(context, 'settings.jsdoc.allowOverrideWithoutParam'));
198-
const allowImplementsWithoutParam = Boolean(_.get(context, 'settings.jsdoc.allowImplementsWithoutParam'));
199-
const allowAugmentsExtendsWithoutParam = Boolean(_.get(context, 'settings.jsdoc.allowAugmentsExtendsWithoutParam'));
200-
const checkSeesForNamepaths = Boolean(_.get(context, 'settings.jsdoc.checkSeesForNamepaths'));
201-
202-
const checkJsdoc = (functionNode) => {
203-
const jsdocNode = sourceCode.getJSDocComment(functionNode);
204-
205-
if (!jsdocNode) {
206-
return;
207-
}
181+
export default (iterator, options) => {
182+
const opts = options || {};
183+
184+
return {
185+
/**
186+
* The entrypoint for the JSDoc rule.
187+
*
188+
* @param {*} context
189+
* a reference to the context which hold all important information
190+
* like settings and the sourcecode to check.
191+
* @returns {Object}
192+
* a list with parser callback function.
193+
*/
194+
create (context) {
195+
const sourceCode = context.getSourceCode();
196+
const tagNamePreference = _.get(context, 'settings.jsdoc.tagNamePreference') || {};
197+
const exampleCodeRegex = _.get(context, 'settings.jsdoc.exampleCodeRegex') || null;
198+
const rejectExampleCodeRegex = _.get(context, 'settings.jsdoc.rejectExampleCodeRegex') || null;
199+
const matchingFileName = _.get(context, 'settings.jsdoc.matchingFileName') || null;
200+
const additionalTagNames = _.get(context, 'settings.jsdoc.additionalTagNames') || {};
201+
const baseConfig = _.get(context, 'settings.jsdoc.baseConfig') || {};
202+
const configFile = _.get(context, 'settings.jsdoc.configFile');
203+
const eslintrcForExamples = _.get(context, 'settings.jsdoc.eslintrcForExamples') !== false;
204+
const allowInlineConfig = _.get(context, 'settings.jsdoc.allowInlineConfig') !== false;
205+
const allowEmptyNamepaths = _.get(context, 'settings.jsdoc.allowEmptyNamepaths') !== false;
206+
const reportUnusedDisableDirectives = _.get(context, 'settings.jsdoc.reportUnusedDisableDirectives') !== false;
207+
const captionRequired = Boolean(_.get(context, 'settings.jsdoc.captionRequired'));
208+
const noDefaultExampleRules = Boolean(_.get(context, 'settings.jsdoc.noDefaultExampleRules'));
209+
const allowOverrideWithoutParam = Boolean(_.get(context, 'settings.jsdoc.allowOverrideWithoutParam'));
210+
const allowImplementsWithoutParam = Boolean(_.get(context, 'settings.jsdoc.allowImplementsWithoutParam'));
211+
const allowAugmentsExtendsWithoutParam = Boolean(_.get(context, 'settings.jsdoc.allowAugmentsExtendsWithoutParam'));
212+
const checkSeesForNamepaths = Boolean(_.get(context, 'settings.jsdoc.checkSeesForNamepaths'));
213+
214+
const checkJsdoc = (node) => {
215+
const jsdocNode = sourceCode.getJSDocComment(node);
216+
217+
if (!jsdocNode) {
218+
return;
219+
}
208220

209-
const ancestors = context.getAncestors();
221+
const ancestors = context.getAncestors();
210222

211-
const indent = _.repeat(' ', jsdocNode.loc.start.column);
223+
const indent = _.repeat(' ', jsdocNode.loc.start.column);
212224

213-
const jsdoc = parseComment(jsdocNode, indent);
225+
const jsdoc = parseComment(jsdocNode, indent);
214226

215-
const report = (message, fixer = null, jsdocLoc = null) => {
216-
let loc;
227+
const report = (message, fixer = null, jsdocLoc = null) => {
228+
let loc;
217229

218-
if (jsdocLoc) {
219-
const lineNumber = jsdocNode.loc.start.line + jsdocLoc.line;
230+
if (jsdocLoc) {
231+
const lineNumber = jsdocNode.loc.start.line + jsdocLoc.line;
220232

221-
loc = {
222-
end: {line: lineNumber},
223-
start: {line: lineNumber}
224-
};
225-
if (jsdocLoc.column) {
226-
const colNumber = jsdocNode.loc.start.column + jsdocLoc.column;
233+
loc = {
234+
end: {line: lineNumber},
235+
start: {line: lineNumber}
236+
};
237+
if (jsdocLoc.column) {
238+
const colNumber = jsdocNode.loc.start.column + jsdocLoc.column;
227239

228-
loc.end.column = colNumber;
229-
loc.start.column = colNumber;
240+
loc.end.column = colNumber;
241+
loc.start.column = colNumber;
242+
}
230243
}
231-
}
232-
if (fixer === null) {
233-
context.report({
234-
loc,
235-
message,
236-
node: jsdocNode
237-
});
238-
} else {
239-
context.report({
240-
fix: fixer,
241-
loc,
242-
message,
243-
node: jsdocNode
244-
});
245-
}
244+
if (fixer === null) {
245+
context.report({
246+
loc,
247+
message,
248+
node: jsdocNode
249+
});
250+
} else {
251+
context.report({
252+
fix: fixer,
253+
loc,
254+
message,
255+
node: jsdocNode
256+
});
257+
}
258+
};
259+
260+
const utils = curryUtils(
261+
node,
262+
jsdoc,
263+
tagNamePreference,
264+
exampleCodeRegex,
265+
rejectExampleCodeRegex,
266+
additionalTagNames,
267+
baseConfig,
268+
configFile,
269+
captionRequired,
270+
matchingFileName,
271+
eslintrcForExamples,
272+
allowInlineConfig,
273+
allowEmptyNamepaths,
274+
reportUnusedDisableDirectives,
275+
noDefaultExampleRules,
276+
allowOverrideWithoutParam,
277+
allowImplementsWithoutParam,
278+
allowAugmentsExtendsWithoutParam,
279+
checkSeesForNamepaths,
280+
ancestors,
281+
sourceCode
282+
);
283+
284+
iterator({
285+
context,
286+
indent,
287+
jsdoc,
288+
jsdocNode,
289+
node,
290+
report,
291+
sourceCode,
292+
utils
293+
});
246294
};
247295

248-
const utils = curryUtils(
249-
functionNode,
250-
jsdoc,
251-
tagNamePreference,
252-
exampleCodeRegex,
253-
rejectExampleCodeRegex,
254-
additionalTagNames,
255-
baseConfig,
256-
configFile,
257-
captionRequired,
258-
matchingFileName,
259-
eslintrcForExamples,
260-
allowInlineConfig,
261-
allowEmptyNamepaths,
262-
reportUnusedDisableDirectives,
263-
noDefaultExampleRules,
264-
allowOverrideWithoutParam,
265-
allowImplementsWithoutParam,
266-
allowAugmentsExtendsWithoutParam,
267-
checkSeesForNamepaths,
268-
ancestors,
269-
sourceCode
270-
);
271-
272-
iterator({
273-
context,
274-
functionNode,
275-
indent,
276-
jsdoc,
277-
jsdocNode,
278-
report,
279-
sourceCode,
280-
utils
281-
});
282-
};
283-
284-
return {
285-
ArrowFunctionExpression: checkJsdoc,
286-
FunctionDeclaration: checkJsdoc,
287-
FunctionExpression: checkJsdoc
288-
};
296+
if (opts.returns) {
297+
return opts.returns(context, sourceCode, checkJsdoc);
298+
}
299+
300+
return {
301+
ArrowFunctionExpression: checkJsdoc,
302+
FunctionDeclaration: checkJsdoc,
303+
FunctionExpression: checkJsdoc
304+
};
305+
},
306+
meta: opts.meta
289307
};
290308
};

Diff for: src/rules/requireJsdoc.js

+39-47
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import iterateJsdoc from '../iterateJsdoc';
2+
13
const OPTIONS_SCHEMA = {
24
additionalProperties: false,
35
properties: {
@@ -58,30 +60,39 @@ const getOptions = (context) => {
5860
};
5961
};
6062

61-
const checkJsDoc = (context, node) => {
62-
const jsDocNode = context.getSourceCode().getJSDocComment(node);
63+
export default iterateJsdoc(null, {
64+
meta: {
65+
doc: {
66+
category: 'Stylistic Issues',
67+
description: 'Require JSDoc comments',
68+
recommended: 'true',
69+
url: 'https://github.com/gajus/eslint-plugin-jsdoc'
70+
},
6371

64-
if (jsDocNode) {
65-
return;
66-
}
72+
messages: {
73+
missingJsDoc: 'Missing JSDoc comment.'
74+
},
6775

68-
context.report({
69-
messageId: 'missingJsDoc',
70-
node
71-
});
72-
};
76+
schema: [
77+
OPTIONS_SCHEMA
78+
],
79+
80+
type: 'suggestion'
81+
},
82+
returns (context, sourceCode) {
83+
const checkJsDoc = (node) => {
84+
const jsDocNode = sourceCode.getJSDocComment(node);
85+
86+
if (jsDocNode) {
87+
return;
88+
}
89+
90+
context.report({
91+
messageId: 'missingJsDoc',
92+
node
93+
});
94+
};
7395

74-
export default {
75-
/**
76-
* The entrypoint for the JSDoc rule.
77-
*
78-
* @param {*} context
79-
* a reference to the context which hold all important information
80-
* like settings and the sourcecode to check.
81-
* @returns {*}
82-
* a list with parser callback function.
83-
*/
84-
create (context) {
8596
const options = getOptions(context);
8697

8798
return {
@@ -94,28 +105,28 @@ export default {
94105
return;
95106
}
96107

97-
checkJsDoc(context, node);
108+
checkJsDoc(node);
98109
},
99110

100111
ClassDeclaration: (node) => {
101112
if (!options.ClassDeclaration) {
102113
return;
103114
}
104115

105-
checkJsDoc(context, node);
116+
checkJsDoc(node);
106117
},
107118

108119
FunctionDeclaration: (node) => {
109120
if (!options.FunctionDeclaration) {
110121
return;
111122
}
112123

113-
checkJsDoc(context, node);
124+
checkJsDoc(node);
114125
},
115126

116127
FunctionExpression: (node) => {
117128
if (options.MethodDefinition && node.parent.type === 'MethodDefinition') {
118-
checkJsDoc(context, node);
129+
checkJsDoc(node);
119130

120131
return;
121132
}
@@ -125,32 +136,13 @@ export default {
125136
}
126137

127138
if (node.parent.type === 'VariableDeclarator') {
128-
checkJsDoc(context, node);
139+
checkJsDoc(node);
129140
}
130141

131142
if (node.parent.type === 'Property' && node === node.parent.value) {
132-
checkJsDoc(context, node);
143+
checkJsDoc(node);
133144
}
134145
}
135146
};
136-
},
137-
138-
meta: {
139-
doc: {
140-
category: 'Stylistic Issues',
141-
description: 'Require JSDoc comments',
142-
recommended: 'true',
143-
url: 'https://github.com/gajus/eslint-plugin-jsdoc'
144-
},
145-
146-
messages: {
147-
missingJsDoc: 'Missing JSDoc comment.'
148-
},
149-
150-
schema: [
151-
OPTIONS_SCHEMA
152-
],
153-
154-
type: 'suggestion'
155147
}
156-
};
148+
});

0 commit comments

Comments
 (0)