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

Commit

Permalink
Added fixer for newline-before-return rule
Browse files Browse the repository at this point in the history
  • Loading branch information
jukben committed Jan 26, 2019
1 parent 4958030 commit a27f247
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/rules/newlineBeforeReturnRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ import { getPreviousStatement } from "tsutils";
import * as ts from "typescript";
import * as Lint from "../index";

const INDENTATION_REGEX = /([ \t]*)$/;

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "newline-before-return",
description: "Enforces blank line before return when not the only line in the block.",
rationale: "Helps maintain a readable style in your codebase.",
hasFix: true,
optionsDescription: "Not configurable.",
options: {},
optionExamples: [true],
Expand Down Expand Up @@ -53,6 +56,12 @@ class NewlineBeforeReturnWalker extends Lint.AbstractWalker<void> {
return ts.forEachChild(sourceFile, cb);
}

private getIndentation(node: ts.Node): string {
const text = this.sourceFile.text.substr(node.pos, node.getStart() - node.pos);
const matches = text.match(INDENTATION_REGEX);
return matches !== null ? matches[1] : "";
}

private visitReturnStatement(node: ts.ReturnStatement) {
const prev = getPreviousStatement(node);
if (prev === undefined) {
Expand All @@ -77,10 +86,18 @@ class NewlineBeforeReturnWalker extends Lint.AbstractWalker<void> {
}
}
const prevLine = ts.getLineAndCharacterOfPosition(this.sourceFile, prev.end).line;

if (prevLine >= line - 1) {
const indentationCurrent = this.getIndentation(node);
const indentationPrev = this.getIndentation(prev);

const fixer = Lint.Replacement.replaceFromTo(
start - indentationCurrent.length,
start,
line === prevLine ? `\n\n${indentationPrev}` : `\n${indentationCurrent}`,
);

// Previous statement is on the same or previous line
this.addFailure(start, start, Rule.FAILURE_STRING);
this.addFailure(start, start, Rule.FAILURE_STRING, fixer);
}
}
}
114 changes: 114 additions & 0 deletions test/rules/newline-before-return/default/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
function foo(bar) {
if (!bar) {
return;
}

return bar;
}

function foo(bar) {
if (!bar) {
var statement = '';

return statement;
}

return bar;
}

function foo(bar) {
if (!bar) {
return;
}

/* multi-line
comment */
return bar;
}

var fn = () => null;
function foo() {
fn();

return;
}

function foo(fn) {
fn();

return;
}

function foo() {
return;
}

function foo() {

return;
}

function foo(bar) {
if (!bar) return;
}

function foo(bar) {
let someCall;
if (!bar) return;
}

function foo(bar) {
if (!bar) { return };
}

function foo(bar) {
if (!bar) {
return;
}
}

function foo(bar) {
if (!bar) {
return;
}

return bar;
}

function foo(bar) {
if (!bar) {

return;
}
}

function foo() {

// comment
return;
}

function test() {
console.log("Any statement");
// Any comment

return;
}

function foo() {
fn();
// comment

// comment
return;
}

function bar() {
"some statement";

//comment
//comment
//comment
return;
}

17 changes: 17 additions & 0 deletions test/rules/newline-before-return/default/test.tsx.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as React from 'react';

<div>{ [].map((child: any) => {
let i = 0;

return <span />;
}) }</div>

<div>{ [].map((child: any) => {
return <span />;
}) }</div>

<div>{ [].map((child: any) =>
<span />;
) }</div>


0 comments on commit a27f247

Please sign in to comment.