<commit_before>import stackTrace from 'stack-chain';
import createStackFilter from './create-stack-filter';

const ORIGINAL_STACK_TRACE_LIMIT = Error.stackTraceLimit;
const STACK_TRACE_LIMIT          = 200;

export default {
    isEnabled: false,

    _hook (err, frames) {
        return frames.filter(createStackFilter(ORIGINAL_STACK_TRACE_LIMIT));
    },

    get enabled () {
        return this.isEnabled;
    },

    set enabled (val) {
        if (this.isEnabled === val)
            return;

        this.isEnabled = val;

        if (this.isEnabled) {
            // NOTE: Babel errors may have really deep stacks,
            // so we increase stack trace capacity
            Error.stackTraceLimit = STACK_TRACE_LIMIT;
            stackTrace.filter.attach(this._hook);
        }
        else {
            Error.stackTraceLimit = ORIGINAL_STACK_TRACE_LIMIT;
            stackTrace.filter.deattach(this._hook);
        }
    }
};<commit_msg>Import stack-chain only when it's necessary (closes #2166) (#2167)<commit_after>import createStackFilter from './create-stack-filter';

const ORIGINAL_STACK_TRACE_LIMIT = Error.stackTraceLimit;
const STACK_TRACE_LIMIT          = 200;

// NOTE: 'stack-chain' can't be imported directly, because only one version of it can be imported in a process.
// It causes the process to crash, when older globally installed TestCafe version loads newer locally installed version.
// See the 'stack-chain' code at https://github.com/AndreasMadsen/stack-chain/blob/001f69e35ecd070c68209d13c4325fe5d23fc136/index.js#L10
let stackTrace = null;

export default {
    isEnabled: false,

    _hook (err, frames) {
        return frames.filter(createStackFilter(ORIGINAL_STACK_TRACE_LIMIT));
    },

    get enabled () {
        return this.isEnabled;
    },

    set enabled (val) {
        if (this.isEnabled === val)
            return;

        this.isEnabled = val;

        if (!stackTrace)
            stackTrace = require('stack-chain');

        if (this.isEnabled) {
            // NOTE: Babel errors may have really deep stacks,
            // so we increase stack trace capacity
            Error.stackTraceLimit = STACK_TRACE_LIMIT;
            stackTrace.filter.attach(this._hook);
        }
        else {
            Error.stackTraceLimit = ORIGINAL_STACK_TRACE_LIMIT;
            stackTrace.filter.deattach(this._hook);
        }
    }
};
{'fraction_non_alphanumeric': 0.09304274937133278, 'fraction_numerical': 0.01802179379715004, 'mean_word_length': 2.4644412191582004, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, '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': '2211334', 'n_tokens_mistral': 729, 'n_tokens_neox': 694, 'n_words': 170}