Socket
Socket
Sign inDemoInstall

@swagger-api/apidom-ast

Package Overview
Dependencies
10
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @swagger-api/apidom-ast

Tools necessary for parsing stage of ApiDOM, specifically for syntactic analysis.


Version published
Weekly downloads
428K
increased by10.74%
Maintainers
1
Created
Weekly downloads
 

Changelog

Source

0.98.0 (2024-03-21)

Bug Fixes

  • reference: fix regression in nested paths dereferencing (#3960) (8435ee1), closes #3959

Features

  • reference: apply dereferencing architecture 2.0 to ApiDOM (#3930) (bbb9a25)
  • reference: apply dereferencing architecture 2.0 to AsyncAPI 2.x (#3933) (856dc5c), closes #3932
  • reference: apply dereferencing architecture 2.0 to OpenAPI 2.0 (#3931) (63a41d4), closes #3924
  • reference: apply dereferencing architecture 2.0 to OpenAPI 3.0.x (#3917) (cc3a970), closes #3916
  • reference: apply dereferencing architecture 2.0 to OpenAPI 3.1.0 (#3942) (d57c318), closes #3941

Readme

Source

@swagger-api/apidom-ast

@swagger-api/apidom-ast contains tools necessary for parsing stage of ApiDOM, specifically for syntactic analysis. Syntactic analysis will take a stream of tokens and turn it into an AST representation. Using the information in the tokens, this phase will reformat them as an AST which represents the structure of input string in a way that makes it easier to work with.

@swagger-api/apidom-ast currently contains AST nodes for JSON and YAML 1.2 formats.

Installation

You can install this package via npm CLI by running the following command:

 $ npm install @swagger-api/apidom-ast

Base AST Nodes

Base AST nodes are nodes that are supplementary to any specific AST nodes. Having standardized AST of various formats (JSON/YAML) allows us to have common syntactic analysis or transformation algorithms. These nodes includes Error, Literal, Position, etc...

Along with base nodes there are predicates that can assert on these nodes.

JSON AST Nodes

Convenient for low lever CST parsers that don't come with it's onw AST nodes. You can find list of JSON AST nodes in this directory.

YAML AST Nodes

Convenient for low lever CST parsers that don't come with it's onw AST nodes. You can find list of YAML AST nodes in this directory. As YAML is very complex format, along with nodes we also expose implementation of YAML Failsafe and JSON schemas along with formatters for canonical block scalars.

Traversal

@swagger-api/apidom-ast comes with its own traversal algorithm convenient for traversing CST or AST.

visit

visit will walk through an CST/AST using a depth first traversal, calling the visitor's enter function at each node in the traversal, and calling the leave function after visiting that node and all of its child nodes.

By returning different values from the enter and leave functions, the behavior of the visitor can be altered, including skipping over a sub-tree of the Node (by returning false), editing the Node Tree by returning a value or null to remove the value, or to stop the whole traversal by returning BREAK.

When using visit to edit an Node Tree, the original Node Tree will not be modified, and a new version of the Node Tree with the changes applied will be returned from the visit function.

import { visit } from '@swagger-api/apidom-ast';

const tree = {
    type: 'root',
    children: [
      {
          type: 'child',
          value: 'this is child node',
          children: [],
      },
    ],
};

const keyMap = {
    root: ['children'],
};

const visitor = {
    child(node) {
        console.dir(node.value); // => 'this is child node'
    },
};

const newTree = visit(tree, visitor, { keyMap }); // => tree{...}

Notice how we used 3rd parameter to visit function. Actually it can consume number of configuration options which can change its behavior.

Configuration optionTypeDefaultDescription
keyMapObjectnullDefines how nodes map to it's children.
stateObject{}Additional state that is provided to the visitor. State is merged inti visitor object in following manner: Object.assign(visitor, state)
breakSymbolObject{}Defines a symbol that can break the traversal. Symbol is compared by strict equality (===).
visitFnGetterFunctiongetVisitFnFunction that extract appropriate method from the visitor given specific Node type.
nodeTypeGetterFunctiongetNodeTypeNode type extractor function.
nodePredicateFunctionisNodePredicate that checks if particular Node can be really considered a Node.
detectCyclesBooleantrueIf the structure that needs to be traversed is represented as directed cyclic graph, visit will skip Nodes that have already been traversed to avoid infinite recursion.

FAQs

Last updated on 21 Mar 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc