Tslint: import-blacklist not working for named imports

Created on 19 Dec 2018  路  15Comments  路  Source: palantir/tslint

Bug Report

Import blacklist is not working on named imports.

  • __TSLint version__: 5.11.0
  • __TypeScript version__: 3.1.6
  • __Running TSLint via__: (pick one) CLI

TypeScript code being linted

import { action } from "mobx";

with tslint.json configuration:

        "import-blacklist": [
            true,
            {
                "mobx": [
                    "action"
                ]
            }
        ]

Actual behavior

Ts lint passes with no issues.

Expected behavior

It should fail since using action from mobx is blacklisted.

External Bug

Most helpful comment

@JoshuaKGoldberg same issue over here. Rule was added but tslint is not marking any issue.
Could you re-open this issue?

@Mr1upMachine did you find a solution to fix?

Current configuration:
@angular/core: 5.2.7
@angular/cli: 1.7.4
@angular/compiler-cli: 5.2.7
Typescript 3.2.2
Tslint 5.9.1

All 15 comments

@Stupar92 I get the error locally. What command are you using to run the CLI?

Closing for inactivity. @Stupar92 please do comment again if you're seeing the bad behavior!

@JoshuaKGoldberg I was able to reproduce the issue above. I am using a base Angular CLI project, which uses typescript ~3.2.2 and tslint ~5.11.0. I attempted it with installing lodash and blacklisting isFunction like this:

"import-blacklist": [
  true,
  "rxjs/Rx",
  {
    "lodash": [
      "isFunction"
    ]
  }
],

I will try and see if I can reproduce the issue in a different context, however a fresh Angular CLI application should work.

@JoshuaKGoldberg same issue over here. Rule was added but tslint is not marking any issue.
Could you re-open this issue?

@Mr1upMachine did you find a solution to fix?

Current configuration:
@angular/core: 5.2.7
@angular/cli: 1.7.4
@angular/compiler-cli: 5.2.7
Typescript 3.2.2
Tslint 5.9.1

@christopheberth I have not yet no.

Same issue here my React/Typescript app. This is not working:

"import-blacklist": [
  true,
  {
    "styled-components": [
      {
        "name": "styled",
        "message": "import from styled-components/macro instead"
      }
    ]
  }
]

typescript: 3.3.3333
tslint: 5.13.0

I just tried the OP's sample code with mobx and action using TSLint 5.13.0 and correctly got a TSLint complaint.

{
  "rules": {
    "import-blacklist": [
      true,
      {
        "mobx": [
          "action"
        ]
      }
    ]
  }
}
// index.ts
import { action } from "mobx";

@bopfer @Mr1upMachine @christopheberth please share:

  • Your tslint.json
  • Your TypeScript and TSLint versions _(if not [email protected] and [email protected], please try again with those)_
  • How you're running TSLint _(command line? particular IDE extension? ...?)_
  • Minimal sample code from a TypeScript file that should get a lint failure but doesn't

Without all those, it's rather difficult to repro what's going wrong.

@JoshuaKGoldberg, I have create a bare minimum repo to show the issue I am having. This repo is using the exact tslint.json file that I am using in my real app:

https://github.com/bopfer/import-blacklist-testing

I would like this to be blacklisted:

import styled from 'styled-components';

In favor of this:

import styled from 'styled-components/macro';

But, in src/index.tsx, VSCode and yarn lint will not report it.

It's very possible I am just plain doing something wrong, but I am not sure what that would be.

Super, thanks @bopfer! I'll take a look.

Also hitting this issue. I tried to upgrade dependencies, but it didn't help.

tslint: 5.15.0
typescript: 3.3.3333
typescript-tslint-plugin: 0.3.1

Edit: It actually started working after playing around a bit. I saw somewhere that you could pass an object to provide a message about an import, but looking at the config schema that is not actually supported. Here's the config I got working:

{
    "import-blacklist": [true, { "lodash": ["isFinite"] }]
}

I never did take a look 馃槥... but thanks @villelahdenvuo! It sounds like the issue is with the rule's documentation and/or config parsing then?

I think the documentation is correct, I just saw the wrong example in some stackoverflow post. And my initial issue is with the VS Code/typescript plug-in because it's not showing lint errors for js files even though it is configured to do so. When I run it via the cli it works fine.

So is there any way to get this working with the VS Code/typescript plug-in. No one in my company actually uses ng lint (just linting on save) so it would be nice to have a fix for this. I've tried both of these rules
"import-blacklist": [ true, [ "^primeng.primeng$", "^rxjs.Rx$" ] ], "import-blacklist": [ true, "rxjs/Rx", "primeng/primeng" ],
and neither throws an error when writing import { MessageModule } from 'primeng/primeng';

Blacklisting named imports have been added in v5.12

馃 Beep boop! 馃憠 TSLint is deprecated 馃憟 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