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

Commit

Permalink
Added util and refactored both fixer for newline-before-return and cu…
Browse files Browse the repository at this point in the history
…rly rule
  • Loading branch information
jukben committed Jan 29, 2019
1 parent 9db2506 commit 37646c5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 32 deletions.
24 changes: 3 additions & 21 deletions src/rules/curlyRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { isBlock, isIfStatement, isIterationStatement, isSameLine } from "tsutil
import * as ts from "typescript";

import * as Lint from "../index";
import { newLineWithIndentation } from "../utils";

import { codeExamples } from "./code-examples/curly.examples";

Expand Down Expand Up @@ -172,29 +173,10 @@ class CurlyWalker extends Lint.AbstractWalker<Options> {
Lint.Replacement.appendText(statement.end, " }"),
];
} else {
const match = /\n([\t ])/.exec(node.getFullText(this.sourceFile)); // determine which character to use (tab or space)
const indentation =
match === null
? ""
: // indentation should match start of statement
match[1].repeat(
ts.getLineAndCharacterOfPosition(
this.sourceFile,
node.getStart(this.sourceFile),
).character,
);

const maybeCarriageReturn =
this.sourceFile.text[this.sourceFile.getLineEndOfPosition(node.pos) - 1] === "\r"
? "\r"
: "";

const newLine = newLineWithIndentation(node, this.sourceFile);
return [
Lint.Replacement.appendText(statement.pos, " {"),
Lint.Replacement.appendText(
statement.end,
`${maybeCarriageReturn}\n${indentation}}`,
),
Lint.Replacement.appendText(statement.end, `${newLine}}`),
];
}
}
Expand Down
16 changes: 5 additions & 11 deletions src/rules/newlineBeforeReturnRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { getPreviousStatement } from "tsutils";
import * as ts from "typescript";

import * as Lint from "../index";
import { newLineWithIndentation } from "../utils";

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
Expand Down Expand Up @@ -55,12 +56,6 @@ 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(/([ \t]*)$/);
return matches !== null ? matches[1] : "";
}

private visitReturnStatement(node: ts.ReturnStatement) {
const prev = getPreviousStatement(node);
if (prev === undefined) {
Expand All @@ -86,13 +81,12 @@ 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,
line === prevLine ? node.pos : node.pos + 1,
start,
line === prevLine ? `\n\n${indentationPrev}` : `\n${indentationCurrent}`,
line === prevLine
? newLineWithIndentation(prev, this.sourceFile, 2)
: newLineWithIndentation(prev, this.sourceFile),
);

// Previous statement is on the same or previous line
Expand Down
25 changes: 25 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,31 @@ function tryResolveSync(packageName: string, relativeTo?: string): string | unde
}
}

/**
* Gets the full indentation of the provided node
*/
export function getIndentation(node: ts.Node, sourceFile: ts.SourceFile): string {
const text = sourceFile.text.substr(node.pos, node.getStart() - node.pos);
const matches = text.match(/([ \t]*)$/);
return matches !== null ? matches[1] : "";
}

/**
* Creates x new lines with a proper indentation at the last one based on the provided node
*/
export function newLineWithIndentation(
node: ts.Node,
sourceFile: ts.SourceFile,
linesCount: number = 1,
) {
const maybeCarriageReturn =
sourceFile.text[sourceFile.getLineEndOfPosition(node.pos) - 1] === "\r" ? "\r" : "";

const indentation = getIndentation(node, sourceFile);

return `${`${maybeCarriageReturn}\n`.repeat(linesCount)}${indentation}`;
}

/**
* @deprecated Copied from tsutils 2.27.2. This will be removed once TSLint requires tsutils > 3.0.
*/
Expand Down
12 changes: 12 additions & 0 deletions test/rules/newline-before-return/default/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ function foo(fn) {
return;
}

function foo(fn) {
fn();

return;
}

function foo(fn) {
fn();

return;
}

function foo() {
return;
}
Expand Down
10 changes: 10 additions & 0 deletions test/rules/newline-before-return/default/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ function foo(fn) {
~nil [0]
}

function foo(fn) {
fn(); return;
~nil [0]
}

function foo(fn) {
fn(); return;
~nil [0]
}

function foo() {
return;
}
Expand Down

0 comments on commit 37646c5

Please sign in to comment.