From a00f057d917f66ea26dd37769c6b810ec4af97e8 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sat, 27 Jul 2019 13:48:38 -0700 Subject: [PATCH] [Tests] add linting --- .eslintignore | 4 ++++ .eslintrc | 44 ++++++++++++++++++++++++++++++++++++++++++++ .travis.yml | 2 ++ example/all.js | 12 ++++++++---- example/circular.js | 4 +++- example/fn.js | 4 +++- example/inspect.js | 5 ++++- package.json | 4 ++++ test/bigint.js | 39 ++++++++++++++++++++------------------- test/browser/dom.js | 6 +++--- test/circular.js | 2 +- test/deep.js | 2 +- test/element.js | 24 ++++++++++++------------ test/err.js | 18 ++++++++++-------- test/fn.js | 12 ++++++------ test/has.js | 7 +++++-- test/holes.js | 2 +- test/inspect.js | 2 +- test/undef.js | 2 +- test/values.js | 40 ++++++++++++++++++++-------------------- 20 files changed, 153 insertions(+), 82 deletions(-) create mode 100644 .eslintignore create mode 100644 .eslintrc diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..716f69a --- /dev/null +++ b/.eslintignore @@ -0,0 +1,4 @@ +# eslintignore + +# TODO: fix +index.js diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..7107008 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,44 @@ +{ + "root": true, + "extends": "@ljharb", + "rules": { + "indent": [2, 4], + "strict": 1, + "no-octal-escape": 1, + }, + "globals": { + "BigInt": false, + }, + "overrides": [ + { + "files": ["test/**", "test-*", "example/**"], + "rules": { + "array-bracket-newline": 0, + "max-params": 0, + "max-statements": 0, + "max-statements-per-line": 0, + "no-magic-numbers": 0, + "object-curly-newline": 0, + "sort-keys": 0, + }, + }, + { + "files": ["example/**"], + "rules": { + "no-console": 0, + }, + }, + { + "files": ["test/browser/**"], + "env": { + "browser": true, + }, + }, + { + "files": ["test/bigint*"], + "rules": { + "new-cap": [2, { "capIsNewExceptions": ["BigInt"] }], + }, + }, + ], +} diff --git a/.travis.yml b/.travis.yml index f17c43b..775081d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -35,6 +35,8 @@ matrix: include: - node_js: "10.0" env: BIGINT=true + - node_js: "lts/*" + env: PRETEST=true - node_js: "node" env: COVERAGE=true - node_js: "9.10" diff --git a/example/all.js b/example/all.js index a2c4d60..2f3355c 100644 --- a/example/all.js +++ b/example/all.js @@ -1,11 +1,15 @@ +'use strict'; + var inspect = require('../'); var Buffer = require('safer-buffer').Buffer; -var holes = [ 'a', 'b' ]; -holes[4] = 'e', holes[6] = 'g'; +var holes = ['a', 'b']; +holes[4] = 'e'; +holes[6] = 'g'; + var obj = { a: 1, - b: [ 3, 4, undefined, null ], + b: [3, 4, undefined, null], c: undefined, d: null, e: { @@ -13,7 +17,7 @@ var obj = { buf: Buffer.from('abc'), holes: holes }, - now: new Date + now: new Date() }; obj.self = obj; console.log(inspect(obj)); diff --git a/example/circular.js b/example/circular.js index 1006d0c..487a7c1 100644 --- a/example/circular.js +++ b/example/circular.js @@ -1,4 +1,6 @@ +'use strict'; + var inspect = require('../'); -var obj = { a: 1, b: [3,4] }; +var obj = { a: 1, b: [3, 4] }; obj.c = obj; console.log(inspect(obj)); diff --git a/example/fn.js b/example/fn.js index 4c00ba6..9b5db8d 100644 --- a/example/fn.js +++ b/example/fn.js @@ -1,3 +1,5 @@ +'use strict'; + var inspect = require('../'); -var obj = [ 1, 2, function f (n) { return n + 5 }, 4 ]; +var obj = [1, 2, function f(n) { return n + 5; }, 4]; console.log(inspect(obj)); diff --git a/example/inspect.js b/example/inspect.js index b5ad4d1..e2df7c9 100644 --- a/example/inspect.js +++ b/example/inspect.js @@ -1,7 +1,10 @@ +'use strict'; + +/* eslint-env browser */ var inspect = require('../'); var d = document.createElement('div'); d.setAttribute('id', 'beep'); d.innerHTML = 'woooiiiii'; -console.log(inspect([ d, { a: 3, b : 4, c: [5,6,[7,[8,[9]]]] } ])); +console.log(inspect([d, { a: 3, b: 4, c: [5, 6, [7, [8, [9]]]] }])); diff --git a/package.json b/package.json index a859ce9..89ec64a 100644 --- a/package.json +++ b/package.json @@ -4,11 +4,15 @@ "description": "string representations of objects in node and the browser", "main": "index.js", "devDependencies": { + "@ljharb/eslint-config": "^13.1.1", "core-js": "^2.5.5", + "eslint": "^5.16.0", "nyc": "^10.3.2", "tape": "^4.9.0" }, "scripts": { + "pretest": "npm run lint", + "lint": "eslint .", "test": "npm run tests-only", "posttest": "npm run test:bigint", "pretests-only": "node test-core-js", diff --git a/test/bigint.js b/test/bigint.js index c9d918b..3259143 100644 --- a/test/bigint.js +++ b/test/bigint.js @@ -2,29 +2,30 @@ var inspect = require('../'); var test = require('tape'); test('bigint', { skip: typeof BigInt === 'undefined' }, function (t) { - t.test('primitives', function (st) { - st.plan(3); + t.test('primitives', function (st) { + st.plan(3); - st.equal(inspect(BigInt(-256)), '-256n'); - st.equal(inspect(BigInt(0)), '0n'); - st.equal(inspect(BigInt(256)), '256n'); - }); + st.equal(inspect(BigInt(-256)), '-256n'); + st.equal(inspect(BigInt(0)), '0n'); + st.equal(inspect(BigInt(256)), '256n'); + }); - t.test('objects', function (st) { - st.plan(3); + t.test('objects', function (st) { + st.plan(3); - st.equal(inspect(Object(BigInt(-256))), 'Object(-256n)'); - st.equal(inspect(Object(BigInt(0))), 'Object(0n)'); - st.equal(inspect(Object(BigInt(256))), 'Object(256n)'); - }); + st.equal(inspect(Object(BigInt(-256))), 'Object(-256n)'); + st.equal(inspect(Object(BigInt(0))), 'Object(0n)'); + st.equal(inspect(Object(BigInt(256))), 'Object(256n)'); + }); - t.test('syntactic primitives', function (st) { - st.plan(3); + t.test('syntactic primitives', function (st) { + st.plan(3); - st.equal(inspect(Function('return -256n')()), '-256n'); - st.equal(inspect(Function('return 0n')()), '0n'); - st.equal(inspect(Function('return 256n')()), '256n'); - }); + /* eslint-disable no-new-func */ + st.equal(inspect(Function('return -256n')()), '-256n'); + st.equal(inspect(Function('return 0n')()), '0n'); + st.equal(inspect(Function('return 256n')()), '256n'); + }); - t.end(); + t.end(); }); diff --git a/test/browser/dom.js b/test/browser/dom.js index 18a3d70..210c0b2 100644 --- a/test/browser/dom.js +++ b/test/browser/dom.js @@ -3,13 +3,13 @@ var test = require('tape'); test('dom element', function (t) { t.plan(1); - + var d = document.createElement('div'); d.setAttribute('id', 'beep'); d.innerHTML = 'woooiiiii'; - + t.equal( - inspect([ d, { a: 3, b : 4, c: [5,6,[7,[8,[9]]]] } ]), + inspect([d, { a: 3, b: 4, c: [5, 6, [7, [8, [9]]]] }]), '[
...
, { a: 3, b: 4, c: [ 5, 6, [ 7, [ 8, [Object] ] ] ] } ]' ); }); diff --git a/test/circular.js b/test/circular.js index 28598a7..e864cfe 100644 --- a/test/circular.js +++ b/test/circular.js @@ -3,7 +3,7 @@ var test = require('tape'); test('circular', function (t) { t.plan(1); - var obj = { a: 1, b: [3,4] }; + var obj = { a: 1, b: [3, 4] }; obj.c = obj; t.equal(inspect(obj), '{ a: 1, b: [ 3, 4 ], c: [Circular] }'); }); diff --git a/test/deep.js b/test/deep.js index a8dbb58..5c443f2 100644 --- a/test/deep.js +++ b/test/deep.js @@ -3,7 +3,7 @@ var test = require('tape'); test('deep', function (t) { t.plan(2); - var obj = [ [ [ [ [ [ 500 ] ] ] ] ] ]; + var obj = [[[[[[500]]]]]]; t.equal(inspect(obj), '[ [ [ [ [ [Object] ] ] ] ] ]'); t.equal(inspect(obj, { depth: 2 }), '[ [ [Object] ] ]'); }); diff --git a/test/element.js b/test/element.js index a36f465..47fa9e2 100644 --- a/test/element.js +++ b/test/element.js @@ -5,24 +5,24 @@ test('element', function (t) { t.plan(3); var elem = { nodeName: 'div', - attributes: [ { name: 'class', value: 'row' } ], - getAttribute: function (key) {}, + attributes: [{ name: 'class', value: 'row' }], + getAttribute: function (key) { return key; }, childNodes: [] }; - var obj = [ 1, elem, 3 ]; + var obj = [1, elem, 3]; t.deepEqual(inspect(obj), '[ 1,
, 3 ]'); t.deepEqual(inspect(obj, { quoteStyle: 'single' }), "[ 1,
, 3 ]"); t.deepEqual(inspect(obj, { quoteStyle: 'double' }), '[ 1,
, 3 ]'); }); - + test('element no attr', function (t) { t.plan(1); var elem = { nodeName: 'div', - getAttribute: function (key) {}, + getAttribute: function (key) { return key; }, childNodes: [] }; - var obj = [ 1, elem, 3 ]; + var obj = [1, elem, 3]; t.deepEqual(inspect(obj), '[ 1,
, 3 ]'); }); @@ -30,10 +30,10 @@ test('element with contents', function (t) { t.plan(1); var elem = { nodeName: 'div', - getAttribute: function (key) {}, - childNodes: [ { nodeName: 'b' } ] + getAttribute: function (key) { return key; }, + childNodes: [{ nodeName: 'b' }] }; - var obj = [ 1, elem, 3 ]; + var obj = [1, elem, 3]; t.deepEqual(inspect(obj), '[ 1,
...
, 3 ]'); }); @@ -45,9 +45,9 @@ test('element instance', function (t) { this.attributes = attr; }; global.HTMLElement.prototype.getAttribute = function () {}; - - var elem = new(global.HTMLElement)('div', []); - var obj = [ 1, elem, 3 ]; + + var elem = new global.HTMLElement('div', []); + var obj = [1, elem, 3]; t.deepEqual(inspect(obj), '[ 1,
, 3 ]'); global.HTMLElement = h; }); diff --git a/test/err.js b/test/err.js index 0f31343..db96338 100644 --- a/test/err.js +++ b/test/err.js @@ -3,21 +3,23 @@ var test = require('tape'); test('type error', function (t) { t.plan(1); - var aerr = new TypeError; + var aerr = new TypeError(); aerr.foo = 555; - aerr.bar = [1,2,3]; - + aerr.bar = [1, 2, 3]; + var berr = new TypeError('tuv'); berr.baz = 555; - - var cerr = new SyntaxError; + + var cerr = new SyntaxError(); cerr.message = 'whoa'; cerr['a-b'] = 5; - + var obj = [ - new TypeError, + new TypeError(), new TypeError('xxx'), - aerr, berr, cerr + aerr, + berr, + cerr ]; t.equal(inspect(obj), '[ ' + [ '[TypeError]', diff --git a/test/fn.js b/test/fn.js index c7d5712..fd644be 100644 --- a/test/fn.js +++ b/test/fn.js @@ -3,25 +3,25 @@ var test = require('tape'); test('function', function (t) { t.plan(1); - var obj = [ 1, 2, function f (n) {}, 4 ]; + var obj = [1, 2, function f(n) { return n; }, 4]; t.equal(inspect(obj), '[ 1, 2, [Function: f], 4 ]'); }); test('function name', function (t) { t.plan(1); var f = (function () { - return function () {}; + return function () {}; }()); - f.toString = function () { return 'function xxx () {}' }; - var obj = [ 1, 2, f, 4 ]; + f.toString = function () { return 'function xxx () {}'; }; + var obj = [1, 2, f, 4]; t.equal(inspect(obj), '[ 1, 2, [Function: xxx], 4 ]'); }); test('anon function', function (t) { var f = (function () { - return function () {}; + return function () {}; }()); - var obj = [ 1, 2, f, 4 ]; + var obj = [1, 2, f, 4]; t.equal(inspect(obj), '[ 1, 2, [Function], 4 ]'); t.end(); diff --git a/test/has.js b/test/has.js index e1970b3..9f624df 100644 --- a/test/has.js +++ b/test/has.js @@ -22,10 +22,13 @@ var withoutProperty = function (object, property, fn) { test('when Object#hasOwnProperty is deleted', function (t) { t.plan(1); - var arr = [1, , 3]; + var arr = [1, , 3]; // eslint-disable-line no-sparse-arrays + + // eslint-disable-next-line no-extend-native Array.prototype[1] = 2; // this is needed to account for "in" vs "hasOwnProperty" + withoutProperty(Object.prototype, 'hasOwnProperty', function () { t.equal(inspect(arr), '[ 1, , 3 ]'); }); - delete Array.prototype[1]; + delete Array.prototype[1]; }); diff --git a/test/holes.js b/test/holes.js index ae54de4..87fc8c8 100644 --- a/test/holes.js +++ b/test/holes.js @@ -1,7 +1,7 @@ var test = require('tape'); var inspect = require('../'); -var xs = [ 'a', 'b' ]; +var xs = ['a', 'b']; xs[5] = 'f'; xs[7] = 'j'; xs[8] = 'k'; diff --git a/test/inspect.js b/test/inspect.js index 12e231a..bf7e35d 100644 --- a/test/inspect.js +++ b/test/inspect.js @@ -3,6 +3,6 @@ var test = require('tape'); test('inspect', function (t) { t.plan(1); - var obj = [ { inspect: function () { return '!XYZ¡' } }, [] ]; + var obj = [{ inspect: function () { return '!XYZ¡'; } }, []]; t.equal(inspect(obj), '[ !XYZ¡, [] ]'); }); diff --git a/test/undef.js b/test/undef.js index 833238f..e3f4961 100644 --- a/test/undef.js +++ b/test/undef.js @@ -1,7 +1,7 @@ var test = require('tape'); var inspect = require('../'); -var obj = { a: 1, b: [ 3, 4, undefined, null ], c: undefined, d: null }; +var obj = { a: 1, b: [3, 4, undefined, null], c: undefined, d: null }; test('undef and null', function (t) { t.plan(1); diff --git a/test/values.js b/test/values.js index 3e1954b..02b0f5b 100644 --- a/test/values.js +++ b/test/values.js @@ -3,18 +3,18 @@ var test = require('tape'); test('values', function (t) { t.plan(1); - var obj = [ {}, [], { 'a-b': 5 } ]; + var obj = [{}, [], { 'a-b': 5 }]; t.equal(inspect(obj), '[ {}, [], { \'a-b\': 5 } ]'); }); test('arrays with properties', function (t) { - t.plan(1); - var arr = [3]; - arr.foo = 'bar'; - var obj = [1, 2, arr]; - obj.baz = 'quux'; - obj.index = -1; - t.equal(inspect(obj), '[ 1, 2, [ 3, foo: \'bar\' ], baz: \'quux\', index: -1 ]'); + t.plan(1); + var arr = [3]; + arr.foo = 'bar'; + var obj = [1, 2, arr]; + obj.baz = 'quux'; + obj.index = -1; + t.equal(inspect(obj), '[ 1, 2, [ 3, foo: \'bar\' ], baz: \'quux\', index: -1 ]'); }); test('has', function (t) { @@ -22,17 +22,17 @@ test('has', function (t) { var has = Object.prototype.hasOwnProperty; delete Object.prototype.hasOwnProperty; t.equal(inspect({ a: 1, b: 2 }), '{ a: 1, b: 2 }'); - Object.prototype.hasOwnProperty = has; + Object.prototype.hasOwnProperty = has; // eslint-disable-line no-extend-native }); test('indexOf seen', function (t) { t.plan(1); - var xs = [ 1, 2, 3, {} ]; + var xs = [1, 2, 3, {}]; xs.push(xs); - + var seen = []; seen.indexOf = undefined; - + t.equal( inspect(xs, {}, 0, seen), '[ 1, 2, 3, {}, [Circular] ]' @@ -41,11 +41,11 @@ test('indexOf seen', function (t) { test('seen seen', function (t) { t.plan(1); - var xs = [ 1, 2, 3 ]; - - var seen = [ xs ]; + var xs = [1, 2, 3]; + + var seen = [xs]; seen.indexOf = undefined; - + t.equal( inspect(xs, {}, 0, seen), '[Circular]' @@ -54,11 +54,11 @@ test('seen seen', function (t) { test('seen seen seen', function (t) { t.plan(1); - var xs = [ 1, 2, 3 ]; - - var seen = [ 5, xs ]; + var xs = [1, 2, 3]; + + var seen = [5, xs]; seen.indexOf = undefined; - + t.equal( inspect(xs, {}, 0, seen), '[Circular]'