Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Added a no-duplicate-switch-case rule #2937

Merged
merged 8 commits into from
Sep 3, 2017
Merged

Added a no-duplicate-switch-case rule #2937

merged 8 commits into from
Sep 3, 2017

Conversation

JoshuaKGoldberg
Copy link
Contributor

PR checklist

Overview of change:

Added a no-duplicate-switch-case rule to enforce that no switch case should contain the same textual value as a previous switch statement.

CHANGELOG.md entry:

[new-rule] no-duplicate-switch-case

}
}

const walk = (ctx: Lint.WalkContext<void>): void => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider converting these lambdas to regular function declarations

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason behind the preference for regular function declarations? Elsewhere I've seen folks prefer arrow lambdas for the saner scoping & lack of hoisting weirdness.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of the codebase seems to follow the pattern to use function declarations where possible and only use arrow functions where this reference matters.
I prefer regular functions because I think it's easier to notice while scanning through the code

const previousCases = new Set<string>();

return (node: ts.CaseBlock): void => {
previousCases.clear();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to return a closure or clear the set.
just make previousCases a local variable and loop over the cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a minor performance optimization to keep the same set across iterations. Do you consider the lessened clarity not worth it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would guess clearing the set is as expensive as creating a new one.
I don't think it's worth optimizing here

continue;
}

const text = clause.expression.getText();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider passing ctx.sourceFile to .getText to avoid unnecessary work

@ajafff
Copy link
Contributor

ajafff commented Jul 21, 2017

@JoshuaKGoldberg The rule needs to be added to all.ts and latest.ts. That's why CI is failing.

@ajafff ajafff added this to the TSLint v5.8 milestone Sep 2, 2017
@ajafff
Copy link
Contributor

ajafff commented Sep 2, 2017

I'd like to get this merged. So I updated the configuration presets and merged master.

@adidahiya do you want to take another look before merging this?

Copy link
Contributor

@adidahiya adidahiya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@ajafff ajafff merged commit 6b4c4ea into palantir:master Sep 3, 2017
@JoshuaKGoldberg JoshuaKGoldberg deleted the no-duplicate-switch-case branch September 3, 2017 14:41
@JoshuaKGoldberg
Copy link
Contributor Author

Thanks @ajafff for the updates!

A side note: there are a few other PRs from me waiting for action (on my part). I don't know when I'll be able to get to them :(

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

Successfully merging this pull request may close these issues.

3 participants