简体   繁体   English

在 vscode 中自动拾取 js 错误

[英]Automatically picking up js errors in vscode

Using VS Code and I keep hitting little snags in my JS workflow, example below:使用 VS Code,我一直在我的 JS 工作流程中遇到小问题,示例如下:

In the following code theres an teeny error (highlighted).在下面的代码中有一个小错误(突出显示)。

module.exports = {
elems: {
    accordions: document.querySelectorAll('ul.m_accordion')
},
init: function() {
    this.render();
}, 
render: function() {
    if (!this.moduleValid()) {
        return;
    }
    this.attachListeners(this.elems.accordions);
}, 
moduleValid: function() {
    return this.elems.**accordion**.length > 0;
}, 
attachListeners: function (accordions) {
    for(var i = 0; i < accordions.length; i++){
        console.log(this);
    }
}
}

Renamed a variable, missed one, webpacked up and (understandably) broke in browser, little debugging and its all working.重命名了一个变量,错过了一个,网络打包并且(可以理解)在浏览器中崩溃了,几乎没有调试并且一切正常。 Larger files though it becomes a bit of a pain.更大的文件虽然它变得有点痛苦。 However it'd be great if those errors were picked up and shown in VSCode.但是,如果这些错误被发现并显示在 VSCode 中,那就太好了。

For referance and to show you what I see heres a small screenshot of the same js code, in the vs code with the error visible, but 'invisible'作为参考并向您展示我在此处看到的相同 js 代码的小屏幕截图,在 vs 代码中,错误可见,但“不可见”

在此处输入图片说明 (Ignore my little scribble - purely to draw the eye to my woe) (忽略我的小涂鸦 - 纯粹是为了引起我的悲哀)

You can try using ESLint extension: https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint您可以尝试使用 ESLint 扩展: https ://marketplace.visualstudio.com/items ? itemName = dbaeumer.vscode-eslint

ESLint is meant to check your javascript for syntactical errors, also forces you to write your script in a specific manner which you can configure. ESLint 旨在检查您的 javascript 是否存在语法错误,还强制您以可以配置的特定方式编写脚本。 Eg you can force every developer to avoid writing JSON in single line etc.例如,您可以强制每个开发人员避免在单行等中编写 JSON。

You can configure the rules using .eslintrc file in root of your application.您可以使用应用程序根目录中的.eslintrc文件配置规则。 Check https://eslint.org/docs/user-guide/configuring .检查https://eslint.org/docs/user-guide/configuring Following is an example of a .eslintrc file以下是.eslintrc文件的示例

OR或者

If you want absolute type checking and compilation benefits like C# or JAVA then use TypeScript .如果您想要像 C# 或 JAVA 这样的绝对类型检查和编译优势,请使用TypeScript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM