Switch to XO for linting

This commit is contained in:
Sindre Sorhus 2019-03-31 20:41:19 +07:00
parent 28913cae88
commit ea4204f0b4
4 changed files with 81 additions and 69 deletions

View file

@ -14,9 +14,8 @@
"node": ">=6" "node": ">=6"
}, },
"scripts": { "scripts": {
"lint": "tslint --format stylish --project .",
"build": "del dist && tsc", "build": "del dist && tsc",
"test": "npm run lint && ava", "test": "xo && ava",
"prepublishOnly": "npm run build" "prepublishOnly": "npm run build"
}, },
"files": [ "files": [
@ -47,20 +46,19 @@
"types" "types"
], ],
"devDependencies": { "devDependencies": {
"@sindresorhus/tsconfig": "^0.2.0", "@sindresorhus/tsconfig": "^0.3.0",
"@types/jsdom": "^11.12.0", "@types/jsdom": "^12.2.3",
"@types/node": "^10.12.21", "@types/node": "^11.12.2",
"@types/tempy": "^0.2.0", "@types/tempy": "^0.2.0",
"@types/zen-observable": "^0.8.0", "@types/zen-observable": "^0.8.0",
"ava": "^1.2.0", "@typescript-eslint/eslint-plugin": "^1.5.0",
"ava": "^1.4.1",
"del-cli": "^1.1.0", "del-cli": "^1.1.0",
"jsdom": "^11.6.2", "jsdom": "^11.6.2",
"rxjs": "^6.4.0", "rxjs": "^6.4.0",
"tempy": "^0.2.1", "tempy": "^0.2.1",
"ts-node": "^8.0.2", "ts-node": "^8.0.3",
"tslint": "^5.9.1", "typescript": "^3.4.1",
"tslint-xo": "^0.10.0",
"typescript": "^3.3.1",
"zen-observable": "^0.8.8" "zen-observable": "^0.8.8"
}, },
"types": "dist", "types": "dist",
@ -74,5 +72,14 @@
"require": [ "require": [
"ts-node/register" "ts-node/register"
] ]
},
"xo": {
"extends": "xo-typescript",
"extensions": [
"ts"
],
"rules": {
"@typescript-eslint/explicit-function-return-type": "off"
}
} }
} }

View file

@ -4,10 +4,10 @@
/// <reference lib="dom"/> /// <reference lib="dom"/>
// TODO: Use the `URL` global when targeting Node.js 10 // TODO: Use the `URL` global when targeting Node.js 10
// tslint:disable-next-line // eslint-disable-next-line @typescript-eslint/no-require-imports
const URLGlobal = typeof URL === 'undefined' ? require('url').URL : URL; const URLGlobal = typeof URL === 'undefined' ? require('url').URL : URL;
export type Class<T = unknown> = new(...args: any[]) => T; export type Class<T = unknown> = new (...args: any[]) => T;
export const enum TypeName { export const enum TypeName {
null = 'null', null = 'null',
@ -46,7 +46,7 @@ export const enum TypeName {
URL = 'URL' URL = 'URL'
} }
const toString = Object.prototype.toString; const {toString} = Object.prototype;
const isOfType = <T>(type: string) => (value: unknown): value is T => typeof value === type; const isOfType = <T>(type: string) => (value: unknown): value is T => typeof value === type;
const getObjectType = (value: unknown): TypeName | undefined => { const getObjectType = (value: unknown): TypeName | undefined => {
@ -55,12 +55,11 @@ const getObjectType = (value: unknown): TypeName | undefined => {
return objectName as TypeName; return objectName as TypeName;
} }
return; return undefined;
}; };
const isObjectOfType = <T>(type: TypeName) => (value: unknown): value is T => getObjectType(value) === type; const isObjectOfType = <T>(type: TypeName) => (value: unknown): value is T => getObjectType(value) === type;
// tslint:disable-next-line: no-use-before-declare
function is(value: unknown): TypeName { function is(value: unknown): TypeName {
switch (value) { switch (value) {
case null: case null:
@ -111,14 +110,15 @@ function is(value: unknown): TypeName {
return TypeName.Object; return TypeName.Object;
} }
// tslint:disable-next-line: strict-type-predicates
const isObject = (value: unknown): value is object => typeof value === 'object'; const isObject = (value: unknown): value is object => typeof value === 'object';
is.undefined = isOfType<undefined>('undefined'); is.undefined = isOfType<undefined>('undefined');
is.string = isOfType<string>('string'); is.string = isOfType<string>('string');
is.number = isOfType<number>('number'); is.number = isOfType<number>('number');
// eslint-disable-next-line @typescript-eslint/ban-types
is.function_ = isOfType<Function>('function'); is.function_ = isOfType<Function>('function');
// tslint:disable-next-line: strict-type-predicates
is.null_ = (value: unknown): value is null => value === null; is.null_ = (value: unknown): value is null => value === null;
is.class_ = (value: unknown): value is Class => is.function_(value) && value.toString().startsWith('class '); is.class_ = (value: unknown): value is Class => is.function_(value) && value.toString().startsWith('class ');
is.boolean = (value: unknown): value is boolean => value === true || value === false; is.boolean = (value: unknown): value is boolean => value === true || value === false;
@ -133,7 +133,10 @@ is.buffer = (value: unknown): value is Buffer => !is.nullOrUndefined(value) && !
is.nullOrUndefined = (value: unknown): value is null | undefined => is.null_(value) || is.undefined(value); is.nullOrUndefined = (value: unknown): value is null | undefined => is.null_(value) || is.undefined(value);
is.object = (value: unknown): value is object => !is.nullOrUndefined(value) && (is.function_(value) || isObject(value)); is.object = (value: unknown): value is object => !is.nullOrUndefined(value) && (is.function_(value) || isObject(value));
is.iterable = (value: unknown): value is IterableIterator<unknown> => !is.nullOrUndefined(value) && is.function_((value as IterableIterator<unknown>)[Symbol.iterator]); is.iterable = (value: unknown): value is IterableIterator<unknown> => !is.nullOrUndefined(value) && is.function_((value as IterableIterator<unknown>)[Symbol.iterator]);
// eslint-disable-next-line no-use-extend-native/no-use-extend-native
is.asyncIterable = (value: unknown): value is AsyncIterableIterator<unknown> => !is.nullOrUndefined(value) && is.function_((value as AsyncIterableIterator<unknown>)[Symbol.asyncIterator]); is.asyncIterable = (value: unknown): value is AsyncIterableIterator<unknown> => !is.nullOrUndefined(value) && is.function_((value as AsyncIterableIterator<unknown>)[Symbol.asyncIterator]);
is.generator = (value: unknown): value is Generator => is.iterable(value) && is.function_(value.next) && is.function_(value.throw); is.generator = (value: unknown): value is Generator => is.iterable(value) && is.function_(value.next) && is.function_(value.throw);
is.nativePromise = (value: unknown): value is Promise<unknown> => is.nativePromise = (value: unknown): value is Promise<unknown> =>
@ -148,7 +151,11 @@ const hasPromiseAPI = (value: unknown): value is Promise<unknown> =>
is.promise = (value: unknown): value is Promise<unknown> => is.nativePromise(value) || hasPromiseAPI(value); is.promise = (value: unknown): value is Promise<unknown> => is.nativePromise(value) || hasPromiseAPI(value);
is.generatorFunction = isObjectOfType<GeneratorFunction>(TypeName.GeneratorFunction); is.generatorFunction = isObjectOfType<GeneratorFunction>(TypeName.GeneratorFunction);
// eslint-disable-next-line @typescript-eslint/ban-types
is.asyncFunction = isObjectOfType<Function>(TypeName.AsyncFunction); is.asyncFunction = isObjectOfType<Function>(TypeName.AsyncFunction);
// eslint-disable-next-line no-prototype-builtins, @typescript-eslint/ban-types
is.boundFunction = (value: unknown): value is Function => is.function_(value) && !value.hasOwnProperty('prototype'); is.boundFunction = (value: unknown): value is Function => is.function_(value) && !value.hasOwnProperty('prototype');
is.regExp = isObjectOfType<RegExp>(TypeName.RegExp); is.regExp = isObjectOfType<RegExp>(TypeName.RegExp);
@ -182,7 +189,7 @@ is.urlString = (value: unknown): value is string => {
} }
try { try {
new URLGlobal(value); // tslint:disable-line no-unused-expression new URLGlobal(value); // eslint-disable-line no-new
return true; return true;
} catch { } catch {
return false; return false;
@ -215,11 +222,13 @@ is.safeInteger = (value: unknown): value is number => Number.isSafeInteger(value
is.plainObject = (value: unknown): value is {[key: string]: unknown} => { is.plainObject = (value: unknown): value is {[key: string]: unknown} => {
// From: https://github.com/sindresorhus/is-plain-obj/blob/master/index.js // From: https://github.com/sindresorhus/is-plain-obj/blob/master/index.js
let prototype; if (getObjectType(value) !== TypeName.Object) {
return false;
}
return getObjectType(value) === TypeName.Object && const prototype = Object.getPrototypeOf(value);
(prototype = Object.getPrototypeOf(value), prototype === null || // tslint:disable-line:ban-comma-operator
prototype === Object.getPrototypeOf({})); return prototype === null || prototype === Object.getPrototypeOf({});
}; };
const typedArrayTypes = new Set([ const typedArrayTypes = new Set([
@ -250,7 +259,7 @@ export interface ArrayLike<T> {
readonly [index: number]: T; readonly [index: number]: T;
} }
const isValidLength = (value: unknown) => is.safeInteger(value) && value >= 0; const isValidLength = (value: unknown): value is number => is.safeInteger(value) && value >= 0;
is.arrayLike = (value: unknown): value is ArrayLike<unknown> => !is.nullOrUndefined(value) && !is.function_(value) && isValidLength((value as ArrayLike<unknown>).length); is.arrayLike = (value: unknown): value is ArrayLike<unknown> => !is.nullOrUndefined(value) && !is.function_(value) && isValidLength((value as ArrayLike<unknown>).length);
is.inRange = (value: number, range: number | number[]): value is number => { is.inRange = (value: number, range: number | number[]): value is number => {
@ -287,6 +296,7 @@ is.observable = (value: unknown): value is ObservableLike => {
return false; return false;
} }
// eslint-disable-next-line no-use-extend-native/no-use-extend-native
if ((value as any)[Symbol.observable] && value === (value as any)[Symbol.observable]()) { if ((value as any)[Symbol.observable] && value === (value as any)[Symbol.observable]()) {
return true; return true;
} }
@ -298,13 +308,14 @@ is.observable = (value: unknown): value is ObservableLike => {
return false; return false;
}; };
// eslint-disable-next-line @typescript-eslint/ban-types
export type NodeStream = object & {readonly pipe: Function}; export type NodeStream = object & {readonly pipe: Function};
is.nodeStream = (value: unknown): value is NodeStream => !is.nullOrUndefined(value) && isObject(value) as unknown && is.function_((value as NodeStream).pipe) && !is.observable(value); is.nodeStream = (value: unknown): value is NodeStream => !is.nullOrUndefined(value) && isObject(value) as unknown && is.function_((value as NodeStream).pipe) && !is.observable(value);
is.infinite = (value: unknown): value is number => value === Infinity || value === -Infinity; is.infinite = (value: unknown): value is number => value === Infinity || value === -Infinity;
const isAbsoluteMod2 = (rem: number) => (value: number): value is number => is.integer(value) && Math.abs(value % 2) === rem; const isAbsoluteMod2 = (remainder: number) => (value: number): value is number => is.integer(value) && Math.abs(value % 2) === remainder;
is.evenInteger = isAbsoluteMod2(0); is.evenInteger = isAbsoluteMod2(0);
is.oddInteger = isAbsoluteMod2(1); is.oddInteger = isAbsoluteMod2(1);
@ -316,7 +327,7 @@ is.emptyString = (value: unknown): value is '' => is.string(value) && value.leng
// TODO: Use `not ''` when the `not` operator is available. // TODO: Use `not ''` when the `not` operator is available.
is.nonEmptyString = (value: unknown): value is string => is.string(value) && value.length > 0; is.nonEmptyString = (value: unknown): value is string => is.string(value) && value.length > 0;
const isWhiteSpaceString = (value: unknown) => is.string(value) && /\S/.test(value) === false; const isWhiteSpaceString = (value: unknown): value is string => is.string(value) && /\S/.test(value) === false;
is.emptyStringOrWhitespace = (value: unknown): value is string => is.emptyString(value) || isWhiteSpaceString(value); is.emptyStringOrWhitespace = (value: unknown): value is string => is.emptyString(value) || isWhiteSpaceString(value);
is.emptyObject = (value: unknown): value is {[key: string]: never} => is.object(value) && !is.map(value) && !is.set(value) && Object.keys(value).length === 0; is.emptyObject = (value: unknown): value is {[key: string]: never} => is.object(value) && !is.map(value) && !is.set(value) && Object.keys(value).length === 0;
@ -347,10 +358,8 @@ const predicateOnArray = (method: ArrayMethod, predicate: Predicate, values: unk
return method.call(values, predicate); return method.call(values, predicate);
}; };
// tslint:disable variable-name
is.any = (predicate: Predicate, ...values: unknown[]): boolean => predicateOnArray(Array.prototype.some, predicate, values); is.any = (predicate: Predicate, ...values: unknown[]): boolean => predicateOnArray(Array.prototype.some, predicate, values);
is.all = (predicate: Predicate, ...values: unknown[]): boolean => predicateOnArray(Array.prototype.every, predicate, values); is.all = (predicate: Predicate, ...values: unknown[]): boolean => predicateOnArray(Array.prototype.every, predicate, values);
// tslint:enable variable-name
// Some few keywords are reserved, but we'll populate them for Node.js users // Some few keywords are reserved, but we'll populate them for Node.js users
// See https://github.com/Microsoft/TypeScript/issues/2536 // See https://github.com/Microsoft/TypeScript/issues/2536

View file

@ -2,8 +2,8 @@ import fs from 'fs';
import net from 'net'; import net from 'net';
import Stream from 'stream'; import Stream from 'stream';
import util from 'util'; import util from 'util';
import tempy from 'tempy';
import {URL} from 'url'; import {URL} from 'url';
import tempy from 'tempy';
import test, {ExecutionContext} from 'ava'; import test, {ExecutionContext} from 'ava';
import {JSDOM} from 'jsdom'; import {JSDOM} from 'jsdom';
import {Subject, Observable} from 'rxjs'; import {Subject, Observable} from 'rxjs';
@ -88,26 +88,24 @@ const types = new Map<string, Test>([
is: is.array, is: is.array,
fixtures: [ fixtures: [
[1, 2], [1, 2],
new Array(2) // tslint:disable-line:prefer-array-literal new Array(2)
] ]
}], }],
['emptyArray', { ['emptyArray', {
is: is.emptyArray, is: is.emptyArray,
fixtures: [ fixtures: [
[], [],
new Array() new Array() // eslint-disable-line @typescript-eslint/no-array-constructor
] ]
}], }],
['function', { ['function', {
is: is.function_, is: is.function_,
fixtures: [ fixtures: [
// tslint:disable:no-unused no-empty no-unused-variable only-arrow-functions no-function-expression function foo() {}, // eslint-disable-line func-names
function foo() {},
function () {}, function () {},
() => {}, () => {},
async function () {}, async function () {},
function * (): unknown {} function * (): unknown {}
// tslint:enable:no-unused no-empty no-unused-variable only-arrow-functions no-function-expression
] ]
}], }],
['buffer', { ['buffer', {
@ -153,7 +151,7 @@ const types = new Map<string, Test>([
['promise', { ['promise', {
is: is.promise, is: is.promise,
fixtures: [ fixtures: [
{then() {}, catch() {}} // tslint:disable-line:no-empty {then() {}, catch() {}}
] ]
}], }],
['generator', { ['generator', {
@ -175,27 +173,27 @@ const types = new Map<string, Test>([
['asyncFunction', { ['asyncFunction', {
is: is.asyncFunction, is: is.asyncFunction,
fixtures: [ fixtures: [
async function () {}, // tslint:disable-line:no-empty only-arrow-functions no-function-expression async function () {},
async () => {} // tslint:disable-line:no-empty async () => {}
] ]
}], }],
['boundFunction', { ['boundFunction', {
is: is.boundFunction, is: is.boundFunction,
fixtures: [ fixtures: [
() => {}, // tslint:disable-line:no-empty () => {},
function () {}.bind(null), // tslint:disable-line:no-empty only-arrow-functions function () {}.bind(null) // eslint-disable-line no-extra-bind
] ]
}], }],
['map', { ['map', {
is: is.map, is: is.map,
fixtures: [ fixtures: [
new Map([['one', '1']]), new Map([['one', '1']])
] ]
}], }],
['emptyMap', { ['emptyMap', {
is: is.emptyMap, is: is.emptyMap,
fixtures: [ fixtures: [
new Map(), new Map()
] ]
}], }],
['set', { ['set', {
@ -207,7 +205,7 @@ const types = new Map<string, Test>([
['emptySet', { ['emptySet', {
is: is.emptySet, is: is.emptySet,
fixtures: [ fixtures: [
new Set(), new Set()
] ]
}], }],
['weakSet', { ['weakSet', {
@ -307,7 +305,7 @@ const types = new Map<string, Test>([
fixtures: [ fixtures: [
{x: 1}, {x: 1},
Object.create(null), Object.create(null),
new Object() new Object() // eslint-disable-line no-new-object
] ]
}], }],
['integer', { ['integer', {
@ -319,8 +317,8 @@ const types = new Map<string, Test>([
['safeInteger', { ['safeInteger', {
is: is.safeInteger, is: is.safeInteger,
fixtures: [ fixtures: [
Math.pow(2, 53) - 1, (2 ** 53) - 1,
-Math.pow(2, 53) + 1 -(2 ** 53) + 1
] ]
}], }],
['domElement', { ['domElement', {
@ -332,8 +330,8 @@ const types = new Map<string, Test>([
'img', 'img',
'canvas', 'canvas',
'script' 'script'
].map(createDomElement) } ].map(createDomElement)
], }],
['non-domElements', { ['non-domElements', {
is: value => !is.domElement(value), is: value => !is.domElement(value),
fixtures: [ fixtures: [
@ -350,7 +348,7 @@ const types = new Map<string, Test>([
fixtures: [ fixtures: [
new Observable(), new Observable(),
new Subject(), new Subject(),
new ZenObservable(() => {}) // tslint:disable-line:no-empty new ZenObservable(() => {})
] ]
}], }],
['nodeStream', { ['nodeStream', {
@ -449,7 +447,7 @@ test('is.function', t => {
}); });
test('is.boundFunction', t => { test('is.boundFunction', t => {
t.false(is.boundFunction(function () {})); // tslint:disable-line:no-empty only-arrow-functions t.false(is.boundFunction(function () {})); // eslint-disable-line prefer-arrow-callback
}); });
test('is.buffer', t => { test('is.buffer', t => {
@ -491,9 +489,11 @@ if (isNode8orHigher) {
testType(t, 'promise', ['nativePromise']); testType(t, 'promise', ['nativePromise']);
}); });
/*test('is.asyncFunction', t => { /*-
test('is.asyncFunction', t => {
testType(t, 'asyncFunction', ['function']); testType(t, 'asyncFunction', ['function']);
});*/ });
*/
} }
test('is.generator', t => { test('is.generator', t => {
@ -642,8 +642,8 @@ test('is.integer', t => {
test('is.safeInteger', t => { test('is.safeInteger', t => {
testType(t, 'safeInteger', ['number', 'integer']); testType(t, 'safeInteger', ['number', 'integer']);
t.false(is.safeInteger(Math.pow(2, 53))); t.false(is.safeInteger(2 ** 53));
t.false(is.safeInteger(-Math.pow(2, 53))); t.false(is.safeInteger(-(2 ** 53)));
}); });
test('is.plainObject', t => { test('is.plainObject', t => {
@ -665,7 +665,7 @@ test('is.iterable', t => {
if (isNode10orHigher) { if (isNode10orHigher) {
test('is.asyncIterable', t => { test('is.asyncIterable', t => {
t.true(is.asyncIterable({ t.true(is.asyncIterable({
[Symbol.asyncIterator]: () => {} // tslint:disable-line:no-empty [Symbol.asyncIterator]: () => {}
})); }));
t.false(is.asyncIterable(null)); t.false(is.asyncIterable(null));
@ -678,14 +678,15 @@ if (isNode10orHigher) {
} }
test('is.class', t => { test('is.class', t => {
class Foo {} // tslint:disable-line class Foo {} // eslint-disable-line @typescript-eslint/no-extraneous-class
const classDeclarations = [ const classDeclarations = [
Foo, Foo,
class Bar extends Foo {} // tslint:disable-line class Bar extends Foo {}
]; ];
for (const x of classDeclarations) { for (const classDeclaration of classDeclarations) {
t.true(is.class_(x)); t.true(is.class_(classDeclaration));
} }
}); });
@ -714,14 +715,15 @@ test('is.typedArray', t => {
}); });
test('is.arrayLike', t => { test('is.arrayLike', t => {
(function () { // tslint:disable-line:only-arrow-functions (function () {
t.true(is.arrayLike(arguments)); t.true(is.arrayLike(arguments)); // eslint-disable-line prefer-rest-params
})(); })();
t.true(is.arrayLike([])); t.true(is.arrayLike([]));
t.true(is.arrayLike('unicorn')); t.true(is.arrayLike('unicorn'));
t.false(is.arrayLike({})); t.false(is.arrayLike({}));
t.false(is.arrayLike(() => {})); // tslint:disable-line:no-empty t.false(is.arrayLike(() => {}));
t.false(is.arrayLike(new Map())); t.false(is.arrayLike(new Map()));
}); });
@ -800,7 +802,7 @@ test('is.emptyArray', t => {
test('is.nonEmptyArray', t => { test('is.nonEmptyArray', t => {
t.true(is.nonEmptyArray([1, 2, 3])); t.true(is.nonEmptyArray([1, 2, 3]));
t.false(is.nonEmptyArray([])); t.false(is.nonEmptyArray([]));
t.false(is.nonEmptyArray(new Array())); t.false(is.nonEmptyArray(new Array())); // eslint-disable-line @typescript-eslint/no-array-constructor
}); });
test('is.emptyString', t => { test('is.emptyString', t => {
@ -823,7 +825,7 @@ test('is.emptyStringOrWhitespace', t => {
test('is.emptyObject', t => { test('is.emptyObject', t => {
t.true(is.emptyObject({})); t.true(is.emptyObject({}));
t.true(is.emptyObject(new Object())); t.true(is.emptyObject(new Object())); // eslint-disable-line no-new-object
t.false(is.emptyObject({unicorn: '🦄'})); t.false(is.emptyObject({unicorn: '🦄'}));
}); });
@ -832,7 +834,7 @@ test('is.nonEmptyObject', t => {
is.nonEmptyObject(foo); is.nonEmptyObject(foo);
t.false(is.nonEmptyObject({})); t.false(is.nonEmptyObject({}));
t.false(is.nonEmptyObject(new Object())); t.false(is.nonEmptyObject(new Object())); // eslint-disable-line no-new-object
t.true(is.nonEmptyObject({unicorn: '🦄'})); t.true(is.nonEmptyObject({unicorn: '🦄'}));
}); });

View file

@ -1,6 +0,0 @@
{
"extends": "tslint-xo",
"rules": {
"interface-over-type-literal": false
}
}