<commit_before>const _ = require('lodash');
const spec = require('../specs');
const {versions, normalizePath} = require('../utils');
const ASTLinter = require('../ast-linter');

function processFileFunction(files, failures, rules) {
    return function processFile(linter, themeFile) {
        if (themeFile.parsed.error) {
            // Ignore parsing errors, they are handled in 005
            return;
        }

        const astResults = linter.verify({
            parsed: themeFile.parsed,
            rules,
            source: themeFile.content,
            moduleId: themeFile.file
        });

        if (astResults.length) {
            failures.push({
                ref: themeFile.file,
                message: astResults[0].message
            });
        }

        linter.partials.forEach((partial) => {
            const file = files.find(f => normalizePath(f.file) === `partials/${normalizePath(partial)}.hbs`);
            if (file) {
                processFile(linter, file);
            }
        });
    };
}

const checkTemplatesCompile = function checkTemplatesCompile(theme, options) {
    const failures = [];
    const checkVersion = _.get(options, 'checkVersion', versions.default);
    const ruleSet = spec.get([checkVersion]);

    const ruleRegex = /GS090-.*/g;

    const rulesToCheck = _.pickBy(ruleSet.rules, function (rule, ruleCode) {
        if (ruleCode.match(ruleRegex)) {
            return rule;
        }
    });

    _.each(rulesToCheck, function (check, ruleCode) {
        const processFile = processFileFunction(
            theme.files,
            failures,
            {
                [ruleCode]: require(`../ast-linter/rules`)[ruleCode]
            }
        );

        const linter = new ASTLinter({
            partials: theme.partials,
            helpers: [],
            customThemeSettings: theme.customSettings
        });

        _.each(theme.files, function (themeFile) {
            let templateTest = themeFile.file.match(/^[^/\\]+.hbs$/);

            if (templateTest) {
                processFile(linter, themeFile);
            }
        });

        if (failures.length > 0) {
            theme.results.fail[ruleCode] = {failures: failures};
        } else {
            theme.results.pass.push(ruleCode);
        }
    });

    return theme;
};

module.exports = checkTemplatesCompile;
<commit_msg>Revert "Reuse existing extracted custom settings instead of parsing package.json again"<commit_after>const _ = require('lodash');
const spec = require('../specs');
const {versions, normalizePath} = require('../utils');
const ASTLinter = require('../ast-linter');

function processFileFunction(files, failures, rules) {
    return function processFile(linter, themeFile) {
        if (themeFile.parsed.error) {
            // Ignore parsing errors, they are handled in 005
            return;
        }

        const astResults = linter.verify({
            parsed: themeFile.parsed,
            rules,
            source: themeFile.content,
            moduleId: themeFile.file
        });

        if (astResults.length) {
            failures.push({
                ref: themeFile.file,
                message: astResults[0].message
            });
        }

        linter.partials.forEach((partial) => {
            const file = files.find(f => normalizePath(f.file) === `partials/${normalizePath(partial)}.hbs`);
            if (file) {
                processFile(linter, file);
            }
        });
    };
}

function getCustomThemeSettings(theme) {
    let [packageJSON] = _.filter(theme.files, {file: 'package.json'});
    let customThemeSettingsConfig;
    if (packageJSON && packageJSON.content) {
        let packageJSONParsed = JSON.parse(packageJSON.content);
        if (packageJSONParsed.config && packageJSONParsed.config.custom) {
            customThemeSettingsConfig = packageJSONParsed.config.custom;
        }
    }
    return customThemeSettingsConfig;
}

const checkTemplatesCompile = function checkTemplatesCompile(theme, options) {
    const failures = [];
    const checkVersion = _.get(options, 'checkVersion', versions.default);
    const ruleSet = spec.get([checkVersion]);
    const customThemeSettings = getCustomThemeSettings(theme);

    const ruleRegex = /GS090-.*/g;

    const rulesToCheck = _.pickBy(ruleSet.rules, function (rule, ruleCode) {
        if (ruleCode.match(ruleRegex)) {
            return rule;
        }
    });

    _.each(rulesToCheck, function (check, ruleCode) {
        const processFile = processFileFunction(
            theme.files,
            failures,
            {
                [ruleCode]: require(`../ast-linter/rules`)[ruleCode]
            }
        );

        const linter = new ASTLinter({
            partials: theme.partials,
            helpers: [],
            customThemeSettings: customThemeSettings
        });

        _.each(theme.files, function (themeFile) {
            let templateTest = themeFile.file.match(/^[^/\\]+.hbs$/);

            if (templateTest) {
                processFile(linter, themeFile);
            }
        });

        if (failures.length > 0) {
            theme.results.fail[ruleCode] = {failures: failures};
        } else {
            theme.results.pass.push(ruleCode);
        }
    });

    return theme;
};

module.exports = checkTemplatesCompile;

{'fraction_non_alphanumeric': 0.12315455055129883, 'fraction_numerical': 0.0029900953092879836, 'mean_word_length': 2.0101237345331833, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3725402', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1442, 'n_words': 291}