Tslint: [Question] Import-blacklist by regex does not work as expected

Created on 12 Apr 2019  路  10Comments  路  Source: palantir/tslint

Question

  • __TSLint version__: 5.15.0
  • __TypeScript version__: 3.4.3
  • __Running TSLint via__: CLI / VSCode

TypeScript code being linted

import { SecondaryService } from "./src";
export class BasicService {
    public State: boolean;

    constructor(public s: SecondaryService) {

    }
}

with tslint.json configuration:

{
    "defaultSeverity": "error",
    "extends": [
        "tslint:recommended"
    ],
    "jsRules": {},
    "rules": {
        "import-blacklist": [
            true,
            "^.*src$",
        ]
    },
    "rulesDirectory": []
}

Actual behavior

Tslint does not emit any errors

Expected behavior

string import { SecondaryService } from "./src"; should mark as wrong by import-blacklist regex rule

Mini-repo

https://github.com/alxpsr/tslint-regex

Questions

  • What im doing wrong?
  • How can i disallow importing from import-blacklist regex patterns?
Easy Documentation Bug good first issue 馃尮 R.I.P. 馃尮

Most helpful comment

Solution: wrap any regular expressions in arrays. ["^.*src$"] instead of "^.*src$".

This isn't described well in the documentation; accepting PRs to explicitly mention this!

{
    "defaultSeverity": "error",
    "jsRules": {},
    "rules": {
        "import-blacklist": [
            true,
            ["^.*src$"]
        ]
    },
    "rulesDirectory": []
}

All 10 comments

Solution: wrap any regular expressions in arrays. ["^.*src$"] instead of "^.*src$".

This isn't described well in the documentation; accepting PRs to explicitly mention this!

{
    "defaultSeverity": "error",
    "jsRules": {},
    "rules": {
        "import-blacklist": [
            true,
            ["^.*src$"]
        ]
    },
    "rulesDirectory": []
}

Thanks

When i run tslint --project . tslint no emit any error. But in VsCode my import mark as green (probably as warning). On the other hand if i added rxjs to config and to my src/service.ts - tslint emit error (VsCode anyway mark import as warning). Why do the two strategies (regex-pattern vs simple string) have different behaviors?

Guys?

Hi @alxpsr - please file a separate issue. It's better to have one issue per discussion (and one discussion per issue) to keep these issues organized. Differences in lint complaints in VS Code vs the CLI are a separate issue from the documentation here.

Sorry for the ambiguity - the original issue here is still accepting PRs 馃槃 just there should be a second issue.

{
    "defaultSeverity": "error",
    "jsRules": {},
    "rules": {
        "import-blacklist": [
            true,
            ["^.*src$"]
        ]
    },
    "rulesDirectory": []
}

doesn't match on organisation names like @mylib/thing/src

@alshdavid Could you find a solution by now? I have exactly the same issue right now with ["^.*@app/bkh.*"].

No solution, sadly

馃 Beep boop! 馃憠 TSLint is deprecated 馃憟 _(#4534)_ and you should switch to typescript-eslint! 馃

馃敀 This issue is being locked to prevent further unnecessary discussions. Thank you! 馃憢

Was this page helpful?
0 / 5 - 0 ratings