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 887
Add arrow-return-shorthand
rule
#1972
Merged
nchen63
merged 6 commits into
palantir:master
from
andy-hanson:expression_valued_functions
Jan 7, 2017
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
158a6cf
Add `expression-valued-functions` rule
andy-hanson 47d4fcc
Respond to PR comments
andy-hanson e104026
Handle object literals
andy-hanson f7ca02f
Merge branch 'master' into expression_valued_functions
andy-hanson ff252e2
Rename to arrow-return-shorthand and fix metadata
andy-hanson bfa97ba
Merge branch 'master' into expression_valued_functions
andy-hanson 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,14 @@ | ||
--- | ||
ruleName: prefer-arrow-shorthand-return | ||
description: 'Suggests to convert `() => { return x; }` to `() => x`.' | ||
optionsDescription: Not configurable. | ||
options: null | ||
optionExamples: | ||
- '[true]' | ||
- '[true, "multiline"]' | ||
type: functionality | ||
typescriptOnly: false | ||
layout: rule | ||
title: 'Rule: prefer-arrow-shorthand-return' | ||
optionsJSON: 'null' | ||
--- |
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
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
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,105 @@ | ||
/** | ||
* @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 * as ts from "typescript"; | ||
|
||
import * as Lint from "../index"; | ||
|
||
const OPTION_MULTILINE = "multiline"; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
/* tslint:disable:object-literal-sort-keys */ | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: "prefer-arrow-shorthand-return", | ||
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. sorry for changing this up again -- I actually think we can simplify the name further and put "shorthand" at the end to be consistent with |
||
description: "Suggests to convert `() => { return x; }` to `() => x`.", | ||
optionsDescription: "Not configurable.", | ||
options: null, | ||
optionExamples: [ | ||
`[true]`, | ||
`[true, "${OPTION_MULTILINE}"]`, | ||
], | ||
type: "functionality", | ||
typescriptOnly: false, | ||
}; | ||
/* tslint:enable:object-literal-sort-keys */ | ||
|
||
public static FAILURE_STRING = | ||
"This arrow function body can be simplified by omitting the curly braces and the keyword 'return'."; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithWalker(new Walker(sourceFile, this.getOptions())); | ||
} | ||
} | ||
|
||
class Walker extends Lint.RuleWalker { | ||
public visitArrowFunction(node: ts.ArrowFunction) { | ||
if (node.body && node.body.kind === ts.SyntaxKind.Block) { | ||
const expr = getSimpleReturnExpression(node.body as ts.Block); | ||
if (expr !== undefined && (this.hasOption(OPTION_MULTILINE) || !this.isMultiline(node.body))) { | ||
this.addFailureAtNode(node.body, Rule.FAILURE_STRING, this.createArrowFunctionFix(node, node.body as ts.Block, expr)); | ||
} | ||
} | ||
|
||
super.visitArrowFunction(node); | ||
} | ||
|
||
private isMultiline(node: ts.Node): boolean { | ||
const getLine = (position: number) => this.getLineAndCharacterOfPosition(position).line; | ||
return getLine(node.getEnd()) > getLine(node.getStart()); | ||
} | ||
|
||
private createArrowFunctionFix(arrowFunction: ts.FunctionLikeDeclaration, body: ts.Block, expr: ts.Expression): Lint.Fix | undefined { | ||
const text = this.getSourceFile().text; | ||
const statement = expr.parent!; | ||
const returnKeyword = Lint.childOfKind(statement, ts.SyntaxKind.ReturnKeyword)!; | ||
const arrow = Lint.childOfKind(arrowFunction, ts.SyntaxKind.EqualsGreaterThanToken)!; | ||
const openBrace = Lint.childOfKind(body, ts.SyntaxKind.OpenBraceToken)!; | ||
const closeBrace = Lint.childOfKind(body, ts.SyntaxKind.CloseBraceToken)!; | ||
const semicolon = Lint.childOfKind(statement, ts.SyntaxKind.SemicolonToken); | ||
|
||
const anyComments = hasComments(arrow) || hasComments(openBrace) || hasComments(statement) || hasComments(returnKeyword) || | ||
hasComments(expr) || (semicolon && hasComments(semicolon)) || hasComments(closeBrace); | ||
return anyComments ? undefined : this.createFix( | ||
// Object literal must be wrapped in `()` | ||
...(expr.kind === ts.SyntaxKind.ObjectLiteralExpression ? [ | ||
this.appendText(expr.getStart(), "("), | ||
this.appendText(expr.getEnd(), ")"), | ||
] : []), | ||
// " {" | ||
deleteFromTo(arrow.end, openBrace.end), | ||
// "return " | ||
deleteFromTo(statement.getStart(), expr.getStart()), | ||
// " }" (may include semicolon) | ||
deleteFromTo(expr.end, closeBrace.end), | ||
); | ||
|
||
function hasComments(node: ts.Node): boolean { | ||
return ts.getTrailingCommentRanges(text, node.getEnd()) !== undefined; | ||
} | ||
} | ||
} | ||
|
||
/** Given `{ return x; }`, return `x`. */ | ||
function getSimpleReturnExpression(block: ts.Block): ts.Expression | undefined { | ||
return block.statements.length === 1 && block.statements[0].kind === ts.SyntaxKind.ReturnStatement | ||
? (block.statements[0] as ts.ReturnStatement).expression | ||
: undefined; | ||
} | ||
|
||
function deleteFromTo(start: number, end: number): Lint.Replacement { | ||
return new Lint.Replacement(start, end - start, ""); | ||
} |
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
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
23 changes: 23 additions & 0 deletions
23
test/rules/prefer-arrow-shorthand-return/default/test.js.fix
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,23 @@ | ||
// Copy of test.ts.lint | ||
|
||
// Invalid: | ||
(() => 0); | ||
(() => ({ x: 1 })); | ||
(() => { | ||
return 0; | ||
}); | ||
|
||
// Valid: | ||
(() => 0); | ||
(() => {}); | ||
(() => { throw 0; }) | ||
(() => { const x = 0; return x; }); | ||
|
||
// No fix if there's a comment. | ||
(() => /**/ { return 0; }); | ||
(() => { /**/ return 0; }); | ||
(() => { return /**/ 0; }); | ||
(() => { return 0 /**/ }); | ||
(() => { return 0 /**/; }); | ||
(() => { return 0; /**/ }); | ||
|
32 changes: 32 additions & 0 deletions
32
test/rules/prefer-arrow-shorthand-return/default/test.js.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,32 @@ | ||
// Copy of test.ts.lint | ||
|
||
// Invalid: | ||
(() => { return 0; }); | ||
~~~~~~~~~~~~~ [0] | ||
(() => { return { x: 1 } }); | ||
~~~~~~~~~~~~~~~~~~~ [0] | ||
(() => { | ||
return 0; | ||
}); | ||
|
||
// Valid: | ||
(() => 0); | ||
(() => {}); | ||
(() => { throw 0; }) | ||
(() => { const x = 0; return x; }); | ||
|
||
// No fix if there's a comment. | ||
(() => /**/ { return 0; }); | ||
~~~~~~~~~~~~~ [0] | ||
(() => { /**/ return 0; }); | ||
~~~~~~~~~~~~~~~~~~ [0] | ||
(() => { return /**/ 0; }); | ||
~~~~~~~~~~~~~~~~~~ [0] | ||
(() => { return 0 /**/ }); | ||
~~~~~~~~~~~~~~~~~ [0] | ||
(() => { return 0 /**/; }); | ||
~~~~~~~~~~~~~~~~~~ [0] | ||
(() => { return 0; /**/ }); | ||
~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
[0]: This arrow function body can be simplified by omitting the curly braces and the keyword 'return'. |
21 changes: 21 additions & 0 deletions
21
test/rules/prefer-arrow-shorthand-return/default/test.ts.fix
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,21 @@ | ||
// Invalid: | ||
(() => 0); | ||
(() => ({ x: 1 })); | ||
(() => { | ||
return 0; | ||
}); | ||
|
||
// Valid: | ||
(() => 0); | ||
(() => {}); | ||
(() => { throw 0; }) | ||
(() => { const x = 0; return x; }); | ||
|
||
// No fix if there's a comment. | ||
(() => /**/ { return 0; }); | ||
(() => { /**/ return 0; }); | ||
(() => { return /**/ 0; }); | ||
(() => { return 0 /**/ }); | ||
(() => { return 0 /**/; }); | ||
(() => { return 0; /**/ }); | ||
|
30 changes: 30 additions & 0 deletions
30
test/rules/prefer-arrow-shorthand-return/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,30 @@ | ||
// Invalid: | ||
(() => { return 0; }); | ||
~~~~~~~~~~~~~ [0] | ||
(() => { return { x: 1 } }); | ||
~~~~~~~~~~~~~~~~~~~ [0] | ||
(() => { | ||
return 0; | ||
}); | ||
|
||
// Valid: | ||
(() => 0); | ||
(() => {}); | ||
(() => { throw 0; }) | ||
(() => { const x = 0; return x; }); | ||
|
||
// No fix if there's a comment. | ||
(() => /**/ { return 0; }); | ||
~~~~~~~~~~~~~ [0] | ||
(() => { /**/ return 0; }); | ||
~~~~~~~~~~~~~~~~~~ [0] | ||
(() => { return /**/ 0; }); | ||
~~~~~~~~~~~~~~~~~~ [0] | ||
(() => { return 0 /**/ }); | ||
~~~~~~~~~~~~~~~~~ [0] | ||
(() => { return 0 /**/; }); | ||
~~~~~~~~~~~~~~~~~~ [0] | ||
(() => { return 0; /**/ }); | ||
~~~~~~~~~~~~~~~~~~ [0] | ||
|
||
[0]: This arrow function body can be simplified by omitting the curly braces and the keyword 'return'. |
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,8 @@ | ||
{ | ||
"rules": { | ||
"prefer-arrow-shorthand-return": true | ||
}, | ||
"jsRules": { | ||
"prefer-arrow-shorthand-return": true | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
test/rules/prefer-arrow-shorthand-return/multiline/test.ts.fix
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,20 @@ | ||
// Invalid: | ||
(() => 0); | ||
(() => ({ x: 1 })); | ||
(() => | ||
0); | ||
|
||
// Valid: | ||
(() => 0); | ||
(() => {}); | ||
(() => { throw 0; }) | ||
(() => { const x = 0; return x; }); | ||
|
||
// No fix if there's a comment. | ||
(() => /**/ { return 0; }); | ||
(() => { /**/ return 0; }); | ||
(() => { return /**/ 0; }); | ||
(() => { return 0 /**/ }); | ||
(() => { return 0 /**/; }); | ||
(() => { return 0; /**/ }); | ||
|
Oops, something went wrong.
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.
can you please add
hasFix: true
to the metadata?