<commit_before>'use strict';

var assert = require('assert');

module.exports = function expectToAssert (file, api, options) {
    var j = api.jscodeshift;
    var ast = j(file.source);
    var FLAGS = [
        'deep',
        'itself',
        'not'
    ];
    var primitiveTypes = [
        'undefined',
        'string',
        'number',
        'boolean',
        'function',
        'object',
        'symbol'
    ];

    function isFirstLetterCapital (str) {
        return str.charAt(0) === str.charAt(0).toUpperCase();
    }

    function beginsWithExpect (node) {
        return (node.type === 'Identifier' && node.name === 'expect' ||
                (node.type === 'CallExpression' && beginsWithExpect(node.callee)) ||
                (node.type === 'MemberExpression' && beginsWithExpect(node.object)));
    }

    function subjects (node) {
        if (node.type === 'CallExpression') {
            if (node.callee.type === 'Identifier' && node.callee.name === 'expect') {
                return node.arguments;
            } else {
                return subjects(node.callee);
            }
        } else if (node.type === 'MemberExpression') {
            return subjects(node.object);
        } else {
            assert(false, 'cannot be here');
            return null;
        }
    }

    function collectFlags (node, collector) {
        if (node.type === 'CallExpression') {
            if (node.callee.type === 'Identifier' && node.callee.name === 'expect') {
                return collector;
            } else {
                return collectFlags(node.callee, collector);
            }
        } else if (node.type === 'MemberExpression') {
            if (node.property.type === 'Identifier' && FLAGS.indexOf(node.property.name) !== -1) {
                collector[node.property.name] = true;
            }
            return collectFlags(node.object, collector);
        } else {
            return assert(false, 'cannot be here');
        }
    }

    function hasFlag (flag, node) {
        return !!collectFlags(node, {})[flag];
    }

    function isNegation (node) {
        return hasFlag('not', node);
    }

    function isDeep (node) {
        return hasFlag('deep', node);
    }

    ast.find(j.MemberExpression, {
        type: 'MemberExpression',
        property: function (node) {
            return node.type === 'Identifier' &&
                [
                    'ok',
                    'true',
                    'false',
                    'null',
                    'undefined'
                ].indexOf(node.name) !== -1;
        },
        object: beginsWithExpect
    }).replaceWith(function (path) {
        var subject = subjects(path.value)[0];
        var rhs;
        switch (path.value.property.name) {
        case 'ok':
            if (isNegation(path.value)) {
                return j.callExpression(j.identifier('assert'), [
                    j.unaryExpression('!', subject)
                ]);
            } else {
                return j.callExpression(j.identifier('assert'), [ subject ]);
            }
        case 'true':
            rhs = j.booleanLiteral(true);
            break;
        case 'false':
            rhs = j.booleanLiteral(false);
            break;
        case 'null':
            rhs = j.literal(null);
            break;
        case 'undefined':
            rhs = j.identifier('undefined');
            break;
        default:
            assert(false, 'cannot be here');
        }
        return j.callExpression(j.identifier('assert'), [
            j.binaryExpression(
                isNegation(path.value) ? '!==' : '===',
                subject,
                rhs
            )
        ]);
    });

    ast.find(j.CallExpression, {
        callee: {
            type: 'MemberExpression',
            property: function (node) {
                return node.type === 'Identifier' &&
                    [
                        'a',
                        'an',
                        'instanceof',
                        'instanceOf',
                        'respondTo',
                        'respondsTo',
                        'match',
                        'matches',
                        'string',
                        'equal',
                        'equals',
                        'eq',
                        'eql',
                        'eqls'
                    ].indexOf(node.name) !== -1;
            },
            object: beginsWithExpect
        }
    }).replaceWith(function (path) {
        var subject = subjects(path.value)[0];
        var matcherArg = path.value.arguments[0];<commit_msg>refactor(chai-to-assert): put aliases in same line<commit_after>'use strict';

var assert = require('assert');

module.exports = function expectToAssert (file, api, options) {
    var j = api.jscodeshift;
    var ast = j(file.source);
    var FLAGS = [
        'deep',
        'itself',
        'not'
    ];
    var primitiveTypes = [
        'undefined',
        'string',
        'number',
        'boolean',
        'function',
        'object',
        'symbol'
    ];

    function isFirstLetterCapital (str) {
        return str.charAt(0) === str.charAt(0).toUpperCase();
    }

    function beginsWithExpect (node) {
        return (node.type === 'Identifier' && node.name === 'expect' ||
                (node.type === 'CallExpression' && beginsWithExpect(node.callee)) ||
                (node.type === 'MemberExpression' && beginsWithExpect(node.object)));
    }

    function subjects (node) {
        if (node.type === 'CallExpression') {
            if (node.callee.type === 'Identifier' && node.callee.name === 'expect') {
                return node.arguments;
            } else {
                return subjects(node.callee);
            }
        } else if (node.type === 'MemberExpression') {
            return subjects(node.object);
        } else {
            assert(false, 'cannot be here');
            return null;
        }
    }

    function collectFlags (node, collector) {
        if (node.type === 'CallExpression') {
            if (node.callee.type === 'Identifier' && node.callee.name === 'expect') {
                return collector;
            } else {
                return collectFlags(node.callee, collector);
            }
        } else if (node.type === 'MemberExpression') {
            if (node.property.type === 'Identifier' && FLAGS.indexOf(node.property.name) !== -1) {
                collector[node.property.name] = true;
            }
            return collectFlags(node.object, collector);
        } else {
            return assert(false, 'cannot be here');
        }
    }

    function hasFlag (flag, node) {
        return !!collectFlags(node, {})[flag];
    }

    function isNegation (node) {
        return hasFlag('not', node);
    }

    function isDeep (node) {
        return hasFlag('deep', node);
    }

    ast.find(j.MemberExpression, {
        type: 'MemberExpression',
        property: function (node) {
            return node.type === 'Identifier' &&
                [
                    'ok',
                    'true',
                    'false',
                    'null',
                    'undefined'
                ].indexOf(node.name) !== -1;
        },
        object: beginsWithExpect
    }).replaceWith(function (path) {
        var subject = subjects(path.value)[0];
        var rhs;
        switch (path.value.property.name) {
        case 'ok':
            if (isNegation(path.value)) {
                return j.callExpression(j.identifier('assert'), [
                    j.unaryExpression('!', subject)
                ]);
            } else {
                return j.callExpression(j.identifier('assert'), [ subject ]);
            }
        case 'true':
            rhs = j.booleanLiteral(true);
            break;
        case 'false':
            rhs = j.booleanLiteral(false);
            break;
        case 'null':
            rhs = j.literal(null);
            break;
        case 'undefined':
            rhs = j.identifier('undefined');
            break;
        default:
            assert(false, 'cannot be here');
        }
        return j.callExpression(j.identifier('assert'), [
            j.binaryExpression(
                isNegation(path.value) ? '!==' : '===',
                subject,
                rhs
            )
        ]);
    });

    ast.find(j.CallExpression, {
        callee: {
            type: 'MemberExpression',
            property: function (node) {
                return node.type === 'Identifier' &&
                    [
                        'a', 'an',
                        'equal', 'equals', 'eq',
                        'eql', 'eqls',
                        'instanceof', 'instanceOf',
                        'match', 'matches',
                        'respondTo', 'respondsTo',
                        'string'
                    ].indexOf(node.name) !== -1;
            },
            object: beginsWithExpect
        }
    }).replaceWith(function (path) {
        var subject = subjects(path.value)[0];
        var matcherArg = path.value.arguments[0];
{'fraction_non_alphanumeric': 0.13552473517527575, 'fraction_numerical': 0.0017472971497215245, 'mean_word_length': 1.3615265600825168, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3183790', 'n_tokens_mistral': 2443, 'n_tokens_neox': 2355, 'n_words': 496}