Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Add arrow-return-shorthand rule #1972

Merged
merged 6 commits into from
Jan 7, 2017

Conversation

andy-hanson
Copy link
Contributor

PR checklist

  • Addresses an existing issue: #0000
  • New feature, bugfix, or enhancement
    • Includes tests
  • Documentation update
  • Enable CircleCI for your fork (https://circleci.com/add-projects)

What changes did you make?

Added the expression-valued-functions rule, which suggests to change () => { return x; } to () => x.

/* tslint:enable:object-literal-sort-keys */

public static FAILURE_STRING =
"Arrow function body may be converted just this expression, with no `{ return ... }`.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about This arrow function body can be simplified by omitting the curly braces and the keyword 'return' where the function body is underlined. Not sure that people will get what "this expression" means and that excluding { return ... } still includes the ...

@@ -0,0 +1,29 @@
// Invalid:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a test case for returning an object literal?

super.visitArrowFunction(node);
}

private fix(arrowFunction: ts.FunctionLikeDeclaration, body: ts.Block, expr: ts.Expression): Lint.Fix | undefined {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename to createArrowFunctionFix

}

class Walker extends Lint.RuleWalker {
public visitArrowFunction(node: ts.FunctionLikeDeclaration) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are only handling ts.ArrowFunctions here, so you can use it as your parameter's type. Same in the fix method below.
You can then omit the check for node.body && in the condition below, as it will always be defined.

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "expression-valued-functions",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like this name. To me it's not clear what this rule does without looking at the description. I'm not a native speaker though.
I'd rename it to prefer-arrow-shorthand-return or similar ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I like prefer-arrow-shorthand-return as well

public static metadata: Lint.IRuleMetadata = {
ruleName: "expression-valued-functions",
description: "Suggests to convert `() => { return x; }` to `() => x`.",
optionsDescription: "Not configurable.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an option to exclude multiline arrow-functions.
Otherwise it would conflict with https://www.npmjs.com/package/vrsource-tslint-rules#multiline-arrow
It's not a core rule, but I like to use it to increase readability.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should skip multiline arrow functions by default; if you want to lint those too, you can add that as an option

]);

function hasComments(start: number, end: number): boolean {
return !isAllWhitespace(text, start, end);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use ts.get{Leading,Trailing}CommentRanges like in https://github.com/palantir/tslint/blob/master/src/rules/noEmptyRule.ts#L51-L52 instead of implementing it on your own?
If you feel like it, you could add a function to utils.js, e.g. hasCommentBetween(sourceFile, fromNode, toNode)

@andy-hanson andy-hanson changed the title Add expression-valued-functions rule Add prefer-arrow-shorthand-return rule Jan 6, 2017

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
Copy link
Contributor

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?

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "prefer-arrow-shorthand-return",
Copy link
Contributor

Choose a reason for hiding this comment

The 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 object-literal-shorthand. how about "arrow-return-shorthand"?

@andy-hanson andy-hanson force-pushed the expression_valued_functions branch from 00b1d2e to ff252e2 Compare January 7, 2017 02:54
@nchen63 nchen63 merged commit 2688021 into palantir:master Jan 7, 2017
@nchen63
Copy link
Contributor

nchen63 commented Jan 7, 2017

@andy-hanson thanks!

@andy-hanson andy-hanson deleted the expression_valued_functions branch January 8, 2017 17:02
@nchen63 nchen63 changed the title Add prefer-arrow-shorthand-return rule Add arrow-shorthand-return rule Jan 23, 2017
@nchen63 nchen63 changed the title Add arrow-shorthand-return rule Add arrow-return-shorthand rule Jan 23, 2017
@Martin-Luft
Copy link

no-floating-promises requires type checking

This is missing in the rules documentation.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants