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    /// ECMAScript 3 (1999).
34    ES3,
35    /// ECMAScript 5 (2009).
36    ES5,
37    /// ECMAScript 2015 (ES6).
38    ES2015,
39    /// ECMAScript 2016.
40    ES2016,
41    /// ECMAScript 2017.
42    ES2017,
43    /// ECMAScript 2018.
44    ES2018,
45    /// ECMAScript 2019.
46    ES2019,
47    /// ECMAScript 2020.
48    ES2020,
49    /// ECMAScript 2021.
50    ES2021,
51    /// ECMAScript 2022.
52    ES2022,
53    /// Latest ECMAScript version.
54    ESNext,
55}
56
57impl TypeScriptLanguage {
58    /// Creates a new TypeScript language configuration.
59    pub fn new() -> Self {
60        Self::default()
61    }
62
63    /// Creates a standard TypeScript configuration.
64    pub fn standard() -> Self {
65        Self { jsx: true, decorators: true, strict: false, target: EcmaVersion::ES2020, experimental: true }
66    }
67
68    /// Creates a TypeScript configuration with JSX support.
69    pub fn with_jsx() -> Self {
70        Self { jsx: true, decorators: false, strict: false, target: EcmaVersion::ES2020, experimental: false }
71    }
72
73    /// Creates a TypeScript configuration with decorator support.
74    pub fn with_decorators() -> Self {
75        Self { jsx: false, decorators: true, strict: false, target: EcmaVersion::ES2020, experimental: false }
76    }
77
78    /// Creates a strict mode TypeScript configuration.
79    pub fn strict() -> Self {
80        Self { jsx: false, decorators: false, strict: true, target: EcmaVersion::ES2020, experimental: false }
81    }
82
83    /// Creates a TypeScript configuration with experimental syntax.
84    pub fn experimental() -> Self {
85        Self { jsx: true, decorators: true, strict: true, target: EcmaVersion::ESNext, experimental: true }
86    }
87}
88
89impl Language for TypeScriptLanguage {
90    const NAME: &'static str = "typescript";
91    const CATEGORY: LanguageCategory = LanguageCategory::Programming;
92
93    type TokenType = crate::lexer::token_type::TypeScriptTokenType;
94    type ElementType = crate::parser::element_type::TypeScriptElementType;
95    type TypedRoot = TypeScriptRoot;
96}