acorn-es7-plugin
Advanced tools
Comparing version 1.0.10 to 1.0.11
var NotAsync = {} ; | ||
var asyncExit = /^async[\t ]+(return|throw)/ ; | ||
var asyncFunction = /^async[\t ]+function/ ; | ||
var atomOrPropertyOrLabel = /^\s*[:;]/ ; | ||
var atomOrPropertyOrLabel = /^\s*[):;]/ ; | ||
var asyncAtEndOfLine = /^async[\t ]*\n/ ; | ||
@@ -48,5 +48,5 @@ | ||
} | ||
this.reservedWords = new RegExp(this.reservedWords.toString().replace(/await|async/g,"").replace("|/","/").replace("/|","/").replace("||","|")) ; | ||
this.reservedWordsStrict = new RegExp(this.reservedWordsStrict.toString().replace(/await|async/g,"").replace("|/","/").replace("/|","/").replace("||","|")) ; | ||
this.reservedWordsStrictBind = new RegExp(this.reservedWordsStrictBind.toString().replace(/await|async/g,"").replace("|/","/").replace("/|","/").replace("||","|")) ; | ||
this.reservedWords = new RegExp(this.reservedWords.toString().replace(/await|async/g,"").replace("|/","/").replace("/|","/").replace("||","|")) ; | ||
this.reservedWordsStrict = new RegExp(this.reservedWordsStrict.toString().replace(/await|async/g,"").replace("|/","/").replace("/|","/").replace("||","|")) ; | ||
this.reservedWordsStrictBind = new RegExp(this.reservedWordsStrictBind.toString().replace(/await|async/g,"").replace("|/","/").replace("/|","/").replace("||","|")) ; | ||
return base.apply(this,arguments); | ||
@@ -92,2 +92,15 @@ } | ||
parser.extend("parseIdent",function(base){ | ||
return function(liberal){ | ||
var id = base.apply(this,arguments); | ||
var st = state(this) ; | ||
if (st.inAsyncFunction && id.name==='await') { | ||
if (arguments.length===0) { | ||
this.raise(id.start,"'await' is reserved within async functions") ; | ||
} | ||
} | ||
return id ; | ||
} | ||
}) ; | ||
parser.extend("parseExprAtom",function(base){ | ||
@@ -109,3 +122,3 @@ return function(refShorthandDefaultPos){ | ||
var parseHooks = { | ||
parseFunctionBody:function(){ | ||
parseFunctionBody:function(node,isArrowFunction){ | ||
try { | ||
@@ -112,0 +125,0 @@ var wasInBody = inBody ; |
{ | ||
"name": "acorn-es7-plugin", | ||
"version": "1.0.10", | ||
"version": "1.0.11", | ||
"description": "A plugin for the Acorn parser that understands the ES7 keywords async and await", | ||
@@ -5,0 +5,0 @@ "main": "acorn-es7-plugin.js", |
@@ -9,3 +9,3 @@ [![NPM](https://nodei.co/npm/acorn-es7-plugin.png?downloads=true&downloadRank=true)](https://nodei.co/npm/acorn-es7-plugin/) | ||
npm install --save acorn-es7-plugin | ||
Usage | ||
@@ -20,6 +20,6 @@ ===== | ||
require('./acorn-es7-plugin')(acorn) ; | ||
Using the plugin | ||
var code = "async function x(){ if (x) return await(x-1) ; return 0 ; }\n"; | ||
Using the plugin | ||
var code = "async function x(){ if (x) return await(x-1) ; return 0 ; }\n"; | ||
var ast = acorn.parse(code,{ | ||
@@ -33,3 +33,3 @@ // Specify use of the plugin | ||
console.log(JSON.stringify(ast,null,2)) ; | ||
Output: | ||
@@ -95,3 +95,3 @@ | ||
} | ||
Options & Compliance | ||
@@ -111,7 +111,8 @@ ==================== | ||
19-Dec-15: v1.0.11 | ||
- Generate error if 'await' is used as an identifier within an async function. | ||
10-Dec-15: v1.0.10 | ||
- Update the plugin code to remove 'async' and 'await' from the super-strict keyword tests introduced in acorn v2.6.x that generate parse errors before the plugin gets a chance to manage them. | ||
@@ -25,16 +25,45 @@ 'use strict'; | ||
}, | ||
{desc:"Await in async",code:"async function x() { await(undefined); }", | ||
pass:function(ast){ return ast.body[0].body.body[0].expression.type==='AwaitExpression'} | ||
{desc:"Await in async is AwaitExpression",code:"async function x() { await(undefined); await undefined ; }", | ||
pass:function(ast){ return ast.body[0].body.body[0].expression.type==='AwaitExpression' && ast.body[0].body.body[1].expression.type==='AwaitExpression'} | ||
}, | ||
{desc:"Await in function",code:"function x() { await(undefined); }", | ||
pass:function(ast){ return ast.body[0].body.body[0].expression.callee.name==='await'} | ||
{desc:"Await in function is identifier",code:"function x() { await(undefined); }", | ||
pass:function(ast){ return ast.body[0].body.body[0].expression.callee.name==='await'} | ||
}, | ||
{desc:"Async method",code:"var a = {async x(){}}", | ||
pass:function(ast){ return ast.body[0].declarations[0].init.properties[0].value.async } | ||
}, | ||
{desc:"Async get method",code:"var a = {async get x(){}}", | ||
pass:function(ast){ return ast.body[0].declarations[0].init.properties[0].value.async } | ||
}, | ||
{desc:"Async arrow",code:"var a = async()=>0", | ||
pass:function(ast){ return ast.body[0].declarations[0].init.async } | ||
}, | ||
{desc:"Async set method fails",code:"var a = {async set x(){}}", | ||
pass:function(ex){ return ex === "'set <member>(value)' cannot be be async (1:15)" } | ||
}, | ||
{desc:"Async constructor fails",code:"var a = {async constructor(){}}", | ||
pass:function(ex){ return ex === "'constructor()' cannot be be async (1:15)" } | ||
}, | ||
{desc:"Await declaration fails in async function",code:"async function x() { var await; }", | ||
pass:function(ex){ return ex === "'await' is reserved within async functions (1:25)" } | ||
}, | ||
{desc:"Await function declaration fails in async function",code:"async function x() { function await() {} }", | ||
pass:function(ex){ return ex === "'await' is reserved within async functions (1:30)" } | ||
}, | ||
{desc:"Await reference fails in async function",code:"async function x() { return 1+await; }", | ||
pass:function(ex){ return ex === "Unexpected token (1:35)" } | ||
} | ||
] ; | ||
var out = { | ||
true:"pass".green, | ||
false:"fail".red | ||
}; | ||
tests.forEach(function(test,idx){ | ||
try { | ||
console.log((idx+1)+")\t",test.desc,test.pass(parse(test.code))?"pass".green:"fail".red); | ||
console.log((idx+1)+")\t",test.desc,test.code.yellow,out[test.pass(parse(test.code))]); | ||
} catch(ex) { | ||
console.log((idx+1)+")\t",test.desc,ex.message.cyan,"fail".red); | ||
console.log((idx+1)+")\t",test.desc,test.code.yellow,ex.message.cyan,out[test.pass(ex.message)]); | ||
} | ||
}) ; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22442
548
114