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

ES6 modules: TypeError: Cannot read property 'type' of undefined #2042

Closed
badsyntax opened this issue Mar 15, 2015 · 11 comments
Closed

ES6 modules: TypeError: Cannot read property 'type' of undefined #2042

badsyntax opened this issue Mar 15, 2015 · 11 comments
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules

Comments

@badsyntax
Copy link

Hi.

I'm using 0.17.0, and I have the following code in a file test.js:

import EventEmitter from 'events';

I have the following eslint config:

"env": {
  "es6": true
},
"ecmaFeatures": {
  "modules": true
}

When running eslint on that file I get the following error: TypeError: Cannot read property 'type' of undefined

Exporting or referencing that object fixes the error:

import EventEmitter from 'events';

export default class Base extends EventEmitter {
  method() {
    return 'foo';
  }
}

Setting a property now causes a different error TypeError: Cannot read property 'name' of undefined:

import EventEmitter from 'events';

export default class Base extends EventEmitter {
  method() {
    this.foo = 'bar';
  }
}

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@btmills
Copy link
Member

btmills commented Mar 15, 2015

Stack trace for TypeError: Cannot read property 'type' of undefined puts the error at no-unused-vars line 43, which is if (definition.type === "VariableDeclarator") {.

Stack trace for TypeError: Cannot read property 'name' of undefined puts the error at no-func-assign line 42, which is if (def.name.name === name && def.type === "FunctionName") {.

@btmills btmills added bug ESLint is working incorrectly rule Relates to ESLint's core rules labels Mar 15, 2015
@benmosher
Copy link
Contributor

I'm seeing the no-func-assign issue on a number of files that were linting properly with v0.16.x, as well.

@nzakas nzakas added the accepted There is consensus among the team that this change meets the criteria for inclusion label Mar 16, 2015
@nzakas
Copy link
Member

nzakas commented Mar 16, 2015

@benmosher one big report per issue, please.

@btmills
Copy link
Member

btmills commented Mar 16, 2015

Technically #2071 is a duplicate of this issue, though it reports the no-func-assign error first, so let's use this issue for no-unused-vars and #2071 for no-func-assign.

@btmills
Copy link
Member

btmills commented Mar 16, 2015

Working on this.

@btmills
Copy link
Member

btmills commented Mar 16, 2015

The no-unused-vars error only occurs when no-undef is also enabled.

config.json:

{
    "env": {
        "es6": true
    },
    "ecmaFeatures": {
        "modules": true
    },
    "rules": {
        "no-undef": 2,
        "no-unused-vars": [2, {"vars": "all", "args": "after-used"}],
    }
}
// test.js
import foo from 'foo';
$ eslint --reset --no-eslintrc -c config.json test.js
~/code/eslint/eslint/lib/rules/no-unused-vars.js:43
            if (definition.type === "VariableDeclarator") {
                          ^
TypeError: Cannot read property 'type' of undefined
    at isExported (~/code/eslint/eslint/lib/rules/no-unused-vars.js:43:27)
    at EventEmitter.Program:exit (~/code/eslint/eslint/lib/rules/no-unused-vars.js:120:70)
    at EventEmitter.emit (events.js:129:20)
    at Controller.controller.traverse.leave (~/code/eslint/eslint/lib/eslint.js:743:25)
    at Controller.__execute (~/code/eslint/eslint/node_modules/estraverse/estraverse.js:393:31)
    at Controller.traverse (~/code/eslint/eslint/node_modules/estraverse/estraverse.js:481:28)
    at EventEmitter.module.exports.api.verify (~/code/eslint/eslint/lib/eslint.js:728:24)
    at processFile (~/code/eslint/eslint/lib/cli-engine.js:193:27)
    at ~/code/eslint/eslint/lib/cli-engine.js:293:26
    at walk (~/code/eslint/eslint/lib/util/traverse.js:81:9)

Removing either of the rules from config.json will fail to cause the crash.

@btmills
Copy link
Member

btmills commented Mar 16, 2015

The fixImport hack temporarily added in #2035 isn't properly cleaning up after itself.

nzakas added a commit that referenced this issue Mar 16, 2015
Fix: no-unused-vars crash from escope workaround (fixes #2042)
@badsyntax
Copy link
Author

Thanks! I've tested this change with 0.17.1 and it's now working as expected 👍

@benmosher
Copy link
Contributor

I am also not seeing a crash anymore, but no-unused-vars no longer highlights unused import specifiers in 0.17.1. Is this expected, given the escope workaround?

(I thought about opening a new issue, but I'm not clear on whether this is understood behavior.)

FWIW: updating to escope 2.0.7 does seem to fix it, but breaks the "Edge cases should not crash when parsing destructured assignment" test.

@nzakas
Copy link
Member

nzakas commented Mar 19, 2015

Please file a new issue and provide sample code plus console output.

@benmosher
Copy link
Contributor

Done: #2114

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 7, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

No branches or pull requests

4 participants