Join our webinar on Wednesday, June 26, at 1pm EDTHow Chia Mitigates Risk in the Crypto Industry.Register
Socket
Socket
Sign inDemoInstall

acorn-es7-plugin

Package Overview
Dependencies
0
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    acorn-es7-plugin

A plugin for the Acorn parser that understands the ES7 keywords async and await


Version published
Maintainers
1
Created

Readme

Source

NPM

acorn-es7-plugin

acorn-es7-plugin is a plugin for the Acorn parser that generates ESTrees following the 'experimental' specification for asynchronous functions.

npm install --save acorn-es7-plugin

Usage

Adding the plugin

// Require acorn as usual
var acorn = require("acorn");
// Add the es7-plugin
require('./acorn-es7-plugin')(acorn) ;

Using the plugin

var code = "async function x(){ if (x) return await(x-1) ; return 0 ; }\n";	
var ast = acorn.parse(code,{
	// Specify use of the plugin
	plugins:{asyncawait:true},
	// Specify the ecmaVersion
	ecmaVersion:7
}) ;
// Show the AST
console.log(JSON.stringify(ast,null,2)) ;

Output:

{
	"type": "Program",
	"body": [
	{
	  "type": "FunctionDeclaration",
	  "id": {
	    "type": "Identifier",
	    "name": "x"
	  },
	  "generator": false,
	  "expression": false,
	  "params": [],
	  "body": {
	    "type": "BlockStatement",
	    "body": [
	      {
	        "type": "IfStatement",
	        "test": {
	          "type": "Identifier",
	          "name": "x"
	        },
	        "consequent": {
	          "type": "ReturnStatement",
	          "argument": {
	            "type": "AwaitExpression",
	            "operator": "await",
	            "argument": {
	              "type": "BinaryExpression",
	              "left": {
	                "type": "Identifier",
	                "name": "x"
	              },
	              "operator": "-",
	              "right": {
	                "type": "Literal",
	                "value": 1,
	                "raw": "1"
	              }
	            }
	          }
	        },
	        "alternate": null
	      },
	      {
	        "type": "ReturnStatement",
	        "argument": {
	          "type": "Literal",
	          "value": 0,
	          "raw": "0"
	        }
	      }
	    ]
	  },
	  "async": true
	}
	],
	"sourceType": "script"
}

Options & Compliance

The parser attempts to enforce strict contextualisation of async and await. Specifically, async is only a keyword if it precedes a function declaration, function expression or arrow function. await is only a keyword inside an async function. Outside of these contexts, both tokens are treated as identifiers (as they were in ES6 and earlier).

When using the plugin, you can supply an object in place of the 'true' flag with the following options.

flagmeaning
awaitAnywhereIf await is used outside of an async function and could not be an identifier, generate an AwaitExpression node. This typically means you can use await anywhere except when its argument would require parentheses, as this parses to a call to 'await(....)'.
asyncExitsAllow the additional sequences async return <optional-expression> and async throw <expression>. These sequences are used with nodent. In each case, as with async functions, a standard ReturnStatement (or ThrowStatement) node is generated, with an additional member 'async' set to true.

Keywords

FAQs

Last updated on 06 Nov 2015

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