<commit_before>/*
 * Copyright 2013 Palantir Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

var OPTION_BRANCH = "check-branch";
var OPTION_DECL = "check-decl";
var OPTION_OPERATOR = "check-operator";
var OPTION_SEPARATOR = "check-separator";
var OPTION_TYPE = "check-type";
var OPTION_TYPECAST = "check-typecast";

export class Rule extends Lint.Rules.AbstractRule {
    public static FAILURE_STRING = "missing whitespace";

    public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
        return this.applyWithWalker(new WhitespaceWalker(sourceFile, this.getOptions()));
    }
}

class WhitespaceWalker extends Lint.RuleWalker {
    private scanner: ts.Scanner;
    private tokensToSkipStartEndMap: {[start: number]: number};

    constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
        super(sourceFile, options);
        this.scanner = ts.createScanner(ts.ScriptTarget.ES5, false, sourceFile.text);
        this.tokensToSkipStartEndMap = {};
    }

    public visitSourceFile(node: ts.SourceFile): void {
        super.visitSourceFile(node);

        var lastShouldBeFollowedByWhitespace = false;
        this.scanner.setTextPos(0);
        Lint.scanAllTokens(this.scanner, (scanner: ts.Scanner) => {
            var startPos = scanner.getStartPos();
            var tokenKind = scanner.getToken();
            if (tokenKind === ts.SyntaxKind.WhitespaceTrivia || tokenKind === ts.SyntaxKind.NewLineTrivia) {
                lastShouldBeFollowedByWhitespace = false;
            } else if (lastShouldBeFollowedByWhitespace) {
                var failure = this.createFailure(startPos, 1, Rule.FAILURE_STRING);
                this.addFailure(failure);
                lastShouldBeFollowedByWhitespace = false;
            }

            if (this.tokensToSkipStartEndMap[startPos] != null) {
                // tokens to skip are places where the scanner gets confused about what the token is, without the proper context
                // (specifically, regex and identifiers). So skip those tokens.
                scanner.setTextPos(this.tokensToSkipStartEndMap[startPos]);
                return;
            }

            // check for trailing space after the given tokens
            switch (tokenKind) {
                case ts.SyntaxKind.CatchKeyword:
                case ts.SyntaxKind.ForKeyword:
                case ts.SyntaxKind.IfKeyword:
                case ts.SyntaxKind.SwitchKeyword:
                case ts.SyntaxKind.WhileKeyword:
                case ts.SyntaxKind.WithKeyword:
                    if (this.hasOption(OPTION_BRANCH)) {
                        lastShouldBeFollowedByWhitespace = true;
                    }
                    break;
                case ts.SyntaxKind.CommaToken:
                case ts.SyntaxKind.SemicolonToken:
                    if (this.hasOption(OPTION_SEPARATOR)) {
                        lastShouldBeFollowedByWhitespace = true;
                    }
                    break;
                case ts.SyntaxKind.EqualsToken:
                    if (this.hasOption(OPTION_DECL)) {
                        lastShouldBeFollowedByWhitespace = true;
                    }
                    break;
                case ts.SyntaxKind.ColonToken:
                    if (this.hasOption(OPTION_TYPE)) {
                        lastShouldBeFollowedByWhitespace = true;
                    }
                    break;

            }
        });
    }

    public visitRegularExpressionLiteral(node: ts.Node) {
        this.tokensToSkipStartEndMap[node.getStart()] = node.getEnd();
        super.visitRegularExpressionLiteral(node);
    }

    public visitIdentifier(node: ts.Identifier) {
        this.tokensToSkipStartEndMap[node.getStart()] = node.getEnd();
        super.visitIdentifier(node);
    }

    // check for spaces between the operator symbol (except in the case of comma statements)
    public visitBinaryExpression(node: ts.BinaryExpression): void {
        var operatorKind = node.operator;
        if (this.hasOption(OPTION_OPERATOR) && operatorKind !== ts.SyntaxKind.CommaToken) {
            var position = node.left.getEnd();<commit_msg>Fix infinite looping bug correctly<commit_after>/*
 * Copyright 2013 <NAME>, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

var OPTION_BRANCH = "check-branch";
var OPTION_DECL = "check-decl";
var OPTION_OPERATOR = "check-operator";
var OPTION_SEPARATOR = "check-separator";
var OPTION_TYPE = "check-type";
var OPTION_TYPECAST = "check-typecast";

export class Rule extends Lint.Rules.AbstractRule {
    public static FAILURE_STRING = "missing whitespace";

    public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
        return this.applyWithWalker(new WhitespaceWalker(sourceFile, this.getOptions()));
    }
}

class WhitespaceWalker extends Lint.RuleWalker {
    private scanner: ts.Scanner;
    private tokensToSkipStartEndMap: {[start: number]: number};

    constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
        super(sourceFile, options);
        this.scanner = ts.createScanner(ts.ScriptTarget.ES5, false, sourceFile.text);
        this.tokensToSkipStartEndMap = {};
    }

    public visitSourceFile(node: ts.SourceFile): void {
        super.visitSourceFile(node);

        var lastShouldBeFollowedByWhitespace = false;
        this.scanner.setTextPos(0);
        Lint.scanAllTokens(this.scanner, (scanner: ts.Scanner) => {
            var startPos = scanner.getStartPos();
            var tokenKind = scanner.getToken();
            if (tokenKind === ts.SyntaxKind.WhitespaceTrivia || tokenKind === ts.SyntaxKind.NewLineTrivia) {
                lastShouldBeFollowedByWhitespace = false;
            } else if (lastShouldBeFollowedByWhitespace) {
                var failure = this.createFailure(startPos, 1, Rule.FAILURE_STRING);
                this.addFailure(failure);
                lastShouldBeFollowedByWhitespace = false;
            }

            if (this.tokensToSkipStartEndMap[startPos] != null) {
                // tokens to skip are places where the scanner gets confused about what the token is, without the proper context
                // (specifically, regex and identifiers). So skip those tokens.
                scanner.setTextPos(this.tokensToSkipStartEndMap[startPos]);
                return;
            }

            // check for trailing space after the given tokens
            switch (tokenKind) {
                case ts.SyntaxKind.CatchKeyword:
                case ts.SyntaxKind.ForKeyword:
                case ts.SyntaxKind.IfKeyword:
                case ts.SyntaxKind.SwitchKeyword:
                case ts.SyntaxKind.WhileKeyword:
                case ts.SyntaxKind.WithKeyword:
                    if (this.hasOption(OPTION_BRANCH)) {
                        lastShouldBeFollowedByWhitespace = true;
                    }
                    break;
                case ts.SyntaxKind.CommaToken:
                case ts.SyntaxKind.SemicolonToken:
                    if (this.hasOption(OPTION_SEPARATOR)) {
                        lastShouldBeFollowedByWhitespace = true;
                    }
                    break;
                case ts.SyntaxKind.EqualsToken:
                    if (this.hasOption(OPTION_DECL)) {
                        lastShouldBeFollowedByWhitespace = true;
                    }
                    break;
                case ts.SyntaxKind.ColonToken:
                    if (this.hasOption(OPTION_TYPE)) {
                        lastShouldBeFollowedByWhitespace = true;
                    }
                    break;

            }
        });
    }

    public visitRegularExpressionLiteral(node: ts.Node) {
        if (node.getStart() < node.getEnd()) {
            // only add to the map nodes whose end comes after their start, to prevent infinite loops
            this.tokensToSkipStartEndMap[node.getStart()] = node.getEnd();
        }
        super.visitRegularExpressionLiteral(node);
    }

    public visitIdentifier(node: ts.Identifier) {
        if (node.getStart() < node.getEnd()) {
            // only add to the map nodes whose end comes after their start, to prevent infinite loops
            this.tokensToSkipStartEndMap[node.getStart()] = node.getEnd();
        }
        super.visitIdentifier(node);
    }

    // check for spaces between the operator symbol (except in the case of comma statements)
    public visitBinaryExpression(node: ts.BinaryExpression): void {
        var operatorKind = node.operator;
        if (this.hasOption(OPTION_OPERATOR) && operatorKind !== ts.SyntaxKind.CommaToken) {
            var position = node.left.getEnd();
{'fraction_non_alphanumeric': 0.0858971711748916, 'fraction_numerical': 0.0022713194301053068, 'mean_word_length': 2.194920844327177, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2105566', 'n_tokens_mistral': 2597, 'n_tokens_neox': 2497, 'n_words': 642}