123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 'use strict'
- const { format } = require('node:util')
- function processWarning () {
- const codes = {}
- const emitted = new Map()
- const opts = Object.create(null)
-
- function create (name, code, message, { unlimited = false } = {}) {
- if (!name) throw new Error('Warning name must not be empty')
- if (!code) throw new Error('Warning code must not be empty')
- if (!message) throw new Error('Warning message must not be empty')
- if (typeof unlimited !== 'boolean') throw new Error('Warning opts.unlimited must be a boolean')
- code = code.toUpperCase()
- if (codes[code] !== undefined) {
- throw new Error(`The code '${code}' already exist`)
- }
- function buildWarnOpts (a, b, c) {
-
- let formatted
- if (a && b && c) {
- formatted = format(message, a, b, c)
- } else if (a && b) {
- formatted = format(message, a, b)
- } else if (a) {
- formatted = format(message, a)
- } else {
- formatted = message
- }
- return {
- code,
- name,
- message: formatted
- }
- }
- Object.assign(opts, { unlimited })
- emitted.set(code, unlimited)
- codes[code] = buildWarnOpts
- return codes[code]
- }
-
- function createDeprecation (code, message, opts = {}) {
- return create('DeprecationWarning', code, message, opts)
- }
-
- function emit (code, a, b, c) {
- 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, true)
- const warning = codes[code](a, b, c)
- process.emitWarning(warning.message, warning.name, warning.code)
- }
- return {
- create,
- createDeprecation,
- emit,
- emitted
- }
- }
- module.exports = processWarning
- module.exports.default = processWarning
- module.exports.processWarning = processWarning
|