Skip to content

Commit

Permalink
feat(ast,parser): add intrinsic keyword (#3767)
Browse files Browse the repository at this point in the history
closes #3759
  • Loading branch information
Boshen committed Jun 19, 2024
1 parent 1190dee commit 5847e16
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 70 deletions.
67 changes: 35 additions & 32 deletions crates/oxc_ast/src/ast/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,73 +553,75 @@ macro_rules! inherit_variants {
/// Inherited from [`TSType`]
TSBooleanKeyword(Box<'a, TSBooleanKeyword>) = 2,
/// Inherited from [`TSType`]
TSNeverKeyword(Box<'a, TSNeverKeyword>) = 3,
TSIntrinsicKeyword(Box<'a, TSIntrinsicKeyword>) = 3,
/// Inherited from [`TSType`]
TSNullKeyword(Box<'a, TSNullKeyword>) = 4,
TSNeverKeyword(Box<'a, TSNeverKeyword>) = 4,
/// Inherited from [`TSType`]
TSNumberKeyword(Box<'a, TSNumberKeyword>) = 5,
TSNullKeyword(Box<'a, TSNullKeyword>) = 5,
/// Inherited from [`TSType`]
TSObjectKeyword(Box<'a, TSObjectKeyword>) = 6,
TSNumberKeyword(Box<'a, TSNumberKeyword>) = 6,
/// Inherited from [`TSType`]
TSStringKeyword(Box<'a, TSStringKeyword>) = 7,
TSObjectKeyword(Box<'a, TSObjectKeyword>) = 7,
/// Inherited from [`TSType`]
TSSymbolKeyword(Box<'a, TSSymbolKeyword>) = 8,
TSStringKeyword(Box<'a, TSStringKeyword>) = 8,
/// Inherited from [`TSType`]
TSThisType(Box<'a, TSThisType>) = 9,
TSSymbolKeyword(Box<'a, TSSymbolKeyword>) = 9,
/// Inherited from [`TSType`]
TSUndefinedKeyword(Box<'a, TSUndefinedKeyword>) = 10,
TSThisType(Box<'a, TSThisType>) = 10,
/// Inherited from [`TSType`]
TSUnknownKeyword(Box<'a, TSUnknownKeyword>) = 11,
TSUndefinedKeyword(Box<'a, TSUndefinedKeyword>) = 11,
/// Inherited from [`TSType`]
TSVoidKeyword(Box<'a, TSVoidKeyword>) = 12,
TSUnknownKeyword(Box<'a, TSUnknownKeyword>) = 12,
/// Inherited from [`TSType`]
TSVoidKeyword(Box<'a, TSVoidKeyword>) = 13,

// Compound
/// Inherited from [`TSType`]
TSArrayType(Box<'a, TSArrayType<'a>>) = 13,
TSArrayType(Box<'a, TSArrayType<'a>>) = 14,
/// Inherited from [`TSType`]
TSConditionalType(Box<'a, TSConditionalType<'a>>) = 14,
TSConditionalType(Box<'a, TSConditionalType<'a>>) = 15,
/// Inherited from [`TSType`]
TSConstructorType(Box<'a, TSConstructorType<'a>>) = 15,
TSConstructorType(Box<'a, TSConstructorType<'a>>) = 16,
/// Inherited from [`TSType`]
TSFunctionType(Box<'a, TSFunctionType<'a>>) = 16,
TSFunctionType(Box<'a, TSFunctionType<'a>>) = 17,
/// Inherited from [`TSType`]
TSImportType(Box<'a, TSImportType<'a>>) = 17,
TSImportType(Box<'a, TSImportType<'a>>) = 18,
/// Inherited from [`TSType`]
TSIndexedAccessType(Box<'a, TSIndexedAccessType<'a>>) = 18,
TSIndexedAccessType(Box<'a, TSIndexedAccessType<'a>>) = 19,
/// Inherited from [`TSType`]
TSInferType(Box<'a, TSInferType<'a>>) = 19,
TSInferType(Box<'a, TSInferType<'a>>) = 20,
/// Inherited from [`TSType`]
TSIntersectionType(Box<'a, TSIntersectionType<'a>>) = 20,
TSIntersectionType(Box<'a, TSIntersectionType<'a>>) = 21,
/// Inherited from [`TSType`]
TSLiteralType(Box<'a, TSLiteralType<'a>>) = 21,
TSLiteralType(Box<'a, TSLiteralType<'a>>) = 22,
/// Inherited from [`TSType`]
TSMappedType(Box<'a, TSMappedType<'a>>) = 22,
TSMappedType(Box<'a, TSMappedType<'a>>) = 23,
/// Inherited from [`TSType`]
TSNamedTupleMember(Box<'a, TSNamedTupleMember<'a>>) = 23,
TSNamedTupleMember(Box<'a, TSNamedTupleMember<'a>>) = 24,
/// Inherited from [`TSType`]
TSQualifiedName(Box<'a, TSQualifiedName<'a>>) = 24,
TSQualifiedName(Box<'a, TSQualifiedName<'a>>) = 25,
/// Inherited from [`TSType`]
TSTemplateLiteralType(Box<'a, TSTemplateLiteralType<'a>>) = 25,
TSTemplateLiteralType(Box<'a, TSTemplateLiteralType<'a>>) = 26,
/// Inherited from [`TSType`]
TSTupleType(Box<'a, TSTupleType<'a>>) = 26,
TSTupleType(Box<'a, TSTupleType<'a>>) = 27,
/// Inherited from [`TSType`]
TSTypeLiteral(Box<'a, TSTypeLiteral<'a>>) = 27,
TSTypeLiteral(Box<'a, TSTypeLiteral<'a>>) = 28,
/// Inherited from [`TSType`]
TSTypeOperatorType(Box<'a, TSTypeOperator<'a>>) = 28,
TSTypeOperatorType(Box<'a, TSTypeOperator<'a>>) = 29,
/// Inherited from [`TSType`]
TSTypePredicate(Box<'a, TSTypePredicate<'a>>) = 29,
TSTypePredicate(Box<'a, TSTypePredicate<'a>>) = 30,
/// Inherited from [`TSType`]
TSTypeQuery(Box<'a, TSTypeQuery<'a>>) = 30,
TSTypeQuery(Box<'a, TSTypeQuery<'a>>) = 31,
/// Inherited from [`TSType`]
TSTypeReference(Box<'a, TSTypeReference<'a>>) = 31,
TSTypeReference(Box<'a, TSTypeReference<'a>>) = 32,
/// Inherited from [`TSType`]
TSUnionType(Box<'a, TSUnionType<'a>>) = 32,
TSUnionType(Box<'a, TSUnionType<'a>>) = 33,

// JSDoc
/// Inherited from [`TSType`]
JSDocNullableType(Box<'a, JSDocNullableType<'a>>) = 33,
JSDocNullableType(Box<'a, JSDocNullableType<'a>>) = 34,
/// Inherited from [`TSType`]
JSDocUnknownType(Box<'a, JSDocUnknownType>) = 34,
JSDocUnknownType(Box<'a, JSDocUnknownType>) = 35,

$($rest)*
}
Expand All @@ -637,6 +639,7 @@ macro_rules! inherit_variants {
TSAnyKeyword,
TSBigIntKeyword,
TSBooleanKeyword,
TSIntrinsicKeyword,
TSNeverKeyword,
TSNullKeyword,
TSNumberKeyword,
Expand Down
76 changes: 44 additions & 32 deletions crates/oxc_ast/src/ast/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,40 +157,41 @@ pub enum TSType<'a> {
TSAnyKeyword(Box<'a, TSAnyKeyword>) = 0,
TSBigIntKeyword(Box<'a, TSBigIntKeyword>) = 1,
TSBooleanKeyword(Box<'a, TSBooleanKeyword>) = 2,
TSNeverKeyword(Box<'a, TSNeverKeyword>) = 3,
TSNullKeyword(Box<'a, TSNullKeyword>) = 4,
TSNumberKeyword(Box<'a, TSNumberKeyword>) = 5,
TSObjectKeyword(Box<'a, TSObjectKeyword>) = 6,
TSStringKeyword(Box<'a, TSStringKeyword>) = 7,
TSSymbolKeyword(Box<'a, TSSymbolKeyword>) = 8,
TSThisType(Box<'a, TSThisType>) = 9,
TSUndefinedKeyword(Box<'a, TSUndefinedKeyword>) = 10,
TSUnknownKeyword(Box<'a, TSUnknownKeyword>) = 11,
TSVoidKeyword(Box<'a, TSVoidKeyword>) = 12,
TSIntrinsicKeyword(Box<'a, TSIntrinsicKeyword>) = 3,
TSNeverKeyword(Box<'a, TSNeverKeyword>) = 4,
TSNullKeyword(Box<'a, TSNullKeyword>) = 5,
TSNumberKeyword(Box<'a, TSNumberKeyword>) = 6,
TSObjectKeyword(Box<'a, TSObjectKeyword>) = 7,
TSStringKeyword(Box<'a, TSStringKeyword>) = 8,
TSSymbolKeyword(Box<'a, TSSymbolKeyword>) = 9,
TSThisType(Box<'a, TSThisType>) = 10,
TSUndefinedKeyword(Box<'a, TSUndefinedKeyword>) = 11,
TSUnknownKeyword(Box<'a, TSUnknownKeyword>) = 12,
TSVoidKeyword(Box<'a, TSVoidKeyword>) = 13,
// Compound
TSArrayType(Box<'a, TSArrayType<'a>>) = 13,
TSConditionalType(Box<'a, TSConditionalType<'a>>) = 14,
TSConstructorType(Box<'a, TSConstructorType<'a>>) = 15,
TSFunctionType(Box<'a, TSFunctionType<'a>>) = 16,
TSImportType(Box<'a, TSImportType<'a>>) = 17,
TSIndexedAccessType(Box<'a, TSIndexedAccessType<'a>>) = 18,
TSInferType(Box<'a, TSInferType<'a>>) = 19,
TSIntersectionType(Box<'a, TSIntersectionType<'a>>) = 20,
TSLiteralType(Box<'a, TSLiteralType<'a>>) = 21,
TSMappedType(Box<'a, TSMappedType<'a>>) = 22,
TSNamedTupleMember(Box<'a, TSNamedTupleMember<'a>>) = 23,
TSQualifiedName(Box<'a, TSQualifiedName<'a>>) = 24,
TSTemplateLiteralType(Box<'a, TSTemplateLiteralType<'a>>) = 25,
TSTupleType(Box<'a, TSTupleType<'a>>) = 26,
TSTypeLiteral(Box<'a, TSTypeLiteral<'a>>) = 27,
TSTypeOperatorType(Box<'a, TSTypeOperator<'a>>) = 28,
TSTypePredicate(Box<'a, TSTypePredicate<'a>>) = 29,
TSTypeQuery(Box<'a, TSTypeQuery<'a>>) = 30,
TSTypeReference(Box<'a, TSTypeReference<'a>>) = 31,
TSUnionType(Box<'a, TSUnionType<'a>>) = 32,
TSArrayType(Box<'a, TSArrayType<'a>>) = 14,
TSConditionalType(Box<'a, TSConditionalType<'a>>) = 15,
TSConstructorType(Box<'a, TSConstructorType<'a>>) = 16,
TSFunctionType(Box<'a, TSFunctionType<'a>>) = 17,
TSImportType(Box<'a, TSImportType<'a>>) = 18,
TSIndexedAccessType(Box<'a, TSIndexedAccessType<'a>>) = 19,
TSInferType(Box<'a, TSInferType<'a>>) = 20,
TSIntersectionType(Box<'a, TSIntersectionType<'a>>) = 21,
TSLiteralType(Box<'a, TSLiteralType<'a>>) = 22,
TSMappedType(Box<'a, TSMappedType<'a>>) = 23,
TSNamedTupleMember(Box<'a, TSNamedTupleMember<'a>>) = 24,
TSQualifiedName(Box<'a, TSQualifiedName<'a>>) = 25,
TSTemplateLiteralType(Box<'a, TSTemplateLiteralType<'a>>) = 26,
TSTupleType(Box<'a, TSTupleType<'a>>) = 27,
TSTypeLiteral(Box<'a, TSTypeLiteral<'a>>) = 28,
TSTypeOperatorType(Box<'a, TSTypeOperator<'a>>) = 29,
TSTypePredicate(Box<'a, TSTypePredicate<'a>>) = 30,
TSTypeQuery(Box<'a, TSTypeQuery<'a>>) = 31,
TSTypeReference(Box<'a, TSTypeReference<'a>>) = 32,
TSUnionType(Box<'a, TSUnionType<'a>>) = 33,
// JSDoc
JSDocNullableType(Box<'a, JSDocNullableType<'a>>) = 33,
JSDocUnknownType(Box<'a, JSDocUnknownType>) = 34,
JSDocNullableType(Box<'a, JSDocNullableType<'a>>) = 34,
JSDocUnknownType(Box<'a, JSDocUnknownType>) = 35,
}

/// Macro for matching `TSType`'s variants.
Expand All @@ -200,6 +201,7 @@ macro_rules! match_ts_type {
$ty::TSAnyKeyword(_)
| $ty::TSBigIntKeyword(_)
| $ty::TSBooleanKeyword(_)
| $ty::TSIntrinsicKeyword(_)
| $ty::TSNeverKeyword(_)
| $ty::TSNullKeyword(_)
| $ty::TSNumberKeyword(_)
Expand Down Expand Up @@ -491,6 +493,16 @@ pub struct TSNeverKeyword {
pub span: Span,
}

/// `type Uppercase<T extends character> = intrinsic;`
#[visited_node]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
#[cfg_attr(feature = "serialize", serde(tag = "type"))]
pub struct TSIntrinsicKeyword {
#[cfg_attr(feature = "serialize", serde(flatten))]
pub span: Span,
}

#[visited_node]
#[derive(Debug, Hash)]
#[cfg_attr(feature = "serialize", derive(Serialize, Tsify))]
Expand Down
5 changes: 5 additions & 0 deletions crates/oxc_ast/src/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1979,6 +1979,11 @@ impl<'a> AstBuilder<'a> {
TSType::TSNeverKeyword(self.alloc(TSNeverKeyword { span }))
}

#[inline]
pub fn ts_intrinsic_keyword(self, span: Span) -> TSType<'a> {
TSType::TSIntrinsicKeyword(self.alloc(TSIntrinsicKeyword { span }))
}

#[inline]
pub fn ts_template_literal_type(
self,
Expand Down
15 changes: 9 additions & 6 deletions crates/oxc_ast/src/ast_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ ast_kinds! {
TSAnyKeyword(&'a TSAnyKeyword),
TSBigIntKeyword(&'a TSBigIntKeyword),
TSBooleanKeyword(&'a TSBooleanKeyword),
TSIntrinsicKeyword(&'a TSIntrinsicKeyword),
TSNeverKeyword(&'a TSNeverKeyword),
TSNullKeyword(&'a TSNullKeyword),
TSNumberKeyword(&'a TSNumberKeyword),
Expand Down Expand Up @@ -251,12 +252,12 @@ impl<'a> AstKind<'a> {

#[rustfmt::skip]
pub fn is_type(self) -> bool {
matches!(self, Self::TSAnyKeyword(_) | Self::TSBigIntKeyword(_) | Self::TSBooleanKeyword(_) | Self::TSNeverKeyword(_)
| Self::TSNullKeyword(_) | Self::TSNumberKeyword(_) | Self::TSObjectKeyword(_) | Self::TSStringKeyword(_)
| Self::TSSymbolKeyword(_) | Self::TSUndefinedKeyword(_) | Self::TSUnknownKeyword(_) | Self::TSVoidKeyword(_)
| Self::TSIndexedAccessType(_) | Self::TSInferType(_) | Self::TSIntersectionType(_) | Self::TSLiteralType(_)
| Self::TSMethodSignature(_) | Self::TSTemplateLiteralType(_) | Self::TSThisType(_) | Self::TSTypeLiteral(_)
| Self::TSTypeReference(_) | Self::TSUnionType(_))
matches!(self, Self::TSAnyKeyword(_) | Self::TSBigIntKeyword(_) | Self::TSBooleanKeyword(_) | Self::TSIntrinsicKeyword(_)
| Self::TSNeverKeyword(_) | Self::TSNullKeyword(_) | Self::TSNumberKeyword(_) | Self::TSObjectKeyword(_)
| Self::TSStringKeyword(_) | Self::TSSymbolKeyword(_) | Self::TSUndefinedKeyword(_) | Self::TSUnknownKeyword(_)
| Self::TSVoidKeyword(_) | Self::TSIndexedAccessType(_) | Self::TSInferType(_) | Self::TSIntersectionType(_)
| Self::TSLiteralType(_) | Self::TSMethodSignature(_) | Self::TSTemplateLiteralType(_) | Self::TSThisType(_)
| Self::TSTypeLiteral(_) | Self::TSTypeReference(_) | Self::TSUnionType(_))
}

pub fn is_literal(self) -> bool {
Expand Down Expand Up @@ -497,6 +498,7 @@ impl<'a> GetSpan for AstKind<'a> {
Self::TSVoidKeyword(x) => x.span,
Self::TSBigIntKeyword(x) => x.span,
Self::TSBooleanKeyword(x) => x.span,
Self::TSIntrinsicKeyword(x) => x.span,
Self::TSNeverKeyword(x) => x.span,
Self::TSNumberKeyword(x) => x.span,
Self::TSObjectKeyword(x) => x.span,
Expand Down Expand Up @@ -698,6 +700,7 @@ impl<'a> AstKind<'a> {
Self::TSVoidKeyword(_) => "TSVoidKeyword".into(),
Self::TSBigIntKeyword(_) => "TSBigIntKeyword".into(),
Self::TSBooleanKeyword(_) => "TSBooleanKeyword".into(),
Self::TSIntrinsicKeyword(_) => "TSIntrinsicKeyword".into(),
Self::TSNeverKeyword(_) => "TSNeverKeyword".into(),
Self::TSNumberKeyword(_) => "TSNumberKeyword".into(),
Self::TSObjectKeyword(_) => "TSObjectKeyword".into(),
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_ast/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ impl<'a> GetSpan for TSType<'a> {
Self::TSStringKeyword(t) => t.span,
Self::TSNeverKeyword(t) => t.span,
Self::TSBooleanKeyword(t) => t.span,
Self::TSIntrinsicKeyword(t) => t.span,
Self::TSSymbolKeyword(t) => t.span,
Self::TSBigIntKeyword(t) => t.span,
Self::TSThisType(t) => t.span,
Expand Down
11 changes: 11 additions & 0 deletions crates/oxc_ast/src/visit/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,10 @@ pub trait Visit<'a>: Sized {
walk_ts_boolean_keyword(self, ty);
}

fn visit_ts_intrinsic_keyword(&mut self, ty: &TSIntrinsicKeyword) {
walk_ts_intrinsic_keyword(self, ty);
}

fn visit_ts_never_keyword(&mut self, ty: &TSNeverKeyword) {
walk_ts_never_keyword(self, ty);
}
Expand Down Expand Up @@ -2690,6 +2694,7 @@ pub mod walk {
TSType::TSAnyKeyword(ty) => visitor.visit_ts_any_keyword(ty),
TSType::TSBigIntKeyword(ty) => visitor.visit_ts_big_int_keyword(ty),
TSType::TSBooleanKeyword(ty) => visitor.visit_ts_boolean_keyword(ty),
TSType::TSIntrinsicKeyword(ty) => visitor.visit_ts_intrinsic_keyword(ty),
TSType::TSNeverKeyword(ty) => visitor.visit_ts_never_keyword(ty),
TSType::TSNullKeyword(ty) => visitor.visit_ts_null_keyword(ty),
TSType::TSNumberKeyword(ty) => visitor.visit_ts_number_keyword(ty),
Expand Down Expand Up @@ -3069,6 +3074,12 @@ pub mod walk {
visitor.leave_node(kind);
}

pub fn walk_ts_intrinsic_keyword<'a, V: Visit<'a>>(visitor: &mut V, ty: &TSIntrinsicKeyword) {
let kind = AstKind::TSIntrinsicKeyword(visitor.alloc(ty));
visitor.enter_node(kind);
visitor.leave_node(kind);
}

pub fn walk_ts_never_keyword<'a, V: Visit<'a>>(visitor: &mut V, ty: &TSNeverKeyword) {
let kind = AstKind::TSNeverKeyword(visitor.alloc(ty));
visitor.enter_node(kind);
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2662,6 +2662,7 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSType<'a> {
Self::TSAnyKeyword(_) => p.print_str(b"any"),
Self::TSBigIntKeyword(_) => p.print_str(b"bigint"),
Self::TSBooleanKeyword(_) => p.print_str(b"boolean"),
Self::TSIntrinsicKeyword(_) => p.print_str(b"intrinsic"),
Self::TSNeverKeyword(_) => p.print_str(b"never"),
Self::TSNullKeyword(_) => p.print_str(b"null"),
Self::TSNumberKeyword(_) => p.print_str(b"number"),
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_parser/src/ts/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ impl<'a> ParserImpl<'a> {
self.bump_any();
Some(self.ast.ts_never_keyword(self.end_span(span)))
}
Kind::Intrinsic => {
self.bump_any();
Some(self.ast.ts_intrinsic_keyword(self.end_span(span)))
}
_ => None,
}
}
Expand Down
7 changes: 7 additions & 0 deletions crates/oxc_prettier/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ impl<'a> Format<'a> for TSType<'a> {
TSType::TSAnyKeyword(v) => v.format(p),
TSType::TSBigIntKeyword(v) => v.format(p),
TSType::TSBooleanKeyword(v) => v.format(p),
TSType::TSIntrinsicKeyword(v) => v.format(p),
TSType::TSNeverKeyword(v) => v.format(p),
TSType::TSNullKeyword(v) => v.format(p),
TSType::TSNumberKeyword(v) => v.format(p),
Expand Down Expand Up @@ -709,6 +710,12 @@ impl<'a> Format<'a> for TSBooleanKeyword {
}
}

impl<'a> Format<'a> for TSIntrinsicKeyword {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
Doc::Str("intrinsic")
}
}

impl<'a> Format<'a> for TSNeverKeyword {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
Doc::Str("never")
Expand Down
15 changes: 15 additions & 0 deletions crates/oxc_traverse/src/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,21 @@ pub trait Traverse<'a> {
#[inline]
fn exit_ts_never_keyword(&mut self, node: &mut TSNeverKeyword, ctx: &mut TraverseCtx<'a>) {}

#[inline]
fn enter_ts_intrinsic_keyword(
&mut self,
node: &mut TSIntrinsicKeyword,
ctx: &mut TraverseCtx<'a>,
) {
}
#[inline]
fn exit_ts_intrinsic_keyword(
&mut self,
node: &mut TSIntrinsicKeyword,
ctx: &mut TraverseCtx<'a>,
) {
}

#[inline]
fn enter_ts_unknown_keyword(&mut self, node: &mut TSUnknownKeyword, ctx: &mut TraverseCtx<'a>) {
}
Expand Down
Loading

0 comments on commit 5847e16

Please sign in to comment.