Socket
Socket
Sign inDemoInstall

@babel/template

Package Overview
Dependencies
16
Maintainers
6
Versions
77
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@babel/template

Generate an AST from a string template.


Version published
Maintainers
6
Weekly downloads
46,256,192
decreased by-9.12%

Weekly downloads

Package description

What is @babel/template?

The @babel/template package is a part of the Babel toolchain that allows developers to create AST (Abstract Syntax Tree) nodes with placeholders programmatically. This is useful for code generation, transformation, and manipulation tasks where you want to produce syntactically correct code snippets based on a template with variable parts.

What are @babel/template's main functionalities?

Building AST Nodes

This feature allows you to build AST nodes using template literals with placeholders. The placeholders are then replaced with actual values to create a syntactically correct code snippet.

const template = require('@babel/template').default;
const buildRequire = template(`
  const %%importName%% = require(%%source%%);
`);
const ast = buildRequire({
  importName: template.identifier('myModule'),
  source: template.stringLiteral('my-module')
});

Using Placeholders

This feature demonstrates how to use placeholders for different parts of the code, such as function names, parameters, and bodies. The placeholders are replaced with AST nodes representing identifiers, arrays of identifiers, and block statements, respectively.

const template = require('@babel/template').default;
const buildFunction = template(`
  function %%funcName%%(%%params%%) {
    return %%body%%;
  }
`);
const ast = buildFunction({
  funcName: template.identifier('myFunction'),
  params: [template.identifier('a'), template.identifier('b')],
  body: template.blockStatement([])
});

Customizing Placeholder Patterns

This feature shows how to customize the placeholder pattern used in the template. This is useful when you want to define a specific pattern for your placeholders to avoid conflicts with other parts of the code.

const template = require('@babel/template').default;
const buildAssertion = template(`
  assert(%%test%%, '%%errorMessage%%');
`, {
  placeholderPattern: /^%%[a-zA-Z0-9_]+%%$/
});
const ast = buildAssertion({
  test: template.binaryExpression('===', template.identifier('a'), template.numericLiteral(3)),
  errorMessage: template.stringLiteral('a is not 3')
});

Other packages similar to @babel/template

Readme

Source

@babel/template

Generate an AST from a string template.

See our website @babel/template for more information or the issues associated with this package.

Install

Using npm:

npm install --save-dev @babel/template

or using yarn:

yarn add @babel/template --dev

FAQs

Last updated on 03 Feb 2021

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