<commit_before>import * as ts_module from "../node_modules/typescript/lib/tsserverlibrary";
import * as tslint from 'tslint';
import * as path from 'path';
import * as mockRequire from 'mock-require';

// Settings for the plugin section in tsconfig.json
interface Settings {
    alwaysShowRuleFailuresAsWarnings?: boolean;
    ignoreDefinitionFiles?: boolean;
    configFile?: string;
    disableNoUnusedVariableRule?: boolean  // support to enable/disable the workaround for https://github.com/Microsoft/TypeScript/issues/15344
    supressWhileTypeErrorsPresent: boolean;
    mockTypeScriptVersion: boolean;
}

const TSLINT_ERROR_CODE = 100000;

function init(modules: { typescript: typeof ts_module }) {
    const ts = modules.typescript;
    let tslint = require('tslint');

    let codeFixActions = new Map<string, Map<string, tslint.RuleFailure>>();
    let registeredCodeFixes = false;

    let configCache = {
        filePath: <string>null,
        configuration: <any>null,
        isDefaultConfig: false,
        configFilePath: <string>null
    };

    // Work around the lack of API to register a CodeFix
    function registerCodeFix(action: codefix.CodeFix) {
        return (ts as any).codefix.registerCodeFix(action);
    }

    if (!registeredCodeFixes && ts && (ts as any).codefix) {
        registerCodeFixes(registerCodeFix);
        registeredCodeFixes = true;
    }

    function registerCodeFixes(registerCodeFix: (action: codefix.CodeFix) => void) {
        // Code fix for that is used for all tslint fixes
        registerCodeFix({
            errorCodes: [TSLINT_ERROR_CODE],
            getCodeActions: (_context: any) => {
                return null;
            }
        });
    }

    function fixRelativeConfigFilePath(config: Settings, projectRoot: string): Settings {
        if (!config.configFile) {
            return config;
        }
        if (path.isAbsolute(config.configFile)) {
            return config;
        }
        config.configFile = path.join(projectRoot, config.configFile);
        return config;
    }

    function create(info: ts.server.PluginCreateInfo) {
        info.project.projectService.logger.info("tslint-language-service loaded");
        let config: Settings = fixRelativeConfigFilePath(info.config, info.project.getCurrentDirectory());
        let configuration: tslint.Configuration.IConfigurationFile = null;

        if(config.mockTypeScriptVersion) {
            mockRequire('typescript', ts);
            tslint = mockRequire.reRequire('tslint');
        }

        // Set up decorator
        const proxy = Object.create(null) as ts.LanguageService;
        const oldLS = info.languageService;
        for (const k in oldLS) {
            (<any>proxy)[k] = function () {
                return (<any>oldLS)[k].apply(oldLS, arguments);
            }
        }

        // key to identify a rule failure
        function computeKey(start: number, end: number): string {
            return `[${start},${end}]`;
        }

        function makeDiagnostic(problem: tslint.RuleFailure, file: ts.SourceFile): ts.Diagnostic {
            let message = (problem.getRuleName() !== null)
                ? `${problem.getFailure()} (${problem.getRuleName()})`
                : `${problem.getFailure()}`;

            let category;
            if (config.alwaysShowRuleFailuresAsWarnings === true) {
                category = ts.DiagnosticCategory.Warning;<commit_msg>Only require tslint once. Fixes #65<commit_after>import * as ts_module from "../node_modules/typescript/lib/tsserverlibrary";
import * as tslint from 'tslint';
import * as path from 'path';
import * as mockRequire from 'mock-require';

// Settings for the plugin section in tsconfig.json
interface Settings {
    alwaysShowRuleFailuresAsWarnings?: boolean;
    ignoreDefinitionFiles?: boolean;
    configFile?: string;
    disableNoUnusedVariableRule?: boolean  // support to enable/disable the workaround for https://github.com/Microsoft/TypeScript/issues/15344
    supressWhileTypeErrorsPresent: boolean;
    mockTypeScriptVersion: boolean;
}

const TSLINT_ERROR_CODE = 100000;

function init(modules: { typescript: typeof ts_module }) {
    const ts = modules.typescript;

    let codeFixActions = new Map<string, Map<string, tslint.RuleFailure>>();
    let registeredCodeFixes = false;

    let configCache = {
        filePath: <string>null,
        configuration: <any>null,
        isDefaultConfig: false,
        configFilePath: <string>null
    };

    // Work around the lack of API to register a CodeFix
    function registerCodeFix(action: codefix.CodeFix) {
        return (ts as any).codefix.registerCodeFix(action);
    }

    if (!registeredCodeFixes && ts && (ts as any).codefix) {
        registerCodeFixes(registerCodeFix);
        registeredCodeFixes = true;
    }

    function registerCodeFixes(registerCodeFix: (action: codefix.CodeFix) => void) {
        // Code fix for that is used for all tslint fixes
        registerCodeFix({
            errorCodes: [TSLINT_ERROR_CODE],
            getCodeActions: (_context: any) => {
                return null;
            }
        });
    }

    function fixRelativeConfigFilePath(config: Settings, projectRoot: string): Settings {
        if (!config.configFile) {
            return config;
        }
        if (path.isAbsolute(config.configFile)) {
            return config;
        }
        config.configFile = path.join(projectRoot, config.configFile);
        return config;
    }

    function create(info: ts.server.PluginCreateInfo) {
        info.project.projectService.logger.info("tslint-language-service loaded");
        let config: Settings = fixRelativeConfigFilePath(info.config, info.project.getCurrentDirectory());
        let configuration: tslint.Configuration.IConfigurationFile = null;

        if(config.mockTypeScriptVersion) {
            mockRequire('typescript', ts);
        }
        const tslint = require('tslint')

        // Set up decorator
        const proxy = Object.create(null) as ts.LanguageService;
        const oldLS = info.languageService;
        for (const k in oldLS) {
            (<any>proxy)[k] = function () {
                return (<any>oldLS)[k].apply(oldLS, arguments);
            }
        }

        // key to identify a rule failure
        function computeKey(start: number, end: number): string {
            return `[${start},${end}]`;
        }

        function makeDiagnostic(problem: tslint.RuleFailure, file: ts.SourceFile): ts.Diagnostic {
            let message = (problem.getRuleName() !== null)
                ? `${problem.getFailure()} (${problem.getRuleName()})`
                : `${problem.getFailure()}`;

            let category;
            if (config.alwaysShowRuleFailuresAsWarnings === true) {
                category = ts.DiagnosticCategory.Warning;
{'fraction_non_alphanumeric': 0.10697062691801841, 'fraction_numerical': 0.0035072336694432268, 'mean_word_length': 2.6501333333333332, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 2, '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': '1245802', 'n_tokens_mistral': 1889, 'n_tokens_neox': 1839, 'n_words': 448}