Skip to content

Commit f2c184e

Browse files
author
Michael Dougall
committed
chore: adds more info around compiler steps
1 parent c2ac781 commit f2c184e

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
const helloWorld = () => { }
1+
function helloWorld() { }

translations/en/transformer-handbook.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,16 @@ We will talk about how to narrow the node to a specific type of node later in th
172172
### Stages
173173

174174
Very similar to Babel -
175-
Typescript has three primary stages,
176-
**parse**,
175+
Typescript however has five stages,
176+
**parser**,
177+
_binder_,
178+
_checker_,
177179
**transform**,
178-
**emit**.
180+
**emitting**.
179181

180-
With two extra steps that are exclusive to Typescript,
181-
**binding** and **checking** (which relate to the _semantics_/type correctness,
182-
which for the most part we're going to skimp over in this handbook).
182+
Two steps are exclusive to Typescript,
183+
_binder_ and _checker_.
184+
We are going to gloss over _checker_ as it relates to Typescripts type checking specifics.
183185

184186
> For a more in-depth understanding of the Typescript compiler internals have a read of [Basarat's handbook](https://basarat.gitbooks.io/typescript/content/docs/compiler/overview.html).
185187
@@ -196,7 +198,7 @@ project out.
196198
This is why enums don't work when parsing Typescript with Babel for example,
197199
it just doesn't have all the information available.
198200

199-
#### Parse
201+
#### Parser
200202

201203
The Typescript parser actually has two parts,
202204
the `scanner`,
@@ -207,9 +209,20 @@ This step will convert source code into an AST.
207209
SourceCode ~~ scanner ~~> Token Stream ~~ parser ~~> AST
208210
```
209211

210-
I definitely recommend reading the [Parser section](https://basarat.gitbooks.io/typescript/content/docs/compiler/parser.html) in the Typescript Handbook.
212+
The parser takes source code and tries to convert it into an in-memory AST representation which you can work with in the compiler. Also: see [Parser](https://basarat.gitbooks.io/typescript/content/docs/compiler/parser.html).
211213

212-
#### Transform
214+
##### Scanner
215+
216+
The scanner is used by the parser to convert a string into tokens in a linear fashion,
217+
then it's up to a parser to tree-ify them.
218+
Also: see [Scanner](https://basarat.gitbooks.io/typescript/docs/compiler/scanner.html).
219+
220+
#### Binder
221+
222+
Creates a symbol map and uses the AST to provide the type system which is important to link references and to be able to know the nodes of imports and exports.
223+
Also: see [Binder](https://basarat.gitbooks.io/typescript/docs/compiler/binder.htmlhttps://basarat.gitbooks.io/typescript/docs/compiler/binder.html).
224+
225+
#### Transforms
213226

214227
This is the step we're all here for.
215228
It allows us,
@@ -230,7 +243,7 @@ but if you need to do some post-compilation transformation,
230243
or modify types,
231244
you'll end up wanting to use `after` and `afterDeclarations`.
232245

233-
#### Emit
246+
#### Emitting
234247

235248
This stage happens last and is responsible for _emitting_ the final code somewhere.
236249
Generally this is usually to the file system -
@@ -500,6 +513,9 @@ These methods are useful for modifying a `node` in some form.
500513
501514
- `ts.updateXyz(node, ...)` - useful for updating a node (to then return), an example of this is `ts.updateVariableDeclaration()`
502515
- `ts.updateSourceFileNode(sourceFile, ...)` - useful for updating a source file to then return
516+
- `ts.setOriginalNode(newNode, parentNode)` - useful for setting a nodes original (parent) node
517+
- `ts.setXyz(...)` - sets things
518+
- `ts.addXyz(...)` - adds things
503519

504520
### `context`
505521

0 commit comments

Comments
 (0)