-
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
tslint: use a better implementation of rule checking unused variables #2156
Conversation
@@ -5,7 +5,6 @@ | |||
|
|||
import {Application} from '@loopback/core'; | |||
import {expect, toJSON} from '@loopback/testlab'; | |||
import * as _ from 'lodash'; |
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.
Unused import discovered by the new rule.
class testHasManyEntityCrudRepository< | ||
T extends Entity, | ||
ID, | ||
TargetRepository extends EntityCrudRepository<T, ID> |
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.
The new rule complains about unused template parameters too. Since this test is not using ID
and TargetRepository
, I simplified this stub class to declare only parameters that are needed.
@@ -76,7 +76,7 @@ async function startServerCheck(app: Application) { | |||
} | |||
|
|||
function sequenceHandler( | |||
{request, response}: RequestContext, | |||
{response}: RequestContext, |
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.
Unused parameters are reported when they are declared via destructuring.
I have mixed feelings about this change. Originally, I used {request, response}
to communicate what properties are available to sequence handlers. I feel that {response}
is not making that clear enough. On the other hand, the type RequestContext
does contain both request & response, so I guess the "request" is still easy to discover even after this change.
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.
On the other hand, the type RequestContext does contain both request & response, so I guess the "request" is still easy to discover even after this change.
I agree 👍
@@ -692,7 +692,7 @@ describe('Routing', () => { | |||
|
|||
it('provides httpHandler compatible with HTTP server API', async () => { | |||
const app = new RestApplication(); | |||
app.handler(({request, response}, sequence) => response.end('hello')); | |||
app.handler(({response}, sequence) => response.end('hello')); |
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.
Unused parameters are reported when they are declared via destructuring.
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.
It's a bit harder to know that the request
property is also available from the RequestContext than d14974a#r241689363.
@@ -44,7 +44,7 @@ describe('Sequence', () => { | |||
}); | |||
|
|||
it('allows users to define a custom sequence as a function', () => { | |||
server.handler(({request, response}, sequence) => { | |||
server.handler(({response}, sequence) => { |
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.
Unused parameters are reported when they are declared via destructuring.
@@ -98,7 +98,7 @@ describe('Sequence', () => { | |||
class MySequence implements SequenceHandler { | |||
constructor(@inject(SequenceActions.SEND) protected send: Send) {} | |||
|
|||
async handle({request, response}: RequestContext) { | |||
async handle({response}: RequestContext) { |
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.
Unused parameters are reported when they are declared via destructuring.
For longer term, I'd like to refactor Maybe I should do that refactoring as the first step before this pull request, to avoid a breaking change in
I think this is actually the best approach. Once tslint config has its own package, we can clearly communicate breaking changes in the autogenerated release notes. |
Nitpick on first commit message:
it looks like
That sounds great too. |
edd29fb
to
3676614
Compare
Rebased on top of the new master. I need to fix the way how we are installing |
In #2159, I moved tslint config into a standalone package and changed our packages to access that config directly, bypassing
I see two ways how to fix this, see below. Considering the pros and cons, I am leaning towards Option A. @raymondfeng @strongloop/loopback-maintainers Which option do you prefer? Can you think of any better solution? Option A Continue slimming down Pros: Users can decide what exact version of tsc & tslint they want to use and still keep using our builders. This can be very useful when tsc makes a breaking change in semver-minor release as it often does. Also, users can decide what version of LoopBack's tslint config they want to use, independently on Cons: We will have to duplicate the specification of Option B Keep the tslint config bundled inside Pros: No duplicated dev-dependencies, everything stays under Also, users of |
3676614
to
3669714
Compare
Upon further investigation of Option A, it turns out that removing build's dependency on typescript and tslint is too complicated as it breaks other things (e.g. integration tests). I think it's enough to change our project templates to add typescript and tslint as explicit dev-dependencies, and fix build to prefer project-local version of typescript/tslint over the version installed as build's dependency - see #2211 |
3669714
to
37262cd
Compare
37262cd
to
5ca18e0
Compare
Done in a5e1aef. This pull request is ready for final review and landing as far as I am concerned. @jannyHou @raymondfeng @strongloop/loopback-maintainers please review. |
Travis CI is failing in code-lint step, I'll investigate the problem next week.
|
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.
Great effort! LGTM, just one nitpick suggestion for adding some doc.
// License text available at https://opensource.org/licenses/MIT | ||
|
||
/** | ||
* This is an internal script to synchronize versions of dev-dependencies |
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.
@bajtos When should we use this command? My guess is when updating a tslint/typescript version in the root level package.json?
A nitpick: maybe add some explanation in the documentation to remind developers to run it when updating dependencies.
Good catch, I was thinking about the same too 👍 |
5ca18e0
to
2e8c69f
Compare
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.
Cool 🚢
This change is necessary to allow `@loopback/tslint-config` to include tslint extensions from 3rd-party packages, because these extensions typically have a peer dependency on `tslint`. Another benefit is that projects can decide now which version of `tsc` and `tslint` they want to use, independently of the version bundled inside `@loopback/build`. To make it easier to keep typescript & tslint version specifiers in different places in sync, a new helper script is added to propagate changes in dev-dependencies from monorepo's root package.json to example projects: "npm run sync-dev-deps"
Fix code-lint to install dependencies of `@loopback/eslint-config`.
The built-in rule "no-unused-variable" is implemented in a hacky way that breaks other rules depending on TSC API. The 3rd-party rule "no-unused" does not require TSC API and thus can be checked from VS Code too. BREAKING CHANGE: The rule "no-unused-variable" has been replaced with "no-unused" rule. Code comments disabling "no-unused-variable" rule will no longer work and must be changed to disable "no-unused" rule instead.
2e8c69f
to
6a400f2
Compare
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.
Nice work! 👏
The built-in
no-unused-variable
rule has many issues.tslint
is printing the following warning now:I did an extensive search for alternatives and found
no-unused
fromtslint-consistent-codestyle
as the best alternative. See their docs to learn more about the rule.In this pull request:
@loopback/tslint-config
and the new moduletslint-consistent-cdestyle
.no-unused-variable
fromtslint.build.json
and configuringno-unused
intslint.common.json
.The third commit fixes configuration of VSCode tasks to use a correct problem matcher.
BREAKING CHANGES
This pull request introduces a breaking change in
@loopback/tslint-config
. People using comments to disable no-unused check for certain lines have to use a different directive after this change.The new rule is also more strict and reports unused variables/parameters that are not detected by the built-in
no-unused-variable
rule.cc @strongloop/loopback-next
Checklist
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated