From 0ec805253e22e94a96341d5a177e9758acdd6b3a Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Thu, 14 May 2020 06:38:16 -0700 Subject: [PATCH] refactor: reduce usage of extend (#699) --- src/bigquery.ts | 7 +++---- src/dataset.ts | 6 +++--- src/job.ts | 4 ++-- src/table.ts | 2 +- test/bigquery.ts | 20 ++++++++++---------- test/dataset.ts | 8 ++++---- test/job.ts | 5 ++--- test/model.ts | 1 - test/table.ts | 25 +++++++++---------------- 9 files changed, 34 insertions(+), 44 deletions(-) diff --git a/src/bigquery.ts b/src/bigquery.ts index 53e895a3..a1b5cc69 100644 --- a/src/bigquery.ts +++ b/src/bigquery.ts @@ -1295,8 +1295,7 @@ export class BigQuery extends common.Service { options: JobOptions, callback?: JobCallback ): void | Promise { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const reqOpts: any = extend({}, options); + const reqOpts = Object.assign({}, options); let jobId = reqOpts.jobId || uuid.v4(); if (reqOpts.jobId) { @@ -1447,7 +1446,7 @@ export class BigQuery extends common.Service { let nextQuery: GetDatasetsOptions | null = null; if (resp.nextPageToken) { - nextQuery = extend({}, options, { + nextQuery = Object.assign({}, options, { pageToken: resp.nextPageToken, }); } @@ -1551,7 +1550,7 @@ export class BigQuery extends common.Service { } let nextQuery: {} | null = null; if (resp.nextPageToken) { - nextQuery = extend({}, options, { + nextQuery = Object.assign({}, options, { pageToken: resp.nextPageToken, }); } diff --git a/src/dataset.ts b/src/dataset.ts index 95759adb..d526b6db 100644 --- a/src/dataset.ts +++ b/src/dataset.ts @@ -772,7 +772,7 @@ class Dataset extends ServiceObject { let nextQuery: {} | null = null; if (resp.nextPageToken) { - nextQuery = extend({}, options, { + nextQuery = Object.assign({}, options, { pageToken: resp.nextPageToken, }); } @@ -868,7 +868,7 @@ class Dataset extends ServiceObject { let nextQuery: {} | null = null; if (resp.nextPageToken) { - nextQuery = extend({}, options, { + nextQuery = Object.assign({}, options, { pageToken: resp.nextPageToken, }); } @@ -957,7 +957,7 @@ class Dataset extends ServiceObject { let nextQuery: {} | null = null; if (resp.nextPageToken) { - nextQuery = extend({}, options, { + nextQuery = Object.assign({}, options, { pageToken: resp.nextPageToken, }); } diff --git a/src/job.ts b/src/job.ts index b2e4adb9..9df7c694 100644 --- a/src/job.ts +++ b/src/job.ts @@ -426,10 +426,10 @@ class Job extends Operation { let nextQuery: {} | null = null; if (resp.jobComplete === false) { // Query is still running. - nextQuery = extend({}, options); + nextQuery = Object.assign({}, options); } else if (resp.pageToken) { // More results exist. - nextQuery = extend({}, options, { + nextQuery = Object.assign({}, options, { pageToken: resp.pageToken, }); } diff --git a/src/table.ts b/src/table.ts index 026536f1..f1efadc0 100644 --- a/src/table.ts +++ b/src/table.ts @@ -1693,7 +1693,7 @@ class Table extends common.ServiceObject { } let nextQuery: GetRowsOptions | null = null; if (resp.pageToken) { - nextQuery = extend({}, options, { + nextQuery = Object.assign({}, options, { pageToken: resp.pageToken, }); } diff --git a/test/bigquery.ts b/test/bigquery.ts index 6a930aa8..c965e7d5 100644 --- a/test/bigquery.ts +++ b/test/bigquery.ts @@ -57,7 +57,7 @@ interface CalledWithService extends Service { } let promisified = false; -const fakePfy = extend({}, pfy, { +const fakePfy = Object.assign({}, pfy, { promisifyAll: (c: Function, options: pfy.PromisifyAllOptions) => { if (c.name !== 'BigQuery') { return; @@ -74,7 +74,7 @@ const fakePfy = extend({}, pfy, { ]); }, }); -const fakeUtil = extend({}, util, { +const fakeUtil = Object.assign({}, util, { ApiError: FakeApiError, }); const originalFakeUtil = extend(true, {}, fakeUtil); @@ -160,12 +160,12 @@ describe('BigQuery', () => { '@google-cloud/paginator': fakePaginator, '@google-cloud/promisify': fakePfy, }).BigQuery; - BigQueryCached = extend({}, BigQuery); + BigQueryCached = Object.assign({}, BigQuery); }); beforeEach(() => { - extend(fakeUtil, originalFakeUtil); - BigQuery = extend(BigQuery, BigQueryCached); + Object.assign(fakeUtil, originalFakeUtil); + BigQuery = Object.assign(BigQuery, BigQueryCached); bq = new BigQuery({projectId: PROJECT_ID}); }); @@ -603,7 +603,7 @@ describe('BigQuery', () => { }); it('should not include fractional digits if not provided', () => { - const input = extend({}, INPUT_OBJ); + const input = Object.assign({}, INPUT_OBJ); delete input.fractional; const time = bq.time(input); @@ -1103,7 +1103,7 @@ describe('BigQuery', () => { c: 'd', }; - const originalOptions = extend({}, options); + const originalOptions = Object.assign({}, options); bq.request = (reqOpts: DecorateRequestOptions) => { assert.notStrictEqual(reqOpts.json, options); @@ -1197,7 +1197,7 @@ describe('BigQuery', () => { a: 'b', }; - const expectedOptions = extend({}, fakeOptions, { + const expectedOptions = Object.assign({}, fakeOptions, { jobReference: { projectId: bq.projectId, jobId: fakeJobId, @@ -1737,7 +1737,7 @@ describe('BigQuery', () => { }); const options = {a: 'b'}; - const expectedOptions = extend({location: LOCATION}, options); + const expectedOptions = Object.assign({location: LOCATION}, options); const ds = bq.dataset(DATASET_ID, options); const args = ds.calledWith_; @@ -2043,7 +2043,7 @@ describe('BigQuery', () => { }); const options = {a: 'b'}; - const expectedOptions = extend({location: LOCATION}, options); + const expectedOptions = Object.assign({location: LOCATION}, options); const job = bq.job(JOB_ID, options); const args = job.calledWith_; diff --git a/test/dataset.ts b/test/dataset.ts index f5adac0e..1680e501 100644 --- a/test/dataset.ts +++ b/test/dataset.ts @@ -39,7 +39,7 @@ interface CalledWithDataset extends ServiceObject { } let promisified = false; -const fakePfy = extend({}, pfy, { +const fakePfy = Object.assign({}, pfy, { promisifyAll: (c: Function, options: pfy.PromisifyAllOptions) => { if (c.name !== 'Dataset') { return; @@ -227,7 +227,7 @@ describe('BigQuery/Dataset', () => { }, }; - const expectedHeaders = extend({}, fakeReqOpts.headers, { + const expectedHeaders = Object.assign({}, fakeReqOpts.headers, { 'If-Match': FAKE_ETAG, }); @@ -521,7 +521,7 @@ describe('BigQuery/Dataset', () => { }); it('should pass the location to the Table', done => { - const response = extend({location: LOCATION}, API_RESPONSE); + const response = Object.assign({location: LOCATION}, API_RESPONSE); ds.request = (reqOpts: DecorateRequestOptions, callback: Function) => { callback(null, response); @@ -555,7 +555,7 @@ describe('BigQuery/Dataset', () => { }); it('should assign metadata to the Table object', done => { - const apiResponse = extend( + const apiResponse = Object.assign( { a: 'b', c: 'd', diff --git a/test/job.ts b/test/job.ts index cb4eba0e..dee9c2b8 100644 --- a/test/job.ts +++ b/test/job.ts @@ -17,7 +17,6 @@ import * as pfy from '@google-cloud/promisify'; import arrify = require('arrify'); import * as assert from 'assert'; import {describe, it, beforeEach, afterEach, before} from 'mocha'; -import * as extend from 'extend'; import * as proxyquire from 'proxyquire'; import * as sinon from 'sinon'; @@ -46,7 +45,7 @@ interface CalledWithJob extends FakeOperation { } let promisified = false; -const fakePfy = extend({}, pfy, { +const fakePfy = Object.assign({}, pfy, { promisifyAll: (c: Function) => { if (c.name === 'Job') { promisified = true; @@ -213,7 +212,7 @@ describe('BigQuery/Job', () => { it('should optionally accept options', done => { const options = {a: 'b'}; - const expectedOptions = extend({location: undefined}, options); + const expectedOptions = Object.assign({location: undefined}, options); BIGQUERY.request = (reqOpts: DecorateRequestOptions) => { assert.deepStrictEqual(reqOpts.qs, expectedOptions); diff --git a/test/model.ts b/test/model.ts index 525f3612..8c31feec 100644 --- a/test/model.ts +++ b/test/model.ts @@ -29,7 +29,6 @@ describe('BigQuery/Model', () => { const MODEL_ID = 'my_model'; const DATASET = {id: 'my_dataset'} as Dataset; - // tslint:disable-next-line variable-name let Model: typeof m.Model; let model: m.Model; diff --git a/test/table.ts b/test/table.ts index f4f6b870..304a333f 100644 --- a/test/table.ts +++ b/test/table.ts @@ -57,7 +57,7 @@ interface CalledWithTable extends ServiceObject { let promisified = false; let makeWritableStreamOverride: Function | null; let isCustomTypeOverride: Function | null; -const fakeUtil = extend({}, util, { +const fakeUtil = Object.assign({}, util, { isCustomType: (...args: Array<{}>) => { return (isCustomTypeOverride || util.isCustomType)(...args); }, @@ -66,7 +66,7 @@ const fakeUtil = extend({}, util, { }, noop: () => {}, }); -const fakePfy = extend({}, pfy, { +const fakePfy = Object.assign({}, pfy, { promisifyAll: (c: Function) => { if (c.name === 'Table') { promisified = true; @@ -94,8 +94,7 @@ const fakePaginator = { }, }; -// eslint-disable-next-line @typescript-eslint/no-explicit-any -let fakeUuid: any = extend(true, {}, uuid); +let fakeUuid = extend(true, {}, uuid); class FakeServiceObject extends ServiceObject { calledWith_: IArguments; @@ -143,7 +142,7 @@ describe('BigQuery/Table', () => { }); beforeEach(() => { - fakeUuid = extend(fakeUuid, uuid); + fakeUuid = Object.assign(fakeUuid, uuid); isCustomTypeOverride = null; makeWritableStreamOverride = null; tableOverrides = {}; @@ -214,7 +213,7 @@ describe('BigQuery/Table', () => { }); it('should inherit from ServiceObject', done => { - const datasetInstance = extend({}, DATASET, { + const datasetInstance = Object.assign({}, DATASET, { createTable: { bind: (context: {}) => { assert.strictEqual(context, datasetInstance); @@ -277,7 +276,7 @@ describe('BigQuery/Table', () => { }, }; - const expectedHeaders = extend({}, fakeReqOpts.headers, { + const expectedHeaders = Object.assign({}, fakeReqOpts.headers, { 'If-Match': FAKE_ETAG, }); @@ -1513,17 +1512,13 @@ describe('BigQuery/Table', () => { }); describe('writable stream', () => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - let fakeJob: any; + let fakeJob: EventEmitter; let fakeJobId: string; beforeEach(() => { fakeJob = new EventEmitter(); fakeJobId = uuid.v4(); - - fakeUuid.v4 = () => { - return fakeJobId; - }; + sandbox.stub(fakeUuid, 'v4').returns(fakeJobId); }); it('should make a writable stream when written to', done => { @@ -2045,9 +2040,7 @@ describe('BigQuery/Table', () => { beforeEach(() => { insertSpy = sinon.spy(table, '_insert'); requestStub = sinon.stub(table, 'request').resolves([{}]); - fakeUuid.v4 = () => { - return fakeInsertId; - }; + sandbox.stub(fakeUuid, 'v4').returns(fakeInsertId); }); afterEach(() => {