简体   繁体   English

在 Angular 中排除某些关键字和短语的 ESLint 规则?

[英]ESLint Rule to Exclude Certain Keywords and Phrases in Angular?

I need to write an Angular Typescript rule to warn people if they utilize certain 'keywords/phrases'.我需要编写一个 Angular Typescript 规则来警告人们是否使用某些“关键字/短语”。 For example, if keyword "Birthdate" or "SSN" is in the source file directly, it should give a warning.例如,如果源文件中直接存在关键字“Birthdate”或“SSN”,则应给出警告。

How would someone write this rule to restrict words using ESLint?有人将如何编写此规则来使用 ESLint 限制单词?

Trying to research currently, did not see any articles in Stackoverflow article search archive,目前正在尝试研究,在Stackoverflow文章搜索存档中没有看到任何文章,

Curious how to manipulate example code below or (open to any other solutions),好奇如何操作下面的示例代码或(对任何其他解决方案开放),

I applied the following below "id-blacklist": ["SSN","Birthdate"], receiving error我在“id-blacklist”下面应用了以下内容:[“SSN”,“Birthdate”],接收错误

https://eslint.org/docs/rules/id-blacklist https://eslint.org/docs/rules/id-blacklist

module.exports = {
    "env": {
        "browser": true,
        "es6": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:@typescript-eslint/eslint-recommended"
    ],
    "globals": {
        "Atomics": "readonly",
        "SharedArrayBuffer": "readonly"
    },
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "ecmaVersion": 2018,
        "sourceType": "module"
    },
    "plugins": [
        "@typescript-eslint"
    ],
    "rules": {
        "id-blacklist": ["SSN","Birthdate"]
    }
};

Error:错误:

Configuration for rule "id-blacklist" is invalid:Severity should be one of the following: 0 = off, 1 = warn, 2 = error规则“id-blacklist”的配置无效:严重性应该是以下之一:0 = 关闭,1 = 警告,2 = 错误

Other Resources:其他资源:

https://rangle.io/blog/custom-tslint-for-angular/ https://rangle.io/blog/custom-tslint-for-angular/

https://medium.com/@andrey.igorevich.borisov/writing-custom-tslint-rules-from-scratch-62e7f0237124 https://medium.com/@andrey.igorevich.borisov/writing-custom-tslint-rules-from-scratch-62e7f0237124

I did it with pipeline check but you can with pre-commit as shown here: Is it possible to ban a list of words with ESlint or anything else when pre-commit?我是通过管道检查完成的,但您可以使用预提交,如下所示: Is it possible to ban a list of words with ESlint or any other when pre-commit?

and you can do it with ESlint [TSlint is deprecated] like this: https://eslint.org/docs/rules/id-blacklist你可以用 ESlint [不推荐使用 TSlint] 来做到这一点: https ://eslint.org/docs/rules/id-blacklist

 'no-restricted-syntax': [ 'error', 'Literal[value=/^(special|zzz|xxx)$/i]', 'BinaryExpression[right.value = /^(special|zzz|xxx)$/i]', ],

This rule is work for me, my requirement is we don't want some key will be hard code in repo.这条规则对我有用,我的要求是我们不希望仓库中的某些键是硬代码。

It will show error if your code like below:如果您的代码如下所示,它将显示错误:

 const t = { pp: 'special', }; const tt = 'special', const f = (arg) => { return arg === 'special'; } function ff(arg) { return arg === 'special'; }

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

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