From dbcfe021a99bd876ef721f4ff47f7ba11619db85 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sun, 10 Dec 2023 18:28:39 +0100 Subject: [PATCH 1/6] functional --- benchmarks/warn.js | 4 ++-- examples/example.js | 2 +- test/emit-interpolated-string.test.js | 4 ++-- test/emit-once-only.test.js | 4 ++-- test/emit-set.test.js | 2 +- test/emit-unlimited.test.js | 2 +- test/issue-88.test.js | 4 ++-- test/jest.test.js | 2 +- 8 files changed, 12 insertions(+), 12 deletions(-) diff --git a/benchmarks/warn.js b/benchmarks/warn.js index 1437598..a9afa43 100644 --- a/benchmarks/warn.js +++ b/benchmarks/warn.js @@ -8,8 +8,8 @@ const err2 = createWarning('FastifyWarning', 'FST_ERROR_CODE_2', 'message') new Suite() .add('warn', function () { - err1.emit() - err2.emit() + err1() + err2() }) .on('cycle', function (event) { console.log(String(event.target)) diff --git a/examples/example.js b/examples/example.js index 3e0faab..6397c77 100644 --- a/examples/example.js +++ b/examples/example.js @@ -4,4 +4,4 @@ const { createWarning } = require('..') const CUSTDEP001 = createWarning('DeprecationWarning', 'CUSTDEP001', 'This is a deprecation warning') -CUSTDEP001.emit() +CUSTDEP001() diff --git a/test/emit-interpolated-string.test.js b/test/emit-interpolated-string.test.js index 12a813a..0d79c2e 100644 --- a/test/emit-interpolated-string.test.js +++ b/test/emit-interpolated-string.test.js @@ -15,8 +15,8 @@ test('emit with interpolated string', t => { } const codeWarning = createWarning('FastifyDeprecation', 'CODE', 'Hello %s') - codeWarning.emit('world') - codeWarning.emit('world') + codeWarning('world') + codeWarning('world') setImmediate(() => { process.removeListener('warning', onWarning) diff --git a/test/emit-once-only.test.js b/test/emit-once-only.test.js index dffe693..d3ff349 100644 --- a/test/emit-once-only.test.js +++ b/test/emit-once-only.test.js @@ -15,8 +15,8 @@ test('emit should emit a given code only once', t => { } const warn = createWarning('FastifyDeprecation', 'CODE', 'Hello world') - warn.emit() - warn.emit() + warn() + warn() setImmediate(() => { process.removeListener('warning', onWarning) t.end() diff --git a/test/emit-set.test.js b/test/emit-set.test.js index da51d08..1f636a6 100644 --- a/test/emit-set.test.js +++ b/test/emit-set.test.js @@ -16,7 +16,7 @@ test('emit should set the emitted state', t => { warn.emitted = true t.ok(warn.emitted) - warn.emit() + warn() t.ok(warn.emitted) setImmediate(() => { diff --git a/test/emit-unlimited.test.js b/test/emit-unlimited.test.js index 621a938..d6078c5 100644 --- a/test/emit-unlimited.test.js +++ b/test/emit-unlimited.test.js @@ -23,7 +23,7 @@ test('emit should emit a given code unlimited times', t => { for (let i = 0; i < times; i++) { expectedRun.push(i) - warn.emit() + warn() } setImmediate(() => { process.removeListener('warning', onWarning) diff --git a/test/issue-88.test.js b/test/issue-88.test.js index 14fc4be..cce2b6f 100644 --- a/test/issue-88.test.js +++ b/test/issue-88.test.js @@ -14,8 +14,8 @@ test('Must not overwrite config', t => { createWarning('FastifyWarning', 'CODE_2', 'Msg', { unlimited: true }) process.on('warning', onWarning) - a.emit('CODE_1') - a.emit('CODE_1') + a('CODE_1') + a('CODE_1') setImmediate(() => { process.removeListener('warning', onWarning) diff --git a/test/jest.test.js b/test/jest.test.js index e013b81..1956d28 100644 --- a/test/jest.test.js +++ b/test/jest.test.js @@ -5,7 +5,7 @@ const { createWarning } = require('..') test('works with jest', done => { const code = createWarning('FastifyDeprecation', 'CODE', 'Hello %s') - code.emit('world') + code('world') // we cannot actually listen to process warning event // because jest messes with it (that's the point of this test) From 151c67862cf02a6e298387a260312d0fd4437caa Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sun, 10 Dec 2023 18:31:26 +0100 Subject: [PATCH 2/6] missing commit --- index.js | 57 ++++++++++++++++++++++---------------------------------- 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/index.js b/index.js index 47bfe86..c5a4728 100644 --- a/index.js +++ b/index.js @@ -62,42 +62,27 @@ function createWarning (name, code, message, { unlimited = false } = {}) { code = code.toUpperCase() - return new WarningItem(name, code, message, unlimited) -} + const warning = unlimited + ? function (a, b, c) { + warning.emitted = true -/** - * Represents a warning item with details. - * @class - * @memberof processWarning - * @param {string} name - The name of the warning. - * @param {string} code - The code associated with the warning. - * @param {string} message - The warning message. - * @param {boolean} unlimited - If true, allows unlimited emissions of the warning. - */ -class WarningItem { - constructor (name, code, message, unlimited) { - this.name = name - this.code = code - this.message = message - this.unlimited = unlimited - this.emitted = false + process.emitWarning(warning.format(a, b, c), warning.name, warning.code) } - - /** - * Emits the warning. - * @param {*} [a] Possible message interpolation value. - * @param {*} [b] Possible message interpolation value. - * @param {*} [c] Possible message interpolation value. - */ - emit (a, b, c) { - if (this.emitted === true && this.unlimited === false) { + : function (a, b, c) { + if (warning.emitted === true) { return } - this.emitted = true - - process.emitWarning(this.format(a, b, c), this.name, this.code) + warning.emitted = true + + process.emitWarning(warning.format(a, b, c), warning.name, warning.code) } + warning.emitted = false; + warning.message = message; + warning.unlimited = unlimited; + Object.defineProperty(warning, 'name', { value: name }) + warning.code = code; + /** * Formats the warning message. * @param {*} [a] Possible message interpolation value. @@ -105,19 +90,21 @@ class WarningItem { * @param {*} [c] Possible message interpolation value. * @returns {string} The formatted warning message. */ - format (a, b, c) { + warning.format = function (a, b, c) { let formatted if (a && b && c) { - formatted = format(this.message, a, b, c) + formatted = format(message, a, b, c) } else if (a && b) { - formatted = format(this.message, a, b) + formatted = format(message, a, b) } else if (a) { - formatted = format(this.message, a) + formatted = format(message, a) } else { - formatted = this.message + formatted = message } return formatted } + + return warning } /** From 5d00d4c219c51def07d57e58a7082b356e38619d Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 12 Dec 2023 14:58:58 +0100 Subject: [PATCH 3/6] remove bottleneck --- index.js | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index c5a4728..9d98c28 100644 --- a/index.js +++ b/index.js @@ -62,26 +62,33 @@ function createWarning (name, code, message, { unlimited = false } = {}) { code = code.toUpperCase() - const warning = unlimited - ? function (a, b, c) { - warning.emitted = true + let warning - process.emitWarning(warning.format(a, b, c), warning.name, warning.code) - } - : function (a, b, c) { - if (warning.emitted === true) { - return - } - warning.emitted = true - - process.emitWarning(warning.format(a, b, c), warning.name, warning.code) - } + const warningContainer = unlimited + ? { + [name]: function (a, b, c) { + warning.emitted = true + + process.emitWarning(warning.format(a, b, c), warning.name, warning.code) + } + + } + : { + [name]: function (a, b, c) { + if (warning.emitted === true && warning.unlimited !== true) { + return + } + warning.emitted = true + + process.emitWarning(warning.format(a, b, c), warning.name, warning.code) + } + } + warning = warningContainer[name] - warning.emitted = false; - warning.message = message; - warning.unlimited = unlimited; - Object.defineProperty(warning, 'name', { value: name }) - warning.code = code; + warning.emitted = false + warning.message = message + warning.unlimited = unlimited + warning.code = code /** * Formats the warning message. From 57c889ea05dda72f5c37501b969de7c83fc79bc5 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 12 Dec 2023 15:04:54 +0100 Subject: [PATCH 4/6] fix typings --- types/index.d.ts | 4 ++-- types/index.test-d.ts | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 0165c33..a11bab0 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,11 +1,11 @@ declare namespace processWarning { - export type WarningItem = { + export interface WarningItem { + (a?: any, b?: any, c?: any): void; name: string; code: string; message: string; emitted: boolean; unlimited: boolean; - emit(a?: any, b?: any, c?: any): void; format(a?: any, b?: any, c?: any): string; } diff --git a/types/index.test-d.ts b/types/index.test-d.ts index f5e83bd..9f2b933 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -13,9 +13,9 @@ expectType(WarnInstance.name) expectType(WarnInstance.emitted) expectType(WarnInstance.unlimited) -expectType(WarnInstance.emit()) -expectType(WarnInstance.emit('foo')) -expectType(WarnInstance.emit('foo', 'bar')) +expectType(WarnInstance()) +expectType(WarnInstance('foo')) +expectType(WarnInstance('foo', 'bar')) const buildWarnUnlimited = createWarning({ name: 'FastifyWarning', @@ -31,6 +31,6 @@ const DeprecationInstance = createDeprecation({ }) expectType(DeprecationInstance.code) -DeprecationInstance.emit() -DeprecationInstance.emit('foo') -DeprecationInstance.emit('foo', 'bar') +DeprecationInstance() +DeprecationInstance('foo') +DeprecationInstance('foo', 'bar') From ad0be0365ad64d70034c9e158804a9d2179576da Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 12 Dec 2023 15:06:38 +0100 Subject: [PATCH 5/6] remove linting error --- index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/index.js b/index.js index 939a745..33fc892 100644 --- a/index.js +++ b/index.js @@ -71,8 +71,6 @@ function createWarning ({ name, code, message, unlimited = false } = {}) { code = code.toUpperCase() - let warning - const warningContainer = unlimited ? { [name]: function (a, b, c) { @@ -92,7 +90,7 @@ function createWarning ({ name, code, message, unlimited = false } = {}) { process.emitWarning(warning.format(a, b, c), warning.name, warning.code) } } - warning = warningContainer[name] + const warning = warningContainer[name] warning.emitted = false warning.message = message From 8473a192018eb3ccd61c66edc7fb18c020eca325 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Tue, 12 Dec 2023 15:10:15 +0100 Subject: [PATCH 6/6] fix docs --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 180f8be..06edfc4 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ const warning = createWarning({ message: 'Hello %s', unlimited: true }) -warning.emit('world') +warning('world') ``` #### Methods @@ -60,7 +60,7 @@ This is a wrapper for `warning.create`. It is equivalent to invoking Deprecation warnings have extended support for the Node.js CLI options: `--throw-deprecation`, `--no-deprecation`, and `--trace-deprecation`. -##### `warning.emit([, a [, b [, c]]])` +##### `warning([, a [, b [, c]]])` The utility also contains an `emit` function that you can use for emitting the warnings you have previously created by passing their respective code. @@ -71,14 +71,14 @@ A warning is guaranteed to be emitted at least once. ```js const { createWarning } = require('process-warning') const FST_ERROR_CODE = createWarning({ name: 'FastifyWarning', code: 'FST_ERROR_CODE', message: 'message' }) -FST_ERROR_CODE.emit() +FST_ERROR_CODE() ``` How to use an interpolated string: ```js const { createWarning } = require('process-warning') const FST_ERROR_CODE = createWarning({ name: 'FastifyWarning', code: 'FST_ERROR_CODE', message: 'Hello %s'}) -FST_ERROR_CODE.emit('world') +FST_ERROR_CODE('world') ``` The `warning` object has methods and properties for managing the warning's state. Useful for testing. @@ -86,20 +86,20 @@ The `warning` object has methods and properties for managing the warning's state const { createWarning } = require('process-warning') const FST_ERROR_CODE = createWarning({ name: 'FastifyWarning', code: 'FST_ERROR_CODE', message: 'Hello %s'}) console.log(FST_ERROR_CODE.emitted) // false -FST_ERROR_CODE.emit('world') +FST_ERROR_CODE('world') console.log(FST_ERROR_CODE.emitted) // true const FST_ERROR_CODE_2 = createWarning('FastifyWarning', 'FST_ERROR_CODE_2', 'Hello %s') FST_ERROR_CODE_2.emitted = true -FST_ERROR_CODE_2.emit('world') // will not be emitted +FST_ERROR_CODE_2('world') // will not be emitted ``` How to use an unlimited warning: ```js const { createWarning } = require('process-warning') const FST_ERROR_CODE = createWarning({ name: 'FastifyWarning', code: 'FST_ERROR_CODE', message: 'Hello %s', unlimited: true }) -FST_ERROR_CODE.emit('world') // will be emitted -FST_ERROR_CODE.emit('world') // will be emitted again +FST_ERROR_CODE('world') // will be emitted +FST_ERROR_CODE('world') // will be emitted again ``` #### Suppressing warnings