Skip to content

Commit

Permalink
chore(lint): Enable several rules (#1617)
Browse files Browse the repository at this point in the history
* Add back `--ignore-path .gitignore`

* Disable `allowElseIf` in `no-else-return` rule

* Enable `no-void` rule

* Enable `array-callback-return` rule

* Enable `no-useless-call` rule

* api/traversing.js: replace if statement with a ternary

* Use `Array.isArray`

* Use `new Error` instead of `Error`

* Enable `no-lonely-if` rule

* test/cheerio.js: fix require

* Enable `yoda` rule
  • Loading branch information
XhmikosR authored Dec 29, 2020
1 parent bdd6018 commit 21de2c5
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 29 deletions.
17 changes: 16 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,27 @@
"extends": ["eslint:recommended", "plugin:jsdoc/recommended", "prettier"],
"globals": { "Set": true, "Symbol": true },
"rules": {
"array-callback-return": [
"error",
{
"allowImplicit": true
}
],
"no-lonely-if": 2,
"no-proto": 2,
"curly": [2, "multi-line"],
"one-var": [2, "never"],
"no-else-return": 2,
"no-else-return": [
2,
{
"allowElseIf": false
}
],
"no-shadow": 2,
"no-useless-call": 2,
"no-use-before-define": [2, "nofunc"],
"no-void": 2,
"yoda": 2,

"jsdoc/require-jsdoc": 0,
"jsdoc/check-param-names": 2,
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
node_modules
npm-debug.log
.DS_Store
/coverage
/docs/out/
/coverage/
/docs/
4 changes: 2 additions & 2 deletions benchmark/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Suites.prototype._benchJsDom = function (suite, markup, options) {
var setupData = options.setup && options.setup.call(null, dom.window.$);

suite.add('jsdom', function () {
testFn.call(null, dom.window.$, setupData);
testFn(dom.window.$, setupData);
});
suite.run();
};
Expand All @@ -82,6 +82,6 @@ Suites.prototype._benchCheerio = function (suite, markup, options) {
var setupData = options.setup && options.setup.call(null, $);

suite.add('cheerio', function () {
testFn.call(null, $, setupData);
testFn($, setupData);
});
};
3 changes: 2 additions & 1 deletion lib/api/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ exports.data = function (name, value) {
setData(el, name, value);
});
return this;
} else if (hasOwn.call(elem.data, name)) {
}
if (hasOwn.call(elem.data, name)) {
return elem.data[name];
}

