# アーキテクチャ構成図

## システム全体構成図

```mermaid
flowchart TB
    subgraph External["外部"]
        FS["ファイルシステム"]
        STDIN["stdin/stdout"]
        NPM["npm/yarn<br/>外部プラグイン"]
    end

    subgraph CLI["CLI Layer"]
        BIN["bin/prettier.cjs"]
        CLI_INDEX["cli/index.js"]
        CLI_FORMAT["cli/format.js"]
        CLI_CONTEXT["cli/context.js"]
        CLI_OPTIONS["cli/options/"]
    end

    subgraph API["API Layer"]
        INDEX["index.js"]
        STANDALONE["standalone.js"]
    end

    subgraph Core["Core Layer"]
        CORE["main/core.js"]
        AST_TO_DOC["main/ast-to-doc.js"]
        PARSER_PRINTER["main/parser-and-printer.js"]
        NORMALIZE["main/normalize-format-options.js"]
        PLUGINS_MGR["main/plugins/"]
    end

    subgraph Document["Document Layer"]
        DOC_BUILDERS["document/builders/"]
        DOC_PRINTER["document/printer/printer.js"]
        DOC_UTILS["document/utilities/"]
    end

    subgraph Plugins["Plugin Layer"]
        subgraph BuiltinPlugins["Built-in Plugins"]
            LANG_JS["language-js/"]
            LANG_CSS["language-css/"]
            LANG_HTML["language-html/"]
            LANG_MD["language-markdown/"]
            LANG_YAML["language-yaml/"]
            LANG_GQL["language-graphql/"]
            LANG_JSON["language-json/"]
            LANG_HBS["language-handlebars/"]
        end
        EXTERNAL_PLUGINS["External Plugins"]
    end

    subgraph Config["Config Layer"]
        RESOLVE_CONFIG["config/resolve-config.js"]
        PRETTIER_CONFIG["config/prettier-config/"]
        EDITORCONFIG["config/editorconfig/"]
    end

    subgraph Utilities["Utilities Layer"]
        UTILS["utilities/"]
        COMMON["common/"]
    end

    %% External connections
    FS --> CLI_FORMAT
    STDIN --> CLI_INDEX
    NPM --> PLUGINS_MGR

    %% CLI Layer
    BIN --> CLI_INDEX
    CLI_INDEX --> CLI_FORMAT
    CLI_INDEX --> CLI_CONTEXT
    CLI_INDEX --> CLI_OPTIONS
    CLI_FORMAT --> INDEX

    %% API Layer
    INDEX --> CORE
    INDEX --> PLUGINS_MGR
    INDEX --> RESOLVE_CONFIG
    STANDALONE --> CORE

    %% Core Layer
    CORE --> AST_TO_DOC
    CORE --> PARSER_PRINTER
    CORE --> NORMALIZE
    AST_TO_DOC --> DOC_BUILDERS
    PARSER_PRINTER --> BuiltinPlugins
    PARSER_PRINTER --> EXTERNAL_PLUGINS
    PLUGINS_MGR --> BuiltinPlugins

    %% Document Layer
    DOC_BUILDERS --> DOC_UTILS
    DOC_PRINTER --> DOC_UTILS

    %% Plugin Layer
    LANG_JS --> DOC_BUILDERS
    LANG_CSS --> DOC_BUILDERS
    LANG_HTML --> DOC_BUILDERS
    LANG_MD --> DOC_BUILDERS
    LANG_YAML --> DOC_BUILDERS
    LANG_GQL --> DOC_BUILDERS
    LANG_JSON --> DOC_BUILDERS
    LANG_HBS --> DOC_BUILDERS

    %% Config Layer
    RESOLVE_CONFIG --> PRETTIER_CONFIG
    RESOLVE_CONFIG --> EDITORCONFIG
    RESOLVE_CONFIG --> FS

    %% Utilities dependencies
    CORE --> UTILS
    CORE --> COMMON
    LANG_JS --> UTILS
```

## フォーマット処理パイプライン

```mermaid
flowchart LR
    subgraph Input["入力"]
        SOURCE["ソースコード<br/>(テキスト)"]
    end

    subgraph Parse["パース"]
        PARSER["Parser<br/>(言語別)"]
        AST["AST<br/>(抽象構文木)"]
    end

    subgraph Print["プリント"]
        PRINTER["Printer<br/>(言語別)"]
        IR["Document IR<br/>(中間表現)"]
    end

    subgraph Output["出力"]
        DOC_TO_STRING["printDocToString"]
        FORMATTED["フォーマット済み<br/>テキスト"]
    end

    SOURCE --> PARSER
    PARSER --> AST
    AST --> PRINTER
    PRINTER --> IR
    IR --> DOC_TO_STRING
    DOC_TO_STRING --> FORMATTED
```

## プラグインアーキテクチャ

```mermaid
classDiagram
    class Plugin {
        +languages: Language[]
        +parsers: Record~string, Parser~
        +printers: Record~string, Printer~
        +options: Record~string, Option~
    }

    class Parser {
        +parse(text: string): AST
        +astFormat: string
        +locStart(node): number
        +locEnd(node): number
        +hasPragma(text): boolean
    }

    class Printer {
        +print(path, options, print): Doc
        +embed(path, print, textToDoc, options): Doc
        +insertPragma(text): string
        +massageAstNode(node): Node
        +canAttachComment(node): boolean
        +printComment(path, options): Doc
    }

    class Language {
        +name: string
        +parsers: string[]
        +extensions: string[]
        +filenames: string[]
    }

    Plugin --> Language
    Plugin --> Parser
    Plugin --> Printer
```

## Document IR 構造

```mermaid
classDiagram
    class Doc {
        <<interface>>
    }

    class StringDoc {
        +type: "string"
        +value: string
    }

    class ArrayDoc {
        +type: "array"
        +parts: Doc[]
    }

    class LineDoc {
        +type: "line"
        +soft: boolean
        +hard: boolean
        +literal: boolean
    }

    class GroupDoc {
        +type: "group"
        +contents: Doc
        +break: boolean
        +expandedStates: Doc[]
        +id: symbol
    }

    class IndentDoc {
        +type: "indent"
        +contents: Doc
    }

    class IfBreakDoc {
        +type: "if-break"
        +breakContents: Doc
        +flatContents: Doc
        +groupId: symbol
    }

    class FillDoc {
        +type: "fill"
        +parts: Doc[]
    }

    Doc <|-- StringDoc
    Doc <|-- ArrayDoc
    Doc <|-- LineDoc
    Doc <|-- GroupDoc
    Doc <|-- IndentDoc
    Doc <|-- IfBreakDoc
    Doc <|-- FillDoc
```

## CLI処理フロー

```mermaid
sequenceDiagram
    participant User as ユーザー
    participant CLI as bin/prettier.cjs
    participant CLIIndex as cli/index.js
    participant Context as Context
    participant Format as formatFiles
    participant API as index.js
    participant Core as core.js

    User->>CLI: prettier --write src/**/*.js
    CLI->>CLIIndex: run(rawArguments)
    CLIIndex->>Context: new Context()
    CLIIndex->>Context: init()
    Context->>Context: parseArguments()
    Context->>Context: loadPlugins()
    CLIIndex->>Format: formatFiles(context)

    loop 各ファイル
        Format->>Format: expandPatterns()
        Format->>Format: getOptionsForFile()
        Format->>API: format(text, options)
        API->>Core: formatWithCursor()
        Core->>Core: parseText()
        Core->>Core: printAstToDoc()
        Core->>Core: printDocToString()
        Core-->>API: { formatted }
        API-->>Format: formatted
        Format->>Format: writeFile()
    end

    Format-->>CLIIndex: 完了
    CLIIndex-->>User: 終了
```

## 設定解決フロー

