Skip to content

Commit 7e6c93b

Browse files
committed
ast trivia.md
1 parent c0518b5 commit 7e6c93b

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@
3434
* [Program](docs/compiler/program.md)
3535
* [AST](docs/compiler/ast.md)
3636
* [TIP: Visit Children](docs/compiler/ast-tip-children.md)
37+
* [Trivia](docs/compiler/ast-trivia.md)

docs/compiler/ast-trivia.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
### Trivia
2+
Trivia (called that because its `trivial`) represent the parts of the source text that are largely insignificant for normal understanding of the code, such as whitespace, comments, and even conflict markers. Trivia is *not stored* in the AST (to keep it lightweight). However it can be fetched *on demand* using a few `ts.` APIs. Before we show them you need to understand
3+
4+
#### Trivia Ownership
5+
In General:
6+
* A token owns any trivia after it on the *same* line *upto* the next token.
7+
* Any comment *after that line* is associated with the following token.
8+
9+
For leading and ending comments in a file:
10+
* The first token in the source file gets all the initial trivia
11+
* The last sequence of trivia in the file is tacked onto the end-of-file token, which otherwise has zero width.
12+
13+
The first token in the source file gets all the initial trivia, and the last sequence of trivia in the file is tacked onto the end-of-file token, which otherwise has zero width.
14+
15+
#### Trivia APIs
16+
For most basic uses, comments are the "interesting" trivia. The comments that belong to a Node which can be fetched through the following functions:
17+
18+
Function | Description
19+
---------|------------
20+
`ts.getLeadingCommentRanges` | Given the source text and position within that text, returns ranges of comments between the first line break following the given position and the token itself (probably most useful with `ts.Node.getFullStart`).
21+
`ts.getTrailingCommentRanges` | Given the source text and position within that text, returns ranges of comments until the first line break following the given position (probably most useful with `ts.Node.getEnd`).
22+
23+
As an example, imagine this portion of a source file:
24+
25+
```ts
26+
debugger;/*hello*/
27+
//bye
28+
/*hi*/ function
29+
```
30+
31+
`getLeadingCommentRanges` for the `function` will only return the last 2 comments `//bye` and `/*hi*/`.
32+
33+
Appropriately, calling `getTrailingCommentRanges` on the end of the debugger statement will extract the `/*hello*/` comment.
34+
35+
#### Token Start/Full Start
36+
Nodes have what is called a "token start" and a "full start".
37+
38+
* Token Start: the more natural version, which is the position in file where the text of a token begins
39+
* Full Start: the point at which the scanner began scanning since the last significant token
40+
41+
AST nodes have an API for `getStart` and `getFullStart`. In the following example:
42+
43+
```ts
44+
debugger;/*hello*/
45+
//bye
46+
/*hi*/ function
47+
```
48+
for `function` the token start is at `function` whereas *full* start is at `/*hello*/`. Note that full start even includes the trivia that would otherwise be owned by the previous node.

docs/compiler/overview.md

+3
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ An AST node.
2929

3030
## File: System
3131
`system.ts`. All interaction of the TypeScript compiler with the operating system goes through a `System` interface. Both the interface and its implementations (`WScript` and `Node`) are defined in `system.ts`. You can think of it as the *Operating Environment* (OE).
32+
33+
34+
Note: Thanks to the TypeScript team for providing much of the docs : https://github.com/Microsoft/TypeScript/wiki/Architectural-Overview that are used to write this story.

docs/compiler/scanner.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
4+
### Standalone scanner
5+
You can create a standalone scanner using `createScanner` and use `setText`/`setTextPos` to scan at different points in a file.

0 commit comments

Comments
 (0)