自定义ESLint规则中的异步代码 [英] Asynchronous code in custom ESLint rules

查看:121
本文介绍了自定义ESLint规则中的异步代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

故事和动机:

我们有一个相当庞大的端到端Protractor测试代码库。有时,测试会等待实现特定修复 - 通常作为TDD方法的一部分,并演示如何重现问题以及预期的行为是什么。我们目前正在做的是使用Jasmine的 pending(),里面有一个Jira问题编号。示例:

We have a rather huge end-to-end Protractor test codebase. Sometimes it happens that a test waits for a specific fix to be implemented - usually as a part of a TDD approach and to demonstrate how a problem is reproduced and what is the intended behavior. What we are currently doing is using Jasmine's pending() with a Jira issue number inside. Example:

pending("Missing functionality (AP-1234)", function () {
    // some testing is done here
});

现在,我们想知道何时可以重命名待定( )返回 it()并运行测试。或者,换句话说,当问题 AP-1234 被解决或发送到测试时。

Now, we'd like to know when we can rename the pending() back to it() and run the test. Or, in other words, when the issue AP-1234 is resolved or sent to testing.

当前方法:

目前,我正在尝试使用自定义 ESLint 解决问题规则 jira NodeJS模块,并 Q 。自定义 ESLint 规则搜索至少有一个参数的 pending()调用。以 AP - 的格式提取票号,后跟4位数,并使用 jira.findIssue()检查其状态在吉拉。如果状态为已解决 - 报告错误。

At the moment, I'm trying to solve it with a custom ESLint rule, jira NodeJS module, and Q. The custom ESLint rule searches for pending() calls with at least one argument. Extracts the ticket numbers in format of AP- followed by 4 digits and uses jira.findIssue() to check its status in Jira. If status is Resolved - report an error.

这是我到目前为止所得到的:

Here is what I've got so far:

"use strict";

var JiraApi = require("jira").JiraApi,
    Q = require('q');
var jira = new JiraApi("https",
    "jira.url.com",
    "443",
    "user",
    "password",
    "2");

module.exports = function (context) {
    var jiraTicketRegex = /AP\-\d+/g;

    return {
        CallExpression: function (node) {
            if (node.callee.name === "pending" && node.arguments.length > 0) {
                var match = node.arguments[0].value.match(jiraTicketRegex);

                if (match) {
                    match.forEach(function(ticket) {
                        console.log(ticket);  // I see the ticket numbers printed
                        getTicket(ticket).then(function (status) {
                            console.log(status);  // I don't see statuses printed
                            if (status === "Resolved") {
                                context.report(node, 'Ticket {{ticket}} is already resolved.', {
                                    ticket: ticket
                                })
                            }
                        });
                    });

                }
            }
        }
    }
};

其中 getTicket()定义为:

function getTicket(ticket) {
    var deferred = Q.defer();

    jira.findIssue(ticket, function(error, issue) {
        if (error) {
            deferred.reject(new Error(error));
        } else {
            deferred.resolve(issue.fields.status.name);
        }
    });

    return deferred.promise;
}

问题是:目前,它成功提取来自 pending()调用的票号,但不打印票证状态。不过没有错误。

The problem is: currently, it successfully extracts the ticket numbers from the pending() calls, but doesn't print ticket statuses. No errors though.

问题:

一般问题是,我猜测,是:我可以使用异步代码块,等待回调,解决自定义 ESLint 规则中的承诺吗?如果没有,我的选择是什么?

The general question is, I guess, would be: can I use asynchronous code blocks, wait for callbacks, resolve promises in custom ESLint rules? And, if not, what are my options?

更具体的问题是:我做错了什么以及如何使用Node.js jira 模块与 ESLint

A more specific question would be: what am I doing wrong and how can I use Node.js jira module with ESLint?

非常感谢任何见解或替代方案方法。

推荐答案

简短的回答是 - 不,你不能在规则中使用异步代码。 ESLint是同步的,并且在它走过AST时严重依赖于 EventEmitter 。将ESLint代码修改为异步非常困难,但同时保证事件将以正确的顺序发出。
我认为您唯一的选择可能是编写一个同步规则,将足够的信息输出到错误消息中,然后使用其中一个可解析的格式化程序,如 JSON UNIX 然后创建另一个应用程序,您可以根据错误消息将ESLint输出传送到Jira并在Jira中执行异步查找。

The short answer is - no, you can't use asynchronous code inside of the rules. ESLint is synchronous and heavily relies on EventEmitter when it walks AST. It would be very hard to modify ESLint code to be async, but at the same time guarantee that events will be emitted in the right order. I think your only choice might be to write a sync rule that outputs enough information into the error message, then use one of the parsable formatters like JSON or UNIX and then create another application that you can pipe ESLint output to and do a async lookup in Jira based on the error message.

这篇关于自定义ESLint规则中的异步代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