Skip to main content

oak_typescript/language/
mod.rs

1use crate::ast::TypeScriptRoot;
2use oak_core::{Language, LanguageCategory};
3use serde::{Deserialize, Serialize};
4
5/// TypeScript 语言配置
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
7pub struct TypeScriptLanguage {
8    /// 是否支持 JSX 语法
9    pub jsx: bool,
10    /// 是否支持装饰器
11    pub decorators: bool,
12    /// 是否启用严格模式
13    pub strict: bool,
14    /// 目标 ECMAScript 版本
15    pub target: EcmaVersion,
16    /// 是否允许实验性语法
17    pub experimental: bool,
18}
19
20impl Default for TypeScriptLanguage {
21    fn default() -> Self {
22        Self::standard()
23    }
24}
25
26/// ECMAScript 版本
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
28pub enum EcmaVersion {
29    ES3,
30    ES5,
31    ES2015,
32    ES2016,
33    ES2017,
34    ES2018,
35    ES2019,
36    ES2020,
37    ES2021,
38    ES2022,
39    ESNext,
40}
41
42impl TypeScriptLanguage {
43    /// 创建新的 TypeScript 语言配置
44    pub fn new() -> Self {
45        Self::default()
46    }
47
48    /// 创建标准 TypeScript 配置
49    pub fn standard() -> Self {
50        Self { jsx: false, decorators: false, strict: false, target: EcmaVersion::ES2020, experimental: false }
51    }
52
53    /// 创建支持 JSX TypeScript 配置
54    pub fn with_jsx() -> Self {
55        Self { jsx: true, decorators: false, strict: false, target: EcmaVersion::ES2020, experimental: false }
56    }
57
58    /// 创建支持装饰器的 TypeScript 配置
59    pub fn with_decorators() -> Self {
60        Self { jsx: false, decorators: true, strict: false, target: EcmaVersion::ES2020, experimental: false }
61    }
62
63    /// 创建严格模式TypeScript 配置
64    pub fn strict() -> Self {
65        Self { jsx: false, decorators: false, strict: true, target: EcmaVersion::ES2020, experimental: false }
66    }
67
68    /// 创建实验性语法的 TypeScript 配置
69    pub fn experimental() -> Self {
70        Self { jsx: true, decorators: true, strict: true, target: EcmaVersion::ESNext, experimental: true }
71    }
72}
73
74impl Language for TypeScriptLanguage {
75    const NAME: &'static str = "typescript";
76    const CATEGORY: LanguageCategory = LanguageCategory::Programming;
77
78    type TokenType = crate::kind::TypeScriptSyntaxKind;
79    type ElementType = crate::kind::TypeScriptSyntaxKind;
80    type TypedRoot = TypeScriptRoot;
81}