From 64cf2fe13c009549dbd7b698cb3891253e0c27ef Mon Sep 17 00:00:00 2001 From: Alice Li Date: Wed, 19 May 2021 13:37:08 -0700 Subject: [PATCH 1/6] Add type parameter to warn function --- src/index.ts | 1 + src/warnings.ts | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3aec2ac47..e1eaf38fb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -105,3 +105,4 @@ export { } from './clientInterface'; export {ServiceError, ChannelCredentials} from '@grpc/grpc-js'; +export {warn} from './warnings'; diff --git a/src/warnings.ts b/src/warnings.ts index 8f96733a2..f9d240d60 100644 --- a/src/warnings.ts +++ b/src/warnings.ts @@ -18,7 +18,8 @@ import {isBrowser} from './isbrowser'; const emittedWarnings = new Set(); -export function warn(code: string, message: string) { +// Type is the type of warning (e.g. 'DeprecationgWarning', 'ExperimentalWarning', etc.) +export function warn(code: string, message: string, type?: string) { // Only show a given warning once if (emittedWarnings.has(code)) { return; @@ -28,6 +29,9 @@ export function warn(code: string, message: string) { if (isBrowser()) { console.warn(message); } else { - process.emitWarning(message); + if(type !== 'undefined') { + process.emitWarning(message, type); + } + else {process.emitWarning(message)} } } From d8011a8b1713be513cb6c689226c17693a6f3a03 Mon Sep 17 00:00:00 2001 From: Alice Li Date: Wed, 19 May 2021 14:23:19 -0700 Subject: [PATCH 2/6] fix: correct linting issues --- src/warnings.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/warnings.ts b/src/warnings.ts index f9d240d60..251a13488 100644 --- a/src/warnings.ts +++ b/src/warnings.ts @@ -29,9 +29,10 @@ export function warn(code: string, message: string, type?: string) { if (isBrowser()) { console.warn(message); } else { - if(type !== 'undefined') { + if (type !== 'undefined') { process.emitWarning(message, type); + } else { + process.emitWarning(message); } - else {process.emitWarning(message)} } } From 2eda73db75c8918e35fb57d008c1c496afb14185 Mon Sep 17 00:00:00 2001 From: Alice Li Date: Mon, 24 May 2021 09:22:37 -0700 Subject: [PATCH 3/6] fix: add unit test for warning function with type --- src/warnings.ts | 9 ++++++--- test/unit/warning.ts | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/warnings.ts b/src/warnings.ts index 251a13488..341f1a866 100644 --- a/src/warnings.ts +++ b/src/warnings.ts @@ -18,7 +18,7 @@ import {isBrowser} from './isbrowser'; const emittedWarnings = new Set(); -// Type is the type of warning (e.g. 'DeprecationgWarning', 'ExperimentalWarning', etc.) +// Type is the type of warning (e.g. 'DeprecationWarning', 'ExperimentalWarning', etc.) export function warn(code: string, message: string, type?: string) { // Only show a given warning once if (emittedWarnings.has(code)) { @@ -29,8 +29,11 @@ export function warn(code: string, message: string, type?: string) { if (isBrowser()) { console.warn(message); } else { - if (type !== 'undefined') { - process.emitWarning(message, type); + if (typeof type !== 'undefined') { + process.emitWarning(message, + { + type: type + }); } else { process.emitWarning(message); } diff --git a/test/unit/warning.ts b/test/unit/warning.ts index f47dafe36..dbea6447b 100644 --- a/test/unit/warning.ts +++ b/test/unit/warning.ts @@ -19,6 +19,7 @@ import * as sinon from 'sinon'; import {describe, it} from 'mocha'; import {warn} from '../../src/warnings'; +import { emitWarning } from 'process'; describe('warnings', () => { it('should warn the given code once with the first message', done => { @@ -42,4 +43,19 @@ describe('warnings', () => { stub.restore(); done(); }); + it('should include warning type if type is provided', done => { + const stub = sinon.stub(process, 'emitWarning'); + warn('codeD', 'messageD-1', 'WarningType1'); + assert(stub.calledWith('messageD-1', + { + type: 'WarningType1' + })); + stub.restore(); + done(); + }); }); + +// const stub = sinon.stub(process, 'emitWarning'); +// warn('codeD', 'messageD-1', 'WarningType1'); +// console.warn(stub.getCall(0).args); +// stub.restore(); From 729c955abd201b5353c5d96e38cef60213e9eb15 Mon Sep 17 00:00:00 2001 From: Alice Li Date: Mon, 24 May 2021 09:49:58 -0700 Subject: [PATCH 4/6] fix: type error --- src/warnings.ts | 13 ++++++------- test/unit/warning.ts | 15 +++++---------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/warnings.ts b/src/warnings.ts index 341f1a866..f3a1f2e38 100644 --- a/src/warnings.ts +++ b/src/warnings.ts @@ -18,8 +18,8 @@ import {isBrowser} from './isbrowser'; const emittedWarnings = new Set(); -// Type is the type of warning (e.g. 'DeprecationWarning', 'ExperimentalWarning', etc.) -export function warn(code: string, message: string, type?: string) { +// warnType is the type of warning (e.g. 'DeprecationWarning', 'ExperimentalWarning', etc.) +export function warn(code: string, message: string, warnType?: string) { // Only show a given warning once if (emittedWarnings.has(code)) { return; @@ -29,11 +29,10 @@ export function warn(code: string, message: string, type?: string) { if (isBrowser()) { console.warn(message); } else { - if (typeof type !== 'undefined') { - process.emitWarning(message, - { - type: type - }); + if (typeof warnType !== 'undefined') { + process.emitWarning(message, { + type: warnType, + }); } else { process.emitWarning(message); } diff --git a/test/unit/warning.ts b/test/unit/warning.ts index dbea6447b..8453e8105 100644 --- a/test/unit/warning.ts +++ b/test/unit/warning.ts @@ -19,7 +19,6 @@ import * as sinon from 'sinon'; import {describe, it} from 'mocha'; import {warn} from '../../src/warnings'; -import { emitWarning } from 'process'; describe('warnings', () => { it('should warn the given code once with the first message', done => { @@ -46,16 +45,12 @@ describe('warnings', () => { it('should include warning type if type is provided', done => { const stub = sinon.stub(process, 'emitWarning'); warn('codeD', 'messageD-1', 'WarningType1'); - assert(stub.calledWith('messageD-1', - { - type: 'WarningType1' - })); + assert( + stub.calledWith('messageD-1', { + type: 'WarningType1', + }) + ); stub.restore(); done(); }); }); - -// const stub = sinon.stub(process, 'emitWarning'); -// warn('codeD', 'messageD-1', 'WarningType1'); -// console.warn(stub.getCall(0).args); -// stub.restore(); From 0e8bc2b83e03e4c7c9b4f3485581a7526f7de4d3 Mon Sep 17 00:00:00 2001 From: Alice Li Date: Mon, 24 May 2021 13:23:59 -0700 Subject: [PATCH 5/6] fix: updating @types/node dependency version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 32967f4a7..50982df07 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "@types/fs-extra": "^8.0.1", "@types/mocha": "^8.0.0", "@types/ncp": "^2.0.1", - "@types/node": "^10.3.2", + "@types/node": ">=15.6.0", "@types/node-fetch": "^2.5.4", "@types/object-hash": "^2.1.0", "@types/proxyquire": "^1.3.28", From 8effc7e0169ac0c0ba6c18cf3c2849a74556c4a9 Mon Sep 17 00:00:00 2001 From: Alice Li Date: Mon, 24 May 2021 16:24:46 -0700 Subject: [PATCH 6/6] fix: flattened nested if/else statement --- src/warnings.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/warnings.ts b/src/warnings.ts index f3a1f2e38..bc1ffef2e 100644 --- a/src/warnings.ts +++ b/src/warnings.ts @@ -28,13 +28,11 @@ export function warn(code: string, message: string, warnType?: string) { if (isBrowser()) { console.warn(message); + } else if (typeof warnType !== 'undefined') { + process.emitWarning(message, { + type: warnType, + }); } else { - if (typeof warnType !== 'undefined') { - process.emitWarning(message, { - type: warnType, - }); - } else { - process.emitWarning(message); - } + process.emitWarning(message); } }