node icon indicating copy to clipboard operation
node copied to clipboard

Enhanced Object Introspection and Help in the Node.js REPL

Open hemanth opened this issue 1 year ago • 2 comments

What is the problem this feature will solve?

Node.js developers, especially those coming from a Python background, may find it less intuitive to quickly explore object properties, available methods, and to access built-in help within the REPL environment.

What is the feature you are proposing to solve the problem?

Implement the following features within the Node.js REPL:

dir(object):

When an object is passed to dir(), provide a structured listing of its properties, methods, and nested structures if applicable. This allows for quick at-a-glance comprehension of an object's composition.

Example: dir(process) might output a list of process-related properties and methods.

^ well, we can do with Object.keys and similar, but we can format the output better for readability if we decide to go with dir

help(objectOrModule):

Attempt to retrieve contextual help based on the provided input:

If an object is passed: Display either internal docstrings (if defined within the object) or type/constructor information.

If a module name is passed: Try to fetch relevant documentation from the official Node.js API reference if available.

Example: help(fs) might link to or display a portion of the file system module's documentation.

What alternatives have you considered?

No response

hemanth avatar Feb 15 '24 00:02 hemanth

Maybe util.format does this already?

marco-ippolito avatar Feb 15 '24 06:02 marco-ippolito

Yes, that comes close;

Examples for py:

image

The format is useful for a quick glance.

>>>help(print)
print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

hemanth avatar Feb 15 '24 17:02 hemanth

If we are talking about the REPL doesn't this already do the job?

> process
process {
  version: 'v21.5.0',
  versions: {
    node: '21.5.0',
    acorn: '8.11.2',
    ada: '2.7.4',
    ares: '1.20.1',
    base64: '0.5.1',
    brotli: '1.0.9',
    cjs_module_lexer: '1.2.2',
    cldr: '44.0',
    icu: '74.1',
    llhttp: '9.1.3',
    ...
}

joyeecheung avatar Feb 20 '24 23:02 joyeecheung

And, as far as printing function goes, I don't think it can be implemented in Python style without docstring support in the JavaScript language.

joyeecheung avatar Feb 20 '24 23:02 joyeecheung