Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement function scoped Warning #97

Merged
merged 7 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benchmarks/warn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion examples/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ const { createWarning } = require('..')

const CUSTDEP001 = createWarning('DeprecationWarning', 'CUSTDEP001', 'This is a deprecation warning')

CUSTDEP001.emit()
CUSTDEP001()
57 changes: 22 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,62 +62,49 @@ 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 })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one slows it down, because it has to lookup the name

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because a function has a non writable name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to change it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we accept a name parameter as input, that can be read by the user.

@Uzlopak What if we rename it to group instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think name is important to detect DeprecationWarnings so that node would ignore them.

The function name is usually set by doing function NAME () {} or const NAME = () => {}, where NAME is the desired Function Name. But I dont know how I could do it at runtime. Maybe by doing abusing new Function, which is basically a eval call. Also some runtimes disable, i think cloudflare, disallow new Function because it is under the hood an eval.

It could be also done by doing:

const NAME = 'DeprecationWarning'
const container = {
  [NAME]: () => {}
}

console.log(container[NAME].name) // 'DeprecationWarning'

But something like

function createNamedFunction (name, fn = () => {}) {
  const container = {
    [name]: fn
  }
    return container[name]
}

const bla = createNamedFunction('DeprecationWarning', () => {})
console.log(bla.name) //  '' <-- empty

warning.code = code;

/**
* Formats the warning message.
* @param {*} [a] Possible message interpolation value.
* @param {*} [b] Possible message interpolation value.
* @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
}

/**
Expand Down
4 changes: 2 additions & 2 deletions test/emit-interpolated-string.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/emit-once-only.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion test/emit-set.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/emit-unlimited.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/issue-88.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/jest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading