<commit_before>﻿/**
 * Code Manipulation - Flatten declaration files.
 * ----------------------------------------------
 * This flattens the definition file output of the TypeScript compiler into one main.d.ts file.
 * ----------------------------------------------
 */
import * as path from "path";
import Project, {SourceFile, ClassDeclaration, TypeGuards, ts, SyntaxKind} from "../src/main";

export function flattenDeclarationFiles(project: Project, mainFile: SourceFile) {
    const definitionFiles = project.getSourceFiles(["dist/**/*.d.ts", "!dist/typescript/typescript.d.ts"]);
    const compilerApiFile = project.getSourceFileOrThrow("dist/typescript/typescript.d.ts");
    const exportedDeclarations = mainFile.getExportedDeclarations();
    mainFile.replaceWithText(`import CodeBlockWriter from "code-block-writer";\n`); // clear the source file

    for (let declaration of exportedDeclarations) {
        if (declaration.getSourceFile() === compilerApiFile)
            continue;
        if (TypeGuards.isVariableDeclaration(declaration))
            declaration = declaration.getFirstAncestorByKindOrThrow(SyntaxKind.VariableStatement);

        mainFile.insertText(mainFile.getFullWidth(), declaration.getFullText() + "\n");
    }

    // add an import to the typescript compiler api file
    mainFile.addImportDeclaration({
        namedImports: ["ts", "SyntaxKind", "CompilerOptions", "EmitHint", "ScriptKind", "NewLineKind", "LanguageVariant", "ScriptTarget",
            "TypeFlags", "ObjectFlags", "SymbolFlags", "TypeFormatFlags", "DiagnosticCategory", "EditorSettings"],
        moduleSpecifier: mainFile.getRelativePathToSourceFileAsModuleSpecifier(compilerApiFile)
    });

    mainFile.addExportDeclaration({ moduleSpecifier: mainFile.getRelativePathToSourceFileAsModuleSpecifier(compilerApiFile) });

    // update the main.d.ts file
    mainFile.getClassOrThrow("Project").setIsDefaultExport(true);
    mainFile.replaceWithText(mainFile.getFullText().replace(/compiler\.([A-Za-z]+)/g, "$1"));

    definitionFiles.filter(f => f !== mainFile).forEach(f => f.delete());
}
<commit_msg>chore: Fix declaration file error.<commit_after>﻿/**
 * Code Manipulation - Flatten declaration files.
 * ----------------------------------------------
 * This flattens the definition file output of the TypeScript compiler into one main.d.ts file.
 * ----------------------------------------------
 */
import * as path from "path";
import Project, {SourceFile, ClassDeclaration, TypeGuards, ts, SyntaxKind} from "../src/main";

export function flattenDeclarationFiles(project: Project, mainFile: SourceFile) {
    const definitionFiles = project.getSourceFiles(["dist/**/*.d.ts", "!dist/typescript/typescript.d.ts"]);
    const compilerApiFile = project.getSourceFileOrThrow("dist/typescript/typescript.d.ts");
    const exportedDeclarations = mainFile.getExportedDeclarations();
    mainFile.replaceWithText(`import CodeBlockWriter from "code-block-writer";\n`); // clear the source file

    for (let declaration of exportedDeclarations) {
        if (declaration.getSourceFile() === compilerApiFile)
            continue;
        if (TypeGuards.isVariableDeclaration(declaration))
            declaration = declaration.getFirstAncestorByKindOrThrow(SyntaxKind.VariableStatement);

        mainFile.insertText(mainFile.getFullWidth(), declaration.getFullText() + "\n");
    }

    // add an import to the typescript compiler api file
    mainFile.addImportDeclaration({
        namedImports: ["ts", "SyntaxKind", "CompilerOptions", "EmitHint", "ScriptKind", "NewLineKind", "LanguageVariant", "ScriptTarget",
            "TypeFlags", "ObjectFlags", "SymbolFlags", "TypeFormatFlags", "DiagnosticCategory", "EditorSettings", "ModuleResolutionKind"],
        moduleSpecifier: mainFile.getRelativePathToSourceFileAsModuleSpecifier(compilerApiFile)
    });

    mainFile.addExportDeclaration({ moduleSpecifier: mainFile.getRelativePathToSourceFileAsModuleSpecifier(compilerApiFile) });

    // update the main.d.ts file
    mainFile.getClassOrThrow("Project").setIsDefaultExport(true);
    mainFile.replaceWithText(mainFile.getFullText().replace(/compiler\.([A-Za-z]+)/g, "$1"));

    definitionFiles.filter(f => f !== mainFile).forEach(f => f.delete());
}

{'fraction_non_alphanumeric': 0.1653764954257565, 'fraction_numerical': 0.0004691531785127844, 'mean_word_length': 3.7856341189674523, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '7039461', 'n_tokens_mistral': 1139, 'n_tokens_neox': 1124, 'n_words': 235}