Skip to main content

oak_typescript/language/
mod.rs

1#![doc = include_str!("readme.md")]
2use crate::ast::TypeScriptRoot;
3use oak_core::{Language, LanguageCategory};
4#[cfg(feature = "serde")]
5use serde::{Deserialize, Serialize};
6
7/// TypeScript language configuration.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10pub struct TypeScriptLanguage {
11    /// Whether to support JSX syntax.
12    pub jsx: bool,
13    /// Whether to support decorators.
14    pub decorators: bool,
15    /// Whether to enable strict mode.
16    pub strict: bool,
17    /// Target ECMAScript version.
18    pub target: EcmaVersion,
19    /// Whether to allow experimental syntax.
20    pub experimental: bool,
21}
22
23impl Default for TypeScriptLanguage {
24    fn default() -> Self {
25        Self::standard()
26    }
27}
28
29/// ECMAScript version.
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
31#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
32pub enum EcmaVersion {
33    ES3,
34    ES5,
35    ES2015,
36    ES2016,
37    ES2017,
38    ES2018,
39    ES2019,
40    ES2020,
41    ES2021,
42    ES2022,
43    ESNext,
44}
45
46impl TypeScriptLanguage {
47    /// Creates a new TypeScript language configuration.
48    pub fn new() -> Self {
49        Self::default()
50    }
51
52    /// Creates a standard TypeScript configuration.
53    pub fn standard() -> Self {
54        Self { jsx: true, decorators: true, strict: false, target: EcmaVersion::ES2020, experimental: true }
55    }
56
57    /// Creates a TypeScript configuration with JSX support.
58    pub fn with_jsx() -> Self {
59        Self { jsx: true, decorators: false, strict: false, target: EcmaVersion::ES2020, experimental: false }
60    }
61
62    /// Creates a TypeScript configuration with decorator support.
63    pub fn with_decorators() -> Self {
64        Self { jsx: false, decorators: true, strict: false, target: EcmaVersion::ES2020, experimental: false }
65    }
66
67    /// Creates a strict mode TypeScript configuration.
68    pub fn strict() -> Self {
69        Self { jsx: false, decorators: false, strict: true, target: EcmaVersion::ES2020, experimental: false }
70    }
71
72    /// Creates a TypeScript configuration with experimental syntax.
73    pub fn experimental() -> Self {
74        Self { jsx: true, decorators: true, strict: true, target: EcmaVersion::ESNext, experimental: true }
75    }
76}
77
78impl Language for TypeScriptLanguage {
79    const NAME: &'static str = "typescript";
80    const CATEGORY: LanguageCategory = LanguageCategory::Programming;
81
82    type TokenType = crate::lexer::token_type::TypeScriptTokenType;
83    type ElementType = crate::parser::element_type::TypeScriptElementType;
84    type TypedRoot = TypeScriptRoot;
85}