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

require-jsdoc with option config produces "TypeError: Cannot read property 'type' of undefined" #378

Closed
ericsoco opened this issue Sep 10, 2019 · 12 comments · Fixed by postmanlabs/postman-collection-transformer#265

Comments

@ericsoco
Copy link

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?
babel-eslint@10.0.1

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.

@brettz9
Copy link
Collaborator

brettz9 commented Sep 10, 2019

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',
    },
  }

@brettz9
Copy link
Collaborator

brettz9 commented Sep 10, 2019

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

@haveyaseen
Copy link

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.

@brettz9
Copy link
Collaborator

brettz9 commented Sep 29, 2019

@Extersky : Any chance for looking at this?

@haveyaseen
Copy link

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 🙂

@aprilmintacpineda
Copy link

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

@brettz9
Copy link
Collaborator

brettz9 commented Oct 1, 2019

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

@brettz9 brettz9 changed the title TypeError: Cannot read property 'type' of undefined require-jsdoc with option config produces "TypeError: Cannot read property 'type' of undefined" Oct 3, 2019
brettz9 added a commit to brettz9/eslint-plugin-jsdoc that referenced this issue Oct 23, 2019
@gajus
Copy link
Owner

gajus commented Oct 23, 2019

🎉 This issue has been resolved in version 15.12.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@gajus gajus added the released label Oct 23, 2019
@brettz9
Copy link
Collaborator

brettz9 commented Oct 23, 2019

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.

@olee
Copy link

olee commented Jan 6, 2020

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.

@brettz9
Copy link
Collaborator

brettz9 commented Jan 7, 2020

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).

brettz9 added a commit to brettz9/eslint-plugin-jsdoc that referenced this issue Jan 7, 2020
@brettz9
Copy link
Collaborator

brettz9 commented Jan 7, 2020

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants