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

[node] util module fixes #56435

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion types/node/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Type definitions for non-npm package Node.js 16.10
// Type definitions for non-npm package Node.js 16.11
Copy link
Contributor

Choose a reason for hiding this comment

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

Please do not bump this version unless you bring up the typings fully up to date.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I remove adding stripVTControlCharacters? It was added in Node.js 16.11.

// Project: https://nodejs.org/
// Definitions by: Microsoft TypeScript <https://github.com/Microsoft>
// DefinitelyTyped <https://github.com/DefinitelyTyped>
Expand Down
12 changes: 11 additions & 1 deletion types/node/test/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as util from 'node:util';
import assert = require('node:assert');
import { readFile } from 'node:fs';
import { access, readFile } from 'node:fs';

// Old and new util.inspect APIs
util.inspect(["This is nice"], false, 5);
Expand Down Expand Up @@ -182,8 +182,18 @@ const errorMap: Map<number, [string, string]> = util.getSystemErrorMap();
const logger: util.DebugLogger = util.debuglog('section');
logger.enabled; // $ExpectType boolean
util.debuglog('section', (fn: util.DebugLoggerFunction) => { });
util.debug('section', (fn: util.DebugLoggerFunction) => { });
}

{
const foo: string = util.toUSVString('foo');
}

access('file/that/does/not/exist', (err) => {
const name = util.getSystemErrorName(err!.errno!);
console.error(name);
});

{
const foo: string = util.stripVTControlCharacters('\u001B[4mvalue\u001B[0m');
}
19 changes: 19 additions & 0 deletions types/node/util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ declare module 'util' {
* @since v10.0.0
*/
export function formatWithOptions(inspectOptions: InspectOptions, format?: any, ...param: any[]): string;
/**
* Returns the string name for a numeric error code that comes from a Node.js API.
* The mapping between error codes and error names is platform-dependent.
* @param err A numeric error code.
* @since v9.7.0
*/
export function getSystemErrorName(err: number): string;
/**
* Returns a Map of all system error codes available from the Node.js API.
* The mapping between error codes and error names is platform-dependent.
Expand Down Expand Up @@ -314,6 +321,9 @@ declare module 'util' {
* Allows changing inspect settings from the repl.
*/
let replDefaults: InspectOptions;
/**
* That can be used to declare custom inspect functions.
*/
const custom: unique symbol;
}
/**
Expand Down Expand Up @@ -520,6 +530,7 @@ declare module 'util' {
* @return The logging function
*/
export function debuglog(section: string, callback?: (fn: DebugLoggerFunction) => void): DebugLogger;
export const debug: typeof debuglog;
/**
* Returns `true` if the given `object` is a `Boolean`. Otherwise, returns `false`.
*
Expand Down Expand Up @@ -961,8 +972,16 @@ declare module 'util' {
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5) => Promise<void>;
export function promisify(fn: Function): Function;
export namespace promisify {
/**
* That can be used to declare custom promisified variants of functions.
*/
const custom: unique symbol;
}
/**
* Returns `str` with any ANSI escape codes removed.
* @since v16.11.0
*/
export function stripVTControlCharacters(str: string): string;
/**
* An implementation of the [WHATWG Encoding Standard](https://encoding.spec.whatwg.org/) `TextDecoder` API.
*
Expand Down
16 changes: 15 additions & 1 deletion types/node/v14/test/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as util from 'util';
import assert = require('assert');
import { readFile } from 'fs';
import { access, readFile } from 'fs';

{
// Old and new util.inspect APIs
Expand Down Expand Up @@ -323,3 +323,17 @@ function testUtilTypes(
object; // $ExpectType WeakSet<any>
}
}

{
const logger: util.DebugLogger = util.debuglog('section');
logger.enabled; // $ExpectType boolean
util.debuglog('section', (fn: util.DebugLoggerFunction) => { });
util.debug('section', (fn: util.DebugLoggerFunction) => { });
}

{
access('file/that/does/not/exist', (err) => {
const name = util.getSystemErrorName(err!.errno!);
console.error(name);
});
}
8 changes: 7 additions & 1 deletion types/node/v14/util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ declare module 'util' {
}
function format(format?: any, ...param: any[]): string;
function formatWithOptions(inspectOptions: InspectOptions, format?: any, ...param: any[]): string;
function getSystemErrorName(err: number): string;
/** @deprecated since v0.11.3 - use a third party module instead. */
function log(string: string): void;
function inspect(object: any, showHidden?: boolean, depth?: number | null, color?: boolean): string;
Expand All @@ -32,7 +33,12 @@ declare module 'util' {
/** @deprecated since v4.0.0 - use `util.types.isNativeError()` instead. */
function isError(object: any): object is Error;
function inherits(constructor: any, superConstructor: any): void;
function debuglog(key: string): (msg: string, ...param: any[]) => void;
type DebugLoggerFunction = (msg: string, ...param: any[]) => void;
interface DebugLogger extends DebugLoggerFunction {
enabled: boolean;
}
function debuglog(key: string, callback?: (fn: DebugLoggerFunction) => void): DebugLogger;
const debug: typeof debuglog;
/** @deprecated since v4.0.0 - use `typeof value === 'boolean'` instead. */
function isBoolean(object: any): object is boolean;
/** @deprecated since v4.0.0 - use `Buffer.isBuffer()` instead. */
Expand Down