```mermaid
flowchart TB
    START["resolveConfig(filePath)"]

    subgraph PrettierConfig["Prettier設定検索"]
        SEARCH_DIR["searchPrettierConfig<br/>(ディレクトリ検索)"]
        CHECK_FILES{".prettierrc<br/>.prettierrc.json<br/>.prettierrc.js<br/>etc."}
        LOAD_CONFIG["loadPrettierConfig"]
    end

    subgraph EditorConfig["EditorConfig検索"]
        LOAD_EDITOR["loadEditorconfig"]
        PARSE_EDITOR["editorconfig解析"]
    end

    subgraph Merge["設定マージ"]
        MERGE_BASE["EditorConfig + PrettierConfig"]
        APPLY_OVERRIDES["overridesの適用"]
        FINAL_OPTIONS["最終オプション"]
    end

    START --> SEARCH_DIR
    START --> LOAD_EDITOR

    SEARCH_DIR --> CHECK_FILES
    CHECK_FILES -->|見つかった| LOAD_CONFIG
    CHECK_FILES -->|見つからない| MERGE_BASE

    LOAD_EDITOR --> PARSE_EDITOR
    PARSE_EDITOR --> MERGE_BASE
    LOAD_CONFIG --> MERGE_BASE

    MERGE_BASE --> APPLY_OVERRIDES
    APPLY_OVERRIDES --> FINAL_OPTIONS
```

## モジュール依存関係図

```mermaid
graph TD
    subgraph Entrypoints["エントリーポイント"]
        INDEX[index.js]
        STANDALONE[standalone.js]
        CLI[cli/index.js]
    end

    subgraph Core["コア"]
        CORE_JS[main/core.js]
        AST_DOC[main/ast-to-doc.js]
        PARSER_PRINTER[main/parser-and-printer.js]
        PLUGINS_INDEX[main/plugins/index.js]
        NORMALIZE_OPTS[main/normalize-format-options.js]
    end

    subgraph Document["ドキュメント"]
        DOC_INDEX[document/index.js]
        DOC_PUBLIC[document/public.js]
        DOC_PRINTER[document/printer/printer.js]
        DOC_BUILDERS[document/builders/index.js]
    end

    subgraph Config["設定"]
        RESOLVE_CONFIG[config/resolve-config.js]
        PRETTIER_CONF[config/prettier-config/index.js]
        EDITORCONF[config/editorconfig/index.js]
    end

    subgraph Languages["言語プラグイン"]
        LANG_JS_IDX[language-js/index.js]
        LANG_CSS_IDX[language-css/index.js]
        LANG_HTML_IDX[language-html/index.js]
        LANG_MD_IDX[language-markdown/index.js]
    end

    subgraph Common["共通"]
        ERRORS[common/errors.js]
        AST_PATH[common/ast-path.js]
        EOL[common/end-of-line.js]
    end

    INDEX --> CORE_JS
    INDEX --> PLUGINS_INDEX
    INDEX --> RESOLVE_CONFIG
    INDEX --> DOC_PUBLIC

    CLI --> INDEX

    STANDALONE --> CORE_JS

    CORE_JS --> AST_DOC
    CORE_JS --> PARSER_PRINTER
    CORE_JS --> NORMALIZE_OPTS
    CORE_JS --> DOC_INDEX

    AST_DOC --> AST_PATH
    AST_DOC --> DOC_BUILDERS

    PARSER_PRINTER --> ERRORS

    RESOLVE_CONFIG --> PRETTIER_CONF
    RESOLVE_CONFIG --> EDITORCONF

    PLUGINS_INDEX --> LANG_JS_IDX
    PLUGINS_INDEX --> LANG_CSS_IDX
    PLUGINS_INDEX --> LANG_HTML_IDX
    PLUGINS_INDEX --> LANG_MD_IDX

    DOC_PRINTER --> DOC_BUILDERS
    DOC_PRINTER --> EOL
```
