Skip to content

Commit fc5f189

Browse files
committed
Minor updates and spell fixes for the compiler section
1 parent 374395f commit fc5f189

15 files changed

+96
-27
lines changed

docs/compiler/ast-tip-children.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function forEachChild<T>(node: Node, cbNode: (node: Node) => T, cbNodeArr
2323
// .... lots more
2424
```
2525
26-
Basically it checks `node.kind` and based on that assumes an interface offered by the `node` and calls the `cbNode` on the children. Note however that this function doesn't call `visitNode` for *all* children (e.g. SyntaxKind.SemicolonToken). If you want *all* the children of a node in the AST just call `.getChildren` member function of the `Node`.
26+
Basically it checks `node.kind` and based on that assumes an interface offered by the `node` and calls the `cbNode` on the children. Note, however that this function doesn't call `visitNode` for *all* children (e.g. SyntaxKind.SemicolonToken). If you want *all* the children of a node in the AST just call `.getChildren` member function of the `Node`.
2727
2828
E.g. here is a function that prints the verbose `AST` of a node:
2929

docs/compiler/ast-tip-syntaxkind.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const enum SyntaxKind {
1010
// ... LOTS more
1111
```
1212
13-
It's a `const enum` (a concept [we covered previously](../enums.md)) so that it gets *inlined* (e.g. `ts.SyntaxKind.EndOfFileToken` becomes `1`) and we don't get a dereferencing cost when working with AST. However the compiler is compiled with `--preserveConstEnums` compiler flag so that the enum *is still available at runtime*. So in JavaScript you can use `ts.SyntaxKind.EndOfFileToken` if you want. Additionally you can convert these enum members to display strings using the following function:
13+
It's a `const enum` (a concept [we covered previously](../enums.md)) so that it gets *inlined* (e.g. `ts.SyntaxKind.EndOfFileToken` becomes `1`) and we don't get a dereferencing cost when working with the AST. However the compiler is compiled with `--preserveConstEnums` compiler flag so that the enum *is still available at runtime*. So in JavaScript you can use `ts.SyntaxKind.EndOfFileToken` if you want. Additionally you can convert these enum members to display strings using the following function:
1414
1515
```ts
1616
export function syntaxKindToName(kind: ts.SyntaxKind) {

docs/compiler/ast-trivia.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
### 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
2+
Trivia (called that because it's `trivial`) represent the parts of the source text that are largely insignificant for normal understanding of the code. For example; 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.
3+
4+
Before we show them you need to understand the following:
35

46
#### Trivia Ownership
57
In General:
68
* A token owns any trivia after it on the *same* line *upto* the next token.
79
* Any comment *after that line* is associated with the following token.
810

911
For leading and ending comments in a file:
10-
* The first token in the source file gets all the initial trivia
12+
* The first token in the source file gets all the initial trivia.
1113
* The last sequence of trivia in the file is tacked onto the end-of-file token, which otherwise has zero width.
1214

1315
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.
1416

1517
#### 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:
18+
For most basic uses, comments are the "interesting" trivia. The comments that belong to a Node can be fetched through the following functions:
1719

1820
Function | Description
1921
---------|------------

docs/compiler/ast.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
## Node
2-
The basic building block of the Abstract Syntax Tree (AST). In general node represent non-terminals in the language grammar; some terminals are kept in the tree such as identifiers and literals.
2+
The basic building block of the Abstract Syntax Tree (AST). In general a `Node` represents non-terminals in the language grammar; however, some terminals are kept in the tree such as identifiers and literals.
33

4-
Two key things make up an AST node documentation. Its `SyntaxKind` which identifies it within the AST and its `interface`, the API the node provides when instantiated for the AST.
4+
Two key things make up an AST node's documentation. The node's `SyntaxKind` which identifies its type within the AST, and its `interface`, the API the node provides when instantiated into the AST.
55

66
Here are a few key `interface Node` members:
77
* `TextRange` members that identify the node's `start` and `end` in the source file.
88
* `parent?: Node` the parent of the node in the AST.
99

10-
There are other additional members for node flags and modifiers etc. that you can lookup by searching `interface Node` in the source code but the ones we mentioned are vital for node traversal.
10+
There are other additional members for `Node` flags and modifiers etc. that you can lookup by searching `interface Node` in the source code but the ones we mentioned are vital for node traversal.
1111

1212
## SourceFile
1313

docs/compiler/binder-container.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,4 @@ function bindChildren(node: Node) {
120120
}
121121
```
122122

123-
As you might recall from section on binder functions : `bindChildren` is called from the `bind` function. So we have the recursive bindig setup : `bind` calls `bindChildren` calls `bind` for each child.
123+
As you might recall from section on binder functions : `bindChildren` is called from the `bind` function. So we have the recursive binding setup : `bind` calls `bindChildren` calls `bind` for each child.

docs/compiler/binder-declarations.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ function addDeclarationToSymbol(symbol: Symbol, node: Declaration, symbolFlags:
2929
```
3030

3131
The important linking portions:
32-
* creates a link to the Symbol from the AST node (`node.symbol`).
33-
* add the node as *one of* the declarations of the Symbol (`symbol.declarations`).
32+
* Creates a link to the Symbol from the AST node (`node.symbol`).
33+
* Adds the node as *one of* the declarations of the Symbol (`symbol.declarations`).
3434

3535
#### Declaration
3636
Declaration is just a `node` with an optional name. In `types.ts`

docs/compiler/binder-symbolflags.md

+68-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,72 @@
11
### SymbolFlags
2-
Symbols have `SymbolFlags`. Below we eplain the meaning of the important ones
2+
Symbols have `SymbolFlags`. Below we have them in their verbatim, as of TypeScript 2.2
3+
4+
```ts
5+
const enum SymbolFlags {
6+
None = 0,
7+
FunctionScopedVariable = 1,
8+
BlockScopedVariable = 2,
9+
Property = 4,
10+
EnumMember = 8,
11+
Function = 16,
12+
Class = 32,
13+
Interface = 64,
14+
ConstEnum = 128,
15+
RegularEnum = 256,
16+
ValueModule = 512,
17+
NamespaceModule = 1024,
18+
TypeLiteral = 2048,
19+
ObjectLiteral = 4096,
20+
Method = 8192,
21+
Constructor = 16384,
22+
GetAccessor = 32768,
23+
SetAccessor = 65536,
24+
Signature = 131072,
25+
TypeParameter = 262144,
26+
TypeAlias = 524288,
27+
ExportValue = 1048576,
28+
ExportType = 2097152,
29+
ExportNamespace = 4194304,
30+
Alias = 8388608,
31+
Prototype = 16777216,
32+
ExportStar = 33554432,
33+
Optional = 67108864,
34+
Transient = 134217728,
35+
Enum = 384,
36+
Variable = 3,
37+
Value = 107455,
38+
Type = 793064,
39+
Namespace = 1920,
40+
Module = 1536,
41+
Accessor = 98304,
42+
FunctionScopedVariableExcludes = 107454,
43+
BlockScopedVariableExcludes = 107455,
44+
ParameterExcludes = 107455,
45+
PropertyExcludes = 0,
46+
EnumMemberExcludes = 900095,
47+
FunctionExcludes = 106927,
48+
ClassExcludes = 899519,
49+
InterfaceExcludes = 792968,
50+
RegularEnumExcludes = 899327,
51+
ConstEnumExcludes = 899967,
52+
ValueModuleExcludes = 106639,
53+
NamespaceModuleExcludes = 0,
54+
MethodExcludes = 99263,
55+
GetAccessorExcludes = 41919,
56+
SetAccessorExcludes = 74687,
57+
TypeParameterExcludes = 530920,
58+
TypeAliasExcludes = 793064,
59+
AliasExcludes = 8388608,
60+
ModuleMember = 8914931,
61+
ExportHasLocal = 944,
62+
HasExports = 1952,
63+
HasMembers = 6240,
64+
BlockScoped = 418,
65+
PropertyOrAccessor = 98308,
66+
Export = 7340032,
67+
ClassMember = 106500,
68+
}
69+
```
370

471
#### ValueModule
572
`ValueModule // Instantiated module` is the SymbolFlag used for `SourceFile` if it an external module.

docs/compiler/binder-symboltable.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
### SymbolTable
22

3-
Its implemented as a simple HashMap. Here is the interface (`types.ts`):
3+
SymbolTable is implemented as a simple HashMap. Here is the interface (`types.ts`):
44

55
```ts
66
interface SymbolTable {
77
[index: string]: Symbol;
88
}
99
```
1010

11-
SymbolTables as initialized by binding. There are a few SymbolTables used by the compiler.
11+
SymbolTables are initialized by binding. There are a few SymbolTables used by the compiler:
1212

1313
On `Node`:
1414
```ts
@@ -25,7 +25,7 @@ exports?: SymbolTable; // Module exports
2525
Note: We saw `locals` getting initialized (to `{}`) by `bindChildren` based on `ContainerFlags`.
2626

2727
#### SymbolTable population
28-
SymbolTable are populated with `Symbols` primarily by a call to `declareSymbol`. This function is presented below in entirety:
28+
SymbolTables are populated with `Symbols` primarily by a call to `declareSymbol`. This function is presented below in entirety:
2929

3030
```ts
3131
/**
@@ -100,7 +100,7 @@ function declareSymbol(symbolTable: SymbolTable, parent: Symbol, node: Declarati
100100
}
101101
```
102102

103-
Which SymbolTable gets populated is driven by the first argument to this function. e.g. when adding a declaration to a *container* of kind `SyntaxKind.ClassDeclaration` or `SytanxKind.ClassExpression` the function `declareClassMember` will get called which has the following code:
103+
Which SymbolTable is populated is driven by the first argument to this function. e.g. when adding a declaration to a *container* of kind `SyntaxKind.ClassDeclaration` or `SytanxKind.ClassExpression` the function `declareClassMember` will get called which has the following code:
104104

105105
```ts
106106
function declareClassMember(node: Declaration, symbolFlags: SymbolFlags, symbolExcludes: SymbolFlags) {

docs/compiler/binder.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## Binder
2-
Most JavaScript transpilers out there are simpler than TypeScript in that they provide little in the way of code analysis. The typical JavaScript transpilers only have the following flow:
2+
Most JavaScript transpilers out there are simpler than TypeScript because they provide little in the way of code analysis. The typical JavaScript transpilers only have the following flow:
33

44
```ts
55
SourceCode ~~Scanner~~> Tokens ~~Parser~~> AST ~~Emitter~~> JavaScript
@@ -8,7 +8,7 @@ SourceCode ~~Scanner~~> Tokens ~~Parser~~> AST ~~Emitter~~> JavaScript
88
While the above architecture is true as a simplified understand of TypeScript js generation, a key feature of TypeScript is its *Semantic* system. In order to assist type checking (performed by `checker`), the `binder` (in `binder.ts`) is used to connect the various parts of the source code into a coherent type system that can then be used by the `checker`. The main responsibility of the binder is to create the _Symbols_.
99

1010
### Symbol
11-
Symbols connect declaration nodes in the AST to other declarations contributing to the same entity. Symbols are the basic building block of the Semantic system. The symbol constructor is defined in `core.ts` (and `binder` actually uses the `objectAllocator.getSymbolConstructor` to get its hands on it). Here is the contructor:
11+
Symbols connect declaration nodes in the AST to other declarations contributing to the same entity. Symbols are the basic building block of the Semantic system. The symbol constructor is defined in `core.ts` (and `binder` actually uses the `objectAllocator.getSymbolConstructor` to get its hands on it). Here is the constructor:
1212

1313
```ts
1414
function Symbol(flags: SymbolFlags, name: string) {

docs/compiler/checker-global.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
### Global Namespace Merging
2-
Within `initializeTypeChecker` the following code exists :
2+
Within `initializeTypeChecker` the following code exists:
33

44
```ts
55
// Initialize global symbol table

docs/compiler/checker.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## Checker
2-
Like we mentioned before *checker* is the thing that makes TypeScript uniquely more powerful than *just another JavaScript transpiler*. The checker is located in `checker.ts` and at this moment it is 15k+ lines of code (largest part of the compiler).
2+
Like we mentioned before *checker* is the thing that makes TypeScript uniquely more powerful than *just another JavaScript transpiler*. The checker is located in `checker.ts` and at this moment it is 23k+ lines of TypeScript (largest part of the compiler).
33

44
### Usage by Program
55
The `checker` is initialized by `program`. The following is a sampling of the call stack (we showed the same one when looking at `binder`):

docs/compiler/emitter-functions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Defined in `emitter.ts` here is the function signature:
55
export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFile?: SourceFile): EmitResult {
66
```
77
8-
`EmitHost` is a just a simplified (as in narrowed down) version of `CompilerHost` (and is at runtime actually a CompilerHost for many use cases).
8+
`EmitHost` is a just a simplified (as in narrowed down) version of `CompilerHost` (and is at runtime actually a `CompilerHost` for many use cases).
99
1010
The most interesting call stack from `emitFiles` is the following:
1111

docs/compiler/emitter-sourcemaps.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
### Emitter SourceMaps
22

3-
We said that the bulk of the `emitter.ts` is the local function `emitJavaScript` (we showed the initialization routine of this function before). It basically sets up a bunch of locals and hits off to `emitSourceFile`. The following is a revisiting of the function, this time focusing on SourceMap stuff:
3+
We said that the bulk of the `emitter.ts` is the local function `emitJavaScript` (we showed the initialization routine of this function before). It basically sets up a bunch of locals and hits off to `emitSourceFile`. The following is a revisiting of the function, this time focusing on `SourceMap` stuff:
44

55
```ts
66
function emitJavaScript(jsFilePath: string, root?: SourceFile) {
@@ -63,7 +63,7 @@ function emitJavaScript(jsFilePath: string, root?: SourceFile) {
6363
/// BUNCH OF LOCAL FUNCTIONS
6464
```
6565
66-
The imporant function call here : `initializeEmitterWithSourceMaps` which is a function local to `emitJavaScript` that overrides some locals that were already defined here. At the bottom of `initializeEmitterWithSourceMaps` you will notice the overriding:
66+
The important function call here : `initializeEmitterWithSourceMaps` which is a function local to `emitJavaScript` that overrides some locals that were already defined here. At the bottom of `initializeEmitterWithSourceMaps` you will notice the overriding:
6767
6868
```ts
6969
// end of `initializeEmitterWithSourceMaps`
@@ -78,4 +78,4 @@ The imporant function call here : `initializeEmitterWithSourceMaps` which is a f
7878
writeComment = writeCommentRangeWithMap;
7979
```
8080
81-
This means that the bulk of emitter code can not care about SourceMap and just use these local functions the same way with or without SourceMaps.
81+
This means that the bulk of emitter code can not care about `SourceMap` and just use these local functions the same way with or without SourceMaps.

docs/compiler/overview.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ It is split into the follow key parts:
1010

1111
Each of these get their own unique files in the source. These parts will be explained later on in this chapter.
1212

13-
## NTypeScript
14-
We have a project called [NTypeScript](https://github.com/TypeStrong/ntypescript) which makes it easier to play around with the compiler API e.g. by exposing internal APIs. You use it the same way you would use `typescript` but just have an `n` prefix for all things (binary : `ntsc`, require: `ntypescript`). This is also the compiler used by atom-typescript and the one we will use to present these examples.
13+
## BYOTS
14+
We have a project called [Bring Your Own TypeScript (BYOTS)](https://github.com/basarat/byots) which makes it easier to play around with the compiler API e.g. by exposing internal APIs. You can use it to expose your local app's version of TypeScript globally.
1515

1616
## Syntax vs. Semantics
1717
Just because something is *syntactically* correct doesn't mean it is *semantically* correct. Consider the following piece of TypeScript code which although *syntactically* valid is *semantically* wrong

docs/compiler/scanner.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## Scanner
2-
The sourcecode for the TypeScript scanner is located entirely in `scanner.ts`. Scanner is *controlled* internally by the `Parser` to convert the source code to an AST. Here is what the desired outcome is.
2+
The source code for the TypeScript scanner is located entirely in `scanner.ts`. Scanner is *controlled* internally by the `Parser` to convert the source code to an AST. Here is what the desired outcome is.
33

44
```
55
SourceCode ~~ scanner ~~> Token Stream ~~ parser ~~> AST

0 commit comments

Comments
 (0)