-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
build: move unused-variable check from tslint to tsc #1648
Conversation
Disable tslint's "no-unused-variable" rule, it has been deprecated. Enable TypeScript rule "noUnusedLocals", rework all places violating this rule to use a different mechanism for turning this rule off. Checking unused variables at compile time has the following benefits: - Unused variables (locals) are highlighted directly in VS Code. Before, we had to run tslint to discover unused variables, since the VS Code extension for tslint was not able to detect unused vars. - The output of tslint is clean again with no deprecation warnings cluttering the output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
FYI: palantir/tslint#4100 |
class TestClass { | ||
constructor(@inject('foo') foo: string) {} | ||
constructor(@inject('foo') public foo: string) {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the following another way to silent the compiler?
constructor(@inject('foo') foo: string) {
disableUnusedLocalError(foo);
}
IMO, these two lines are functionally different:
constructor(@inject('foo') foo: string) {} // foo is a local arg
constructor(@inject('foo') public foo: string) {} // foo becomes a public property of TestClass
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, these two lines are functionally different
Yes, those lines are different. I thought that difference does not matter in this particular unit test.
Is the following another way to silent the compiler?
It should be, I can give it a try if we agree this is the direction to pursue.
} | ||
disableUnusedLocalError(TestClass); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sigh, this is ugly but it does not seem that TS has fine control or comment based rules.
* | ||
* @param value A value to mark as used for the compiler. | ||
*/ | ||
export function disableUnusedLocalError<T>(value: T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we expect disableUnusedLocalError
to be used in tests only? In most cases, @loopback/testlab
is a dev dependency. What if we have some code in src
that want to use this hack?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we expect
disableUnusedLocalError
to be used in tests only? In most cases,@loopback/testlab
is a dev dependency. What if we have some code insrc
that want to use this hack?
Good point. I hope we won't have unused dependences in production code - that's why we are enabling this linting/compiler rule in the first place. So far, the production code does not need this hack after tsc
fixed re-exports of implicit dependencies in [email protected]
.
Having said that, if there comes a time that we need to keep an unused local in our code, then we will indeed need something like disableUnusedLocalError
to be available in production code too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
build: fix tslint issues related to promises (fd3317e)
This commit LGTM, but for removing unused-variable
check from tslint to tsc, we just need to decide based on the pros and cons of the switch and if the team agrees, I'm fine with it.
@@ -15,9 +15,8 @@ | |||
// User-land promises like Bluebird implement "PromiseLike" (not "Promise") | |||
// interface only. The string "PromiseLike" bellow is needed to | |||
// tell tslint that it's ok to `await` such promises. | |||
"await-promise": [true, "PromiseLike"], | |||
"no-floating-promises": true, | |||
"no-unused-variable": true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had looked into making this PR but decided against it based on palantir/tslint#4100 ... TypeScript doesn't recommend using the noUnusedLocals
flag as an alternative to linting which provides finer line-by-line control. Maybe we should keep this while also using the noUnusedLocals
flag ... at least until there is a final decision from tslint
on deprecating or undeprecating the rule.
To be honest, I am not entirely happy with Based on what I read in palantir/tslint#4100 and the linked discussions in My main objection against |
Given the concern post in #1648 (comment) I tend to not remove
the unused variable/constant are grey in my editor even without adding the rule in tsconfig: Did I miss something? 🤔 And if we do need |
It is a small annoyance indeed ... maybe it'll be resolved once |
Based on the discussion above and a poll we took in our SlackChat, I am closing this pull request as rejected and going to open a new PR with the second commit. |
Now that we no longer need additional imports of referenced types to satisfy the compiler (see #1641), we have the opportunity to enable unused-variable checking at TypeScript level.
This pull request has two parts:
build: move unused-variable check from tslint to tsc (94c4440)
Disable tslint's "no-unused-variable" rule, it has been deprecated.
Enable TypeScript rule "noUnusedLocals", rework all places violating
this rule to use a different mechanism for turning this rule off.
Checking unused variables at compile time has the following benefits:
Unused variables (locals) are highlighted directly in VS Code.
Before, we had to run tslint to discover unused variables, since
the VS Code extension for tslint was not able to detect unused vars.
The output of tslint is clean again with no deprecation warnings
cluttering the output.
build: fix tslint issues related to promises (fd3317e)
After I disabled no-unused-variable check, tslint started to complain about places incorrectly using promises. Some of the warnings were legitimate and I fixed the code. Other cases were not valid, I fixed them by modifying tslint config and adding comments to disable the problematic rule for few source code lines.
Checklist
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated