Remove assert and util in lib code (#201)

Added a tslint rule so we don't accidentally import them again
This commit is contained in:
Steve Faulkner 2018-12-03 15:36:36 -08:00 коммит произвёл GitHub
Родитель 057c5910b5
Коммит 20693f8681
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
16 изменённых файлов: 135 добавлений и 106 удалений

78
package-lock.json сгенерированный
Просмотреть файл

@ -3170,6 +3170,24 @@
"requires": {
"is-extendable": "^0.1.0"
}
},
"is-number": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/is-number/-/is-number-3.0.0.tgz",
"integrity": "sha1-JP1iAaR4LPUFYcgQJ2r8fRLXEZU=",
"dev": true,
"requires": {
"kind-of": "^3.0.2"
}
},
"kind-of": {
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz",
"integrity": "sha1-MeohpzS6ubuw8yRm2JOupR5KPGQ=",
"dev": true,
"requires": {
"is-buffer": "^1.1.5"
}
}
}
},
@ -4256,6 +4274,26 @@
"kind-of": "^4.0.0"
},
"dependencies": {
"is-number": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/is-number/-/is-number-3.0.0.tgz",
"integrity": "sha1-JP1iAaR4LPUFYcgQJ2r8fRLXEZU=",
"dev": true,
"requires": {
"kind-of": "^3.0.2"
},
"dependencies": {
"kind-of": {
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz",
"integrity": "sha1-MeohpzS6ubuw8yRm2JOupR5KPGQ=",
"dev": true,
"requires": {
"is-buffer": "^1.1.5"
}
}
}
},
"kind-of": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/kind-of/-/kind-of-4.0.0.tgz",
@ -4657,26 +4695,6 @@
"is-extglob": "^2.1.1"
}
},
"is-number": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/is-number/-/is-number-3.0.0.tgz",
"integrity": "sha1-JP1iAaR4LPUFYcgQJ2r8fRLXEZU=",
"dev": true,
"requires": {
"kind-of": "^3.0.2"
},
"dependencies": {
"kind-of": {
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz",
"integrity": "sha1-MeohpzS6ubuw8yRm2JOupR5KPGQ=",
"dev": true,
"requires": {
"is-buffer": "^1.1.5"
}
}
}
},
"is-object": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/is-object/-/is-object-1.0.1.tgz",
@ -8119,6 +8137,26 @@
"requires": {
"is-number": "^3.0.0",
"repeat-string": "^1.6.1"
},
"dependencies": {
"is-number": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/is-number/-/is-number-3.0.0.tgz",
"integrity": "sha1-JP1iAaR4LPUFYcgQJ2r8fRLXEZU=",
"dev": true,
"requires": {
"kind-of": "^3.0.2"
}
},
"kind-of": {
"version": "3.2.2",
"resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz",
"integrity": "sha1-MeohpzS6ubuw8yRm2JOupR5KPGQ=",
"dev": true,
"requires": {
"is-buffer": "^1.1.5"
}
}
}
},
"trim-right": {

Просмотреть файл

@ -1,5 +1,4 @@
/// <reference lib="esnext.asynciterable" />
import { isNumber } from "util";
import { ChangeFeedOptions } from "./ChangeFeedOptions";
import { ChangeFeedResponse } from "./ChangeFeedResponse";
import { Resource } from "./client";
@ -99,7 +98,7 @@ export class ChangeFeedIterator<T> {
}
const feedOptions: FeedOptions = { initialHeaders: {}, a_im: "Incremental feed" };
if (isNumber(this.changeFeedOptions.maxItemCount)) {
if (typeof this.changeFeedOptions.maxItemCount === "number") {
feedOptions.maxItemCount = this.changeFeedOptions.maxItemCount;
}

Просмотреть файл

@ -11,11 +11,9 @@ import { Item } from "./Item";
import { ItemDefinition } from "./ItemDefinition";
import { ItemResponse } from "./ItemResponse";
import assert from "assert";
import { isBoolean, isNumber, isString } from "util";
function isChangeFeedOptions(options: unknown): options is ChangeFeedOptions {
return options && !(isString(options) || isBoolean(options) || isNumber(options));
const optionsType = typeof options;
return options && !(optionsType === "string" || optionsType === "boolean" || optionsType === "number");
}
/**

Просмотреть файл

@ -1,4 +1,3 @@
import assert from "assert";
import { FetchFunctionCallback, SqlQuerySpec } from ".";
import { ClientContext } from "../ClientContext";
import { Constants, Helper, StatusCodes, SubStatusCodes } from "../common";
@ -234,7 +233,9 @@ export class DocumentProducer {
const fetchResult = this.fetchResults.shift();
this._updateStates(undefined, result === undefined);
assert.equal(fetchResult.feedResponse, result);
if (fetchResult.feedResponse !== result) {
throw new Error(`Expected ${fetchResult.feedResponse} to equal ${result}`);
}
switch (fetchResult.fetchResultType) {
case FetchResultType.Done:
return { result: undefined, headers };

Просмотреть файл

@ -1,6 +1,4 @@
import assert from "assert";
import * as util from "util";
import { Constants } from "../common";
import { Constants } from "../common";
import { QueryMetrics } from "../queryMetrics";
export interface IHeaders {

Просмотреть файл

@ -1,6 +1,4 @@
import assert from "assert";
import * as util from "util";
import { DocumentProducer } from "./documentProducer";
import { DocumentProducer } from "./documentProducer";
// TODO: this smells funny
/** @hidden */
@ -93,7 +91,9 @@ export class OrderByDocumentProducerComparator {
}
const compFunc = TYPEORDCOMPARATOR[type1].compFunc;
assert.notEqual(compFunc, undefined, "cannot find the comparison function");
if (typeof compFunc === "undefined") {
throw new Error("Cannot find the comparison function");
}
// same type and type is defined compare the items
return compFunc(item1, item2);
}
@ -105,13 +105,13 @@ export class OrderByDocumentProducerComparator {
}
public validateOrderByItems(res1: string[], res2: string[]) {
this._throwIf(res1.length !== res2.length, util.format("Expected %s, but got %s.", res1.length, res2.length));
this._throwIf(res1.length !== res2.length, `Expected ${res1.length}, but got ${res2.length}.`);
this._throwIf(res1.length !== this.sortOrder.length, "orderByItems cannot have a different size than sort orders.");
for (let i = 0; i < this.sortOrder.length; i++) {
const type1 = this.getType(res1[i]);
const type2 = this.getType(res2[i]);
this._throwIf(type1 !== type2, util.format("Expected %s, but got %s.", type1, type2));
this._throwIf(type1 !== type2, `Expected ${type1}, but got ${type2}.`);
}
}
@ -121,7 +121,7 @@ export class OrderByDocumentProducerComparator {
return "NoValue";
}
const type = typeof orderByItem.item;
this._throwIf(TYPEORDCOMPARATOR[type] === undefined, util.format("unrecognizable type %s", type));
this._throwIf(TYPEORDCOMPARATOR[type] === undefined, `unrecognizable type ${type}`);
return type;
}

Просмотреть файл

@ -1,8 +1,6 @@
import assert from "assert";
import * as bs from "binary-search-bounds";
import * as bs from "binary-search-bounds";
import PriorityQueue from "priorityqueuejs";
import semaphore from "semaphore";
import * as util from "util";
import {
DocumentProducer,
HeaderUtils,
@ -421,10 +419,8 @@ export abstract class ParallelQueryExecutionContextBase implements IExecutionCon
// because the documentProducer already has buffered an item
// assert item !== undefined
this.err = new Error(
util.format(
`Extracted DocumentProducer from the priority queue \
`Extracted DocumentProducer from the priority queue \
doesn't have any buffered item!`
)
);
// release the lock before invoking callback
this.sem.leave();
@ -454,11 +450,9 @@ export abstract class ParallelQueryExecutionContextBase implements IExecutionCon
} else {
try {
const headItem = documentProducer.fetchResults[0];
assert.notStrictEqual(
headItem,
undefined,
"Extracted DocumentProducer from PQ is invalid state with no result!"
);
if (typeof headItem === "undefined") {
throw new Error("Extracted DocumentProducer from PQ is invalid state with no result!");
}
this.orderByPQ.enq(documentProducer);
} catch (e) {
// if comparing elements in priority queue throws exception

Просмотреть файл

@ -1,6 +1,3 @@
import assert from "assert";
import * as util from "util";
/** @hidden */
const PartitionedQueryContants = {
QueryInfoPath: "queryInfo",
@ -39,7 +36,9 @@ export class PartitionedQueryExecutionContextInfoParser {
if (typeof path === "string") {
return item[path];
}
assert.ok(Array.isArray(path), util.format("%s is expected to be an array", JSON.stringify(path)));
if (!Array.isArray(path)) {
throw new Error(`JSON.stringify(path is expected to be an array`);
}
for (const p of path) {
item = item[p];
if (item === undefined) {

Просмотреть файл

@ -1,4 +1,3 @@
import assert from "assert";
import {
DefaultQueryExecutionContext,
FetchFunctionCallback,
@ -63,11 +62,12 @@ export class ProxyQueryExecutionContext implements IExecutionContext {
}
private _createPipelinedExecutionContext(partitionedExecutionInfo: PartitionedQueryExecutionContextInfo) {
assert.notStrictEqual(this.resourceLink, undefined, "for top/orderby resourceLink is required.");
assert.ok(
!Array.isArray(this.resourceLink) || this.resourceLink.length === 1,
"for top/orderby exactly one collectionLink is required"
);
if (!this.resourceLink) {
throw new Error("for top/orderby resourceLink is required");
}
if (Array.isArray(this.resourceLink) && this.resourceLink.length !== 1) {
throw new Error("for top/orderby exactly one collectionLink is required");
}
const collectionLink = Array.isArray(this.resourceLink) ? this.resourceLink[0] : this.resourceLink;

Просмотреть файл

@ -1,4 +1,3 @@
import { isNumber } from "util";
import { TimeSpan } from "./timeSpan";
export class QueryMetricsUtils {

Просмотреть файл

@ -1,5 +1,3 @@
import { isNumber, isString } from "util";
export class LocationRouting {
private pIgnorePreferredLocation: boolean;
private pLocationIndexToRoute: number;
@ -19,11 +17,11 @@ export class LocationRouting {
public routeToLocation(locationEndpoint: string): void;
public routeToLocation(locationIndex: number, ignorePreferredLocation: boolean): void;
public routeToLocation(endpointOrIndex: string | number, ignorePreferredLocation?: boolean) {
if (arguments.length === 2 && isNumber(endpointOrIndex)) {
if (arguments.length === 2 && typeof endpointOrIndex === "number") {
this.pLocationIndexToRoute = endpointOrIndex;
this.pIgnorePreferredLocation = ignorePreferredLocation;
this.pLocationEndpointToRoute = undefined;
} else if (arguments.length === 1 && isString(endpointOrIndex)) {
} else if (arguments.length === 1 && typeof endpointOrIndex === "string") {
this.pLocationEndpointToRoute = endpointOrIndex;
this.pLocationIndexToRoute = undefined;
this.pIgnorePreferredLocation = undefined;

Просмотреть файл

@ -1,4 +1,3 @@
import assert from "assert";
import * as bs from "binary-search-bounds"; // TODO: missing types
import { Constants } from "../common";
import { Range } from "../range";
@ -63,7 +62,9 @@ export class InMemoryCollectionRoutingMap {
InMemoryCollectionRoutingMap._vbCompareFunction
);
// that's an error
assert.ok(index >= 0, "error in collection routing map, queried partition key is less than the start range.");
if (index < 0) {
throw new Error("error in collection routing map, queried partition key is less than the start range.");
}
return this.orderedPartitionKeyRanges[index];
}
@ -105,17 +106,19 @@ export class InMemoryCollectionRoutingMap {
{ v: queryRange.min, b: !queryRange.isMinInclusive },
InMemoryCollectionRoutingMap._vbCompareFunction
);
assert.ok(minIndex >= 0, "error in collection routing map, queried value is less than the start range.");
if (minIndex < 0) {
throw new Error("error in collection routing map, queried value is less than the start range.");
}
const maxIndex = bs.ge(
sortedHigh,
{ v: queryRange.max, b: queryRange.isMaxInclusive },
InMemoryCollectionRoutingMap._vbCompareFunction
);
assert.ok(
maxIndex < sortedHigh.length,
"error in collection routing map, queried value is greater than the end range."
);
if (maxIndex > sortedHigh.length) {
throw new Error("error in collection routing map, queried value is greater than the end range.");
}
// the for loop doesn't invoke any async callback
for (let j = minIndex; j < maxIndex + 1; j++) {

Просмотреть файл

@ -1,4 +1,3 @@
import assert from "assert";
import { PartitionKeyRangeCache, QueryRange } from ".";
import { ClientContext } from "../ClientContext";
import { Constants } from "../common";
@ -14,8 +13,13 @@ export class SmartRoutingMapProvider {
this.partitionKeyRangeCache = new PartitionKeyRangeCache(clientContext);
}
private static _secondRangeIsAfterFirstRange(range1: QueryRange, range2: QueryRange) {
assert.notEqual(range1.max, undefined, "invalid arg");
assert.notEqual(range2.min, undefined, "invalid arg");
if (typeof range1.max === "undefined") {
throw new Error("range1 must have max");
}
if (typeof range2.min === "undefined") {
throw new Error("range2 must have min");
}
if (range1.max > range2.min) {
// r.min < #previous_r.max
@ -100,20 +104,21 @@ export class SmartRoutingMapProvider {
}
const overlappingRanges = collectionRoutingMap.getOverlappingRanges(queryRange);
assert.ok(
overlappingRanges.length > 0,
`error: returned overlapping ranges for queryRange ${queryRange} is empty`
);
if (overlappingRanges.length <= 0) {
throw new Error(`error: returned overlapping ranges for queryRange ${queryRange} is empty`);
}
partitionKeyRanges = partitionKeyRanges.concat(overlappingRanges);
const lastKnownTargetRange = QueryRange.parsePartitionKeyRange(partitionKeyRanges[partitionKeyRanges.length - 1]);
assert.notEqual(lastKnownTargetRange, undefined);
if (!lastKnownTargetRange) {
throw new Error("expected lastKnowTargetRange to be truthy");
}
// the overlapping ranges must contain the requested range
assert.ok(
SmartRoutingMapProvider._stringCompare(currentProvidedRange.max, lastKnownTargetRange.max) <= 0,
`error: returned overlapping ranges ${overlappingRanges} \
does not contain the requested range ${queryRange}`
);
if (SmartRoutingMapProvider._stringCompare(currentProvidedRange.max, lastKnownTargetRange.max) > 0) {
throw new Error(`error: returned overlapping ranges ${overlappingRanges} \
does not contain the requested range ${queryRange}`);
}
// the current range is contained in partitionKeyRanges just move forward
if (++index >= sortedRanges.length) {

Просмотреть файл

@ -1,5 +1,3 @@
import { isNumber } from "util";
/**
* Models vector clock bases session token. Session token has the following format:
* {Version}#{GlobalLSN}#{RegionId1}={LocalLsn1}#{RegionId2}={LocalLsn2}....#{RegionIdN}={LocalLsnN}
@ -48,7 +46,7 @@ export class VectorSessionToken {
const version = parseInt(versionStr, 10);
const globalLsn = parseFloat(globalLsnStr);
if (!isNumber(version) || !isNumber(globalLsn)) {
if (typeof version !== "number" || typeof globalLsn !== "number") {
return null;
}
@ -68,7 +66,7 @@ export class VectorSessionToken {
// TODO: log error
return null;
}
if (!isNumber(regionId)) {
if (typeof regionId !== "number") {
return null;
}

Просмотреть файл

@ -1,9 +1,10 @@
{
"extends": "../../tslint.json",
"rules":{
"only-arrow-functions": false,
"space-before-function-paren": false,
"no-console": false,
"no-implicit-dependencies": [true, "dev"]
}
}
"extends": "../../tslint.json",
"rules": {
"only-arrow-functions": false,
"space-before-function-paren": false,
"no-console": false,
"no-implicit-dependencies": [true, "dev"],
"import-blacklist": false
}
}

Просмотреть файл

@ -1,14 +1,12 @@
{
"extends": [
"tslint:recommended",
"tslint-config-prettier"
],
"extends": ["tslint:recommended", "tslint-config-prettier"],
"exclude": "./node_modules",
"rules": {
"interface-name": false,
"no-string-literal": false,
"object-literal-sort-keys": false,
"member-ordering": false, // TODO: might want to look at this eventually...
"no-floating-promises": true
"interface-name": false,
"no-string-literal": false,
"object-literal-sort-keys": false,
"member-ordering": false, // TODO: might want to look at this eventually...
"no-floating-promises": true,
"import-blacklist": [true, "assert", "util"]
}
}
}