Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

v1.3.8 - Lint task is throwing many "All imports are unused." #1052

Closed
hardikns opened this issue Jun 22, 2017 · 27 comments
Closed

v1.3.8 - Lint task is throwing many "All imports are unused." #1052

hardikns opened this issue Jun 22, 2017 · 27 comments

Comments

@hardikns
Copy link

Note: for support questions, please use one of these channels:

https://forum.ionicframework.com/
http://ionicworldwide.herokuapp.com/

Short description of the problem:

Lint task is throwing many "All imports are unused." kind of error after upgrading to 1.3.8. Some error are false positives.

What behavior are you expecting?

Such false positives should not appear. Not sure if this is issue with tslint version 5.2 or something I need to set in the tslint.json

Steps to reproduce:

  1. Create a simple ionic project with one file as under:
  2. Do ionic serve

Sample code below causes
ERROR: ..../src/utils/translate-utils.ts[1, 1]: All imports are unused.

translate-utils.ts:

import { Http } from '@angular/http';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';


export function createTranslateLoader(http: Http) {
  return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}

export function _(s:string) {
  return s;
}

tslint.json:

{
  "rules": {
    "no-duplicate-variable": true,
    "no-unused-variable": [
      true
    ]
  },
  "rulesDirectory": [
    "node_modules/tslint-eslint-rules/dist/rules"
  ]
}

Which @ionic/app-scripts version are you using?
1.3.8

Other information: (e.g. stacktraces, related issues, suggestions how to fix, stackoverflow links, forum links, etc)

@kensodemann
Copy link
Member

I got this as well when going from v1.3.7 to v1.3.12. After finding your post I verified that indeed the issue would occur when going from v1.3.7 to v1.3.8. Plenty of false positives with "no-unused-variable": true

I haven't found a solution, but as a work-around, I went to an angular-cli project that I have and copied the rules from there, leaving out the codelyzer specific rules. The rules used for that project do not include no-unused-variables but do include a lot more formatting stuff. This had the added benefit of catching some places where I had gotten sloppy with spacing or placement of braces.

FWIW, turning no-unused-variable checking on in the angular-cli project had a similar effect with lots of false positives, so I am guessing that there is an issue with tslint and/or TypeScript language services, but that is only a guess on my part.

Here is what my tslint.json is for now:

{
  "rules": {
        "callable-types": true,
    "class-name": true,
    "comment-format": [
      true,
      "check-space"
    ],
    "curly": true,
    "eofline": true,
    "forin": true,
    "import-blacklist": [true, "rxjs"],
    "import-spacing": true,
    "indent": [
      true,
      "spaces"
    ],
    "interface-over-type-literal": true,
    "label-position": true,
    "max-line-length": [
      true,
      140
    ],
    "member-access": false,
    "member-ordering": [
      true,
      "static-before-instance",
      "variables-before-functions"
    ],
    "no-arg": true,
    "no-bitwise": true,
    "no-console": [
      true,
      "debug",
      "info",
      "time",
      "timeEnd",
      "trace"
    ],
    "no-construct": true,
    "no-debugger": true,
    "no-duplicate-variable": true,
    "no-empty": false,
    "no-empty-interface": true,
    "no-eval": true,
    "no-inferrable-types": true,
    "no-shadowed-variable": true,
    "no-string-literal": false,
    "no-string-throw": true,
    "no-switch-case-fall-through": true,
    "no-trailing-whitespace": true,
    "no-unused-expression": true,
    "no-use-before-declare": true,
    "no-var-keyword": true,
    "object-literal-sort-keys": false,
    "one-line": [
      true,
      "check-open-brace",
      "check-catch",
      "check-else",
      "check-whitespace"
    ],
    "prefer-const": true,
    "quotemark": [
      true,
      "single"
    ],
    "radix": true,
    "semicolon": [
      "always"
    ],
    "triple-equals": [
      true,
      "allow-null-check"
    ],
    "typedef-whitespace": [
      true,
      {
        "call-signature": "nospace",
        "index-signature": "nospace",
        "parameter": "nospace",
        "property-declaration": "nospace",
        "variable-declaration": "nospace"
      }
    ],
    "typeof-compare": true,
    "unified-signatures": true,
    "variable-name": false,
    "whitespace": [
      true,
      "check-branch",
      "check-decl",
      "check-operator",
      "check-separator",
      "check-type"
    ]
  },
  "rulesDirectory": [
    "node_modules/tslint-eslint-rules/dist/rules"
  ]
}

@akaztp
Copy link

akaztp commented Jul 4, 2017

Same here.
I've read that using typescript 2.4 solves it for tslint.
Unfortunately Typescript 2.4 is incompatible is RxJS5...

@napcat
Copy link

napcat commented Jul 18, 2017

I'm facing the same problem.

A lot of false positives. For example by simple importing OnInit and implement it in my class will trow an warning after the compilation saying the OnInt is not being used.

@esase
Copy link

esase commented Jul 18, 2017

I have the same trouble. Ionic team why you didnt test carefully packages before release them?

@JaneDawson
Copy link

Unfortunately, the same issue of many unused import warnings occurs with current app-scripts v. 2.0.2

@napcat
Copy link

napcat commented Jul 19, 2017

Yes, I'm also on 2.0.2 and having this issue.

global packages:
ionic/cli-utils : 1.5.0
Cordova CLI : 6.5.0
Ionic CLI : 3.5.0

local packages:
ionic/app-scripts : 2.0.2
ionic/cli-plugin-cordova : 1.4.1
ionic/cli-plugin-ionic-angular : 1.3.2
Cordova Platforms : android 6.2.2
Ionic Framework : ionic-angular 3.5.3

System:
Node : v6.11.1
OS : Windows 10
Xcode : not installed
ios-deploy : not installed
ios-sim : not installed
npm : 5.3.0

@AlejandroSilva
Copy link

AlejandroSilva commented Jul 21, 2017

one trick to remove this warning in the import section of our .ts files, is disable temporary the rule 'no-unused-variable at the start of the file, and re-enable after all the imports are done:

// tslint:disable: no-unused-variable
import {a, b,c } from 'something'
// tslint:enable: no-unused-variable

@Component({...}) ...

source: https://palantir.github.io/tslint/usage/rule-flags/

@mburger81
Copy link
Contributor

@AlejandroSilva what do intend wirh re-enable after all imports are done.
The imports are all fine, tslints warning is justieren wrong.

@PiWiBardy
Copy link

Same issue with version 2.1.0

@daveywc
Copy link

daveywc commented Jul 31, 2017

I am getting the same issue on 2.0.2:

cli packages:

@ionic/cli-plugin-cordova       : 1.5.0 
@ionic/cli-plugin-ionic-angular : 1.4.0
@ionic/cli-utils                : 1.6.0
ionic (Ionic CLI)               : 3.6.0 

global packages:

Cordova CLI : 7.0.1

local packages:

@ionic/app-scripts : 2.0.2
Cordova Platforms  : android 6.2.2 ios 4.4.0 windows 4.4.3
Ionic Framework    : ionic-angular 3.5.3

System:

Android SDK Tools : 26.0.2
Node              : v8.1.2
OS                : Windows 10
npm               : 5.3.0

@Blackbaud-SteveBrush
Copy link

Blackbaud-SteveBrush commented Jul 31, 2017

I've seen this error fire when the imported class isn't being used appropriately. For example, if I import a class and use it as an interface, TSLint doesn't like it:

Fires the "All imports are unused":

import { MyClass } from './my-class';

export class AnotherClass {
  public instance: MyClass;
  constructor() {
    this.instance = {
        name: 'Dave'
    };
  }
}

Passes TSLint:

import { MyClass } from './my-class';

export class AnotherClass {
  public instance: MyClass;
  constructor() {
    this.instance = new MyClass();
    this.instance.name = 'Dave';
  }
}

So, essentially, the imported class isn't recognized as being used, if it's used incorrectly.

Edit:

In Angular 2x, the EventEmitter requires a type; if the type used is a class, you need to instantiate the returned value (otherwise, TSLint will think MyClass is never used). For example:

public myEmitter = new EventEmitter<MyClass>();
// ...
this.myEmitter.emit(new MyClass({ ... }));
// Or...
this.myEmitter.emit({ ... } as MyClass);

@jczaplew
Copy link

Per a comment here, removing declare var * from declarations.d.ts resolved this issue for me.

@daveywc
Copy link

daveywc commented Aug 24, 2017

@jczaplew Thanks for that.... for me it was removing declare module '*' from declarations.d.ts.

@josh-m-sharpe
Copy link

josh-m-sharpe commented Aug 29, 2017

Also removed declare module '*' from declarations.d.ts and cleared a lot of this up for me. Worth noting declarations.d.ts is not generated in a new ionic project.

@mebibou
Copy link

mebibou commented Sep 6, 2017

Yes the declaration.d.ts could be removed from ionic 3.3.0, per changelog:

Another optional step is to remove the src/declarations.d.ts file. This is a legacy file introduced early with ionic-angular projects to improve compatibility between TypeScript and third-party libraries. Due to improvements in TypeScript, this file is no longer necessary. By removing this file, the TypeScript compiler will be able to provide more accurate error messages for import statements.

I believe this issue can be closed.

@rroque6428
Copy link

Yeah, just remove 'src/declarations.d.ts' file. That did the trick. Thanks a LOT. One less problem in my life.

@hardikns
Copy link
Author

Yes removing src/declarations.d.ts worked. We can close this issue.

@josh-m-sharpe
Copy link

Seems inappropriate to close this since others can still run into this and now have to find a closed GitHub issue to resolve it.

Removing the file should be baked into an upgrade script.

@mburger81
Copy link
Contributor

mburger81 commented Oct 31, 2017

I have to import some JavaScript modules files like this
import Locomote from './assets/js/locomote.min';

removing declartion.d.ts file I run into this error:

Typescript Error
Module './assets/js/locomote.min' was resolved to '/abc/src/components/player/flash-player/assets/js/locomote.min.js', but '--allowJs' is not set.

Not sure which should be the right behavior of get TSLINT and custom js imports work together
 
Edit:
Adding allowJs to tsconfig.json does not resolve the problem and have other problems

@Aqib33
Copy link

Aqib33 commented Jan 19, 2018

how to remove this declaration.d.ts file???????

@mebibou
Copy link

mebibou commented Jan 19, 2018

@Aqib33 right click + delete ?

@Aqib33
Copy link

Aqib33 commented Jan 19, 2018

i mean where is this file please guide

@Aqib33
Copy link

Aqib33 commented Jan 19, 2018

@mebibou ??ill be able to delete only if i know where it is & how to do it

@mebibou
Copy link

mebibou commented Jan 19, 2018

@Aqib33 I think it was in the src folder before, but if you dont see it, then it's probably not there.

@Aqib33
Copy link

Aqib33 commented Jan 19, 2018

its not there ;( please tell some, im stuck

@mebibou
Copy link

mebibou commented Jan 19, 2018

@Aqib33 if you have a specific problem, then either go to stackoverflow or create a new issue, this is not a Q&A

@Aqib33
Copy link

Aqib33 commented Jan 19, 2018 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests