docs(ast): document TryStatement and related nodes (#5970)

Part of #5870
This commit is contained in:
DonIsaac 2024-09-22 15:54:27 +00:00
parent 612f638bcd
commit 47c2faa7a9
2 changed files with 76 additions and 18 deletions

View file

@ -1511,6 +1511,12 @@ pub struct LabeledStatement<'a> {
} }
/// Throw Statement /// Throw Statement
///
/// # Example
/// ```ts
/// throw new Error('something went wrong!');
/// // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ argument
/// ```
#[ast(visit)] #[ast(visit)]
#[derive(Debug)] #[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash)] #[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash)]
@ -1519,10 +1525,25 @@ pub struct LabeledStatement<'a> {
pub struct ThrowStatement<'a> { pub struct ThrowStatement<'a> {
#[serde(flatten)] #[serde(flatten)]
pub span: Span, pub span: Span,
/// The expression being thrown, e.g. `err` in `throw err;`
pub argument: Expression<'a>, pub argument: Expression<'a>,
} }
/// Try Statement /// Try Statement
///
/// # Example
/// ```ts
/// var x;
/// let didRun = false;
///
/// try { // block
/// x = 1;
/// } catch (e) { // handler
/// console.error(e);
/// } finally { // finalizer
/// didRun = true;
/// }
/// ```
#[ast(visit)] #[ast(visit)]
#[derive(Debug)] #[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash)] #[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash)]
@ -1531,12 +1552,27 @@ pub struct ThrowStatement<'a> {
pub struct TryStatement<'a> { pub struct TryStatement<'a> {
#[serde(flatten)] #[serde(flatten)]
pub span: Span, pub span: Span,
/// Statements in the `try` block
pub block: Box<'a, BlockStatement<'a>>, pub block: Box<'a, BlockStatement<'a>>,
/// The `catch` clause, including the parameter and the block statement
pub handler: Option<Box<'a, CatchClause<'a>>>, pub handler: Option<Box<'a, CatchClause<'a>>>,
/// The `finally` clause
#[visit(as(FinallyClause))] #[visit(as(FinallyClause))]
pub finalizer: Option<Box<'a, BlockStatement<'a>>>, pub finalizer: Option<Box<'a, BlockStatement<'a>>>,
} }
/// Catch Clause in a [`try/catch` statement](TryStatement).
///
/// This node creates a new scope inside its `body`.
///
/// # Example
/// ```ts
/// try {
/// throw new Error('foo');
/// } catch (e) { // `param` is `e`
/// console.error(e); // `body`
/// }
/// ```
#[ast(visit)] #[ast(visit)]
#[scope(flags(ScopeFlags::CatchClause))] #[scope(flags(ScopeFlags::CatchClause))]
#[derive(Debug)] #[derive(Debug)]
@ -1546,13 +1582,28 @@ pub struct TryStatement<'a> {
pub struct CatchClause<'a> { pub struct CatchClause<'a> {
#[serde(flatten)] #[serde(flatten)]
pub span: Span, pub span: Span,
/// The caught error parameter, e.g. `e` in `catch (e) {}`
pub param: Option<CatchParameter<'a>>, pub param: Option<CatchParameter<'a>>,
/// The statements run when an error is caught
pub body: Box<'a, BlockStatement<'a>>, pub body: Box<'a, BlockStatement<'a>>,
#[serde(skip)] #[serde(skip)]
#[clone_in(default)] #[clone_in(default)]
pub scope_id: Cell<Option<ScopeId>>, pub scope_id: Cell<Option<ScopeId>>,
} }
/// A caught error parameter in a [catch clause](CatchClause).
///
/// # Examples
///
/// ```ts
/// try {} catch (err) {}
/// // ^^^ pattern
/// ```
///
/// ```ts
/// try {} catch ({ err }) {}
/// // ^^^^^^^ pattern
/// ```
#[ast(visit)] #[ast(visit)]
#[derive(Debug)] #[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash)] #[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash)]
@ -1561,10 +1612,17 @@ pub struct CatchClause<'a> {
pub struct CatchParameter<'a> { pub struct CatchParameter<'a> {
#[serde(flatten)] #[serde(flatten)]
pub span: Span, pub span: Span,
/// The bound error
pub pattern: BindingPattern<'a>, pub pattern: BindingPattern<'a>,
} }
/// Debugger Statement /// Debugger Statement
///
/// # Example
/// ```ts
/// let x = 1;
/// debugger; // <--
/// ```
#[ast(visit)] #[ast(visit)]
#[derive(Debug)] #[derive(Debug)]
#[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash)] #[generate_derive(CloneIn, GetSpan, GetSpanMut, ContentEq, ContentHash)]

View file

@ -3925,7 +3925,7 @@ impl<'a> AstBuilder<'a> {
/// ///
/// ## Parameters /// ## Parameters
/// - span: The [`Span`] covering this node /// - span: The [`Span`] covering this node
/// - argument /// - argument: The expression being thrown, e.g. `err` in `throw err;`
#[inline] #[inline]
pub fn statement_throw(self, span: Span, argument: Expression<'a>) -> Statement<'a> { pub fn statement_throw(self, span: Span, argument: Expression<'a>) -> Statement<'a> {
Statement::ThrowStatement(self.alloc(self.throw_statement(span, argument))) Statement::ThrowStatement(self.alloc(self.throw_statement(span, argument)))
@ -3946,9 +3946,9 @@ impl<'a> AstBuilder<'a> {
/// ///
/// ## Parameters /// ## Parameters
/// - span: The [`Span`] covering this node /// - span: The [`Span`] covering this node
/// - block /// - block: Statements in the `try` block
/// - handler /// - handler: The `catch` clause, including the parameter and the block statement
/// - finalizer /// - finalizer: The `finally` clause
#[inline] #[inline]
pub fn statement_try<T1, T2, T3>( pub fn statement_try<T1, T2, T3>(
self, self,
@ -5183,7 +5183,7 @@ impl<'a> AstBuilder<'a> {
/// ///
/// ## Parameters /// ## Parameters
/// - span: The [`Span`] covering this node /// - span: The [`Span`] covering this node
/// - argument /// - argument: The expression being thrown, e.g. `err` in `throw err;`
#[inline] #[inline]
pub fn throw_statement(self, span: Span, argument: Expression<'a>) -> ThrowStatement<'a> { pub fn throw_statement(self, span: Span, argument: Expression<'a>) -> ThrowStatement<'a> {
ThrowStatement { span, argument } ThrowStatement { span, argument }
@ -5195,7 +5195,7 @@ impl<'a> AstBuilder<'a> {
/// ///
/// ## Parameters /// ## Parameters
/// - span: The [`Span`] covering this node /// - span: The [`Span`] covering this node
/// - argument /// - argument: The expression being thrown, e.g. `err` in `throw err;`
#[inline] #[inline]
pub fn alloc_throw_statement( pub fn alloc_throw_statement(
self, self,
@ -5211,9 +5211,9 @@ impl<'a> AstBuilder<'a> {
/// ///
/// ## Parameters /// ## Parameters
/// - span: The [`Span`] covering this node /// - span: The [`Span`] covering this node
/// - block /// - block: Statements in the `try` block
/// - handler /// - handler: The `catch` clause, including the parameter and the block statement
/// - finalizer /// - finalizer: The `finally` clause
#[inline] #[inline]
pub fn try_statement<T1, T2, T3>( pub fn try_statement<T1, T2, T3>(
self, self,
@ -5241,9 +5241,9 @@ impl<'a> AstBuilder<'a> {
/// ///
/// ## Parameters /// ## Parameters
/// - span: The [`Span`] covering this node /// - span: The [`Span`] covering this node
/// - block /// - block: Statements in the `try` block
/// - handler /// - handler: The `catch` clause, including the parameter and the block statement
/// - finalizer /// - finalizer: The `finally` clause
#[inline] #[inline]
pub fn alloc_try_statement<T1, T2, T3>( pub fn alloc_try_statement<T1, T2, T3>(
self, self,
@ -5266,8 +5266,8 @@ impl<'a> AstBuilder<'a> {
/// ///
/// ## Parameters /// ## Parameters
/// - span: The [`Span`] covering this node /// - span: The [`Span`] covering this node
/// - param /// - param: The caught error parameter, e.g. `e` in `catch (e) {}`
/// - body /// - body: The statements run when an error is caught
#[inline] #[inline]
pub fn catch_clause<T1>( pub fn catch_clause<T1>(
self, self,
@ -5292,8 +5292,8 @@ impl<'a> AstBuilder<'a> {
/// ///
/// ## Parameters /// ## Parameters
/// - span: The [`Span`] covering this node /// - span: The [`Span`] covering this node
/// - param /// - param: The caught error parameter, e.g. `e` in `catch (e) {}`
/// - body /// - body: The statements run when an error is caught
#[inline] #[inline]
pub fn alloc_catch_clause<T1>( pub fn alloc_catch_clause<T1>(
self, self,
@ -5313,7 +5313,7 @@ impl<'a> AstBuilder<'a> {
/// ///
/// ## Parameters /// ## Parameters
/// - span: The [`Span`] covering this node /// - span: The [`Span`] covering this node
/// - pattern /// - pattern: The bound error
#[inline] #[inline]
pub fn catch_parameter(self, span: Span, pattern: BindingPattern<'a>) -> CatchParameter<'a> { pub fn catch_parameter(self, span: Span, pattern: BindingPattern<'a>) -> CatchParameter<'a> {
CatchParameter { span, pattern } CatchParameter { span, pattern }
@ -5325,7 +5325,7 @@ impl<'a> AstBuilder<'a> {
/// ///
/// ## Parameters /// ## Parameters
/// - span: The [`Span`] covering this node /// - span: The [`Span`] covering this node
/// - pattern /// - pattern: The bound error
#[inline] #[inline]
pub fn alloc_catch_parameter( pub fn alloc_catch_parameter(
self, self,