<commit_before>/// <reference path="../estree.ts" />
module Styx.ExpressionStringifier {
    export function stringify(expression: ESTree.Expression): string {
        if (expression.type === ESTree.NodeType.ArrayExpression) {
            return stringifyArrayExpression(<ESTree.ArrayExpression>expression);
        }
        
        if (expression.type === ESTree.NodeType.Literal) {
            return stringifyLiteral(<ESTree.Literal>expression);
        }
        
        if (expression.type === ESTree.NodeType.Identifier) {
            return stringifyIdentifier(<ESTree.Identifier>expression);
        }
        
        if (expression.type === ESTree.NodeType.UpdateExpression) {
            return stringifyUpdateExpression(<ESTree.UpdateExpression>expression);
        }
        
        if (expression.type === ESTree.NodeType.UnaryExpression) {
            return stringifyUnaryExpression(<ESTree.UnaryExpression>expression);
        }
        
        if (expression.type === ESTree.NodeType.BinaryExpression) {
            return stringifyBinaryExpression(<ESTree.BinaryExpression>expression);
        }
        
        if (expression.type === ESTree.NodeType.LogicalExpression) {
            return stringifyLogicalExpression(<ESTree.LogicalExpression>expression);
        }
        
        if (expression.type === ESTree.NodeType.AssignmentExpression) {
            return stringifyAssignmentExpression(<ESTree.AssignmentExpression>expression);
        }
        
        if (expression.type === ESTree.NodeType.CallExpression) {
            return stringifyCallExpression(<ESTree.CallExpression>expression);
        }
        
        if (expression.type === ESTree.NodeType.MemberExpression) {
            return stringifyMemberExpression(<ESTree.MemberExpression>expression);
        }
        
        return "<UNEXPECTED>";
    }
    
    function stringifyArrayExpression(expression: ESTree.ArrayExpression): string {
        let arrayLiteral = "";        
        let isFirst = true;
        let previousElementWasNull = false;
        
        for (let element of expression.elements) {
            if (element === null) {
                if (!isFirst && !previousElementWasNull) {
                    arrayLiteral += ",";
                }
                
                arrayLiteral += ",";
                previousElementWasNull = true;
            } else {
                let leadingComma = isFirst || previousElementWasNull ? "" : ",";
                let elementString = ExpressionStringifier.stringify(element);
                
                arrayLiteral += leadingComma + elementString;
                previousElementWasNull = false;
            }
            
            isFirst = false;
        }
        
        return `[${arrayLiteral}]`;
    }
    
    function stringifyLiteral(literal: ESTree.Literal): string {
        return typeof literal.value === "string"
            ? `"${literal.value}"`
            : literal.value.toString();
    }
    
    function stringifyIdentifier(identifier: ESTree.Identifier): string {
        return identifier.name;
    }
    
    function stringifyUpdateExpression(expression: ESTree.UpdateExpression): string {<commit_msg>Stringifies null literals correctly<commit_after>/// <reference path="../estree.ts" />
module Styx.ExpressionStringifier {
    export function stringify(expression: ESTree.Expression): string {
        if (expression.type === ESTree.NodeType.ArrayExpression) {
            return stringifyArrayExpression(<ESTree.ArrayExpression>expression);
        }
        
        if (expression.type === ESTree.NodeType.Literal) {
            return stringifyLiteral(<ESTree.Literal>expression);
        }
        
        if (expression.type === ESTree.NodeType.Identifier) {
            return stringifyIdentifier(<ESTree.Identifier>expression);
        }
        
        if (expression.type === ESTree.NodeType.UpdateExpression) {
            return stringifyUpdateExpression(<ESTree.UpdateExpression>expression);
        }
        
        if (expression.type === ESTree.NodeType.UnaryExpression) {
            return stringifyUnaryExpression(<ESTree.UnaryExpression>expression);
        }
        
        if (expression.type === ESTree.NodeType.BinaryExpression) {
            return stringifyBinaryExpression(<ESTree.BinaryExpression>expression);
        }
        
        if (expression.type === ESTree.NodeType.LogicalExpression) {
            return stringifyLogicalExpression(<ESTree.LogicalExpression>expression);
        }
        
        if (expression.type === ESTree.NodeType.AssignmentExpression) {
            return stringifyAssignmentExpression(<ESTree.AssignmentExpression>expression);
        }
        
        if (expression.type === ESTree.NodeType.CallExpression) {
            return stringifyCallExpression(<ESTree.CallExpression>expression);
        }
        
        if (expression.type === ESTree.NodeType.MemberExpression) {
            return stringifyMemberExpression(<ESTree.MemberExpression>expression);
        }
        
        return "<UNEXPECTED>";
    }
    
    function stringifyArrayExpression(expression: ESTree.ArrayExpression): string {
        let arrayLiteral = "";        
        let isFirst = true;
        let previousElementWasNull = false;
        
        for (let element of expression.elements) {
            if (element === null) {
                if (!isFirst && !previousElementWasNull) {
                    arrayLiteral += ",";
                }
                
                arrayLiteral += ",";
                previousElementWasNull = true;
            } else {
                let leadingComma = isFirst || previousElementWasNull ? "" : ",";
                let elementString = ExpressionStringifier.stringify(element);
                
                arrayLiteral += leadingComma + elementString;
                previousElementWasNull = false;
            }
            
            isFirst = false;
        }
        
        return `[${arrayLiteral}]`;
    }
    
    function stringifyLiteral(literal: ESTree.Literal): string {
        let value = literal.value;
        
        return typeof value === "string"
            ? `"${value}"`
            : value === null
                ? "null"
                : value.toString();
    }
    
    function stringifyIdentifier(identifier: ESTree.Identifier): string {
        return identifier.name;
    }
    
    function stringifyUpdateExpression(expression: ESTree.UpdateExpression): string {
{'fraction_non_alphanumeric': 0.09791602819491266, 'fraction_numerical': 0.0, 'mean_word_length': 2.017568192325474, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2216015', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1526, 'n_words': 261}