有没有办法在 vscode 中打开 ES6/ES7 语法支持?

Is there a way to turn on ES6/ES7 syntax support in vscode?

编辑 3:从 0.4.0 版本开始,可以通过将 jsconfig.json 文件添加到项目文件夹来打开 ES6 语法,其内容如下:

{
    "compilerOptions": {
        "target": "ES6"
    }
}

编辑 2:您可以 vote 在用户语音上使用此功能


有没有办法在Visual Studio代码中"turn on"ES6/ES7?

编辑 1

尝试了@sarvesh 的建议 - 覆盖 javascript.validate.target 并重新启动 vscode。没有帮助。

目前,使用 ES6 和 ES7 特性的唯一方法是使用 Typescript

另一方面,here您可以看到有针对 ES6 和 ES7 的功能请求

这很简单,在项目的根目录下创建一个 jsconfig.json 文件并在其中写入此对象:

{
    "compilerOptions": {
        "target": "ES6",
        "module": "commonjs"
    }
}

否则你可以使用 ESLint 高亮显示 ES7 错误(使用 babel 解析器或其他):

添加以上答案...

根据 VS 代码文档..

确保将 jsconfig.json 放在 JavaScript 项目的根目录 而不仅仅是工作区的根目录。下面是一个 jsconfig.json 文件,它将 JavaScript 目标定义为 ES6,排除属性排除了 node_modules 文件夹。

{
    "compilerOptions": {
        "target": "ES6"
    },
    "exclude": [
        "node_modules"
    ]
}

这是一个带有显式文件属性的示例。

{
    "compilerOptions": {
        "target": "ES6"
    },
    "files": [
        "src/app.js"
    ]
}

文件属性不能与排除属性一起使用。如果两者都指定,则文件属性优先。

也尝试在 tsconfig.json

中编辑 "target" 属性
{
  "compilerOptions": {
    "target": "es5",//es6
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ]
}

或者,您可以使用 Flow 而不是 Typescript,后者更易于设置和迁移。我在how to setup Flow with VS Code上写了一篇小文章。

link帮了大忙。将 jsconfig.json 文件添加到项目中并没有多大帮助,或者说这不是最佳解决方案。转到文件 > 首选项 > 设置。在 settings.json 文件中添加这一行:

"jshint.options": { "esversion": 6 }

您还可以通过在项目的根目录中创建一个 .jshintrc 文件并添加此内容来为整个项目启用此设置。

{
  "esversion": 6
}

截至目前,根据 VSCode Marketplace 上的 ESLint 文档,项目根目录中包含一个 .eslintrc 配置文件,可在 ESLint 中启用 ES6 linting VSCode 扩展。

我的 .eslintrc 配置文件如下所示:

extends:
  - standard
parser: babel-eslint
rules:
  object-curly-spacing: [ error, always ]
  react/prop-types: off
  space-before-function-paren: off

我在 node_modules 中通过 npm 安装了 eslint,我所知道的是在项目根文件夹中使用 .eslintrc ES6 linting 可以工作,没有它,它不会。

希望这对您有所帮助...

为了更方便。在您的文件夹中设置 jsconfig.json。

{
    "compilerOptions": {
        "target": "esnext"
    }
}