0
# Token Types
1
2
Enumeration of lexical token types for parsing and syntax highlighting in the TypeScript-ESTree ecosystem. These constants enable type-safe token classification and processing.
3
4
## Capabilities
5
6
### AST_TOKEN_TYPES Enum
7
8
Complete enumeration of all token types recognized by the TypeScript parser.
9
10
```typescript { .api }
11
enum AST_TOKEN_TYPES {
12
// Literal tokens
13
Boolean = 'Boolean',
14
Null = 'Null',
15
Numeric = 'Numeric',
16
String = 'String',
17
RegularExpression = 'RegularExpression',
18
Template = 'Template',
19
20
// Identifier tokens
21
Identifier = 'Identifier',
22
JSXIdentifier = 'JSXIdentifier',
23
PrivateIdentifier = 'PrivateIdentifier',
24
25
// Special tokens
26
JSXText = 'JSXText',
27
Keyword = 'Keyword',
28
Punctuator = 'Punctuator',
29
30
// Comment tokens
31
Block = 'Block',
32
Line = 'Line',
33
}
34
```
35
36
**Usage Examples:**
37
38
```typescript
39
import { AST_TOKEN_TYPES } from "@typescript-eslint/types";
40
41
// Token classification
42
function classifyToken(token: Token): string {
43
switch (token.type) {
44
case AST_TOKEN_TYPES.Identifier:
45
return `Identifier: ${token.value}`;
46
47
case AST_TOKEN_TYPES.Keyword:
48
return `Keyword: ${token.value}`;
49
50
case AST_TOKEN_TYPES.String:
51
return `String literal: ${token.value}`;
52
53
case AST_TOKEN_TYPES.Numeric:
54
return `Number: ${token.value}`;
55
56
case AST_TOKEN_TYPES.Boolean:
57
return `Boolean: ${token.value}`;
58
59
case AST_TOKEN_TYPES.Block:
60
case AST_TOKEN_TYPES.Line:
61
return `Comment: ${token.value}`;
62
63
default:
64
return `Other token: ${token.type}`;
65
}
66
}
67
68
// Filter tokens by category
69
function isLiteralToken(tokenType: AST_TOKEN_TYPES): boolean {
70
return [
71
AST_TOKEN_TYPES.Boolean,
72
AST_TOKEN_TYPES.Null,
73
AST_TOKEN_TYPES.Numeric,
74
AST_TOKEN_TYPES.String,
75
AST_TOKEN_TYPES.RegularExpression,
76
AST_TOKEN_TYPES.Template,
77
].includes(tokenType);
78
}
79
80
function isCommentToken(tokenType: AST_TOKEN_TYPES): boolean {
81
return tokenType === AST_TOKEN_TYPES.Block || tokenType === AST_TOKEN_TYPES.Line;
82
}
83
```
84
85
## Token Categories
86
87
### Literal Tokens
88
89
Tokens representing literal values in the source code:
90
91
- **Boolean**: `true`, `false`
92
- **Null**: `null`
93
- **Numeric**: Integer and floating-point numbers (`42`, `3.14`, `0x1F`)
94
- **String**: String literals (`"hello"`, `'world'`, `` `template` ``)
95
- **RegularExpression**: Regular expression literals (`/pattern/flags`)
96
- **Template**: Template literal parts (`` `hello ${name}` ``)
97
98
### Identifier Tokens
99
100
Tokens representing names and identifiers:
101
102
- **Identifier**: Standard JavaScript identifiers (`variable`, `functionName`)
103
- **JSXIdentifier**: JSX element and attribute names (`<div>`, `className`)
104
- **PrivateIdentifier**: Private class field names (`#privateField`)
105
106
### Special Tokens
107
108
- **JSXText**: Text content within JSX elements
109
- **Keyword**: Reserved JavaScript/TypeScript keywords (`function`, `class`, `interface`)
110
- **Punctuator**: Operators and punctuation (`;`, `{`, `}`, `+`, `=>`)
111
112
### Comment Tokens
113
114
Tokens representing comments in the source code:
115
116
- **Block**: Multi-line comments (`/* comment */`)
117
- **Line**: Single-line comments (`// comment`)
118
119
**Usage in Syntax Highlighting:**
120
121
```typescript
122
import { AST_TOKEN_TYPES } from "@typescript-eslint/types";
123
124
function getTokenStyle(tokenType: AST_TOKEN_TYPES): string {
125
switch (tokenType) {
126
case AST_TOKEN_TYPES.Keyword:
127
return 'keyword';
128
129
case AST_TOKEN_TYPES.String:
130
case AST_TOKEN_TYPES.Template:
131
return 'string';
132
133
case AST_TOKEN_TYPES.Numeric:
134
case AST_TOKEN_TYPES.Boolean:
135
case AST_TOKEN_TYPES.Null:
136
return 'literal';
137
138
case AST_TOKEN_TYPES.Block:
139
case AST_TOKEN_TYPES.Line:
140
return 'comment';
141
142
case AST_TOKEN_TYPES.Identifier:
143
return 'identifier';
144
145
case AST_TOKEN_TYPES.RegularExpression:
146
return 'regex';
147
148
default:
149
return 'default';
150
}
151
}
152
```