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

Bug in nodeToValue conversion of object literals #2101

Open
dongryphon opened this issue Jan 6, 2024 · 0 comments
Open

Bug in nodeToValue conversion of object literals #2101

dongryphon opened this issue Jan 6, 2024 · 0 comments

Comments

@dongryphon
Copy link

dongryphon commented Jan 6, 2024

Input code

Doc comments using @default will produce bogus defaultvalue values when the value is an object literal:

            /**
             * words
             * @config {type}
             * @default
             */
            sorters : {
                $config : ['lazy'],
                value   : []
            }

The config is a custom tag but the bug will happen not only for @default but likely many other places since the bug is in nodeToValue.

JSDoc configuration

n/a - happens regardless of configuration if you enter nodeToValue with an object literal

JSDoc debug output

n/a

Expected behavior

The defaultvalue should be {"$config":"[\"lazy\"]","value":"[]"} (and it is with my patch below)

Current behavior

The doclet.defaultvalue was { $config: "", value: "" }

This value comes from this code:

    default: {
        onTagged(doclet, {value}) {
            const nodeToValue = jsdoc.src.astnode.nodeToValue;

            if (value) {
                doclet.defaultvalue = value;
            }
            else if (doclet.meta && doclet.meta.code &&
                typeof doclet.meta.code.value !== 'undefined') {
                switch (doclet.meta.code.type) {
...
                    case Syntax.ObjectExpression:
                        doclet.defaultvalue = nodeToValue(doclet.meta.code.node);
                        doclet.defaultvaluetype = 'object';
                        break;

Which gets its value from this piece of code which always produces "" for object properties:

export function nodeToValue(node) {
  let key;
  let parent;
  let str;
  let tempObject;

  switch (node.type) {
    // ...snip...
    case Syntax.ObjectExpression:
      tempObject = {};
      node.properties.forEach((prop) => {
        // ExperimentalSpreadProperty have no key
        // like var hello = {...hi};
        if (!prop.key) {
          return;
        }

        key = prop.key.name;

        // preserve literal values so that the JSON form shows the correct type
        if (prop.value.type === Syntax.Literal) {
          tempObject[key] = prop.value.value;
        } else {
          tempObject[key] = nodeToValue(prop);  // << bug is here
        }
      });

Clearly, this is the correct code for nodeToValue to use for an object property value:

        if (prop.value.type === Syntax.Literal) {
          tempObject[key] = prop.value.value;
        } else {
          tempObject[key] = nodeToValue(prop.value);   // << not "nodeToValue(prop)"
        }

Your environment

Software Version
JSDoc 4.0.0
Node.js 19.8.1
npm 9.5.1
Operating system Ubuntu 22.04

This is from package.json:

  "name": "jsdoc",
  "version": "4.0.0",
  "revision": "1667500635552",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant