1use oak_core::{ElementType, UniversalElementRole};
2#[cfg(feature = "serde")]
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7#[repr(u16)]
8pub enum TypeScriptElementType {
9 NamedImports,
10 Decorator,
11 ArrowFunction,
12 PredefinedType,
13 Abstract,
14 Any,
15 As,
16 Asserts,
17 Async,
18 Await,
19 Boolean,
20 Break,
21 Case,
22 Catch,
23 Class,
24 Const,
25 Constructor,
26 Continue,
27 Debugger,
28 Declare,
29 Default,
30 Delete,
31 Do,
32 Else,
33 Enum,
34 Export,
35 Extends,
36 False,
37 Finally,
38 For,
39 From,
40 Function,
41 Get,
42 Global,
43 If,
44 Implements,
45 Import,
46 In,
47 Infer,
48 Instanceof,
49 Interface,
50 Is,
51 Keyof,
52 Let,
53 Namespace,
54 Never,
55 New,
56 Null,
57 Number,
58 Object,
59 Of,
60 Override,
61 Package,
62 Private,
63 Protected,
64 Public,
65 Readonly,
66 Require,
67 Return,
68 Set,
69 Static,
70 String,
71 Super,
72 Switch,
73 Symbol,
74 This,
75 Throw,
76 True,
77 Try,
78 Type,
79 Typeof,
80 Undefined,
81 Unique,
82 Unknown,
83 Var,
84 Void,
85 While,
86 With,
87 Yield,
88 Plus,
89 Minus,
90 Star,
91 Slash,
92 Percent,
93 StarStar,
94 Question,
95 DotDotDot,
96 Less,
97 Greater,
98 LessEqual,
99 GreaterEqual,
100 EqualEqual,
101 NotEqual,
102 EqualEqualEqual,
103 NotEqualEqual,
104 AmpersandAmpersand,
105 PipePipe,
106 Exclamation,
107 Ampersand,
108 Pipe,
109 Caret,
110 Tilde,
111 LeftShift,
112 RightShift,
113 UnsignedRightShift,
114 Equal,
115 PlusEqual,
116 MinusEqual,
117 StarEqual,
118 SlashEqual,
119 PercentEqual,
120 StarStarEqual,
121 LeftShiftEqual,
122 RightShiftEqual,
123 UnsignedRightShiftEqual,
124 AmpersandEqual,
125 PipeEqual,
126 CaretEqual,
127 AmpersandAmpersandEqual,
128 PipePipeEqual,
129 QuestionQuestionEqual,
130 PlusPlus,
131 MinusMinus,
132 QuestionQuestion,
133 QuestionDot,
134 Arrow,
135 LeftParen,
136 RightParen,
137 LeftBrace,
138 RightBrace,
139 LeftBracket,
140 RightBracket,
141 Semicolon,
142 Comma,
143 Dot,
144 Colon,
145 At,
146 StringLiteral,
147 NumericLiteral,
148 BigIntLiteral,
149 BooleanLiteral,
150 TemplateString,
151 RegexLiteral,
152 IdentifierName,
153 LineComment,
154 BlockComment,
155 Whitespace,
156 Newline,
157 Eof,
158 Root,
159 SourceFile,
160 Module,
161 VariableDeclaration,
162 FunctionDeclaration,
163 ClassDeclaration,
164 InterfaceDeclaration,
165 TypeAliasDeclaration,
166 EnumDeclaration,
167 NamespaceDeclaration,
168 ClassBody,
169 ImportDeclaration,
170 ExportDeclaration,
171 ImportClause,
172 ImportSpecifier,
173 NamespaceImport,
174 NamedExports,
175 ExportSpecifier,
176 Parameter,
177 CallArgument,
178 PropertyDeclaration,
179 MethodDeclaration,
180 ConstructorDeclaration,
181 PropertyAssignment,
182 ShorthandPropertyAssignment,
183 SpreadElement,
184 Error,
185 JsxElement,
186 JsxSelfClosingElement,
187 JsxOpeningElement,
188 JsxClosingElement,
189 JsxFragment,
190 JsxOpeningFragment,
191 JsxClosingFragment,
192 JsxAttribute,
193 JsxAttributes,
194 JsxExpressionContainer,
195 JsxSpreadAttribute,
196 JsxText,
197 BinaryExpression,
198 UnaryExpression,
199 ConditionalExpression,
200 CallExpression,
201 NewExpression,
202 MemberExpression,
203 ArrayExpression,
204 ObjectExpression,
205 FunctionExpression,
206 TemplateExpression,
207 TaggedTemplateExpression,
208 AsExpression,
209 TypeAssertionExpression,
210 NonNullExpression,
211 UpdateExpression,
212 ExpressionStatement,
213 BlockStatement,
214 IfStatement,
215 WhileStatement,
216 ForStatement,
217 ForInStatement,
218 ForOfStatement,
219 DoWhileStatement,
220 SwitchStatement,
221 CaseClause,
222 DefaultClause,
223 TryStatement,
224 CatchClause,
225 FinallyClause,
226 ThrowStatement,
227 ReturnStatement,
228 BreakStatement,
229 ContinueStatement,
230 DebuggerStatement,
231 WithStatement,
232 BindingPattern,
233 ArrayBindingPattern,
234 ObjectBindingPattern,
235 BindingElement,
236 TypeReference,
237 TypeLiteral,
238 FunctionType,
239 ConstructorType,
240 ArrayType,
241 TupleType,
242 UnionType,
243 IntersectionType,
244 ConditionalType,
245 MappedType,
246 IndexedAccessType,
247 PropertySignature,
248 MethodSignature,
249 LiteralType,
250 TypeQuery,
251 TypePredicate,
252 TypeAnnotation,
253 TypeParameter,
254 HeritageClause,
255 EnumMember,
256}
257
258impl TypeScriptElementType {
259 pub fn from_keyword(text: &str) -> Option<Self> {
260 match text {
261 "abstract" => Some(Self::Abstract),
262 "any" => Some(Self::Any),
263 "as" => Some(Self::As),
264 "asserts" => Some(Self::Asserts),
265 "async" => Some(Self::Async),
266 "await" => Some(Self::Await),
267 "boolean" => Some(Self::Boolean),
268 "break" => Some(Self::Break),
269 "case" => Some(Self::Case),
270 "catch" => Some(Self::Catch),
271 "class" => Some(Self::Class),
272 "const" => Some(Self::Const),
273 "constructor" => Some(Self::Constructor),
274 "continue" => Some(Self::Continue),
275 "debugger" => Some(Self::Debugger),
276 "declare" => Some(Self::Declare),
277 "default" => Some(Self::Default),
278 "delete" => Some(Self::Delete),
279 "do" => Some(Self::Do),
280 "else" => Some(Self::Else),
281 "enum" => Some(Self::Enum),
282 "export" => Some(Self::Export),
283 "extends" => Some(Self::Extends),
284 "false" => Some(Self::False),
285 "finally" => Some(Self::Finally),
286 "for" => Some(Self::For),
287 "from" => Some(Self::From),
288 "function" => Some(Self::Function),
289 "get" => Some(Self::Get),
290 "global" => Some(Self::Global),
291 "if" => Some(Self::If),
292 "implements" => Some(Self::Implements),
293 "import" => Some(Self::Import),
294 "in" => Some(Self::In),
295 "infer" => Some(Self::Infer),
296 "instanceof" => Some(Self::Instanceof),
297 "interface" => Some(Self::Interface),
298 "is" => Some(Self::Is),
299 "keyof" => Some(Self::Keyof),
300 "let" => Some(Self::Let),
301 "namespace" => Some(Self::Namespace),
302 "never" => Some(Self::Never),
303 "new" => Some(Self::New),
304 "null" => Some(Self::Null),
305 "number" => Some(Self::Number),
306 "object" => Some(Self::Object),
307 "of" => Some(Self::Of),
308 "override" => Some(Self::Override),
309 "package" => Some(Self::Package),
310 "private" => Some(Self::Private),
311 "protected" => Some(Self::Protected),
312 "public" => Some(Self::Public),
313 "readonly" => Some(Self::Readonly),
314 "require" => Some(Self::Require),
315 "return" => Some(Self::Return),
316 "set" => Some(Self::Set),
317 "static" => Some(Self::Static),
318 "string" => Some(Self::String),
319 "super" => Some(Self::Super),
320 "switch" => Some(Self::Switch),
321 "symbol" => Some(Self::Symbol),
322 "this" => Some(Self::This),
323 "throw" => Some(Self::Throw),
324 "true" => Some(Self::True),
325 "try" => Some(Self::Try),
326 "type" => Some(Self::Type),
327 "typeof" => Some(Self::Typeof),
328 "undefined" => Some(Self::Undefined),
329 "unique" => Some(Self::Unique),
330 "unknown" => Some(Self::Unknown),
331 "var" => Some(Self::Var),
332 "void" => Some(Self::Void),
333 "while" => Some(Self::While),
334 "with" => Some(Self::With),
335 "yield" => Some(Self::Yield),
336 _ => None,
337 }
338 }
339}
340
341impl ElementType for TypeScriptElementType {
342 type Role = UniversalElementRole;
343
344 fn role(&self) -> Self::Role {
345 match self {
346 Self::Root => UniversalElementRole::Root,
347 Self::SourceFile => UniversalElementRole::Root,
348 Self::Error => UniversalElementRole::Error,
349 _ => UniversalElementRole::None,
350 }
351 }
352}
353
354impl From<crate::lexer::token_type::TypeScriptTokenType> for TypeScriptElementType {
355 fn from(token: crate::lexer::token_type::TypeScriptTokenType) -> Self {
356 unsafe { std::mem::transmute(token) }
357 }
358}