This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 889
[new-rule] newline-per-chained-call #3278
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f097cfe
init implementation
e014a53
Tried to simplify getChainLength function
bf43759
narrowed walker to propertyAccessExpression nodes
716fa65
files
d95884d
description update
20f5a7d
additional test cases; removed nested if
09880b0
moving to doc issue branch...
0fcb7c5
test files deleted; simplified rule logic; options removed for now
2f4aa08
writes newline index to mem once
8296591
trying approach which grabs all relevant nodes first, then loops thro…
696d933
hasChildCall method to check nodes for callExpr
7c1dd59
hasChildCall logic -> while loop
d23cc2e
latest change requests minus element access fix
160c3b8
Merge remote-tracking branch 'tslint/master' into newline-per-chained…
eb8aa65
latest package.json
9ecaffd
latest master
5f50428
additional tests
06cd7e0
package.json modification reverted
63af30a
additional tests for 3x nested calls
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* @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, | ||
isElementAccessExpression, | ||
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.expression) | ||
) { | ||
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.PropertyAccessExpression): boolean { | ||
let { expression } = node; | ||
while ( | ||
isPropertyAccessExpression(expression) || | ||
isElementAccessExpression(expression) | ||
) { | ||
({ expression } = expression); | ||
} | ||
return expression.kind === ts.SyntaxKind.CallExpression; | ||
} |
120 changes: 120 additions & 0 deletions
120
test/rules/newline-per-chained-call/default/test.ts.lint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
this.getFoo()[0].toString(); | ||
~~~~~~~~~ [ERROR] | ||
|
||
this.foo()["bar"]().buzz(); | ||
~~~~~ [ERROR] | ||
|
||
this.foo()["bar"](); | ||
|
||
foo().bar(); | ||
~~~~ [ERROR] | ||
|
||
const y: string[] = _observable | ||
.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 | ||
.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.methodB(() => { | ||
return "hello Yay!"; | ||
}) | ||
.helloYay((a: number) => { | ||
return obj.method() | ||
.chainedButOkay( | ||
objB.nested().superNestedCall() | ||
~~~~~~~~~~~~~~~~ [ERROR] | ||
) | ||
}); | ||
|
||
SomeClass.propA.propB.methodB(() => { | ||
return "hello Yay!"; | ||
}) | ||
.helloYay((a: number) => { | ||
return obj.method() | ||
.chainedButOkay( | ||
objB.nested() | ||
.superNestedCall() | ||
) | ||
}); | ||
|
||
SomeClass | ||
.propA | ||
.propB | ||
.methodC(() => { | ||
return "hello Yay!"; | ||
}) | ||
.helloYay(() => { | ||
return 1; | ||
}); | ||
|
||
[ERROR]: When chaining calls, put method calls on new lines. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"newline-per-chained-call": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
please add the two test cases from my comment above
and in addition: