简体   繁体   English

如何使用 ESLint 自定义规则分析 JS 文字/标识符

[英]How to analyze JS Literals/Identifiers using ESLint Custom Rules

I'll explain the scenario with an example.我会用一个例子来解释这个场景。

Suppose I have following JS code:假设我有以下 JS 代码:

$.ajax({
    url: '/Department/GetAllUsers',
    type: "POST",
    data: data,
    success: function (result) {
        //Some Code
    },
    error: function () {
        //Some Code
    }
});

And I want to restrict call to that action of the controller.我想限制对控制器的那个动作的调用。 So I have written following Custom ES Lint rule for this:所以我为此编写了以下自定义 ES Lint 规则:

module.exports = {
    meta: {
        type: "problem",
        docs: {
            description: "Prohibited Method",
            category: "Method",
            recommended: true,
            url: ""
        },
        messages: {
            messageDefault: "This method is Prohibited to use"
        },
        fixable: "code",
        schema: [] // no options
    },
    create: function (context) {
        return {
            Literal(node) {
                var literalValue = node.value.toString();
                var cont = literalValue.split("/").filter(x => x.length > 1);
                {
                    if (cont[0] === 'Department' && cont[1] === 'GetAllUsers') {

                        context.report({
                            node: node,
                            messageId: "messageDefault",
                        });
                    }
                }
            }
        };
    }
};

So here I am restricting use of 'Department/GetAllUsers' which is working great.所以在这里我限制使用'Department/GetAllUsers' ,这很好用。 The problem arises when I split the string or assign the string to a variable.当我拆分字符串或将字符串分配给变量时会出现问题。 For example例如

var controller = "Department";
var action = "GetAllUsers";
$.ajax({
    url: "/" + controller + "/" + action,  
    //or '/Department/' + 'GetAllUsers'
    type: "POST",
    data: data,
    success: function (result) {
        //Some Code
    },
    error: function () {
        //Some Code
    }
});

Here the restriction does not work, is there a way in which I can resolve the variable values at the url?这里的限制不起作用,有没有办法解决 url 中的变量值? Is this even possible using ESLint?这甚至可以使用 ESLint 吗?

In short I want something like the context.SemanticModel.GetSymbolInfo(node) which is used in Roslyn for C# code analysis.简而言之,我想要一些类似 context.SemanticModel.GetSymbolInfo(node) 的东西,它在 Roslyn 中用于 C# 代码分析。

Thanks谢谢

You can use ESLint's scope managerhttps://eslint.org/docs/developer-guide/scope-manager-interface您可以使用 ESLint 的范围管理器https://eslint.org/docs/developer-guide/scope-manager-interface

There are many examples of using the scope manager in ESLint's own codebase: https://github.com/eslint/eslint/search?q=getScope ESLint 自己的代码库中有很多使用范围管理器的例子: https : //github.com/eslint/eslint/search? q = getScope

Using this API you can follow the variable reference and inspect its assignments.使用此 API,您可以跟踪变量引用并检查其分配。

Note that this has some limitations.请注意,这有一些限制。 For example you won't be able to track values across module boundaries or function boundaries.例如,您将无法跨模块边界或函数边界跟踪值。

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

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