ESLint Rule to Exclude Certain Keywords and Phrases in Angular?
Asked Answered
S

2

5

I need to write an Angular Typescript rule to warn people if they utilize certain 'keywords/phrases'. For example, if keyword "Birthdate" or "SSN" is in the source file directly, it should give a warning.

How would someone write this rule to restrict words using ESLint?

Trying to research currently, did not see any articles in Stackoverflow article search archive,

Curious how to manipulate example code below or (open to any other solutions),

I applied the following below "id-blacklist": ["SSN","Birthdate"], receiving error

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

Other Resources:

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

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

Stuppy answered 16/12, 2019 at 19:29 Comment(2)
Hmm... don't you think a git pre-commit hook might be a better alternative? And isn't TSLint deprecated?Horny
hi @JaredSmith we already have existing examples of this in our code, want to flag as warningsStuppy
R
2

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?

and you can do it with ESlint [TSlint is deprecated] like this: https://eslint.org/docs/rules/id-blacklist

Replevin answered 16/12, 2019 at 21:21 Comment(3)
hi, not try to prevent in precommit, I want it allowed, just with warningsStuppy
looking for coding example, this helpsStuppy
So you can check the second solution if it helps you please upvote and mark as answer.Replevin
N
2

'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';
}
Nembutal answered 9/12, 2021 at 6:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.