<commit_before>'use strict';

const path = require('path');
const prettier = require('prettier');
const stylelint = require('stylelint');
// const cosmiconfig = require('cosmiconfig');
const debug = require('debug')('prettier-stylelint:main');

// const explorer = cosmiconfig('stylelint');
const linterAPI = stylelint.createLinter({ fix: true });

/**
 * Resolve Config for the given file
 *
 * @export
 * @param {string} file - filepath
 * @param {Object} options - options
 * @returns {Promise} -
 */
function resolveConfig(file, options = {}) {
    const resolve = resolveConfig.resolve;

    if (options.stylelintConfig) {
        return Promise.resolve(resolve(options.stylelintConfig));
    }

    return linterAPI
        .getConfigForFile(file)
        .then(({ config }) => resolve(config));

    // return explorer.load(file).then(({ config }) => resolve(config));
}

resolveConfig.resolve = (stylelintConfig) => {
    const prettierConfig = {};
    const { rules } = stylelintConfig;

    if (rules['max-line-length']) {
        const printWidth = rules['max-line-length'][0];

        prettierConfig.printWidth = printWidth;
    }

    if (rules['string-quotes']) {
        const quotes = rules['string-quotes'][0];

        if (quotes === 'single') {
            prettierConfig.singleQuote = true;
        }
    }

    if (rules.indentation) {
        const indentation = rules.indentation[0];

        if (indentation === 'tab') {
            prettierConfig.useTabs = true;
            prettierConfig.tabWidth = 2;
        } else {
            prettierConfig.useTabs = false;
            prettierConfig.tabWidth = indentation;
        }
    }
    prettierConfig.parser = 'postcss';
    debug('prettier %O', prettierConfig);
    debug('linter %O', stylelintConfig);

    return [prettierConfig, stylelintConfig];
};

function stylelinter(code, filePath) {
    return linterAPI
        ._lintSource({
            code,
            codeFilename: filePath
        })
        .then((result) => {
            const fixed = result.root.toString(result.opts.syntax);

            return fixed;
        });
}

function format(options) {
    const { filePath = '', text } = options;

    return resolveConfig(filePath, options).then(([prettierConfig]) =>
        stylelinter(<commit_msg>fix: dont re use the same linter instance because we can't invalidate the config cache<commit_after>'use strict';

const path = require('path');
const prettier = require('prettier');
const stylelint = require('stylelint');
// const cosmiconfig = require('cosmiconfig');
const debug = require('debug')('prettier-stylelint:main');

// const explorer = cosmiconfig('stylelint');

/**
 * Resolve Config for the given file
 *
 * @export
 * @param {string} file - filepath
 * @param {Object} options - options
 * @returns {Promise} -
 */
function resolveConfig(file, options = {}) {
    const resolve = resolveConfig.resolve;
    const linterAPI = stylelint.createLinter({ fix: true });

    if (options.stylelintConfig) {
        return Promise.resolve(resolve(options.stylelintConfig));
    }

    return linterAPI
        .getConfigForFile(file)
        .then(({ config }) => resolve(config));

    // return explorer.load(file).then(({ config }) => resolve(config));
}

resolveConfig.resolve = (stylelintConfig) => {
    const prettierConfig = {};
    const { rules } = stylelintConfig;

    if (rules['max-line-length']) {
        const printWidth = rules['max-line-length'][0];

        prettierConfig.printWidth = printWidth;
    }

    if (rules['string-quotes']) {
        const quotes = rules['string-quotes'][0];

        if (quotes === 'single') {
            prettierConfig.singleQuote = true;
        }
    }

    if (rules.indentation) {
        const indentation = rules.indentation[0];

        if (indentation === 'tab') {
            prettierConfig.useTabs = true;
            prettierConfig.tabWidth = 2;
        } else {
            prettierConfig.useTabs = false;
            prettierConfig.tabWidth = indentation;
        }
    }
    prettierConfig.parser = 'postcss';
    debug('prettier %O', prettierConfig);
    debug('linter %O', stylelintConfig);

    return [prettierConfig, stylelintConfig];
};

function stylelinter(code, filePath) {
    const linterAPI = stylelint.createLinter({ fix: true });

    return linterAPI
        ._lintSource({
            code,
            codeFilename: filePath
        })
        .then((result) => {
            const fixed = result.root.toString(result.opts.syntax);

            return fixed;
        });
}

function format(options) {
    const { filePath = '', text } = options;

    return resolveConfig(filePath, options).then(([prettierConfig]) =>
        stylelinter(
{'fraction_non_alphanumeric': 0.13720093161126404, 'fraction_numerical': 0.0016938386618674571, 'mean_word_length': 2.7671451355661882, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '2792082', 'n_tokens_mistral': 1400, 'n_tokens_neox': 1342, 'n_words': 296}