Skip to content

Commit

Permalink
Revert "fix: avoid global unlimited config (#89)"
Browse files Browse the repository at this point in the history
This reverts commit d455a6e.
  • Loading branch information
Eomm committed Dec 2, 2023
1 parent 9b576dc commit 30471e1
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 43 deletions.
21 changes: 6 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const { format } = require('node:util')
function processWarning () {
const codes = {}
const emitted = new Map()
const opts = Object.create(null)

/**
* Builds a new {@link ProcessWarning} and adds it to the
Expand Down Expand Up @@ -90,12 +91,8 @@ function processWarning () {
}
}

// We need to manage 4 states:
// - START: code 0: unlimited emission
// - END: code -1: unlimited event emitted
// - START: code 1 (default): limited emission to 1
// - END: code 2: limited event emitted
emitted.set(code, unlimited ? 0 : 1)
Object.assign(opts, { unlimited })
emitted.set(code, unlimited)
codes[code] = buildWarnOpts

return codes[code]
Expand Down Expand Up @@ -140,10 +137,9 @@ function processWarning () {
* @param {*} [c] Possible message interpolation value.
*/
function emit (code, a, b, c) {
const state = emitted.get(code)
if (state === 2) return
if (emitted.get(code) === true && opts.unlimited === false) return
if (codes[code] === undefined) throw new Error(`The code '${code}' does not exist`)
emitted.set(code, state <= 0 ? -1 : (state + 1))
emitted.set(code, true)

const warning = codes[code](a, b, c)
process.emitWarning(warning.message, warning.name, warning.code)
Expand All @@ -153,12 +149,7 @@ function processWarning () {
create,
createDeprecation,
emit,
emitted: {
get (code) {
const state = emitted.get(code)
return state === -1 || state === 2
}
}
emitted
}
}

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"scripts": {
"lint": "standard",
"lint:fix": "standard --fix",
"benchmark": "node benchmarks/warn.js",
"test": "npm run test:unit && npm run test:jest && npm run test:typescript",
"test:jest": "jest jest.test.js",
"test:unit": "tap",
Expand Down
2 changes: 1 addition & 1 deletion test/emit-once-only.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { test } = require('tap')
const test = require('tap').test
const build = require('..')

test('emit should emit a given code only once', t => {
Expand Down
26 changes: 0 additions & 26 deletions test/issue-88.test.js

This file was deleted.

0 comments on commit 30471e1

Please sign in to comment.