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

whitespace: add option check-postblock-prekeyword #3308

Closed

Conversation

yubaoquan
Copy link

@yubaoquan yubaoquan commented Oct 11, 2017

check-postblock-prekeyword

Example:

before lint:

try {
}catch {
}finally {
}

if () {
}else {
}

after lint:

try {
} catch {
} finally {
}


if () {
} else {
}

PR checklist

Overview of change:

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

CHANGELOG.md entry:

[new-rule] check-postblock-prekeyword
[new-rule]

check-postblock-prekeyword
@palantirtech
Copy link
Member

Thanks for your interest in palantir/tslint, @yubaoquan! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request.

@yubaoquan
Copy link
Author

signed the individual CLA on October 12, 2017 at 12:03 AM. @palantirtech

* \`"check-preblock"\` checks for whitespace before the opening brace of a block`,
* \`"check-preblock"\` checks for whitespace before the opening brace of a block.
* \`"check-postblock-prekeyword"\` checks for whitespace between the closing brace of a block
and the following keyword (\`} else\` / \`} catch\` / \`} while\`).`,
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the difference to one-line with "check-whitespace"?

Copy link
Author

Choose a reason for hiding this comment

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

I reviewed the one-line rule code and its test case. Didn't find that rule solve the situation of this PR. And I tried to add one-line with all its options to my test file, the linter didn't report error of missing whitespace between closing bracket and else

Copy link
Contributor

Choose a reason for hiding this comment

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

#3200 includes the check for whitespace before else.
The only thing missing is whitespace before while in do {...} while (...); loops. But that could easily be added.

Copy link
Author

Choose a reason for hiding this comment

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

So is my pr going to be merged?

Copy link
Contributor

Choose a reason for hiding this comment

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

I have no preference into which rule the functionality should go. However, we generally try to avoid duplication.
I'll leave that decision to @adidahiya

Copy link
Contributor

Choose a reason for hiding this comment

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

@yubaoquan do you generally use one-line with "check-whitespace" in your lint config? if so, I think we should just add the additional check @ajafff suggested to one-line instead of a new option here.

case ts.SyntaxKind.ElseKeyword:
case ts.SyntaxKind.CatchKeyword:
case ts.SyntaxKind.FinallyKeyword:
if (node.end === nextToken.getStart()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

you can simply use checkForTrailingWhitespace(node.end)

Copy link
Author

Choose a reason for hiding this comment

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

Changed as you said.

break;
}
switch (nextToken.kind) {
case ts.SyntaxKind.WhileKeyword:
Copy link
Contributor

Choose a reason for hiding this comment

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

This may do a bit too much. That WhileKeyword could be from the while (...) {} statement following the block.
That may not be a big deal, because it will only complain about such code:

if (condition) { /* code */ }while(condition) {};
                             ~ [missing whitespace]

Anyway, there's an easier way to do this check without getNextToken:

const parent = node.parent!;
if (isDoStatement(parent) || // Block is followed by WhiteKeyword
    isIfStatement(parent) && parent.elseStatement !== undefined && parent.thenStatement === node || // Block is followed by ElseKeyword
    isTryStatement(parent) && (
        parent.tryBlock === node || // Block is followed by CatchKeyword or FinallyKeyword
        parent.catchClause !== undefined && parent.catchClause.block === node // Block is followed by FinallyKeyword
    )
) {
    // do stuff
}

Well, while writing it down I realized it's not really easier. But it's clearer what kind of statement is checked.

Copy link
Author

Choose a reason for hiding this comment

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

OK. I wrote a function to check conditions as you mentions.

@ajafff ajafff changed the title Add Feature whitespace: add option check-postblock-prekeyword Oct 12, 2017
&& parent.elseStatement !== undefined
&& parent.elseStatement !== node;
const isTryCatchFinally = utils.isTryStatement(parent)
&& (parent.tryBlock === node || parent.catchClause === node);
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use the following to avoid the additional case for CatchClause above (sorry my initial suggestion got that part wrong):

const isTryCatchFinally = utils.isTryStatement(parent) && parent.tryBlock === node ||
    utils.isCatchClause(parent) && parent.parent!.finallyBlock !== undefined;

Copy link
Author

Choose a reason for hiding this comment

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

Done

@@ -306,7 +320,16 @@ function walk(ctx: Lint.WalkContext<Options>) {
}
}
});

function matchPostblockPreKeyword(node: ts.Node): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider moving this function out of the walk closure. It doesn't use any variables from that scope, so it doesn't need to be here.

Copy link
Author

Choose a reason for hiding this comment

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

Done

* \`"check-preblock"\` checks for whitespace before the opening brace of a block`,
* \`"check-preblock"\` checks for whitespace before the opening brace of a block.
* \`"check-postblock-prekeyword"\` checks for whitespace between the closing brace of a block
and the following keyword (\`} else\` / \`} catch\` / \`} while\`).`,
Copy link
Contributor

Choose a reason for hiding this comment

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

I have no preference into which rule the functionality should go. However, we generally try to avoid duplication.
I'll leave that decision to @adidahiya

1. move function `matchPositionblockPreKeyword` out of walk closure
2. update logic of checking for finally clause in
`matchPositionblockPreKeyword`
3. remove check for catch block in walk
@jkillian
Copy link
Contributor

jkillian commented May 1, 2018

it's a little confusing, but functionality should go in the one-line rule likely, see #3308 (comment)

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

Successfully merging this pull request may close these issues.

5 participants