Skip to content

Commit

Permalink
[Tests] add linting
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 27, 2019
1 parent 27527bb commit a00f057
Show file tree
Hide file tree
Showing 20 changed files with 153 additions and 82 deletions.
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# eslintignore

# TODO: fix
index.js
44 changes: 44 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -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"] }],
},
},
],
}
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 8 additions & 4 deletions example/all.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
'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: {
regex: /^x/i,
buf: Buffer.from('abc'),
holes: holes
},
now: new Date
now: new Date()
};
obj.self = obj;
console.log(inspect(obj));
4 changes: 3 additions & 1 deletion example/circular.js
Original file line number Diff line number Diff line change
@@ -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));
4 changes: 3 additions & 1 deletion example/fn.js
Original file line number Diff line number Diff line change
@@ -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));
5 changes: 4 additions & 1 deletion example/inspect.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
'use strict';

/* eslint-env browser */
var inspect = require('../');

var d = document.createElement('div');
d.setAttribute('id', 'beep');
d.innerHTML = '<b>wooo</b><i>iiiii</i>';

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]]]] }]));
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
39 changes: 20 additions & 19 deletions test/bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
6 changes: 3 additions & 3 deletions test/browser/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<b>wooo</b><i>iiiii</i>';

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]]]] }]),
'[ <div id="beep">...</div>, { a: 3, b: 4, c: [ 5, 6, [ 7, [ 8, [Object] ] ] ] } ]'
);
});
2 changes: 1 addition & 1 deletion test/circular.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] }');
});
2 changes: 1 addition & 1 deletion test/deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] ] ]');
});
24 changes: 12 additions & 12 deletions test/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@ 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, <div class="row"></div>, 3 ]');
t.deepEqual(inspect(obj, { quoteStyle: 'single' }), "[ 1, <div class='row'></div>, 3 ]");
t.deepEqual(inspect(obj, { quoteStyle: 'double' }), '[ 1, <div class="row"></div>, 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, <div></div>, 3 ]');
});

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, <div>...</div>, 3 ]');
});

Expand All @@ -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, <div></div>, 3 ]');
global.HTMLElement = h;
});
18 changes: 10 additions & 8 deletions test/err.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]',
Expand Down
12 changes: 6 additions & 6 deletions test/fn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 5 additions & 2 deletions test/has.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
});
2 changes: 1 addition & 1 deletion test/holes.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion test/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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¡, [] ]');
});
2 changes: 1 addition & 1 deletion test/undef.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
Loading

0 comments on commit a00f057

Please sign in to comment.