<commit_before>import * as ts from "typescript";
import * as path from "path";
import * as fs from "fs";

// todo: Clean and split up this file

const NAMEOF_DEFINITION_FILE_NAME = "ts-nameof.d.ts";

interface FileInfo {
    readonly fileName: string;
    readonly callExpressionReplaces: ReplaceInfo[];
}

interface ReplaceInfo {
    pos: number;
    end: number;
    readonly text: string;
}

export function replaceInFiles(fileNames: string[], onFinished?: (err?: NodeJS.ErrnoException) => void) {
    const compilerOptions: ts.CompilerOptions = {
        allowJs: true,
        experimentalDecorators: true
    };

    const program = ts.createProgram([...fileNames, path.join(__dirname, NAMEOF_DEFINITION_FILE_NAME)], compilerOptions);
    const typeChecker = program.getTypeChecker();
    const sourceFiles = program.getSourceFiles().filter(f => f.fileName.indexOf("/node_modules/") === -1);

    const nameOfSymbol = getNameOfFunctionSymbol(sourceFiles);
    const fileInfos = getFileInfos(sourceFiles, nameOfSymbol, typeChecker).filter(f => f.callExpressionReplaces.length > 0);
    let completeCount = 0;

    function tryOnFinished(err?: NodeJS.ErrnoException) {
        /* istanbul ignore else  */
        if (onFinished != null) {
            onFinished();
        }
    }<commit_msg>Fixed passing error to callback when there's an error<commit_after>import * as ts from "typescript";
import * as path from "path";
import * as fs from "fs";

// todo: Clean and split up this file

const NAMEOF_DEFINITION_FILE_NAME = "ts-nameof.d.ts";

interface FileInfo {
    readonly fileName: string;
    readonly callExpressionReplaces: ReplaceInfo[];
}

interface ReplaceInfo {
    pos: number;
    end: number;
    readonly text: string;
}

export function replaceInFiles(fileNames: string[], onFinished?: (err?: NodeJS.ErrnoException) => void) {
    const compilerOptions: ts.CompilerOptions = {
        allowJs: true,
        experimentalDecorators: true
    };

    const program = ts.createProgram([...fileNames, path.join(__dirname, NAMEOF_DEFINITION_FILE_NAME)], compilerOptions);
    const typeChecker = program.getTypeChecker();
    const sourceFiles = program.getSourceFiles().filter(f => f.fileName.indexOf("/node_modules/") === -1);

    const nameOfSymbol = getNameOfFunctionSymbol(sourceFiles);
    const fileInfos = getFileInfos(sourceFiles, nameOfSymbol, typeChecker).filter(f => f.callExpressionReplaces.length > 0);
    let completeCount = 0;

    function tryOnFinished(err?: NodeJS.ErrnoException) {
        /* istanbul ignore else  */
        if (onFinished != null) {
            onFinished(err);
        }
    }
{'fraction_non_alphanumeric': 0.11347248576850096, 'fraction_numerical': 0.0022770398481973433, 'mean_word_length': 3.784029038112523, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '6556838', 'n_tokens_mistral': 781, 'n_tokens_neox': 762, 'n_words': 196}