Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

astring

Package Overview
Dependencies
Maintainers
1
Versions
83
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

astring

JavaScript code generator from an ESTree-compliant AST.

  • 1.9.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.3M
increased by10.09%
Maintainers
1
Weekly downloads
 
Created

What is astring?

Astring is a lightweight JavaScript library used for generating JavaScript code from an Abstract Syntax Tree (AST). It is designed to be fast and efficient, making it suitable for use in compilers, transpilers, and other tools that need to generate JavaScript code programmatically.

What are astring's main functionalities?

Basic Code Generation

This feature allows you to generate JavaScript code from a simple AST. In this example, astring generates a string literal from the provided AST.

const astring = require('astring');
const ast = { type: 'Literal', value: 'Hello, world!' };
const code = astring.generate(ast);
console.log(code); // Output: 'Hello, world!'

Complex Code Generation

This feature demonstrates generating more complex JavaScript code from an AST. In this example, astring generates a variable declaration with an initializer.

const astring = require('astring');
const ast = {
  type: 'Program',
  body: [
    {
      type: 'VariableDeclaration',
      declarations: [
        {
          type: 'VariableDeclarator',
          id: { type: 'Identifier', name: 'x' },
          init: { type: 'Literal', value: 42 }
        }
      ],
      kind: 'const'
    }
  ]
};
const code = astring.generate(ast);
console.log(code); // Output: 'const x = 42;'

Custom Code Generation

Astring allows for custom code generation by extending the base generator. In this example, the custom generator wraps the literal value in comment syntax.

const astring = require('astring');
const customGenerator = Object.assign({}, astring.baseGenerator, {
  Literal(node, state) {
    state.write(`/*${node.value}*/`);
  }
});
const ast = { type: 'Literal', value: 'Hello, world!' };
const code = astring.generate(ast, { generator: customGenerator });
console.log(code); // Output: '/*Hello, world!*/'

Other packages similar to astring

Keywords

FAQs

Package last updated on 25 Aug 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc