Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only warn for unused functions and imports during development #45043

Closed
tdymel opened this issue Jul 15, 2021 · 12 comments
Closed

Only warn for unused functions and imports during development #45043

tdymel opened this issue Jul 15, 2021 · 12 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@tdymel
Copy link

tdymel commented Jul 15, 2021

Suggestion

πŸ” Search Terms

allow unused imports, allow unused functions

βœ… Viability Checklist

My suggestion meets these guidelines:

  • [ X ] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [ X ] This wouldn't change the runtime behavior of existing JavaScript code
  • [ X ] This could be implemented without emitting different JS based on the types of the expressions
  • [ X ] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • [ X ] This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

I, as developer, dont want to be constantly annoyed by the compiler exciting because I uncommented some function while debugging code, such that it became unused.

Ideally I would like the compiler to spit out warning during development and spit out errors during production compile.
I can also imagine to add compiler options such as "allowUnusedImport" and "allowUnusedFunction". Currently only unused Variables and parameters are supported.

πŸ“ƒ Motivating Example

function debuggedFunction(): void {
   ... some code ...
   // someFunctionIBrieflyWantToCommentOut();
   ... some code ...
}

private someFunctionIBrieflyWantToCommentOut(): void {
   ... some code ...
}

πŸ’» Use Cases

  • Development
  • Debugging
@RyanCavanaugh
Copy link
Member

Ideally I would like the compiler to spit out warning during development and spit out errors during production compile.

See https://www.typescriptlang.org/tsconfig#extends for how to easily configure separate "development" and "production" compiles.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jul 15, 2021
@RyanCavanaugh
Copy link
Member

Or maybe it sounds like you're asking for something to flag unused imports, separately from the dev/prod distinction? There are lint rules you can enable for those already, which fits your criteria well

@tdymel
Copy link
Author

tdymel commented Jul 15, 2021

Hey @RyanCavanaugh ,
Im not sure we understand each other. Im aware that I can configure separate configs for production and development.
What frustrates me is that I constantly have to either remove unused imports/functions or flag them with "@ts-ignore",
when I just want to briefly comment something out to see if that was the issue.

The error that is spit out at least when I have an unused function is "TS6133". In my opinion it should be configurable if it is a warning or a hard error. As hard error it is just really really annoying during development.

@RyanCavanaugh
Copy link
Member

What frustrates me is that I constantly have to either remove unused imports/functions or flag them with @ts-ignore,
when I just want to briefly comment something out to see if that was the issue.

If you have separate configs, why isn't your dev config set up to ignore unused imports? Isn't that the request?

In my opinion it should be configurable if it is a warning or a hard error

There's no warning/error distinction anywhere in TypeScript.

@tdymel
Copy link
Author

tdymel commented Jul 15, 2021

@RyanCavanaugh
Yeah, that and unused functions.
Could you link me the unused imports/function flag that I have to set?
I cant seem to find those, otherwise I wouldnt have made this suggestion in the first place, I suppose :)

@RyanCavanaugh
Copy link
Member

There isn't a TS setting for this (see #30517), but it can be detected by a syntactic linter

@tdymel
Copy link
Author

tdymel commented Jul 15, 2021

I dont want to detect dead exports though. On the contrary. I dont want the typescript compiler to throw an error on an unsused import or if the file has an unused function.
So if there is no option for this, this suggestion is quite valid then, isnt it?

In the example I gave:

function debuggedFunction(): void {
   ... some code ...
   // someFunctionIBrieflyWantToCommentOut();
   ... some code ...
}

private someFunctionIBrieflyWantToCommentOut(): void {
   ... some code ...
}

The compiler would throw an error that someFunctionIBrieflyWantToCommentOut is unused. But I dont want to compiler to throw an error and exit. I want it to throw a warning an proceed.

@RyanCavanaugh
Copy link
Member

But I dont want to compiler to throw an error and exit. I want it to throw a warning an proceed.

The behavior you're describing here is noEmitOnError being on vs off

@tdymel
Copy link
Author

tdymel commented Jul 16, 2021

@RyanCavanaugh ,
Hi, thanks for answering.
Neither true, nor false seems to have the desired effect.
In both cases this error is emitted and the compilation does not proceed. I have to annotate or remove the function in order to proceed.

Failed to compile.

src/app/app/components/app/app.component.ts:38:11 - error TS6133: 'wambo' is declared but its value is never read.

38   private wambo(): void {

tsconfig.app.json

{
....
"compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": [],
    "noEmitOnError": true // Also tried with false, same result
  },
....
}

@RyanCavanaugh
Copy link
Member

TS does not issue the "Failed to compile" message; this indicates you're using a third-party build tool which would need to provide functionality for disregarding the tsc error code

@tdymel
Copy link
Author

tdymel commented Jul 16, 2021

True, I use angular.
I will dig deeper if that is something specific to it.

@tdymel
Copy link
Author

tdymel commented Jul 19, 2021

For anyone stumbling over it as I did, set following configurations:

"angularCompilerOptions": {
    "noUnusedLocals": false,
    "allowUnusedLabels": true
    ...
}

Thanks @RyanCavanaugh for bearing with me!

@tdymel tdymel closed this as completed Jul 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

2 participants