diff --git a/src/rules/arrowReturnShorthandRule.ts b/src/rules/arrowReturnShorthandRule.ts index 4b6c7830d45..cbbfef6eca6 100644 --- a/src/rules/arrowReturnShorthandRule.ts +++ b/src/rules/arrowReturnShorthandRule.ts @@ -60,10 +60,10 @@ interface Options { function walk(ctx: Lint.WalkContext): void { const { sourceFile, options: { multiline } } = ctx; - ts.forEachChild(sourceFile, function cb(node: ts.Node): void { + return ts.forEachChild(sourceFile, function cb(node: ts.Node): void { if (utils.isArrowFunction(node) && utils.isBlock(node.body)) { const expr = getSimpleReturnExpression(node.body); - if (expr !== undefined && (multiline || !node.body.getText(sourceFile).includes("\n"))) { + if (expr !== undefined && (multiline || utils.isSameLine(sourceFile, node.body.getStart(sourceFile), node.body.end))) { const isObjectLiteral = expr.kind === ts.SyntaxKind.ObjectLiteralExpression; ctx.addFailureAtNode(node.body, Rule.FAILURE_STRING(isObjectLiteral), createFix(node, node.body, expr, sourceFile.text)); } diff --git a/src/rules/semicolonRule.ts b/src/rules/semicolonRule.ts index 2dc2a0d6c31..063183cc921 100644 --- a/src/rules/semicolonRule.ts +++ b/src/rules/semicolonRule.ts @@ -140,8 +140,7 @@ class SemicolonWalker extends Lint.AbstractWalker { // check if this is a multi-line arrow function if (node.initializer !== undefined && node.initializer.kind === ts.SyntaxKind.ArrowFunction && - ts.getLineAndCharacterOfPosition(this.sourceFile, node.getStart(this.sourceFile)).line - !== ts.getLineAndCharacterOfPosition(this.sourceFile, node.getEnd()).line) { + !utils.isSameLine(this.sourceFile, node.getStart(this.sourceFile), node.end)) { if (this.options.boundClassMethods) { if (this.sourceFile.text[node.end - 1] === ";" && this.isFollowedByLineBreak(node.end)) { @@ -234,7 +233,6 @@ class SemicolonWalker extends Lint.AbstractWalker { if (nextStatement === undefined) { return false; } - return ts.getLineAndCharacterOfPosition(this.sourceFile, node.end).line - === ts.getLineAndCharacterOfPosition(this.sourceFile, nextStatement.getStart(this.sourceFile)).line; + return utils.isSameLine(this.sourceFile, node.end, nextStatement.getStart(this.sourceFile)); } } diff --git a/src/rules/trailingCommaRule.ts b/src/rules/trailingCommaRule.ts index 2397d2427ef..000a9a288ff 100644 --- a/src/rules/trailingCommaRule.ts +++ b/src/rules/trailingCommaRule.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { getChildOfKind } from "tsutils"; +import { getChildOfKind, isSameLine } from "tsutils"; import * as ts from "typescript"; import * as Lint from "../index"; @@ -216,18 +216,14 @@ class TrailingCommaWalker extends Lint.AbstractWalker { /* Expects `list.length !== 0` */ private checkComma(hasTrailingComma: boolean, list: ts.NodeArray, closeTokenPos: number, optionKey: OptionName) { - const lastElementLine = ts.getLineAndCharacterOfPosition(this.sourceFile, list[list.length - 1].end).line; - const closeTokenLine = ts.getLineAndCharacterOfPosition(this.sourceFile, closeTokenPos).line; - const option = lastElementLine === closeTokenLine ? this.options.singleline : this.options.multiline; - const shouldHandle = option[optionKey]; + const options = isSameLine(this.sourceFile, list[list.length - 1].end, closeTokenPos) + ? this.options.singleline + : this.options.multiline; + const option = options[optionKey]; - if (shouldHandle === "ignore") { - return; - } - - if (shouldHandle === "always" && !hasTrailingComma) { + if (option === "always" && !hasTrailingComma) { this.addFailureAt(list.end, 0, Rule.FAILURE_STRING_ALWAYS, Lint.Replacement.appendText(list.end, ",")); - } else if (shouldHandle === "never" && hasTrailingComma) { + } else if (option === "never" && hasTrailingComma) { this.addFailureAt(list.end - 1, 1, Rule.FAILURE_STRING_NEVER, Lint.Replacement.deleteText(list.end - 1, 1)); } }