Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(lint): Add jest & node eslint plugins #1642

Merged
merged 5 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
"node": true
},
"plugins": ["jsdoc"],
"extends": ["eslint:recommended", "plugin:jsdoc/recommended", "prettier"],
"extends": [
"eslint:recommended",
"plugin:jsdoc/recommended",
"plugin:jest/recommended",
"plugin:node/recommended",
"prettier"
],
"globals": { "Set": true, "Symbol": true },
"rules": {
// TODO
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth renaming .eslintrc.json to .eslintrc.js later so that the file is valid, but that's nitpicking since ESLint works with comments in JSON too.

BTW you could set it to 4.5.0 so that it works, but the discrepancy between the engines version and the current code will still exist.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah — we really need to find a solution for this. I've disabled the rule for now and will revisit it soon.

"node/no-unsupported-features/es-builtins": 0,

"array-callback-return": [
"error",
{
Expand Down
5 changes: 1 addition & 4 deletions benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#!/usr/bin/env node
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep the shebang since the script is a CLI script

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eslint-plugin-node's recommended config only allows a shebang when the script is actually a part of the package.json's bins. There isn't too much of a point for exposing this externally, so removing it seems reasonable. We always call this with node anyway, so nothing should change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, exactly why the shebang should stay :)

IMO this is wrong, and the file should be treated as a CLI script since that's what it is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See also mysticatea/eslint-plugin-node#96

and https://github.com/xojs/xo/blob/master/config/plugins.js#L309-L310

Personally, I'd disable the rule and keep the shebang.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the references. This was super helpful, I'm convinced 😄


var Suites = require('./suite');
var suites = new Suites();

var regexIdx = process.argv.indexOf('--regex') + 1;
if (regexIdx > 0) {
if (regexIdx === process.argv.length) {
console.error('Error: the "--regex" option requires a value');
process.exit(1);
throw new Error('Error: the "--regex" option requires a value');
}
suites.filter(process.argv[regexIdx]);
}
Expand Down
118 changes: 118 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
"benchmark": "^2.1.4",
"eslint": "^7.17.0",
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-jest": "^24.1.3",
"eslint-plugin-jsdoc": "^30.7.13",
"eslint-plugin-node": "^11.1.0",
"husky": "^4.3.6",
"jest": "^26.6.3",
"jquery": "^3.5.1",
Expand Down
26 changes: 13 additions & 13 deletions test/api/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ describe('$(...)', function () {
expect(attr).toBe('autofocus');
});

it('(key, value) : should set attr', function () {
it('(key, value) : should set one attr', function () {
var $pear = $('.pear').attr('id', 'pear');
expect($('#pear')).toHaveLength(1);
expect($pear).toBeInstanceOf($);
});

it('(key, value) : should set attr', function () {
it('(key, value) : should set multiple attr', function () {
var $el = cheerio('<div></div> <div></div>').attr('class', 'pear');

expect($el[0].attribs['class']).toBe('pear');
Expand Down Expand Up @@ -539,26 +539,26 @@ describe('$(...)', function () {
});

describe('.hasClass', function () {
function test(attr) {
function withClass(attr) {
return cheerio('<div class="' + attr + '"></div>');
}

it('(valid class) : should return true', function () {
var cls = $('.apple').hasClass('apple');
expect(cls).toBe(true);

expect(test('foo').hasClass('foo')).toBe(true);
expect(test('foo bar').hasClass('foo')).toBe(true);
expect(test('bar foo').hasClass('foo')).toBe(true);
expect(test('bar foo bar').hasClass('foo')).toBe(true);
expect(withClass('foo').hasClass('foo')).toBe(true);
expect(withClass('foo bar').hasClass('foo')).toBe(true);
expect(withClass('bar foo').hasClass('foo')).toBe(true);
expect(withClass('bar foo bar').hasClass('foo')).toBe(true);
});

it('(invalid class) : should return false', function () {
var cls = $('#fruits').hasClass('fruits');
expect(cls).toBe(false);
expect(test('foo-bar').hasClass('foo')).toBe(false);
expect(test('foo-bar').hasClass('foo')).toBe(false);
expect(test('foo-bar').hasClass('foo-ba')).toBe(false);
expect(withClass('foo-bar').hasClass('foo')).toBe(false);
expect(withClass('foo-bar').hasClass('foo')).toBe(false);
expect(withClass('foo-bar').hasClass('foo-ba')).toBe(false);
});

it('should check multiple classes', function () {
Expand All @@ -573,9 +573,9 @@ describe('$(...)', function () {
});

it('(empty string argument) : should return false', function () {
expect(test('foo').hasClass('')).toBe(false);
expect(test('foo bar').hasClass('')).toBe(false);
expect(test('foo bar').removeClass('foo').hasClass('')).toBe(false);
expect(withClass('foo').hasClass('')).toBe(false);
expect(withClass('foo bar').hasClass('')).toBe(false);
expect(withClass('foo bar').removeClass('foo').hasClass('')).toBe(false);
});
});

Expand Down
39 changes: 9 additions & 30 deletions test/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,9 @@ describe('$(...)', function () {

describe('.load', function () {
it('should throw a TypeError if given invalid input', function () {
try {
(function () {
cheerio.load();
})();

throw new Error('Function did not throw');
} catch (err) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toBe('cheerio.load() expects a string');
}
expect(function () {
cheerio.load();
}).toThrow('cheerio.load() expects a string');
});
});

Expand Down Expand Up @@ -81,16 +74,9 @@ describe('$(...)', function () {
});

it('should throw an Error if given an invalid selector', function () {
try {
(function () {
$('#fruits').find(':bah');
})();

throw new Error('Function did not throw');
} catch (err) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toContain('unmatched pseudo-class');
}
expect(function () {
$('#fruits').find(':bah');
}).toThrow('unmatched pseudo-class');
});

describe('(cheerio object) :', function () {
Expand Down Expand Up @@ -464,16 +450,9 @@ describe('$(...)', function () {
});

it('(selector) : should throw an Error if given an invalid selector', function () {
try {
(function () {
$('.orange').siblings(':bah');
})();

throw new Error('Function did not throw');
} catch (err) {
expect(err).toBeInstanceOf(Error);
expect(err.message).toContain('unmatched pseudo-class');
}
expect(function () {
$('.orange').siblings(':bah');
}).toThrow('unmatched pseudo-class');
});

it('(selector) : does not consider the contents of siblings when filtering (GH-374)', function () {
Expand Down
13 changes: 7 additions & 6 deletions test/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,16 @@ describe('cheerio', function () {
expect($.html()).toBe('<body><b>foo</b> <b>bar</b></body>');
});

// TODO:
// it('(html) : should handle xml tag option', function() {
// var $ = $.load('<body><script>oh hai</script></body>', { xml : true });
// console.log($('script')[0].type);
// expect($('script')[0].type).to.be('tag');
// });
it('(html) : should handle xml tag option', function () {
var $ = cheerio.load('<body><script><foo></script></body>', {
xml: true,
});
expect($('script')[0].children[0].type).toBe('tag');
});

it('(buffer) : should accept a buffer', function () {
var html = '<html><head></head><body>foo</body></html>';
// eslint-disable-next-line node/no-unsupported-features/node-builtins
var $html = cheerio.load(Buffer.from(html));
expect($html.html()).toBe(html);
});
Expand Down
2 changes: 1 addition & 1 deletion test/xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('render', function () {
});

describe('(dom)', function () {
it('should keep camelCase for new nodes', function () {
it('should not keep camelCase for new nodes', function () {
var str = '<g><someElem someAttribute="something">hello</someElem></g>';
expect(dom(str, { xml: false })).toBe(
'<someelem someattribute="something">hello</someelem>'
Expand Down