Expand Down
7 changes: 4 additions & 3 deletions lib/api/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ exports.css = function (prop, val) {
* @param {number} [idx] - Optional index within the selection.
*/
function setCss(el, prop, val, idx) {
if ('string' == typeof prop) {
if (typeof prop == 'string') {
var styles = getCss(el);
if (typeof val === 'function') {
val = val.call(el, idx, styles[prop]);
Expand All @@ -51,7 +51,7 @@ function setCss(el, prop, val, idx) {
}

el.attribs.style = stringify(styles);
} else if ('object' == typeof prop) {
} else if (typeof prop == 'object') {
Object.keys(prop).forEach(function (k) {
setCss(el, k, prop[k]);
});
Expand All @@ -73,7 +73,8 @@ function getCss(el, prop) {
var styles = parse(el.attribs.style);
if (typeof prop === 'string') {
return styles[prop];
} else if (Array.isArray(prop)) {
}
if (Array.isArray(prop)) {
var newStyles = {};
prop.forEach(function (item) {
if (styles[item] != null) {
Expand Down
12 changes: 8 additions & 4 deletions lib/api/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ var DomUtils = require('htmlparser2').DomUtils;
exports._makeDomArray = function makeDomArray(elem, clone) {
if (elem == null) {
return [];
} else if (elem.cheerio) {
}
if (elem.cheerio) {
return clone ? cloneDom(elem.get()) : elem.get();
} else if (Array.isArray(elem)) {
}
if (Array.isArray(elem)) {
return elem.reduce(
function (newElems, el) {
return newElems.concat(this._makeDomArray(el, clone));
}.bind(this),
[]
);
} else if (typeof elem === 'string') {
}
if (typeof elem === 'string') {
return parse(elem, this.options, false).children;
}
return clone ? cloneDom([elem]) : [elem];
Expand Down Expand Up @@ -798,7 +801,8 @@ exports.text = function (str) {
// If `str` is undefined, act as a "getter"
if (str === undefined) {
return text(this);
} else if (typeof str === 'function') {
}
if (typeof str === 'function') {
// Function support
return domEach(this, function (i, el) {
return exports.text.call(this._make(el), str.call(el, i, text([el])));
Expand Down
12 changes: 6 additions & 6 deletions lib/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,8 @@ function getFilterFn(match) {
return function (el, i) {
return match.call(el, i, el);
};
} else if (match.cheerio) {
}
if (match.cheerio) {
return match.is.bind(match);
}
return function (el) {
Expand Down Expand Up @@ -644,11 +645,10 @@ exports.filter = function (match, container) {
container = container || this;
var elements = this.toArray ? this.toArray() : this;

if (typeof match === 'string') {
elements = select.filter(match, elements, container.options);
} else {
elements = elements.filter(getFilterFn(match));
}
elements =
typeof match === 'string'
? select.filter(match, elements, container.options)
: elements.filter(getFilterFn(match));

return container._make(elements);
};
Expand Down
2 changes: 1 addition & 1 deletion lib/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ exports.load = function (content, options, isDocument) {

options = Object.assign({}, defaultOptions, flattenOptions(options));

if (isDocument === void 0) isDocument = true;
if (typeof isDocument === 'undefined') isDocument = true;

var root = parse(content, options, isDocument);

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@
"test:jest:cov": "npm run test:jest -- --coverage",
"test:types": "tsd",
"lint": "npm run lint:es && npm run lint:prettier",
"lint:es": "eslint .",
"lint:es": "eslint --ignore-path .gitignore .",
"lint:prettier": "npm run format:prettier:raw -- --check",
"format": "npm run format:es && npm run format:prettier",
"format:es": "npm run lint:es -- --fix",
"format:prettier": "npm run format:prettier:raw -- --write",
"format:prettier:raw": "prettier \"**/*.{js,ts,md,json,yml}\"",
"format:prettier:raw": "prettier \"**/*.{js,ts,md,json,yml}\" --ignore-path .gitignore",
"build:docs": "jsdoc --configure jsdoc-config.json",
"benchmark": "node benchmark/benchmark.js --regex \"^(?!.*highmem)\"",
"pre-commit": "lint-staged"
Expand Down
2 changes: 1 addition & 1 deletion test/api/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('deprecated APIs', function () {
it('(arraylike, arraylike) : should return an array', function () {
var ret = cheerio.merge(arr1, arr2);
expect(typeof ret).toBe('object');
expect(ret instanceof Array).toBeTruthy();
expect(Array.isArray(ret)).toBe(true);
});

it('(arraylike, arraylike) : should modify the first array', function () {
Expand Down
8 changes: 4 additions & 4 deletions test/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('$(...)', function () {
cheerio.load();
})();

throw Error('Function did not throw');
throw new Error('Function did not throw');
} catch (err) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe('cheerio.load() expects a string');
Expand Down Expand Up @@ -86,7 +86,7 @@ describe('$(...)', function () {
$('#fruits').find(':bah');
})();

throw Error('Function did not throw');
throw new Error('Function did not throw');
} catch (err) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toContain('unmatched pseudo-class');
Expand Down Expand Up @@ -469,7 +469,7 @@ describe('$(...)', function () {
$('.orange').siblings(':bah');
})();

throw Error('Function did not throw');
throw new Error('Function did not throw');
} catch (err) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toContain('unmatched pseudo-class');
Expand Down Expand Up @@ -704,7 +704,7 @@ describe('$(...)', function () {
var args = [];
var thisVals = [];

$fruits.map(function () {
$fruits.each(function () {
args.push(Array.prototype.slice.call(arguments));
thisVals.push(this);
});
Expand Down
2 changes: 1 addition & 1 deletion test/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ describe('cheerio', function () {
it('(arraylike, arraylike) : should return an array', function () {
var ret = $.merge(arr1, arr2);
expect(typeof ret).toBe('object');
expect(ret instanceof Array).toBeTruthy();
expect(Array.isArray(ret)).toBe(true);
});

it('(arraylike, arraylike) : should modify the first array', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/cheerio.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var htmlparser2 = require('htmlparser2');
var cheerio = require('../');
var cheerio = require('..');
var fixtures = require('./__fixtures__/fixtures');
var fruits = fixtures.fruits;
var food = fixtures.food;
Expand Down

0 comments on commit 21de2c5

Please sign in to comment.