# Trivia

Trivia(`trivial`だからそう呼ばれます)は、コードの正常な理解のためにはほとんど重要でないソーステキストの部分を表します。例えば;、空白、コメント、さらには競合マーカー(conflict markers)が含まれます。TriviaはASTに保存されません(軽量に保つため)。しかし、いくつかの`ts.*`APIを使用してオンデマンドでフェッチすることができます。

それらを表示する前に、以下を理解する必要があります。

## Triviaのオーナーシップ

一般に：

* トークンは、次のトークンまでの間に何らかのtriviaを同じ行の後ろに保持しています
* その行の後の何らかのコメントは、次のトークンに関連付けられます。

ファイル内の先頭と末尾のコメント：

* ソースファイルの最初のトークンはすべての初期状態のtriviaを取得します。
* ファイル内のtriviaの最後のシーケンスは、ファイルの終わりのトークンに付けられます。それ以外の場合はゼロ幅です。

## TriviaのAPI

ほとんどの基本的な用途では、コメントは"interesting"なtriviaです。Nodeに属するコメントは、次の関数を使用して取得できます。

| 関数                            | 説明                                                                                                    |
| ----------------------------- | ----------------------------------------------------------------------------------------------------- |
| `ts.getLeadingCommentRanges`  | そのテキスト内のソーステキストと位置を指定すると、与えられた位置に続く、最初の改行と、トークン自体の間のコメントの範囲を返します(おそらく、`ts.Node.getFullStart`で最も有用です)。 |
| `ts.getTrailingCommentRanges` | そのテキスト内のソーステキストと位置を指定すると、与えられた位置に続く最初の改行までのコメントの範囲を返します(おそらく `ts.Node.getEnd`で最も有用です)。                |

例として、ソースファイルのこの部分を想像してみてください：

```typescript
debugger;/*hello*/
    //bye
  /*hi*/    function
```

`function`の`getLeadingCommentRanges`は、最後の2つのコメント`//bye`と`/*hi*/`だけを返します。

状況にフィットするように、debugger文の後ろで`getTrailingCommentRanges`を呼び出すと、`/*hello*/`コメントが展開されます。

## Token Start/Full Start

Nodesには、"token start"と"full start"というものがあります。

* Token Start：より自然なバージョン。トークンのテキストが始まるファイル内の位置
* Full Start：Scannerが最後の意味のあるトークンからスキャンを開始した位置

ASTノードには、`getStart`と`getFullStart`のためのAPIがあります。次の例です:

```typescript
debugger;/*hello*/
    //bye
  /*hi*/    function
```

`function`の場合、token startは`function`にありますが、full startは`/*hello*/`にあります。full startには、(それ以外の場合は前のノードに属する)Triviaも含まれます。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://typescript-jp.gitbook.io/deep-dive/overview/ast/ast-trivia.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
