Skip to content

Commit 5dc82ff

Browse files
committed
docs(semantic): make doc tests for AstNode::contains_any etc runnable (#13595)
Follow-on after #13137. That PR added methods to `AstNodes`, and doc comments include examples. Make those examples into proper runnable doctests, and fix them (`AstType::ExportDeclaration` doesn't exist).
1 parent fd277b4 commit 5dc82ff

File tree

1 file changed

+44
-20
lines changed

1 file changed

+44
-20
lines changed

crates/oxc_semantic/src/node.rs

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -253,44 +253,68 @@ impl<'a> AstNodes<'a> {
253253

254254
/// Checks if the AST contains any nodes of the given types.
255255
///
256-
/// Example:
257-
/// ```ignore
256+
/// ## Example
257+
/// ```
258+
/// # fn get_nodes<'a>() -> AstNodes<'a> { AstNodes::default() }
259+
///
260+
/// use oxc_ast::AstType;
261+
/// use oxc_semantic::{AstNodes, AstTypesBitset};
262+
///
258263
/// let for_stmt = AstTypesBitset::from_types(&[AstType::ForStatement]);
259-
/// let import_export_decl = AstTypesBitset::from_types(&[AstType::ImportDeclaration, AstType::ExportDeclaration]);
264+
/// let import_export_decl = AstTypesBitset::from_types(&[
265+
/// AstType::ImportDeclaration,
266+
/// AstType::ExportNamedDeclaration,
267+
/// ]);
260268
///
261-
/// // returns true if there is a `for` loop anywhere in the AST.
262-
/// nodes.contains_any(&for_stmt)
263-
/// // returns true if there is at least one import OR one export in the AST.
264-
/// nodes.contains_any(&import_export_decl)
269+
/// let nodes: AstNodes = get_nodes();
270+
/// // `true` if there is a `for` loop anywhere in the AST
271+
/// nodes.contains_any(&for_stmt);
272+
/// // `true` if there is at least one import OR one export in the AST
273+
/// nodes.contains_any(&import_export_decl);
265274
/// ```
266275
pub fn contains_any(&self, bitset: &AstTypesBitset) -> bool {
267276
self.node_kinds_set.intersects(bitset)
268277
}
269278

270279
/// Checks if the AST contains all of the given types.
271280
///
272-
/// Example:
273-
/// ```ignore
281+
/// ## Example
282+
/// ```
283+
/// # fn get_nodes<'a>() -> AstNodes<'a> { AstNodes::default() }
284+
///
285+
/// use oxc_ast::AstType;
286+
/// use oxc_semantic::{AstNodes, AstTypesBitset};
287+
///
274288
/// let for_stmt = AstTypesBitset::from_types(&[AstType::ForStatement]);
275-
/// let import_export_decl = AstTypesBitset::from_types(&[AstType::ImportDeclaration, AstType::ExportDeclaration]);
289+
/// let import_export_decl = AstTypesBitset::from_types(&[
290+
/// AstType::ImportDeclaration,
291+
/// AstType::ExportNamedDeclaration,
292+
/// ]);
276293
///
277-
/// // returns true if there is a `for` loop anywhere in the AST.
278-
/// nodes.contains_all(&for_stmt)
279-
/// // returns true only if there is at least one import AND one export in the AST.
280-
/// nodes.contains_all(&import_export_decl)
294+
/// let nodes: AstNodes = get_nodes();
295+
/// // `true` if there is a `for` loop anywhere in the AST
296+
/// nodes.contains_all(&for_stmt);
297+
/// // `true` if there is at least one import AND one export in the AST
298+
/// nodes.contains_all(&import_export_decl);
281299
/// ```
282300
pub fn contains_all(&self, bitset: &AstTypesBitset) -> bool {
283301
self.node_kinds_set.contains(bitset)
284302
}
285303

286304
/// Checks if the AST contains a node of the given type.
287305
///
288-
/// Example:
289-
/// ```ignore
290-
/// // returns true if there is a `for` loop anywhere in the AST.
291-
/// nodes.contains(AstType::ForStatement)
292-
/// // returns true if there is an `ImportDeclaration` anywhere in the AST.
293-
/// nodes.contains(AstType::ImportDeclaration)
306+
/// ## Example
307+
/// ```
308+
/// # fn get_nodes<'a>() -> AstNodes<'a> { AstNodes::default() }
309+
///
310+
/// use oxc_ast::AstType;
311+
/// use oxc_semantic::{AstNodes, AstTypesBitset};
312+
///
313+
/// let nodes: AstNodes = get_nodes();
314+
/// // `true` if there is a `for` loop anywhere in the AST
315+
/// nodes.contains(AstType::ForStatement);
316+
/// // `true` if there is an `ImportDeclaration` anywhere in the AST
317+
/// nodes.contains(AstType::ImportDeclaration);
294318
/// ```
295319
pub fn contains(&self, ty: AstType) -> bool {
296320
self.node_kinds_set.has(ty)

0 commit comments

Comments
 (0)