How to use the tsutils.isSameLine function in tsutils

To help you get started, we’ve selected a few tsutils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github fossasia / susper.com / node_modules / tslint / lib / rules / objectLiteralSortKeysRule.js View on Github external
function checkAlphabetical(node) {
        if (tsutils_1.isSameLine(ctx.sourceFile, node.properties.pos, node.end)) {
            return;
        }
        var lastKey;
        var lastPropertyWasShorthand;
        for (var _i = 0, _a = node.properties; _i < _a.length; _i++) {
            var property = _a[_i];
            switch (property.kind) {
                case ts.SyntaxKind.SpreadAssignment:
                    lastKey = undefined; // reset at spread
                    lastPropertyWasShorthand = undefined; // reset at spread
                    break;
                case ts.SyntaxKind.ShorthandPropertyAssignment:
                case ts.SyntaxKind.PropertyAssignment:
                    if (shorthandFirst) {
                        if (property.kind === ts.SyntaxKind.ShorthandPropertyAssignment) {
                            if (lastPropertyWasShorthand === false) {
github palantir / tslint / src / rules / curlyRule.ts View on Github external
private checkStatement(
        statement: ts.Statement,
        node: ts.IterationStatement | ts.IfStatement,
        childIndex: number,
        end = statement.end,
    ) {
        const sameLine = isSameLine(this.sourceFile, statement.pos, statement.end);
        if (statement.kind !== ts.SyntaxKind.Block && !(this.options.ignoreSameLine && sameLine)) {
            const token = node.getChildAt(childIndex, this.sourceFile);
            const tokenText = ts.tokenToString(token.kind)!;
            this.addFailure(
                token.end - tokenText.length,
                end,
                Rule.FAILURE_STRING_FACTORY(tokenText),
                this.createMissingBraceFix(statement, node, sameLine),
            );
        }
    }
github palantir / tslint / src / rules / preferConditionalExpressionRule.ts View on Github external
return undefined;
        }
        const elze = detectAssignment(statement.elseStatement, sourceFile, checkElseIf, true);
        return elze !== undefined && nodeEquals(then, elze, sourceFile) ? then : undefined;
    } else if (isBlock(statement)) {
        return statement.statements.length === 1
            ? detectAssignment(statement.statements[0], sourceFile, checkElseIf, inElse)
            : undefined;
    } else if (isExpressionStatement(statement) && isBinaryExpression(statement.expression)) {
        const {
            operatorToken: { kind },
            left,
            right,
        } = statement.expression;
        return kind === ts.SyntaxKind.EqualsToken &&
            isSameLine(sourceFile, right.getStart(sourceFile), right.end)
            ? left
            : undefined;
    } else {
        return undefined;
    }
}
github nativescript-rtl / ui / node_modules / tslint / lib / rules / trailingCommaRule.js View on Github external
TrailingCommaWalker.prototype.checkComma = function (hasTrailingComma, list, closeTokenPos, optionKey, isRest) {
        var last = list[list.length - 1];
        if (this.options.specCompliant && isRest(last)) {
            if (hasTrailingComma) {
                this.addFailureAt(list.end - 1, 1, Rule.FAILURE_STRING_FORBIDDEN, Lint.Replacement.deleteText(list.end - 1, 1));
            }
            return;
        }
        var options = tsutils_1.isSameLine(this.sourceFile, last.end, closeTokenPos)
            ? this.options.singleline
            : this.options.multiline;
        var option = options[optionKey];
        if (option === "always" && !hasTrailingComma) {
            this.addFailureAt(list.end, 0, Rule.FAILURE_STRING_ALWAYS, Lint.Replacement.appendText(list.end, ","));
        }
        else if (option === "never" && hasTrailingComma) {
            this.addFailureAt(list.end - 1, 1, Rule.FAILURE_STRING_NEVER, Lint.Replacement.deleteText(list.end - 1, 1));
        }
    };
    return TrailingCommaWalker;
github palantir / tslint / src / rules / oneLineRule.ts View on Github external
private check(range: ts.TextRange, kind?: "catch" | "else" | "finally") {
        const tokenStart = range.end - (kind === undefined ? 1 : kind.length);
        if (
            this.options[kind === undefined ? "brace" : kind] &&
            !isSameLine(this.sourceFile, range.pos, tokenStart)
        ) {
            this.addFailure(
                tokenStart,
                range.end,
                `misplaced ${kind === undefined ? "opening brace" : `'${kind}'`}`,
                Lint.Replacement.replaceFromTo(
                    range.pos,
                    tokenStart,
                    this.options.whitespace ? " " : "",
                ),
            );
        } else if (this.options.whitespace && range.pos === tokenStart) {
            this.addFailure(
                tokenStart,
                range.end,
                Rule.WHITESPACE_FAILURE_STRING,
github fossasia / susper.com / node_modules / tslint / lib / rules / newlinePerChainedCallRule.js View on Github external
var checkForSameLine = function (node) {
            if (tsutils_1.isCallExpression(node) &&
                tsutils_1.isPropertyAccessExpression(node.expression) &&
                tsutils_1.isSameLine(sourceFile, node.expression.expression.end, node.expression.name.pos) &&
                hasChildCall(node.expression)) {
                _this.addFailure(node.expression.name.pos - 1, node.expression.name.end, Rule.FAILURE_STRING);
            }
            return ts.forEachChild(node, checkForSameLine);
        };
        return ts.forEachChild(sourceFile, checkForSameLine);
github nativescript-rtl / ui / node_modules / tslint / lib / rules / semicolonRule.js View on Github external
var nextToken = utils.getNextToken(node, this.sourceFile);
        switch (nextToken.kind) {
            case ts.SyntaxKind.OpenParenToken:
            case ts.SyntaxKind.OpenBracketToken:
            case ts.SyntaxKind.PlusToken:
            case ts.SyntaxKind.MinusToken:
            case ts.SyntaxKind.RegularExpressionLiteral:
            case ts.SyntaxKind.LessThanToken:
            case ts.SyntaxKind.NoSubstitutionTemplateLiteral:
            case ts.SyntaxKind.TemplateHead:
                break;
            case ts.SyntaxKind.CloseBraceToken:
            case ts.SyntaxKind.EndOfFileToken:
                return this.reportUnnecessary(node.end);
            default:
                if (!utils.isSameLine(this.sourceFile, node.end, nextToken.end)) {
                    this.reportUnnecessary(node.end);
                }
        }
    };
    SemicolonWalker.prototype.checkEmptyStatement = function (node) {
github fossasia / susper.com / node_modules / tslint / lib / rules / preferConditionalExpressionRule.js View on Github external
var then = detectAssignment(statement.thenStatement, sourceFile, checkElseIf, false);
        if (then === undefined) {
            return undefined;
        }
        var elze = detectAssignment(statement.elseStatement, sourceFile, checkElseIf, true);
        return elze !== undefined && nodeEquals(then, elze, sourceFile) ? then : undefined;
    }
    else if (tsutils_1.isBlock(statement)) {
        return statement.statements.length === 1
            ? detectAssignment(statement.statements[0], sourceFile, checkElseIf, inElse)
            : undefined;
    }
    else if (tsutils_1.isExpressionStatement(statement) && tsutils_1.isBinaryExpression(statement.expression)) {
        var _a = statement.expression, kind = _a.operatorToken.kind, left = _a.left, right = _a.right;
        return kind === ts.SyntaxKind.EqualsToken &&
            tsutils_1.isSameLine(sourceFile, right.getStart(sourceFile), right.end)
            ? left
            : undefined;
    }
    else {
        return undefined;
    }
}
function nodeEquals(a, b, sourceFile) {