-
Notifications
You must be signed in to change notification settings - Fork 885
[new-rule] newline-per-chained-call #3278
Changes from 11 commits
f097cfe
e014a53
bf43759
716fa65
d95884d
20f5a7d
09880b0
0fcb7c5
2f4aa08
8296591
696d933
7c1dd59
d23cc2e
160c3b8
eb8aa65
9ecaffd
5f50428
06cd7e0
63af30a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* @license | ||
* Copyright 2017 Palantir Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { isCallExpression, isPropertyAccessExpression, isSameLine } from "tsutils"; | ||
import * as ts from "typescript"; | ||
import * as Lint from ".."; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
/* tslint:disable:object-literal-sort-keys */ | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: "newline-per-chained-call", | ||
description: Lint.Utils.dedent` | ||
Requires that chained method calls be broken apart onto separate lines.`, | ||
rationale: Lint.Utils.dedent` | ||
This style helps to keep code 'vertical', avoiding the need for side-scrolling in IDEs or text editors.`, | ||
optionsDescription: "Not configurable", | ||
options: null, | ||
type: "style", | ||
typescriptOnly: false, | ||
}; | ||
/* tslint:enable:object-literal-sort-keys */ | ||
public static FAILURE_STRING = "When chaining calls, put method calls on new lines."; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithWalker( | ||
new NewlinePerChainedCallWalker(sourceFile, this.ruleName, undefined), | ||
); | ||
} | ||
} | ||
|
||
class NewlinePerChainedCallWalker extends Lint.AbstractWalker<void> { | ||
public walk(sourceFile: ts.SourceFile) { | ||
const checkForSameLine = (node: ts.Node): void => { | ||
if ( | ||
isCallExpression(node) && | ||
isPropertyAccessExpression(node.expression) && | ||
isSameLine( | ||
sourceFile, | ||
node.expression.expression.end, | ||
node.expression.name.pos, | ||
) && | ||
hasChildCall(node) | ||
) { | ||
this.addFailure( | ||
node.expression.name.pos - 1, | ||
node.expression.name.end, | ||
Rule.FAILURE_STRING, | ||
); | ||
} | ||
return ts.forEachChild(node, checkForSameLine); | ||
}; | ||
return ts.forEachChild(sourceFile, checkForSameLine); | ||
} | ||
} | ||
|
||
function hasChildCall(node: ts.CallExpression): boolean { | ||
let callExists = false; | ||
const checkForCall = (child: ts.CallExpression | ts.PropertyAccessExpression): void => { | ||
if (isCallExpression(child) || isPropertyAccessExpression(child)) { | ||
if (isCallExpression(child)) { | ||
callExists = true; | ||
return; | ||
} | ||
return checkForCall(child.expression as | ||
| ts.CallExpression | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this line will not be reached for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nevermind, misread your code |
||
| ts.PropertyAccessExpression); | ||
} | ||
}; | ||
ts.forEachChild(node, checkForCall); | ||
return callExists; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
this.foo(bar()); Instead you can just recurse into the AST by accessing the function hasChildCall(node: ts.PropertyAccessExpression): boolean {
let {expression} = node;
while (isPropertyAccessExpression(expression) || isElementAccessExpression(expression)) {
({expression} = expression);
}
return expression.kind === ts.SyntaxKind.CallExpression;
} also note that you need to handle this.getFoo()[0].toString(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see you already kinda did it this way. I was confused by the closure and the call to |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const y: string[] = _observable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add the two test cases from my comment above and in addition: this.foo()["bar"](); // no error here
foo().bar(); // error here? the current implementation adds one |
||
.map(function(item) { return item.helloYay().another() } | ||
~~~~~~~~ [ERROR] | ||
.operator() | ||
.another(function(result) { return result.hello.Yay! }).wrong(); | ||
~~~~~~ [ERROR] | ||
|
||
SomeClass.propA.helloYay((a: number) => { | ||
return a + 1; | ||
}); | ||
|
||
this.some.nested(); | ||
|
||
const y: string[] = _observable | ||
.map(function(item) { return item.helloYay! }) | ||
.operator() | ||
.another(function(result) { return result.hello.Yay! }); | ||
|
||
|
||
const y: string[] = _observable.map(item => item.helloYay).operator().another(function(result) { return result.helloYay! }); | ||
~~~~~~~~ [ERROR] | ||
~~~~~~~~~ [ERROR] | ||
|
||
const x: string[] = _observable.map(item => item.helloYay); | ||
|
||
SomeClass.propA.propB.helloYay(); | ||
|
||
SomeClass | ||
.propA | ||
.propB | ||
.helloYay(); | ||
|
||
SomeClass | ||
.propA | ||
.propB.helloYay(); | ||
|
||
SomeClass | ||
.propA | ||
.propB | ||
.helloYay(function() { | ||
return 1; | ||
}).test(); | ||
~~~~~ [ERROR] | ||
|
||
SomeClass | ||
.propA | ||
.propB | ||
.helloYay(function() { | ||
return 1; | ||
}). | ||
~ | ||
test(); | ||
~~~~~~~~ [ERROR] | ||
|
||
SomeClass.propA.propB.methodB(() => { | ||
return "hello Yay!"; | ||
}).helloYay((a: number) => { | ||
~~~~~~~~~ [ERROR] | ||
return a + 1; | ||
}); | ||
|
||
SomeClass.propA.propB.methodB(() => { | ||
return "hello Yay!"; | ||
}) | ||
.helloYay((a: number) => { | ||
return a + 1; | ||
}); | ||
|
||
SomeClass | ||
.propA | ||
.propB | ||
.methodC(() => { | ||
return "hello Yay!"; | ||
}) | ||
.helloYay(() => { | ||
return 1; | ||
}); | ||
|
||
[ERROR]: When chaining calls, put method calls on new lines. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"newline-per-chained-call": [true, 1] | ||
} | ||
} |
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.
if you
return true
here,forEachChild
will return early without visiting the remaining children.you could then use
return ts.forEachChild(node, checkForCall) === true
below