From 9c3ed12c12f4bb1e17af7440c6371c4cefddcd59 Mon Sep 17 00:00:00 2001 From: "Benjamin E. Coe" Date: Mon, 23 Mar 2020 11:51:57 -0700 Subject: [PATCH] build!: update to latest version of gts/typescript (#183) --- .eslintrc.json | 3 +++ .eslintrc.yml | 15 --------------- .github/workflows/ci.yaml | 2 +- .prettierignore | 8 +++++--- .prettierrc | 8 -------- .prettierrc.js | 17 +++++++++++++++++ package.json | 7 ++++--- samples/package.json | 4 ++++ samples/test/test.js | 2 +- src/index.ts | 21 ++++++++++----------- test/index.ts | 32 ++++++++++++++------------------ tsconfig.json | 1 + 12 files changed, 60 insertions(+), 60 deletions(-) create mode 100644 .eslintrc.json delete mode 100644 .eslintrc.yml delete mode 100644 .prettierrc create mode 100644 .prettierrc.js diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..7821534 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,3 @@ +{ + "extends": "./node_modules/gts" +} diff --git a/.eslintrc.yml b/.eslintrc.yml deleted file mode 100644 index 73eeec2..0000000 --- a/.eslintrc.yml +++ /dev/null @@ -1,15 +0,0 @@ ---- -extends: - - 'eslint:recommended' - - 'plugin:node/recommended' - - prettier -plugins: - - node - - prettier -rules: - prettier/prettier: error - block-scoped-var: error - eqeqeq: error - no-warning-comments: warn - no-var: error - prefer-const: error diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index c5cbc55..e8f5ba6 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -9,7 +9,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - node: [8, 10, 12, 13] + node: [10, 12, 13] steps: - uses: actions/checkout@v1 - uses: actions/setup-node@v1 diff --git a/.prettierignore b/.prettierignore index f6fac98..a4ac7b3 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,3 +1,5 @@ -node_modules/* -samples/node_modules/* -src/**/doc/* +**/node_modules +**/.coverage +build/ +docs/ +protos/ diff --git a/.prettierrc b/.prettierrc deleted file mode 100644 index df6eac0..0000000 --- a/.prettierrc +++ /dev/null @@ -1,8 +0,0 @@ ---- -bracketSpacing: false -printWidth: 80 -semi: true -singleQuote: true -tabWidth: 2 -trailingComma: es5 -useTabs: false diff --git a/.prettierrc.js b/.prettierrc.js new file mode 100644 index 0000000..08cba37 --- /dev/null +++ b/.prettierrc.js @@ -0,0 +1,17 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +module.exports = { + ...require('gts/.prettierrc.json') +} diff --git a/package.json b/package.json index 3dbdf63..91db19f 100644 --- a/package.json +++ b/package.json @@ -33,13 +33,14 @@ "@types/node": "^10.5.2", "@types/sinon": "^7.0.0", "c8": "^7.0.0", + "chai": "^4.2.0", "codecov": "^3.0.4", - "gts": "^1.0.0", + "gts": "^2.0.0-alpha.5", "hard-rejection": "^2.1.0", "linkinator": "^2.0.0", - "mocha": "^7.0.0", + "mocha": "^7.1.1", "sinon": "^9.0.0", - "typescript": "3.6.4" + "typescript": "^3.8.3" }, "engines": { "node": ">=10" diff --git a/samples/package.json b/samples/package.json index 3942172..568c5e8 100644 --- a/samples/package.json +++ b/samples/package.json @@ -5,6 +5,10 @@ "engines": { "node": ">=8" }, + "files": [ + "*.js", + "!test/" + ], "repository": "googleapis/nodejs-promisify", "private": true, "scripts": { diff --git a/samples/test/test.js b/samples/test/test.js index 6331a6c..5851c9f 100644 --- a/samples/test/test.js +++ b/samples/test/test.js @@ -16,7 +16,7 @@ const {assert} = require('chai'); const {describe, it} = require('mocha'); const cp = require('child_process'); -const execSync = (cmd) => cp.execSync(cmd, {encoding: 'utf-8'}); +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); describe('quickstart samples', () => { it('should run the quickstart', async () => { diff --git a/src/index.ts b/src/index.ts index 0237e21..5f83074 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,5 @@ +/* eslint-disable prefer-rest-params */ + // Copyright 2014 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -66,8 +68,7 @@ export function promisify( const slice = Array.prototype.slice; // tslint:disable-next-line:no-any - const wrapper: any = function(this: WithPromise) { - const context = this; + const wrapper: any = function (this: WithPromise) { let last; for (last = arguments.length - 1; last >= 0; last--) { @@ -81,7 +82,7 @@ export function promisify( break; // non-callback last argument found. } - return originalMethod.apply(context, arguments); + return originalMethod.apply(this, arguments); } // peel trailing undefined. @@ -93,8 +94,8 @@ export function promisify( // Because dedupe will likely create a single install of // @google-cloud/common to be shared amongst all modules, we need to // localize it at the Service level. - if (context && context.Promise) { - PromiseCtor = context.Promise; + if (this && this.Promise) { + PromiseCtor = this.Promise; } return new PromiseCtor((resolve, reject) => { @@ -114,7 +115,7 @@ export function promisify( } }); - originalMethod.apply(context, args); + originalMethod.apply(this, args); }); }; @@ -165,16 +166,14 @@ export function callbackify(originalMethod: CallbackMethod) { } // tslint:disable-next-line:no-any - const wrapper = function(this: any) { - const context = this; - + const wrapper = function (this: any) { if (typeof arguments[arguments.length - 1] !== 'function') { - return originalMethod.apply(context, arguments); + return originalMethod.apply(this, arguments); } const cb = Array.prototype.pop.call(arguments); - originalMethod.apply(context, arguments).then( + originalMethod.apply(this, arguments).then( // tslint:disable-next-line:no-any (res: any) => { res = Array.isArray(res) ? res : [res]; diff --git a/test/index.ts b/test/index.ts index 267b9f2..07915ee 100644 --- a/test/index.ts +++ b/test/index.ts @@ -12,8 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +/* eslint-disable @typescript-eslint/no-empty-function,prefer-rest-params */ + import * as assert from 'assert'; -import {describe, it, afterEach} from 'mocha'; +import {describe, it, afterEach, beforeEach} from 'mocha'; import * as sinon from 'sinon'; import * as util from '../src'; @@ -24,13 +26,12 @@ describe('promisifyAll', () => { const fakeArgs = [null, 1, 2, 3]; const fakeError = new Error('err.'); - // tslint:disable-next-line let FakeClass: any; beforeEach(() => { FakeClass = class { methodName(callback: Function) { - callback.apply(null, fakeArgs); + callback(...fakeArgs); } methodSingle(callback: Function) { callback(null, fakeArgs[1]); @@ -83,7 +84,6 @@ describe('promisifyAll', () => { }); `); } catch (error) { - // tslint:disable-next-line ban it.skip('should work on ES classes'); } @@ -131,8 +131,7 @@ describe('promisify', () => { beforeEach(() => { fakeArgs = [null, 1, 2, 3]; - func = util.promisify(function(this: {}, callback: () => void) { - // tslint:disable-next-line no-any + func = util.promisify(function (this: {}, callback: () => void) { (callback as any).apply(this, fakeArgs); }); }); @@ -144,12 +143,13 @@ describe('promisify', () => { }); it('should not return a promise in callback mode', done => { - let returnVal: {}; - returnVal = func.call(fakeContext, function(this: {}) { - const args = [].slice.call(arguments); + let returnVal: any; + returnVal = func.call(fakeContext, function (this: {}) { + const args = [...arguments]; assert.deepStrictEqual(args, fakeArgs); assert.strictEqual(this, fakeContext); assert(!returnVal); + returnVal = null; // this is to suppress prefer-const. done(); }); }); @@ -174,7 +174,6 @@ describe('promisify', () => { }); it('should allow the Promise object to be overridden', () => { - // tslint:disable-next-line:variable-name const FakePromise = class {}; const promise = func.call({Promise: FakePromise}); assert(promise instanceof FakePromise); @@ -185,7 +184,6 @@ describe('promisify', () => { func = util.promisify( (callback: () => void) => { - // tslint:disable-next-line no-any (callback as any).apply(func, [null, fakeArg]); }, { @@ -199,7 +197,6 @@ describe('promisify', () => { }); it('should ignore singular when multiple args are present', () => { - // tslint:disable-next-line:no-any const fakeArgs: any[] = ['a', 'b']; func = util.promisify( @@ -259,7 +256,6 @@ describe('callbackifyAll', () => { const fakeArgs = [1, 2, 3]; const fakeError = new Error('err.'); - // tslint:disable-next-line let FakeClass: any; beforeEach(() => { @@ -313,7 +309,7 @@ describe('callbackify', () => { beforeEach(() => { fakeArgs = [1, 2, 3]; - func = util.callbackify(async function(this: {}) { + func = util.callbackify(async (_this: {}) => { return fakeArgs; }); }); @@ -331,7 +327,7 @@ describe('callbackify', () => { }); it('should call the callback if it is provided', done => { - func(function(this: {}) { + func(function (this: {}) { const args = [].slice.call(arguments); assert.deepStrictEqual(args, [null, ...fakeArgs]); done(); @@ -339,7 +335,7 @@ describe('callbackify', () => { }); it('should call the provided callback with undefined', done => { - func = util.callbackify(async function(this: {}) {}); + func = util.callbackify(async (_this: {}) => {}); func((err: Error, resp: {}) => { assert.strictEqual(err, null); assert.strictEqual(resp, undefined); @@ -348,10 +344,10 @@ describe('callbackify', () => { }); it('should call the provided callback with null', done => { - func = util.callbackify(async function(this: {}) { + func = util.callbackify(async (_this: {}) => { return null; }); - func(function(this: {}) { + func(function (this: {}) { const args = [].slice.call(arguments); assert.deepStrictEqual(args, [null, null]); done(); diff --git a/tsconfig.json b/tsconfig.json index 371d4fa..4c9c90d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,7 @@ { "extends": "./node_modules/gts/tsconfig-google.json", "compilerOptions": { + "lib": ["es2018", "dom"], "rootDir": ".", "outDir": "build", },