微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否有 ESLint 规则与现在已弃用的 TSLint 中的“无浮动承诺”相同?

如何解决是否有 ESLint 规则与现在已弃用的 TSLint 中的“无浮动承诺”相同?

我正在将几个项目从 TSLint 迁移到 ESLint,我发现 ESLint 没有阻止 promise 被正确处理的规则,或者至少我没有在文档中找到它。

TSLint 中的no-floating-promises 规则迫使程序员要么let flag_create_element = false; let flag_mouse_down = false; $('.btn_create').off('mousedown').on('mousedown',function (event) { flag_mouse_down = true; flag_create_element = true; let created_option_title = '<div class="new-element-added"></div>'; if (flag_create_element){ $('body').append(created_option_title); $('.new-element-added').draggable(); } }); $('body').off('mousemove').on('mousemove',function (event) { $(this).on('mousemove',createAndMoveElement(event)); }).off('mouseup').on('mouseup',function () { flag_mouse_down = false; flag_create_element = false; //$('.new-element-added').remove(); }); function createAndMoveElement(event) { if (flag_mouse_down){ $('.new-element-added').draggable(); } flag_create_element = false; } $('#droppable_zone').droppable({ drop: function () { alert('ok'); } });,要么#droppable_zone{ position: fixed; left: 0; right:0; top:0; bottom:0; margin: auto; width: 50vw; min-height: 50vh; background-color: bisque; } .btn_create{ position: absolute; left:0; top:0; width: 45px; height: 45px; background-color: green; } .new-element-added{ width: 200px; height: 200px; background-color: blue; position: absolute; display: block; z-index: 999; } 所有承诺。有没有办法在 ESLint 中获得相同的功能

我知道 this answer,但我认为 OP 指的是具有不同行为的规则:<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script> <article id="droppable_zone"> </article> <button type="button" class="btn_create"> <i class="fas fa-plus align-middle font-15"></i> </button> 所有承诺。此外,这个问题的答案是 npm 包 eslint-plugin-package,它已经两年没有发布了。

解决方法

typescript-eslint plugin 有一个 rule,它强制正确处理所有承诺。

安装 ESLint:

安装 typescript-eslint 插件:

npm i --save-dev eslint typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin

将其添加到您当前的 ESLint 配置中:

module.exports = {
  root: true,parser: '@typescript-eslint/parser',plugins: [
    '@typescript-eslint',],extends: [
    'eslint:recommended','plugin:@typescript-eslint/recommended',};

如果您在安装 ESLint 时选择了“强制流行编码风格”并选择了“标准”,那么您可能已经安装了这个插件。不过,如果您有疑问,请检查您的 ESLint 配置文件。

并将规则添加到您的规则对象中:

// .eslintrc.js
/* the rest of the configuration */
rules: {
    '@typescript-eslint/no-floating-promises': 'error',/8 the rest of the rules */
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。