Eslint-plugin-jsdoc: `require-jsdoc` with option config produces "TypeError: Cannot read property 'type' of undefined"

Created on 10 Sep 2019  路  12Comments  路  Source: gajus/eslint-plugin-jsdoc

First filed on eslint, narrowed down to (probably) eslint-plugin-jsdoc.

Tell us about your environment

  • ESLint Version:
    5.16.0
  • Node Version:
    10.16.0
  • npm Version:
    6.9.0

What parser (default, Babel-ESLint, etc.) are you using?
[email protected]

Please show your full configuration:
https://gist.github.com/ericsoco/4fcaacaed0bb083fd72688fee5ee381f

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

Minimal repro seems to include spreading an object in a const definition, followed by a function declaration or expression.

const foo = {...{}};
function bar() {}

What did you expect to happen?
Lints as usual

What actually happened? Please include the actual, raw output from ESLint.

TypeError: Cannot read property 'type' of undefined
Occurred while linting ..../src/temp-lint-test.test.js:2
    at getSymbol (..../node_modules/eslint-plugin-jsdoc/dist/exportParser.js:73:16)
    at node.properties.forEach.prop (..../node_modules/eslint-plugin-jsdoc/dist/exportParser.js:145:27)
    at Array.forEach (<anonymous>)
    at getSymbol (..../node_modules/eslint-plugin-jsdoc/dist/exportParser.js:144:25)
    at createSymbol (..../node_modules/eslint-plugin-jsdoc/dist/exportParser.js:194:31)
    at node.declarations.forEach.declaration (..../node_modules/eslint-plugin-jsdoc/dist/exportParser.js:327:26)
    at Array.forEach (<anonymous>)
    at mapVariables (..../node_modules/eslint-plugin-jsdoc/dist/exportParser.js:324:27)
    at node.body.forEach.childNode (..../node_modules/eslint-plugin-jsdoc/dist/exportParser.js:302:13)
    at Array.forEach (<anonymous>)

Workaround is to add a JSDoc comment to the function declaration:

const foo = {...{}};
/**
 * JSDoc header
 */
function bar() {}

Don't know for a fact the error is thrown from eslint-plugin-jsdoc, but this workaround makes it seem likely.

bug released

Most helpful comment

@Extersky : Any chance for looking at this?

All 12 comments

While this looks like a dupe of #376 , unlike that one, I was able to confirm this issue in our testing environment:

{
    code: `
    const foo = {...{}};
    function bar() {}
    `,
    options: [
      {
        exemptEmptyFunctions: false,
        publicOnly: true,
        require: {
          ArrowFunctionExpression: true,
          ClassDeclaration: true,
          ClassExpression: true,
          FunctionDeclaration: true,
          FunctionExpression: false,
          MethodDefinition: true,
        },
      },
    ],
    parser: require.resolve('babel-eslint'),
    parserOptions: {
      ecmaFeatures: {
        jsx: true,
      },
      ecmaVersion: 2017,
      sourceType: 'module',
    },
  }

@Extersky : Would you be able to take a look?

This bug is blocking redux-orm/redux-orm#296 (failing build):

TypeError: Cannot read property 'type' of undefined
Occurred while linting /home/travis/build/redux-orm/redux-orm/src/selectors/FieldSelectorSpec.js:13
    at checkDestructured (/home/travis/build/redux-orm/redux-orm/node_modules/eslint/lib/rules/no-useless-rename.js:104:43)
    at listeners.(anonymous function).forEach.listener (/home/travis/build/redux-orm/redux-orm/node_modules/eslint/lib/linter/safe-emitter.js:45:58)
    at Array.forEach (<anonymous>)
    at Object.emit (/home/travis/build/redux-orm/redux-orm/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (/home/travis/build/redux-orm/redux-orm/node_modules/eslint/lib/linter/node-event-generator.js:253:26)
    at NodeEventGenerator.applySelectors (/home/travis/build/redux-orm/redux-orm/node_modules/eslint/lib/linter/node-event-generator.js:282:22)
    at NodeEventGenerator.enterNode (/home/travis/build/redux-orm/redux-orm/node_modules/eslint/lib/linter/node-event-generator.js:296:14)
    at CodePathAnalyzer.enterNode (/home/travis/build/redux-orm/redux-orm/node_modules/eslint/lib/linter/code-path-analysis/code-path-analyzer.js:646:23)
    at nodeQueue.forEach.traversalInfo (/home/travis/build/redux-orm/redux-orm/node_modules/eslint/lib/linter/linter.js:935:32)
    at Array.forEach (<anonymous>)

The parsed code can be found at https://github.com/redux-orm/redux-orm/blob/db1f15d22c9890d236e57a53bb4afa2d5ce40dfc/src/selectors/FieldSelectorSpec.js.

@Extersky : Any chance for looking at this?

It turns out our issue seems to have been a different one (eslint/eslint#12335) and has been fixed by the ESLint v6.5.1 release. Thanks anyways :slightly_smiling_face:

I ran into this too, confirmed fixed in Eslint v6.5.1

With ten thumbs up on the issue, if folks are antsy for a fix, there's always funding on Issuehunt to potentially expedite things.

:tada: This issue has been resolved in version 15.12.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

While my fix won't allow catching things like method2 in this:

        const foo = {
          method2 () {

          }
        };
        module.exports = {
          /**
           *
           */
          method1 () {

          },
          ...foo
        };

...it should at least avoid the error, and for those using sourceType: 'module' at least, I don't think there would be anything in an object spread which would likely be relevant.

This rule still breaks for me with 20.0.0 when I have publicOnly option enabled with typescript files:

TypeError: Cannot read property 'type' of undefined
Occurred while linting C:\dev\C4\web\packages\frontend\src\app.tsx:22
    at getSymbol (C:\dev\C4\web\node_modules\eslint-plugin-jsdoc\dist\exportParser.js:72:16)
    at C:\dev\C4\web\node_modules\eslint-plugin-jsdoc\dist\exportParser.js:148:27
    at Array.forEach (<anonymous>)
    at getSymbol (C:\dev\C4\web\node_modules\eslint-plugin-jsdoc\dist\exportParser.js:143:25)
    at createSymbol (C:\dev\C4\web\node_modules\eslint-plugin-jsdoc\dist\exportParser.js:197:31)
    at C:\dev\C4\web\node_modules\eslint-plugin-jsdoc\dist\exportParser.js:330:26
    at Array.forEach (<anonymous>)
    at mapVariables (C:\dev\C4\web\node_modules\eslint-plugin-jsdoc\dist\exportParser.js:327:27)
    at C:\dev\C4\web\node_modules\eslint-plugin-jsdoc\dist\exportParser.js:305:13
    at Array.forEach (<anonymous>)

When I remove the publicOnly option, it works.

I am not getting an error when adding a test locally. But from the looks of the stack trace,
it appears our code has trouble handling some kind of ObjectExpression in TypeScript.

Do you have an example file, preferably a minimal one that triggers the error (and there could be errors in more than one place, as we don't have any tests for publicOnly with TypeScript).

I could provide a guard that avoids the specific exception, but I'd like to know what other cases there may be where there is a property but no value for it (and we are already handling ExperimentalSpreadProperty).

@olee : I see there is a difference in the AST with typescript-parser and the default parser. Fixed in v20.0.1 . If there are other issues remaining, please file a new issue with a reproducible test case. Thanks.

Was this page helpful?
0 / 5 - 0 ratings