Repository: babel/babel-eslint (JavaScript)

Issue:
regression v8.2.2: optional chained properties reported as undefined
it appears that v8.2.2 is reporting optional chained properties as undefined (works with v8.2.1)

```javascript
foo?.bar?.foobar
```

```console
77:15  error  'bar' is not defined     no-undef
77:22  error  'foobar' is not defined  no-undef
```


Discussion:
I approached same issue however I am using "babel-eslint": "^8.2.1" together with "@babel/plugin-proposal-optional-chaining": "^7.0.0-beta.40". @dnalborczyk could you please tell me what babel plugin you are using? Thanks in advance.
I'm facing the same issue.
I'm using `@babel/plugin-proposal-optional-chaining`

Thanks for your great work ;)
I am seeing this too. Downgrading back to `8.2.1` resolves the issue for me.
Ref: https://github.com/babel/babel/pull/7288

We need to now handle `OptionalMemberExpressions` and `OptionalCallExpressions`.

Added this to my TODO list if no one gets to it first!
@existentialism babel/babel#7288 has been resolved, does this mean this can be fixed?
@julien-f yes, my reference to it was more that it is the cause of the issue, not that I was waiting for it to land. Sorry for not being clear!
Not a surprise given the state of discussions, but just to prevent other from having to test it:

`8.2.3` still suffers from this issue.
I downgrade `8.2.1` and this doesn't solves the problem for me. How did you fix it?
I'm using:
```
ESLint: 4.19.1
Babel: 7.0.0-beta.46
babel-eslint: 8.2.3
@babel/plugin-proposal-optional-chaining: 7.0.0-beta.46
```
And I having the same issue [here](https://github.com/babel/babel-eslint/issues/595#issue-298934862).

Have someone found how to solver that?

hours on this... downgrading to 8.2.1 works for me. Thanks
It seems like this should be fixed once the babylon dep (`@babel/parser` now) is upgraded past beta45 (which includes https://github.com/babel/babel/pull/7668). Right now it's at beta44.

Should be a pretty easy bump, is anyone working on this already (@hzoo?)
@hansonw that alone doesn't seem to fix it. I just tried by installing and linking `babel-eslint` locally, and upgrading its dependencies. I still get the same linting errors with its `@babel` dependencies updated to `beta.48`
In fact, downgrading to 8.2.1 have worked. Any idea when this issue will be fixed in next releases?
@lehni have you tried with `beta.49`?
I'm seeing it with `beta.49`. I too had to downgrade to `8.2.1`
@richardgirges Have you seen with `beta.49`? Did Work?
Even downgrading to `8.2.1` doesn't fix this for me
We need the see your .babelrc and .eslintrc, at least.
Downgraded to 8.2.1 and I got rid of the 'undefined' errors. Now it seems to be expecting a ternary (probably due to the '?').

Here are my dependencies
```
    "@babel/core": "^7.0.0-beta.49",
    "@babel/plugin-proposal-optional-chaining": "^7.0.0-beta.49",
    "@babel/plugin-syntax-optional-chaining": "^7.0.0-beta.49",
    "@babel/polyfill": "^7.0.0-beta.49",
    "@babel/preset-env": "^7.0.0-beta.49",
    "@babel/preset-react": "^7.0.0-beta.49",
    "babel-eslint": "^8.2.1",
    "babel-loader": "^8.0.0-beta.3",
    "eslint": "^4.19.1",
```
My .babelrc and .eslintrc, respectively

```
{
  "presets": [
    "@babel/react",
    "@babel/preset-env"
  ],
  "plugins": [
    "@babel/plugin-proposal-optional-chaining",
    "react-hot-loader/babel"
  ]
}
```

```
{
  "parser": "babel-eslint",
  "env": {
    "browser": true,
    "es6": true
  },
  "extends": ["eslint:recommended", "airbnb"],
  "parserOptions": {
    "ecmaFeatures": {
      "experimentalObjectRestSpread": true,
      "jsx": true
    },
    "sourceType": "module"
  },
  "plugins": ["react", "jsx-a11y", "import"],
  "rules": { ... }
``` 
@brneto 
`.babelrc`
```json
{
    "presets": [
        [
            "@babel/preset-env",
            {
                "targets": {
                    "node": "current"
                }
            }
        ]
    ],
    "sourceMaps": "inline",
    "plugins": [
        "@babel/plugin-proposal-optional-chaining",
        "import-glob"
    ]
}
```

`.eslintrc`
```json
{
    "parser": "babel-eslint",
    "env": {
        "es6": true,
        "node": true
    },
    "extends": "eslint:recommended",
    "parserOptions": {
        "ecmaVersion": 8,
        "sourceType": "module"
    },
    "rules": {...}
}
```
@LennyPenny Are you importing `@babel/polyfill` at the root of your code? Are you using `webpack` or `node` as the environment to run your code?
@existentialism I guess I got to it first. Can you please take a look at #630 where I put a fix for this? This is the last thing we need in order to roll out this at Facebook. Thanks!
@brneto I'm using node with the latest babel. I never needed `@babel/polyfill` before, but even with it it doesn't work
Unfortunately we're not out of the woods yet: #643
@lehni What is the process of helping with this?  I'd like to get to a point that I can use optional chaining with eslint and cra@next, but I'm not sure how to plug in to help.
@jlaustill I don't know! I haven't been involved in fixing those issues, just been observing them and pointing them out.
even with assigments this error gets thrown using babel-eslint 9.0.0 final

https://github.com/babel/babel-eslint/issues/643#issuecomment-417282457

downgrading to 8.2.1 solves it. 



Patch:
diff --git a/lib/patch-eslint-scope.js b/lib/patch-eslint-scope.js
index aec71fc6..2bf2b350 100644
--- a/lib/patch-eslint-scope.js
+++ b/lib/patch-eslint-scope.js
@@ -233,6 +233,10 @@ function monkeypatch(modules) {
   // visit ClassPrivateProperty as a Property.
   referencer.prototype.ClassPrivateProperty = visitClassProperty;
 
+  // visit OptionalMemberExpression as a MemberExpression.
+  referencer.prototype.OptionalMemberExpression =
+    referencer.prototype.MemberExpression;
+
   // visit flow type in FunctionDeclaration, FunctionExpression, ArrowFunctionExpression
   var visitFunction = referencer.prototype.visitFunction;
   referencer.prototype.visitFunction = function(node) {


Test patch:
diff --git a/test/non-regression.js b/test/non-regression.js
index 2a23ee58..687af37c 100644
--- a/test/non-regression.js
+++ b/test/non-regression.js
@@ -1651,6 +1651,18 @@ describe("verify", () => {
     });
   });
 
+  describe("optional chaining operator", () => {
+    it("should not be undefined #595", () => {
+      verifyAndAssertMessages(
+        `
+            const foo = {};
+            foo?.bar;
+        `,
+        { "no-undef": 1 }
+      );
+    });
+  });
+
   it("flow types on class method should be visited correctly", () => {
     verifyAndAssertMessages(
       `