# all files / api/transformers/ transforms.js

100% Statements 81/81
100% Branches 26/26
100% Functions 17/17
100% Lines 35/35
20 statements, 1 function, 13 branches Ignored

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76

    1
    6
    6
    2

    6

    1

    4
    4
    4
    3

    4
    4
    4

    1
    2
    2
    1
    1

    1
    2
    2
    2
    2
    2
    2

    2

    1
    3
    3
    2

    2

    1
    4
    4

     'use strict';

    // Utilities:
    import _ from 'lodash';
    import Promise from 'bluebird';
    import path from 'path';

    // Dependencies:
    import changecase from 'change-case';
    import esquery from 'esquery';
    import fileStructure from '../../file-structure';

    export default {
        transformIdentifiers,
        transformMetadata,
        transformReferenceIdentifiers,
        transformReferencePath,
        transformReferences
    };

    function transformIdentifiers (file, oldName, newName) {
        let query = `Identifier[name="${oldName}"]`;
        _.each(esquery(file.ast, query), (identifier) => {
            identifier.name = newName;
        });
        return file.save();
    }

    function transformMetadata (file, type, oldName, newName) {
        let [comment] = file.ast.comments;
        let metaData = JSON.parse(comment.value);
        let item = metaData;
        if (type) {
            item = _.find(item[type], { name: oldName });
        }
        item.name = newName;
        comment.value = JSON.stringify(metaData);
        return file.save();
    }

    function transformReferenceIdentifiers (oldFilePath, oldName, newName) {
        let referencePaths = fileStructure.references[oldFilePath] || [];
        return Promise.map(referencePaths, (referencePath) => {
            let file = fileStructure.allFilesByPath[referencePath];
            return transformIdentifiers(file, oldName, newName);
        });
    }

    function transformReferencePath (file, oldFromPath, oldToPath, newFromPath, newToPath) {
        let oldRequirePath = getRelativeRequirePath(path.dirname(oldFromPath), oldToPath);
        let newRequirePath = getRelativeRequirePath(path.dirname(newFromPath), newToPath);
        let query = `CallExpression[callee.name="require"] Literal[value="${oldRequirePath}"]`;
        _.each(esquery(file.ast, query), (requirePathLiteral) => {
            requirePathLiteral.value = newRequirePath;
            requirePathLiteral.raw = `'${newRequirePath}'`;
        });
        return file.save();
    }

    function transformReferences (type, oldFilePath, newFilePath, oldName, newName) {
        let referencePaths = fileStructure.references[oldFilePath] || [];
        return Promise.map(referencePaths, (referencePath) => {
            let file = fileStructure.allFilesByPath[referencePath];

            return transformIdentifiers(file, changecase.pascal(oldName), changecase.pascal(newName))
            .then(() => transformIdentifiers(file, changecase.camel(oldName), changecase.camel(newName)))
            .then(() => transformMetadata(file, type, oldName, newName))
            .then(() => transformReferencePath(file, file.path, oldFilePath, file.path, newFilePath));
        });
    }

    function getRelativeRequirePath (from, to) {
        let relativePath = path.relative(from, to).replace(/\\/g, '/');
        return /^\./.test(relativePath) ? relativePath : `./${relativePath}`;
    }