babel / babel-eslint

:tokyo_tower: A wrapper for Babel's parser used for ESLint (renamed to @babel/eslint-parser)
https://github.com/babel/babel/tree/main/eslint/babel-eslint-parser
MIT License
2.96k stars 239 forks source link

optional chaining operator error #811

Closed bosens-China closed 4 years ago

bosens-China commented 4 years ago

The question of "the" Use the optional chaining operator prompt for an expression.

Reappear

Var a = {};
console.log(a?.b);

config

// babel.config.js
module.exports = {
  presets: [
    [
      "@babel/preset-env",
      {
        useBuiltIns: "usage",
        corejs: 3,
        modules: false
      }
    ]
  ],
  // 实验性语法
  plugins: ["@babel/plugin-proposal-class-properties", '@babel/plugin-proposal-optional-chaining']
};
// .eslintrc.js
module.exports = {
  env: {
    browser: true,
    commonjs: true,
    es6: true,
    node: true
  },
  extends: "eslint:recommended",
  globals: {
    Atomics: "readonly",
    SharedArrayBuffer: "readonly"
  },
  parser: "babel-eslint",
  parserOptions: {
    ecmaVersion: 2018,
    sourceType: "module"
  },
  rules: {
    "no-console": process.env.NODE_ENV === "production" ? "error" : "off",
    "no-debugger": process.env.NODE_ENV === "production" ? "error" : "off",

  }
};

Why is it so?

I tried to search for some questions. Some of them were downgraded to @ 8.2.1, which proved to be invalid. I want to know an exact answer

nicolo-ribaudo commented 4 years ago

What version of babel-eslint are you using? What unexpected behaviour are you getting?

Also, you should add @babel/plugin-proposal-optional-chaining to your Babel config.

bosens-China commented 4 years ago

What version of babel-eslint are you using? What unexpected behaviour are you getting?

Also, you should add @babel/plugin-proposal-optional-chaining to your Babel config.

"eslint": "^6.7.2",
"babel-eslint": "^10.0.3",

babel.config.js

module.exports = {
  presets: [
    [
      "@babel/preset-env",
      {
        useBuiltIns: "usage",
        corejs: 3,
        modules: false
      }
    ]
  ],
  // 实验性语法
  plugins: ["@babel/plugin-proposal-class-properties", '@babel/plugin-proposal-optional-chaining']
};

Accident

Expression expected.
Expected ':'.
kaicataldo commented 4 years ago

Strange - 10.0.3 should have this plugin enabled.

existentialism commented 4 years ago

Can't reproduce this locally... @bosens-China can you create a small repo that reproduces the problem?

bosens-China commented 4 years ago

Can't reproduce this locally... @bosens-China can you create a small repo that reproduces the problem?

Of course, this is the new warehouse https://github.com/bosens-China/Test-file

existentialism commented 4 years ago

@bosens-China I cloned your repo, added eslint to devDependencies (it was missing), and was able to run npm run build without any errors?

image

and just to verify, I modified src/main.js with:

var a = {};
console.log(a?.b, c);

So it would force a lint error:

image

bosens-China commented 4 years ago

@bosens-China I cloned your repo, added eslint to devDependencies (it was missing), and was able to run npm run build without any errors?

image

and just to verify, I modified src/main.js with:

var a = {};
console.log(a?.b, c);

So it would force a lint error:

image

Thank you very much.