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