# all files / files/ StepDefinitionFile.js

97.96% Statements 96/98
94.74% Branches 36/38
95.24% Functions 20/21
90% Lines 18/20
17 statements, 16 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

    1
    1

    3

    3

    1

    6
    2

    6
    6
    2
    2
    2
    2

    1
    6
    6
    6

     'use strict';

    // Constants:
    const REQUIRE_QUERY = 'CallExpression[callee.name="require"] Literal';
    const PENDING_QUERY = 'CallExpression[callee.name="callback"] .arguments[value]';

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

    // Dependencies:
    import esquery from 'esquery';
    import JavaScriptFile from './JavaScriptFile';

    export default class StepDefinitionFile extends JavaScriptFile {
        read () {
            return super.read()
            .then(() => getFileReferences.call(this))
            .then(() => checkIfPending.call(this));
        }

        save (data) {
            return super.save(data)
            .then(() => getFileReferences.call(this))
            .then(() => checkIfPending.call(this));
        }
    }

    function getFileReferences () {
        let { references } = this.directory.fileStructure;

        _.each(references, (referencePaths) => {
            _.remove(referencePaths, (referencePath) => referencePath === this.path);
        });

        let requirePaths = esquery(this.ast, REQUIRE_QUERY);
        _.each(requirePaths, (requirePath) => {
            let directoryPath = path.dirname(this.path);
            let referencePath = path.resolve(directoryPath, requirePath.value);
            references[referencePath] = references[referencePath] || [];
            references[referencePath].push(this.path);
        });
    }

    function checkIfPending () {
        this.isPending = false;
        let pendingIdentifiers = esquery(this.ast, PENDING_QUERY);
        _.each(pendingIdentifiers, (pendingIdentifier) => {
            if (pendingIdentifier.value === 'pending') {
                this.isPending = true;
            }
        });
    }