Conversation
Thanks for your interest in palantir/tslint, @artursvonda! 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. |
src/rules/newlineBeforeReturnRule.ts
Outdated
|
||
private getIndex(node: ts.Node) { | ||
const children = node.parent ? node.parent.getChildren() : []; | ||
// For some reason instances are not the same and indexOf doesn't work with given node directly |
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.
getChildren
does not return what you expect, that's why indexOf
does not work here:
node.parent
is either ts.BlockLike
or ts.IfStatement
, ts.IterationStatement
, ...
If it is not ts.BlockLike
the return statement is the only child, e.g. an if statement without a block and only a single return statement. So you don't have to call getChildren
.
If it is ts.BlockLike
getChildren
will return [OpenBraceToken, SyntaxList, CloseBraceToken]
where SyntaxList is an array of statements within the block. But you don't need this SyntaxList, because BlockLike has a statements
property (which also contains all statements, duh)
here is a simpler solution:
private getIndex(node: ts.Node) {
const parent = node.parent!; // use ! (non null assertion) because we know every node has a parent (except ts.SourceFile)
if (isBlockLike(parent) {
return parent.statements.indexOf(node);
} {
// `node` is the only statement within this "block scope" -> do whatever you need to do in this case
}
}
...
function isBlockLike(node: ts.Node): node is ts.BlockLike {
return "statements" in node; // could also be node.kind === ts.SyntaxKind.Block || node.kind === ts.SyntaxKind.CaseClause || to be continued ...
}
src/rules/newlineBeforeReturnRule.ts
Outdated
const position = source.getPositionOfLineAndCharacter(line - 1, 0); | ||
const end = source.getPositionOfLineAndCharacter(line, 0) - 1; | ||
const all = source.getText().substr(position, end - position); | ||
const trimmed = all.replace(/\s+/, ""); |
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 could also just use all.search(/\S/) !== -1
src/rules/newlineBeforeReturnRule.ts
Outdated
super.visitReturnStatement(node); | ||
|
||
let start = node.getStart(); | ||
const comments = ts.getLeadingCommentRanges(node.getSourceFile().text, node.getFullStart()); |
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.
use this.getSourceFile()
instead of node.getSourceFile()
the latter walks up the whole AST until it finds the SourceFile, which is unnecessary work
Check for block-like parent to better understand return's position within parent
Thanks @ajafff for all the comments and suggestions. I addressed those comments and simplified the code a bit. |
src/rules/newlineBeforeReturnRule.ts
Outdated
|
||
private validateBlankLine(line: number) { | ||
const source = this.getSourceFile(); | ||
const position = source.getPositionOfLineAndCharacter(line - 1, 0); |
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 would happen there was a return on the first line?
does this handle other cases where someone writes a return statement on the same line as another statement?
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.
Good point. Not sure how that should be handled. Any ideas/suggestions? Meanwhile I'll add a test case.
I'm leaning toward always flagging if there's another statement on the same line as the return statement |
@nchen63 Yeah, makes sense. The idea is that |
@artursvonda thanks! |
@artursvonda this rule is a good candidate for an auto fixer too |
@nchen63 No problem. And yeah, will see if I can add the fixed over some evening. |
This should probably have been called |
PR checklist
What changes did you make?
A new rule to force blank lines before return. Based on http://eslint.org/docs/rules/newline-before-return
Is there anything you'd like reviewers to focus on?
Making sure all test cases are covered. I took the base file from eslint and added a few cases.
[new-rule]
newline-before-return