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

[bugfix] callable types suggestion forgot parens on unions and intersections #3342

Merged
merged 4 commits into from Oct 18, 2017
Merged

[bugfix] callable types suggestion forgot parens on unions and intersections #3342

merged 4 commits into from Oct 18, 2017

Conversation

cyrilgandon
Copy link
Contributor

PR checklist

Overview of change:

Is there anything you'd like reviewers to focus on?

CHANGELOG.md entry:

[bugfix] callable-types auto fix produces invalid results


let suggestion = `${text.substr(0, colonPos)} =>${text.substr(colonPos + 1)}`;
if (parent.parent !== undefined && shouldWrapSuggestion(parent.parent)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

.parent can never be undefined here. you can simply assert that it is not undefined with shouldWrapSuggestion(parent.parent!)

@@ -32,6 +32,16 @@ var fn: {(): void;};
~~~~~~~~~ [type % ('() => void')]
function f(x: { (): void }): void;
~~~~~~~~ [type % ('() => void')]
function f(x: string | { (): 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.

also add a test where the type is already wrapped in parentheses, e.g. : string | ({ (): void })

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The funny thing is that it still works and doesn't add parens in this case. I guess that if there is already parens, parent.parent doesn't reference the Union, but reference the node with the extra parens.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's right, parent.parent is ts.ParenthesizedType in that case. That's why I wanted this test case. To verify that we don't add parens if they are already present.

@@ -46,7 +46,7 @@ export class Rule extends Lint.Rules.AbstractRule {
function walk(ctx: Lint.WalkContext<void>) {
return ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void {
if ((isInterfaceDeclaration(node) && noSupertype(node)
|| isTypeLiteralNode(node))
|| isTypeLiteralNode(node))
Copy link
Contributor

Choose a reason for hiding this comment

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

the alignment here was actually intentional because of the parens. it makes more sense to just remove the line break

@ajafff ajafff merged commit 74d84be into palantir:master Oct 18, 2017
@ajafff
Copy link
Contributor

ajafff commented Oct 18, 2017

Thanks @cyrilgandon

@cyrilgandon cyrilgandon deleted the bugfix-callable-types-3334 branch October 18, 2017 14:06
HyphnKnight pushed a commit to HyphnKnight/tslint that referenced this pull request Apr 9, 2018
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.

None yet

2 participants