-
Notifications
You must be signed in to change notification settings - Fork 885
whitespace: add option check-postblock-prekeyword #3308
whitespace: add option check-postblock-prekeyword #3308
Conversation
check-postblock-prekeyword
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. |
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\`).`, |
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.
what's the difference to one-line
with "check-whitespace"
?
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 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
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.
#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.
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.
So is my pr going to be merged?
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 have no preference into which rule the functionality should go. However, we generally try to avoid duplication.
I'll leave that decision to @adidahiya
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.
@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.
src/rules/whitespaceRule.ts
Outdated
case ts.SyntaxKind.ElseKeyword: | ||
case ts.SyntaxKind.CatchKeyword: | ||
case ts.SyntaxKind.FinallyKeyword: | ||
if (node.end === nextToken.getStart()) { |
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.
you can simply use checkForTrailingWhitespace(node.end)
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.
Changed as you said.
src/rules/whitespaceRule.ts
Outdated
break; | ||
} | ||
switch (nextToken.kind) { | ||
case ts.SyntaxKind.WhileKeyword: |
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.
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.
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.
OK. I wrote a function to check conditions as you mentions.
src/rules/whitespaceRule.ts
Outdated
&& parent.elseStatement !== undefined | ||
&& parent.elseStatement !== node; | ||
const isTryCatchFinally = utils.isTryStatement(parent) | ||
&& (parent.tryBlock === node || parent.catchClause === node); |
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.
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;
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.
Done
src/rules/whitespaceRule.ts
Outdated
@@ -306,7 +320,16 @@ function walk(ctx: Lint.WalkContext<Options>) { | |||
} | |||
} | |||
}); | |||
|
|||
function matchPostblockPreKeyword(node: ts.Node): boolean { |
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.
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.
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.
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\`).`, |
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 have no preference into which rule the functionality should go. However, we generally try to avoid duplication.
I'll leave that decision to @adidahiya
it's a little confusing, but functionality should go in the |
check-postblock-prekeyword
Example:
before lint:
after lint:
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]