Skip to content

Commit

Permalink
[Tests] lint last file
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 29, 2019
1 parent a8b5425 commit 2698047
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 70 deletions.
4 changes: 0 additions & 4 deletions .eslintignore

This file was deleted.

20 changes: 18 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
"root": true,
"extends": "@ljharb",
"rules": {
"complexity": 0,
"func-style": [2, 'declaration'],
"indent": [2, 4],
"strict": 1,
"max-lines-per-function": [2, 120],
"max-params": [2, 4],
"max-statements": [2, 80],
"max-statements-per-line": [2, { "max": 2 }],
"no-magic-numbers": 0,
"no-param-reassign": 1,
"strict": 0, // TODO
},
"globals": {
"BigInt": false,
Expand All @@ -16,7 +24,6 @@
"max-params": 0,
"max-statements": 0,
"max-statements-per-line": 0,
"no-magic-numbers": 0,
"object-curly-newline": 0,
"sort-keys": 0,
},
Expand All @@ -39,5 +46,14 @@
"new-cap": [2, { "capIsNewExceptions": ["BigInt"] }],
},
},
{
"files": "index.js",
"globals": {
"HTMLElement": false,
},
"rules": {
"no-use-before-define": 1,
},
},
],
}
128 changes: 66 additions & 62 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ var setSize = hasSet && setSizeDescriptor && typeof setSizeDescriptor.get === 'f
var setForEach = hasSet && Set.prototype.forEach;
var booleanValueOf = Boolean.prototype.valueOf;
var objectToString = Object.prototype.toString;
var match = String.prototype.match;
var bigIntValueOf = typeof BigInt === 'function' ? BigInt.prototype.valueOf : null;

var inspectCustom = require('./util.inspect').custom;
var inspectSymbol = (inspectCustom && isSymbol(inspectCustom)) ? inspectCustom : null;
var inspectSymbol = inspectCustom && isSymbol(inspectCustom) ? inspectCustom : null;

module.exports = function inspect_ (obj, opts, depth, seen) {
if (!opts) opts = {};
module.exports = function inspect_(obj, options, depth, seen) {
var opts = options || {};

if (has(opts, 'quoteStyle') && (opts.quoteStyle !== 'single' && opts.quoteStyle !== 'double')) {
throw new TypeError('option "quoteStyle" must be "single" or "double"');
Expand All @@ -34,27 +35,28 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
return inspectString(obj, opts);
}
if (typeof obj === 'number') {
if (obj === 0) {
return Infinity / obj > 0 ? '0' : '-0';
}
return String(obj);
if (obj === 0) {
return Infinity / obj > 0 ? '0' : '-0';
}
return String(obj);
}
if (typeof obj === 'bigint') {
return String(obj) + 'n';
if (typeof obj === 'bigint') { // eslint-disable-line valid-typeof
return String(obj) + 'n';
}

var maxDepth = typeof opts.depth === 'undefined' ? 5 : opts.depth;
if (typeof depth === 'undefined') depth = 0;
if (typeof depth === 'undefined') { depth = 0; }
if (depth >= maxDepth && maxDepth > 0 && typeof obj === 'object') {
return '[Object]';
}

if (typeof seen === 'undefined') seen = [];
else if (indexOf(seen, obj) >= 0) {
if (typeof seen === 'undefined') {
seen = [];
} else if (indexOf(seen, obj) >= 0) {
return '[Circular]';
}

function inspect (value, from) {
function inspect(value, from) {
if (from) {
seen = seen.slice();
seen.push(from);
Expand All @@ -77,17 +79,17 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
s += ' ' + attrs[i].name + '=' + wrapQuotes(quote(attrs[i].value), 'double', opts);
}
s += '>';
if (obj.childNodes && obj.childNodes.length) s += '...';
if (obj.childNodes && obj.childNodes.length) { s += '...'; }
s += '</' + String(obj.nodeName).toLowerCase() + '>';
return s;
}
if (isArray(obj)) {
if (obj.length === 0) return '[]';
if (obj.length === 0) { return '[]'; }
return '[ ' + arrObjKeys(obj, inspect).join(', ') + ' ]';
}
if (isError(obj)) {
var parts = arrObjKeys(obj, inspect);
if (parts.length === 0) return '[' + String(obj) + ']';
if (parts.length === 0) { return '[' + String(obj) + ']'; }
return '{ [' + String(obj) + '] ' + parts.join(', ') + ' }';
}
if (typeof obj === 'object') {
Expand All @@ -98,18 +100,18 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
}
}
if (isMap(obj)) {
var parts = [];
var mapParts = [];
mapForEach.call(obj, function (value, key) {
parts.push(inspect(key, obj) + ' => ' + inspect(value, obj));
mapParts.push(inspect(key, obj) + ' => ' + inspect(value, obj));
});
return collectionOf('Map', mapSize.call(obj), parts);
return collectionOf('Map', mapSize.call(obj), mapParts);
}
if (isSet(obj)) {
var parts = [];
setForEach.call(obj, function (value ) {
parts.push(inspect(value, obj));
var setParts = [];
setForEach.call(obj, function (value) {
setParts.push(inspect(value, obj));
});
return collectionOf('Set', setSize.call(obj), parts);
return collectionOf('Set', setSize.call(obj), setParts);
}
if (isNumber(obj)) {
return markBoxed(inspect(Number(obj)));
Expand All @@ -125,55 +127,56 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
}
if (!isDate(obj) && !isRegExp(obj)) {
var xs = arrObjKeys(obj, inspect);
if (xs.length === 0) return '{}';
if (xs.length === 0) { return '{}'; }
return '{ ' + xs.join(', ') + ' }';
}
return String(obj);
};

function wrapQuotes (s, defaultStyle, opts) {
function wrapQuotes(s, defaultStyle, opts) {
var quoteChar = (opts.quoteStyle || defaultStyle) === 'double' ? '"' : "'";
return quoteChar + s + quoteChar;
}

function quote (s) {
function quote(s) {
return String(s).replace(/"/g, '&quot;');
}

function isArray (obj) { return toStr(obj) === '[object Array]'; }
function isDate (obj) { return toStr(obj) === '[object Date]'; }
function isRegExp (obj) { return toStr(obj) === '[object RegExp]'; }
function isError (obj) { return toStr(obj) === '[object Error]'; }
function isSymbol (obj) { return toStr(obj) === '[object Symbol]'; }
function isString (obj) { return toStr(obj) === '[object String]'; }
function isNumber (obj) { return toStr(obj) === '[object Number]'; }
function isBigInt (obj) { return toStr(obj) === '[object BigInt]'; }
function isBoolean (obj) { return toStr(obj) === '[object Boolean]'; }
function isArray(obj) { return toStr(obj) === '[object Array]'; }
function isDate(obj) { return toStr(obj) === '[object Date]'; }
function isRegExp(obj) { return toStr(obj) === '[object RegExp]'; }
function isError(obj) { return toStr(obj) === '[object Error]'; }
function isSymbol(obj) { return toStr(obj) === '[object Symbol]'; }
function isString(obj) { return toStr(obj) === '[object String]'; }
function isNumber(obj) { return toStr(obj) === '[object Number]'; }
function isBigInt(obj) { return toStr(obj) === '[object BigInt]'; }
function isBoolean(obj) { return toStr(obj) === '[object Boolean]'; }

var hasOwn = Object.prototype.hasOwnProperty || function (key) { return key in this; };
function has (obj, key) {
function has(obj, key) {
return hasOwn.call(obj, key);
}

function toStr (obj) {
function toStr(obj) {
return objectToString.call(obj);
}

function nameOf (f) {
if (f.name) return f.name;
var m = String(f).match(/^function\s*([\w$]+)/);
if (m) return m[1];
function nameOf(f) {
if (f.name) { return f.name; }
var m = match.call(f, /^function\s*([\w$]+)/);
if (m) { return m[1]; }
return null;
}

function indexOf (xs, x) {
if (xs.indexOf) return xs.indexOf(x);
function indexOf(xs, x) {
if (xs.indexOf) { return xs.indexOf(x); }
for (var i = 0, l = xs.length; i < l; i++) {
if (xs[i] === x) return i;
if (xs[i] === x) { return i; }
}
return -1;
}

function isMap (x) {
function isMap(x) {
if (!mapSize) {
return false;
}
Expand All @@ -189,7 +192,7 @@ function isMap (x) {
return false;
}

function isSet (x) {
function isSet(x) {
if (!setSize) {
return false;
}
Expand All @@ -205,37 +208,38 @@ function isSet (x) {
return false;
}

function isElement (x) {
if (!x || typeof x !== 'object') return false;
function isElement(x) {
if (!x || typeof x !== 'object') { return false; }
if (typeof HTMLElement !== 'undefined' && x instanceof HTMLElement) {
return true;
}
return typeof x.nodeName === 'string'
&& typeof x.getAttribute === 'function'
;
return typeof x.nodeName === 'string' && typeof x.getAttribute === 'function';
}

function inspectString (str, opts) {
function inspectString(str, opts) {
// eslint-disable-next-line no-control-regex
var s = str.replace(/(['\\])/g, '\\$1').replace(/[\x00-\x1f]/g, lowbyte);
return wrapQuotes(s, 'single', opts);
}

function lowbyte (c) {
function lowbyte(c) {
var n = c.charCodeAt(0);
var x = { 8: 'b', 9: 't', 10: 'n', 12: 'f', 13: 'r' }[n];
if (x) return '\\' + x;
var x = {
8: 'b', 9: 't', 10: 'n', 12: 'f', 13: 'r'
}[n];
if (x) { return '\\' + x; }
return '\\x' + (n < 0x10 ? '0' : '') + n.toString(16);
}

function markBoxed (str) {
function markBoxed(str) {
return 'Object(' + str + ')';
}

function collectionOf (type, size, entries) {
function collectionOf(type, size, entries) {
return type + ' (' + size + ') {' + entries.join(', ') + '}';
}

function arrObjKeys (obj, inspect) {
function arrObjKeys(obj, inspect) {
var isArr = isArray(obj);
var xs = [];
if (isArr) {
Expand All @@ -244,10 +248,10 @@ function arrObjKeys (obj, inspect) {
xs[i] = has(obj, i) ? inspect(obj[i], obj) : '';
}
}
for (var key in obj) {
if (!has(obj, key)) continue;
if (isArr && String(Number(key)) === key && key < obj.length) continue;
if (/[^\w$]/.test(key)) {
for (var key in obj) { // eslint-disable-line no-restricted-syntax
if (!has(obj, key)) { continue; } // eslint-disable-line no-restricted-syntax, no-continue
if (isArr && String(Number(key)) === key && key < obj.length) { continue; } // eslint-disable-line no-restricted-syntax, no-continue
if ((/[^\w$]/).test(key)) {
xs.push(inspect(key, obj) + ': ' + inspect(obj[key], obj));
} else {
xs.push(key + ': ' + inspect(obj[key], obj));
Expand Down
4 changes: 2 additions & 2 deletions test/has.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var inspect = require('../');
var test = require('tape');

var withoutProperty = function (object, property, fn) {
function withoutProperty(object, property, fn) {
var original;
if (Object.getOwnPropertyDescriptor) {
original = Object.getOwnPropertyDescriptor(object, property);
Expand All @@ -18,7 +18,7 @@ var withoutProperty = function (object, property, fn) {
object[property] = original;
}
}
};
}

test('when Object#hasOwnProperty is deleted', function (t) {
t.plan(1);
Expand Down

0 comments on commit 2698047

Please sign in to comment.