Skip to content

Commit

Permalink
fix(jsdoc): Fix parameter types (#1615)
Browse files Browse the repository at this point in the history
And rename unused vars to `_` and remove unused `cloneDom` argument.
  • Loading branch information
fb55 committed Dec 28, 2020
1 parent 36c4c77 commit 15e39cf
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 73 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.json
Expand Up @@ -33,6 +33,8 @@
},
"preferredTypes": {
"node": "Node",
"nodewithchildren": "NodeWithChildren",
"element": "Element",
"cheerio": "Cheerio"
}
}
Expand Down
59 changes: 28 additions & 31 deletions lib/api/attributes.js
Expand Up @@ -32,9 +32,9 @@ var rbrace = /^(?:\{[\w\W]*\}|\[[\w\W]*\])$/;
* Also supports getting the `value` of several form elements.
*
* @private
* @param {Node} elem - Elenent to get the attribute of.
* @param {Element} elem - Elenent to get the attribute of.
* @param {string} name - Name of the attribute.
* @returns {string | undefined} The attribute's value.
* @returns {object | string | undefined} The attribute's value.
*/
function getAttr(elem, name) {
if (!elem || !isTag(elem)) return;
Expand Down Expand Up @@ -72,7 +72,7 @@ function getAttr(elem, name) {
* Sets the value of an attribute. The attribute will be deleted if the value is `null`.
*
* @private
* @param {Node} el - The element to set the attribute on.
* @param {Element} el - The element to set the attribute on.
* @param {string} name - The attribute's name.
* @param {string | null} value - The attribute's value.
*/
Expand All @@ -98,7 +98,7 @@ function setAttr(el, name, value) {
* //=> <li class="apple" id="favorite">Apple</li>
*
* @param {string} name - Name of the attribute.
* @param {string} [value] - If specified sets the value of the attribute.
* @param {string | Function} [value] - If specified sets the value of the attribute.
* @see {@link https://api.jquery.com/attr/}
*
* @returns {string | Cheerio} If `value` is specified the instance itself,
Expand Down Expand Up @@ -151,7 +151,7 @@ function getProp(el, name) {
* Sets the value of a prop.
*
* @private
* @param {Node} el - The element to set the prop on.
* @param {Element} el - The element to set the prop on.
* @param {string} name - The prop's name.
* @param {string | null} value - The prop's value.
*/
Expand Down Expand Up @@ -193,23 +193,21 @@ exports.prop = function (name, value) {

property.length = keys.length;

break;
return property;
}
case 'tagName':
case 'nodeName':
property = this[0].name.toUpperCase();
break;
return this[0].name.toUpperCase();

case 'outerHTML':
property = this.clone().wrap('<container />').parent().html();
break;
return this.clone().wrap('<container />').parent().html();

case 'innerHTML':
property = this.html();
break;
return this.html();

default:
property = getProp(this[0], name);
return getProp(this[0], name);
}

return property;
}

if (typeof name === 'object' || value !== undefined) {
Expand Down Expand Up @@ -238,8 +236,8 @@ exports.prop = function (name, value) {
* Sets the value of a data attribute.
*
* @private
* @param {Node} el - The element to set the data attribute on.
* @param {string|object<string,*>} name - The data attribute's name.
* @param {Element} el - The element to set the data attribute on.
* @param {string | object} name - The data attribute's name.
* @param {string | null} value - The data attribute's value.
*/
function setData(el, name, value) {
Expand All @@ -259,7 +257,7 @@ function setData(el, name, value) {
* attribute name is specified, read *all* HTML5 `data-*` attributes in this manner.
*
* @private
* @param {Node} el - Elenent to get the data attribute of.
* @param {Element} el - Elenent to get the data attribute of.
* @param {string} [name] - Name of the data attribute.
* @returns {any} The data attribute's value, or a map with all of the data attribute.
*/
Expand Down Expand Up @@ -325,7 +323,7 @@ function readData(el, name) {
* @param {any} [value] - If specified new value.
* @see {@link https://api.jquery.com/data/}
*
* @returns {string | Cheerio} If `value` is specified the instance itself,
* @returns {string | Cheerio | undefined} If `value` is specified the instance itself,
* otherwise the data attribute's value.
*/
exports.data = function (name, value) {
Expand Down Expand Up @@ -366,10 +364,9 @@ exports.data = function (name, value) {
* $('input[type="text"]').val('test').html();
* //=> <input type="text" value="test"/>
*
* @param {string} [value] - If specified new value.
* @param {string | string[]} [value] - If specified new value.
* @see {@link https://api.jquery.com/val/}
*
* @returns {string | Cheerio} If a new `value` is specified the instance
* @returns {string | Cheerio | undefined} If a new `value` is specified the instance
* itself, otherwise the value.
*/
exports.val = function (value) {
Expand All @@ -383,7 +380,7 @@ exports.val = function (value) {
return this.text(value);
case 'select': {
var option = this.find('option:selected');
if (!option) return undefined;
if (!option) return;
if (!querying) {
if (this.attr('multiple') == null && typeof value == 'object') {
return this;
Expand Down Expand Up @@ -414,7 +411,7 @@ exports.val = function (value) {
* Remove an attribute.
*
* @private
* @param {Node} elem - Node to remove attribute from.
* @param {Element} elem - Node to remove attribute from.
* @param {string} name - Name of the attribute to remove.
*/
function removeAttribute(elem, name) {
Expand Down Expand Up @@ -453,7 +450,7 @@ exports.removeAttr = function (name) {
var attrNames = splitNames(name);

for (var i = 0; i < attrNames.length; i++) {
domEach(this, function (j, elem) {
domEach(this, function (_, elem) {
removeAttribute(elem, attrNames[i]);
});
}
Expand Down Expand Up @@ -496,6 +493,8 @@ exports.hasClass = function (className) {
}
}
}

return false;
});
};

Expand All @@ -509,9 +508,8 @@ exports.hasClass = function (className) {
* $('.apple').addClass('fruit red').html();
* //=> <li class="apple fruit red">Apple</li>
*
* @param {string} value - Name of new class.
* @param {string | Function} value - Name of new class.
* @see {@link https://api.jquery.com/addClass/}
*
* @returns {Cheerio} The instance itself.
*/
exports.addClass = function (value) {
Expand Down Expand Up @@ -566,9 +564,8 @@ exports.addClass = function (value) {
* $('.apple').addClass('red').removeClass().html();
* //=> <li class="">Apple</li>
*
* @param {string} value - Name of the class.
* @param {string | Function} value - Name of the class.
* @see {@link https://api.jquery.com/removeClass/}
*
* @returns {Cheerio} The instance itself.
*/
exports.removeClass = function (value) {
Expand All @@ -586,7 +583,7 @@ exports.removeClass = function (value) {
var numClasses = classes.length;
var removeAll = arguments.length === 0;

return domEach(this, function (i, el) {
return domEach(this, function (_, el) {
if (!isTag(el)) return;

if (removeAll) {
Expand Down Expand Up @@ -689,7 +686,7 @@ exports.toggleClass = function (value, stateVal) {
* @param {string | Function | Cheerio | Node} selector - Selector for the selection.
* @see {@link https://api.jquery.com/is/}
*
* @returns {Cheerio} The instance itself.
* @returns {boolean} Whether or not the selector matches an element of the instance.
*/
exports.is = function (selector) {
if (selector) {
Expand Down
14 changes: 6 additions & 8 deletions lib/api/css.js
Expand Up @@ -32,9 +32,9 @@ exports.css = function (prop, val) {
*
* @private
*
* @param {object} el - Element to set style of.
* @param {Element} el - Element to set style of.
* @param {string | object} prop - Name of property.
* @param {string} val - Value to set property to.
* @param {string | Function} val - Value to set property to.
* @param {number} [idx] - Optional index within the selection.
*/
function setCss(el, prop, val, idx) {
Expand Down Expand Up @@ -63,14 +63,12 @@ function setCss(el, prop, val, idx) {
*
* @private
*
* @param {Node} el - Element to get styles from.
* @param {string} prop - Name of the prop.
* @returns {object} The parsed styles.
* @param {Element} el - Element to get styles from.
* @param {string | string[]} [prop] - Name of the prop.
* @returns {object | undefined} The parsed styles.
*/
function getCss(el, prop) {
if (!el || !el.attribs) {
return undefined;
}
if (!el || !el.attribs) return;

var styles = parse(el.attribs.style);
if (typeof prop === 'string') {
Expand Down
5 changes: 3 additions & 2 deletions lib/api/forms.js
Expand Up @@ -35,12 +35,13 @@ exports.serialize = function () {
*
* @see {@link https://api.jquery.com/serializeArray/}
*
* @this {Cheerio}
* @returns {object[]} The serialized form.
*/
exports.serializeArray = function () {
// Resolve all form elements from either forms or collections of form elements
var Cheerio = this.constructor;
return this.map(function (i, elem) {
return this.map(function (_, elem) {
var $elem = Cheerio(elem);
if (elem.name === 'form') {
return $elem.find(submittableSelector).toArray();
Expand All @@ -56,7 +57,7 @@ exports.serializeArray = function () {
':matches([checked], :not(:checkbox, :radio))'
// Convert each of the elements to its value(s)
)
.map(function (i, elem) {
.map(function (_, elem) {
var $elem = Cheerio(elem);
var name = $elem.attr('name');
var value = $elem.val();
Expand Down
22 changes: 11 additions & 11 deletions lib/api/manipulation.js
Expand Up @@ -29,7 +29,7 @@ exports._makeDomArray = function makeDomArray(elem, clone) {
if (elem == null) {
return [];
} else if (elem.cheerio) {
return clone ? cloneDom(elem.get(), elem.options) : elem.get();
return clone ? cloneDom(elem.get()) : elem.get();
} else if (Array.isArray(elem)) {
return elem.reduce(
function (newElems, el) {
Expand Down Expand Up @@ -70,7 +70,7 @@ function _insert(concatenator) {
* @param {number} spliceIdx - Index at which to begin changing the array.
* @param {number} spliceCount - Number of elements to remove from the array.
* @param {Node[]} newElems - Elements to insert into the array.
* @param {Node} parent - The parent of the node.
* @param {NodeWithChildren} parent - The parent of the node.
* @returns {Node[]} The spliced array.
*/
function uniqueSplice(array, spliceIdx, spliceCount, newElems, parent) {
Expand Down Expand Up @@ -612,7 +612,7 @@ exports.insertBefore = function (target) {
}
target = this._makeDomArray(target);
self.remove();
domEach(target, function (i, el) {
domEach(target, function (_, el) {
var clonedSelf = self._makeDomArray(self.clone());
var parent = el.parent;
if (!parent) {
Expand Down Expand Up @@ -653,7 +653,7 @@ exports.remove = function (selector) {
// Filter if we have selector
var elems = selector ? this.filter(selector) : this;

domEach(elems, function (i, el) {
domEach(elems, function (_, el) {
DomUtils.removeElement(el);
el.prev = el.next = el.parent = null;
});
Expand Down Expand Up @@ -719,7 +719,7 @@ exports.replaceWith = function (content) {
* @returns {Cheerio} The instance itself.
*/
exports.empty = function () {
return domEach(this, function (i, el) {
return domEach(this, function (_, el) {
el.children.forEach(function (child) {
child.next = child.prev = child.parent = null;
});
Expand All @@ -739,7 +739,7 @@ exports.empty = function () {
* $('#fruits').html('<li class="mango">Mango</li>').html();
* //=> <li class="mango">Mango</li>
*
* @param {string} str - If specified used to replace selection's contents.
* @param {string | Cheerio} str - If specified used to replace selection's contents.
* @see {@link https://api.jquery.com/html/}
*
* @returns {Cheerio} The instance itself.
Expand All @@ -752,7 +752,7 @@ exports.html = function (str) {

var opts = this.options;

return domEach(this, function (i, el) {
return domEach(this, function (_, el) {
el.children.forEach(function (child) {
child.next = child.prev = child.parent = null;
});
Expand Down Expand Up @@ -788,9 +788,9 @@ exports.toString = function () {
* // Orange
* // Pear
*
* @param {string} [str] - If specified replacement for the selected element's contents.
* @param {string | Function} [str] - If specified replacement for the selected
* element's contents.
* @see {@link https://api.jquery.com/text/}
*
* @returns {Cheerio | string} The instance itself when setting text, otherwise
* the rendered document.
*/
Expand All @@ -806,7 +806,7 @@ exports.text = function (str) {
}

// Append text node to each selected elements
return domEach(this, function (i, el) {
return domEach(this, function (_, el) {
el.children.forEach(function (child) {
child.next = child.prev = child.parent = null;
});
Expand All @@ -828,5 +828,5 @@ exports.text = function (str) {
* @returns {Cheerio} The cloned object.
*/
exports.clone = function () {
return this._make(cloneDom(this.get(), this.options));
return this._make(cloneDom(this.get()));
};

0 comments on commit 15e39cf

Please sign in to comment.