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

jsdc

Package Overview
Dependencies
Maintainers
1
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsdc - npm Package Compare versions

Comparing version 0.3.6 to 0.4.0

5

package.json
{
"name": "jsdc",
"version": "0.3.6",
"description": "transform ecmascript6 to ecmascript5",
"version": "0.4.0",
"description": "compiler ecmascript6 to ecmascript5",
"maintainers": [

@@ -45,2 +45,3 @@ {

"ecmascript5",
"es6toes6",
"transform",

@@ -47,0 +48,0 @@ "compiler"

42

README.md
# Javascript Downcast
### transform ecmascript6 to ecmascript5
### compiler ecmascript6 to ecmascript5

@@ -605,6 +605,6 @@ [![NPM version](https://badge.fury.io/js/jsdc.png)](https://npmjs.org/package/jsdc)

function *a(_0_) {
var a = void 0;return;a=_0_
var a;return;a=_0_
}
```
`yield`的返回值将变成一个对象的value:
`yield`的返回值将变成一个对象的`value`,同时添加`done`属性标明是否结束:
```js

@@ -617,3 +617,3 @@ function *a() {

function *a(_0_) {
var a = void 0;return {value:1};a=_0_
var a;return {value:1,done:true};a=_0_
}

@@ -633,3 +633,3 @@ ```

return{value:1,done:false}
return{value:2,done:false}
return{value:2,done:true}
}

@@ -647,3 +647,3 @@ ```

return{value:1,done:false}
return{value:2,done:false}
return{value:2,done:true}
}}();

@@ -663,3 +663,3 @@ ```

return{value:1,done:false}
return{value:2,done:false}
return{value:2,done:true}
}}();

@@ -677,6 +677,6 @@ ```

return{value:1,done:false}
return{value:2,done:false}
return{value:2,done:true}
}}();
```
当出现`yield`语句时,添加`switch`语句来模拟顺序执行:
当出现`yield`语句时,添加`while`和`switch`语句来模拟顺序执行:
```js

@@ -690,6 +690,8 @@ function *a(){

var a=function(){var _1_=0;return function(){return{next:_0_}};function _0_(){
switch(_1_){case 0:_1_++;return{value:1,done:false}
case 1:return{value:1,done:false}}
while(1){switch(_1_){case 0:_1_=1;return{value:1,done:false}
case 1:_1_=-1;return{value:1,done:true}}}
}}();
```
> 注意状态在`switch`各分支语句之间的跳转
同时函数里面的`var`声明需要前置,以免每次调用`next()`方法时又重新声明一遍失去了状态:

@@ -705,5 +707,5 @@ ```js

var a=function(){var _1_=0;return function(){return{next:_0_}};var a;function _0_(){
switch(_1_){case 0:a = 1;
_1_++;return{value:a++,done:false};
case 1:_1_++;return{value:a++,done:false;}
while(1){switch(_1_){case 0:a = 1;
_1_=1;return{value:a++,done:false};
case 1:_1_=-1;return{value:a++,done:true;}}}
}}();

@@ -715,3 +717,3 @@ ```

添加`default`语句,更改最后一个`yield`的`done`为`true`:
添加`default`语句:
```js

@@ -726,5 +728,5 @@ function *a(){

var a=function(){var _0_=0;return function(){return{next:_1_}};var a;function _1_(_2_){
switch(_0_){case 0:a = 1;
_0_++;return{value:a++,done:false};case 1:
_0_++;return{value:a++,done:true};default:return{done:true}}
while(1){switch(_0_){case 0:a = 1;
_0_=1;return{value:a++,done:false};case 1:
_0_=-1;return{value:a++,done:true};default:return{done:true}}}
}}();

@@ -740,6 +742,6 @@ ```

var a=function(){var _0_=0;return function(){return{next:_1_}};function _1_(_2_){
switch(_0_){case 0:_0_++;var _3_=b();if(!_3_.done)_0_--;return _3_;default:return{done:true}}
while(1){switch(_0_){case 0:_0_=1;var _3_=b();if(!_3_.done)_0_=0;return _3_;default:return{done:true}}}
}}();
```
表达式也一样:
表达式也一样,没有`yield`则不会添加`while`和`switch`语句:
```js

@@ -746,0 +748,0 @@ ~function *(){

@@ -13,2 +13,6 @@ var homunculus = require('homunculus');

parse: function(node, start) {
//有可能被Generator中该写过
if(node.gen) {
return;
}
if(start) {

@@ -19,3 +23,6 @@ var of = node.leaf(3);

&& of.token().content() == 'of') {
this.hash[node.nid()] = true;
//存放临时li供block首尾改写引用
this.hash[node.nid()] = {
temp: this.jsdc.uid()
};
this.jsdc.ignore(of, 'forof1');

@@ -26,5 +33,19 @@ }

var last = node.last();
var s = '';
if(!this.jsdc.endsWith(';')
&& !this.jsdc.endsWith(':')
&& !this.jsdc.endsWith('{')
&& !this.jsdc.endsWith('\n')) {
s = ';';
}
s += this.hash[node.nid()].id + '=' + this.hash[node.nid()].temp;
if(last.name() != JsNode.BLOCKSTMT) {
//临时引用写回,使循环正常
this.jsdc.appendBefore(s);
//}闭合
this.jsdc.appendBefore('}');
}
else {
this.jsdc.insert(s, this.jsdc.res.length - 1);
}
}

@@ -40,3 +61,2 @@ },

prts: function(node, start) {
//for of的语句如果省略{}则加上
var parent = node.parent();

@@ -60,5 +80,6 @@ if(parent.name() == JsNode.ITERSTMT

else {
//for of的语句如果省略{}则加上
var last = parent.last();
if(last.name() != JsNode.BLOCKSTMT) {
this.jsdc.append('{');
this.jsdc.appendBefore('{');
this.assign(parent);

@@ -91,6 +112,10 @@ }

}
this.jsdc.append(k + '=' + k + '.value;');
//先存下临时引用
var o = this.hash[node.nid()];
o.id = k;
this.jsdc.append('var ' + o.temp + '=' + k + ';');
this.jsdc.append(k + '=' + o.temp + '.value;');
}
});
module.exports = Forof;
module.exports = Forof;

@@ -146,3 +146,23 @@ var homunculus = require('homunculus');

else {
self.jsdc.appendBefore(',done:' + (o.index == o.count) + '}');
var belong = self.belong(node);
if(belong.length) {
var done = belong.map(function(o) {
return o.done || 0;
});
done.push((o.index == o.count) ? 1 : 0);
done.push((o.return) ? 0 : 1);
if(done.indexOf(0) > -1) {
done = [false];
}
var hash = {};
done = done.filter(function(o) {
var res = !hash.hasOwnProperty(o);
hash[o] = true;
return res;
});
self.jsdc.appendBefore(',done:' + done.join('&&') + '}');
}
else {
self.jsdc.appendBefore(',done:' + (o.index == o.count && !o.return) + '}');
}
o.yield.push({

@@ -177,3 +197,10 @@ i: self.jsdc.i

if(o.count) {
this.jsdc.appendBefore(';' + o.state + '=-1');
if(!this.jsdc.endsWith(';')
&& !this.jsdc.endsWith(':')
&& !this.jsdc.endsWith('{')
&& !this.jsdc.endsWith('}')
&& !this.jsdc.endsWith('\n')) {
this.jsdc.appendBefore(';');
}
this.jsdc.appendBefore(o.state + '=-1');
this.jsdc.appendBefore(';default:return{done:true}}}');

@@ -237,10 +264,26 @@ }

});
eventbus.on(node.leaf(1).nid(), function(node, start) {
if(start) {
self.jsdc.append('{value:');
}
else {
self.jsdc.appendBefore(',done:true}');
}
});
//无return内容分开侦听
if(node.leaf(1).name() == JsNode.TOKEN) {
eventbus.on(node.leaf(0).nid(), function(node, start) {
if(!start) {
self.jsdc.append('{value:');
}
});
eventbus.on(node.nid(), function(node, start) {
if(!start) {
self.jsdc.appendBefore(',done:true}');
}
});
}
//有则侦听内容
else {
eventbus.on(node.leaf(1).nid(), function(node, start) {
if(start) {
self.jsdc.append('{value:');
}
else {
self.jsdc.appendBefore(',done:true}');
}
});
}
break;

@@ -271,9 +314,10 @@ //忽略这些节点中的yield语句

if(self.stmt.hasOwnProperty(node.nid())) {
var ifstmt = node;
res.index++;
var block = node.leaf(4);
var block = ifstmt.leaf(4);
//改写if语句
self.jsdc.ignore(node.first(), 'gen14');
self.jsdc.ignore(node.leaf(1), 'gen15');
self.jsdc.ignore(node.leaf(2), 'gen16');
self.jsdc.ignore(node.leaf(3), 'gen17');
self.jsdc.ignore(ifstmt.first(), 'gen14');
self.jsdc.ignore(ifstmt.leaf(1), 'gen15');
self.jsdc.ignore(ifstmt.leaf(2), 'gen16');
self.jsdc.ignore(ifstmt.leaf(3), 'gen17');
if(block.name() == JsNode.BLOCKSTMT) {

@@ -283,9 +327,20 @@ self.jsdc.ignore(block.first().first(), 'gen18');

}
var temp;
var elseTemp;
var ifEndTemp;
var top;
var ifstmt = node;
var elset = block.next();
var elseblock;
if(elset && elset.name() == JsNode.TOKEN) {
elseblock = elset.next();
}
//if结束后的状态
eventbus.on(node.nid(), function(node, start) {
eventbus.on(ifstmt.nid(), function(node, start) {
if(!start) {
if(!self.jsdc.endsWith(';')
&& !self.jsdc.endsWith(':')
&& !self.jsdc.endsWith('{')
&& !self.jsdc.endsWith('}')
&& !self.jsdc.endsWith('\n')) {
self.jsdc.appendBefore(';');
}
self.jsdc.append('case ' + ifEndTemp + ':');

@@ -302,8 +357,16 @@ }

self.jsdc.append('?');
self.jsdc.append(++top.index2 + ':' + ++top.index2 + ';break;');
self.jsdc.append('case ' + (top.index2 - 1) + ':');
temp = top.index2;
self.jsdc.append(++top.index2 + ':');
self.jsdc.append((elseblock ? ++top.index2 : top.index2 + 1) + ';break;');
self.jsdc.append('case ' + (elseblock ? top.index2 - 1 : top.index2) + ':');
elseTemp = top.index2;
ifEndTemp = ++top.index2;
}
else {
if(!self.jsdc.endsWith(';')
&& !self.jsdc.endsWith(':')
&& !self.jsdc.endsWith('{')
&& !self.jsdc.endsWith('}')
&& !self.jsdc.endsWith('\n')) {
self.jsdc.appendBefore(';');
}
self.jsdc.appendBefore(top.state + '=');

@@ -315,21 +378,275 @@ self.jsdc.appendBefore(ifEndTemp);

//else语句忽略{}
var elset = block.next();
if(elset && elset.name() == JsNode.TOKEN) {
if(elseblock) {
self.jsdc.ignore(elset, 'gen20');
block = elset.next();
if(elseblock.name() == JsNode.BLOCKSTMT) {
self.jsdc.ignore(elseblock.first().first(), 'gen21');
self.jsdc.ignore(elseblock.first().last(), 'gen22');
}
eventbus.on(elseblock.nid(), function(node, start) {
if(start) {
self.jsdc.append('case ' + elseTemp + ':');
}
else if(elseblock.name() != JsNode.IFSTMT
&& elseblock.parent().name() != JsNode.IFSTMT) {
if(!self.jsdc.endsWith(';')
&& !self.jsdc.endsWith(':')
&& !self.jsdc.endsWith('{')
&& !self.jsdc.endsWith('}')
&& !self.jsdc.endsWith('\n')) {
self.jsdc.appendBefore(';');
}
self.jsdc.appendBefore(top.state + '=');
self.jsdc.appendBefore(ifEndTemp);
self.jsdc.appendBefore(';break;');
}
});
}
}
break;
case JsNode.ITERSTMT:
var itstmt = node;
var first = itstmt.first();
var top;
var loopTemp;
var itTemp;
var itEndTemp;
var endTemp;
switch(first.token().content()) {
case 'for':
var block = itstmt.last();
if(block.name() == JsNode.BLOCKSTMT) {
self.jsdc.ignore(block.first().first(), 'gen21');
self.jsdc.ignore(block.first().last(), 'gen22');
self.jsdc.ignore(block.first().first(), 'gen23');
self.jsdc.ignore(block.first().last(), 'gen24');
}
self.jsdc.ignore(first, 'gen25');
self.jsdc.ignore(node.leaf(1), 'gen26');
self.jsdc.ignore(block.prev(), 'gen27');
switch(itstmt.leaf(3).token().content()) {
case 'in':
var keys = self.jsdc.uid();
var len = self.jsdc.uid();
var index = self.jsdc.uid();
var id;
if(node.leaf(2).name() == JsNode.VARSTMT) {
id = join(node.leaf(2).last());
}
else {
id = join(node.leaf(2));
}
var obj = self.jsdc.uid();
self.jsdc.ignore(node.leaf(2), 'gen28');
self.jsdc.ignore(node.leaf(3), 'gen29');
eventbus.on(itstmt.leaf(4).nid(), function(node, start) {
if(start) {
self.jsdc.append('var ' + obj + '=');
top = self.hash[nid];
}
else {
self.jsdc.appendBefore(',' + keys + '=Object.keys(');
self.jsdc.appendBefore(obj);
self.jsdc.appendBefore('),' + len);
self.jsdc.appendBefore('=' + keys + '.length,');
self.jsdc.appendBefore(index + '=0;');
endTemp = ++top.index2;
self.jsdc.appendBefore('case ' + endTemp + ':');
self.jsdc.appendBefore(top.state + '=');
self.jsdc.appendBefore(index + '++<' + len + '?');
itTemp = ++top.index2;
itEndTemp = ++top.index2;
self.jsdc.appendBefore(itTemp + ':' + itEndTemp + ';break;');
//供yield判断
itstmt.done = index + '<' + len;
}
});
eventbus.on(block.nid(), function(node, start) {
if(start) {
self.jsdc.append('case ' + itTemp + ':');
self.jsdc.append(id + '=');
self.jsdc.append(keys + '[');
self.jsdc.append(index + '];');
}
else {
self.jsdc.appendBefore(top.state + '=' + endTemp);
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':');
}
});
break;
case 'of':
var iterator;
//标记使Forof类处理失效
itstmt.gen = true;
var next = self.jsdc.uid();
var id;
if(node.leaf(2).name() == JsNode.VARSTMT) {
id = join(node.leaf(2).last());
}
else {
id = join(node.leaf(2));
}
var obj = self.jsdc.uid();
self.jsdc.ignore(node.leaf(2), 'gen30');
self.jsdc.ignore(node.leaf(3), 'gen31');
eventbus.on(itstmt.leaf(4).nid(), function(node, start) {
if(start) {
self.jsdc.append('var ' + obj + '=');
top = self.hash[nid];
}
else {
self.jsdc.appendBefore(',' + next);
self.jsdc.appendBefore('=' + obj + '.next();');
endTemp = ++top.index2;
self.jsdc.appendBefore('case ' + endTemp + ':');
self.jsdc.appendBefore(top.state + '=');
self.jsdc.appendBefore(next + '.done' + '?');
iterator = ++top.index2;
itTemp = ++top.index2;
itEndTemp = ++top.index2;
self.jsdc.appendBefore(itTemp + ':' + itEndTemp + ';break;');
//供yield判断
itstmt.done = next + '.done';
}
});
eventbus.on(block.nid(), function(node, start) {
if(start) {
self.jsdc.append('case ' + iterator + ':');
self.jsdc.append(next + '=' + obj + '.next();');
self.jsdc.append(top.state + '=' + endTemp);
self.jsdc.append(';break;case ' + itTemp + ':');
self.jsdc.append(id + '=' + next + '.value;');
}
else {
self.jsdc.appendBefore(top.state + '=' + endTemp);
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':');
}
});
break;
default:
eventbus.on(itstmt.leaf(4).nid(), function(node, start) {
if(start) {
top = self.hash[nid];
loopTemp = ++top.index2;
self.jsdc.append('case ' + loopTemp + ':');
self.jsdc.append(top.state + '=');
//防止优先级错误
if(itstmt.leaf(4).name() == JsNode.ASSIGNEXPR) {
self.jsdc.append('(');
}
itTemp = ++top.index2;
}
else {
if(itstmt.leaf(4).name() == JsNode.ASSIGNEXPR) {
self.jsdc.appendBefore(')');
}
itEndTemp = ++top.index2;
endTemp = ++top.index2;
self.jsdc.appendBefore('?' + itTemp + ':' + itEndTemp);
self.jsdc.appendBefore(';break');
//供yield判断
itstmt.done = join(itstmt.leaf(4));
}
});
eventbus.on(itstmt.leaf(6).nid(), function(node, start) {
if(start) {
self.jsdc.append('case ' + endTemp + ':');
}
else {
self.jsdc.appendBefore(';' + top.state + '=' + loopTemp);
self.jsdc.appendBefore(';break');
}
});
eventbus.on(block.nid(), function(node, start) {
if(start) {
self.jsdc.append(';case ' + itTemp + ':');
}
else {
self.jsdc.appendBefore(top.state + '=' + endTemp);
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':');
}
});
}
break;
case 'while':
self.jsdc.ignore(itstmt.first());
loopTemp = self.jsdc.uid();
var block = itstmt.last();
if(block.name() == JsNode.BLOCKSTMT) {
self.jsdc.ignore(block.first().first(), 'gen32');
self.jsdc.ignore(block.first().last(), 'gen33');
}
eventbus.on(itstmt.nid(), function(node, start) {
if(start) {
self.jsdc.append('var ' + loopTemp + ';');
top = self.hash[nid];
endTemp = ++top.index2;
self.jsdc.append('case ' + endTemp + ':');
self.jsdc.append(top.state + '=');
}
});
eventbus.on(itstmt.leaf(2).nid(), function(node, start) {
if(start) {
self.jsdc.append(loopTemp + '=');
}
});
eventbus.on(block.nid(), function(node, start) {
if(start) {
self.jsdc.append('case ' + temp + ':');
itTemp = ++top.index2;
itEndTemp = ++top.index2;
self.jsdc.append('?' + itTemp + ':' + itEndTemp);
self.jsdc.append(';break;case ' + itTemp + ':');
//供yield判断
itstmt.done = loopTemp;
}
else {
self.jsdc.appendBefore(top.state + '=' + endTemp);
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':');
}
});
break;
case 'do':
loopTemp = self.jsdc.uid();
//供yield判断
itstmt.done = loopTemp;
self.jsdc.ignore(itstmt.first());
self.jsdc.ignore(itstmt.leaf(2));
var block = itstmt.leaf(1);
if(block.name() == JsNode.BLOCKSTMT) {
self.jsdc.ignore(block.first().first(), 'gen34');
self.jsdc.ignore(block.first().last(), 'gen35');
}
eventbus.on(itstmt.nid(), function(node, start) {
if(start) {
self.jsdc.append('var ' + loopTemp + ';');
}
else {
self.jsdc.appendBefore('?' + itTemp + ':' + itEndTemp);
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':');
}
});
eventbus.on(itstmt.leaf(4).nid(), function(node, start) {
if(start) {
self.jsdc.append(loopTemp + '=');
}
});
eventbus.on(block.nid(), function(node, start) {
if(start) {
top = self.hash[nid];
itTemp = ++top.index2;
endTemp = ++top.index2;
self.jsdc.append('case ' + itTemp + ':');
}
else {
if(!self.jsdc.endsWith(';')
&& !self.jsdc.endsWith(':')
&& !self.jsdc.endsWith('{')
&& !self.jsdc.endsWith('}')
&& !self.jsdc.endsWith('\n')) {
self.jsdc.appendBefore(';');
}
self.jsdc.appendBefore(top.state + '=' + endTemp);
itEndTemp = ++top.index2;
self.jsdc.appendBefore(';break;case ' + endTemp + ':');
self.jsdc.appendBefore(top.state + '=');
self.jsdc.appendBefore(ifEndTemp);
self.jsdc.appendBefore('break;');
}
});
}
break;
}

@@ -352,2 +669,16 @@ break;

},
belong: function(node) {
var res = [];
while(node = node.parent()) {
switch(node.name()) {
case JsNode.IFSTMT:
case JsNode.ITERSTMT:
res.push(node);
case JsNode.GENDECL:
case JsNode.GENEXPR:
break;
}
}
return res;
},
getLast: function(node) {

@@ -371,3 +702,3 @@ while(node = node.last()) {

if(token.content() == value) {
this.jsdc.ignore(token, 'gen23');
this.jsdc.ignore(token, 'gen36');
return;

@@ -380,16 +711,2 @@ }

}
},
belong: function(node) {
var res = [];
while(node = node.parent()) {
switch(node.name()) {
case JsNode.IFSTMT:
case JsNode.ITERSTMT:
res.push(node);
case JsNode.GENDECL:
case JsNode.GENEXPR:
break;
}
}
return res;
}

@@ -396,0 +713,0 @@ });

@@ -88,2 +88,3 @@ var homunculus = require('homunculus');

append: function(s) {
s = String(s);
this.res += s;

@@ -93,2 +94,3 @@ this.i = this.res.length;

appendBefore: function(s) {
s = String(s);
if(this.i < this.res.length) {

@@ -103,2 +105,3 @@ this.insert(s, this.i);

insert: function(s, i) {
s = String(s);
this.res = this.res.slice(0, i) + s + this.res.slice(i);

@@ -110,2 +113,11 @@ },

},
endsWith: function(s) {
s = String(s);
if(this.i < this.res.length) {
return this.res.slice(this.i - s.length, this.i) == s;
}
else {
return this.res.slice(this.res.length - s.length) == s;
}
},
next: function() {

@@ -155,2 +167,6 @@ var i = ++this.index;

else if(token.type() == Token.KEYWORD
&& content == 'return') {
eventbus.emit(node.nid(), [node, true]);
}
else if(token.type() == Token.KEYWORD
&& content == 'super'){

@@ -185,2 +201,5 @@ this.klass.super(node);

}
else if(content == 'return') {
eventbus.emit(node.nid(), [node]);
}
}

@@ -187,0 +206,0 @@ var ignore = token.ignore;

@@ -14,2 +14,6 @@ define(function(require, exports, module) {

parse: function(node, start) {
//有可能被Generator中该写过
if(node.gen) {
return;
}
if(start) {

@@ -20,3 +24,6 @@ var of = node.leaf(3);

&& of.token().content() == 'of') {
this.hash[node.nid()] = true;
//存放临时li供block首尾改写引用
this.hash[node.nid()] = {
temp: this.jsdc.uid()
};
this.jsdc.ignore(of, 'forof1');

@@ -27,5 +34,19 @@ }

var last = node.last();
var s = '';
if(!this.jsdc.endsWith(';')
&& !this.jsdc.endsWith(':')
&& !this.jsdc.endsWith('{')
&& !this.jsdc.endsWith('\n')) {
s = ';';
}
s += this.hash[node.nid()].id + '=' + this.hash[node.nid()].temp;
if(last.name() != JsNode.BLOCKSTMT) {
//临时引用写回,使循环正常
this.jsdc.appendBefore(s);
//}闭合
this.jsdc.appendBefore('}');
}
else {
this.jsdc.insert(s, this.jsdc.res.length - 1);
}
}

@@ -41,3 +62,2 @@ },

prts: function(node, start) {
//for of的语句如果省略{}则加上
var parent = node.parent();

@@ -61,5 +81,6 @@ if(parent.name() == JsNode.ITERSTMT

else {
//for of的语句如果省略{}则加上
var last = parent.last();
if(last.name() != JsNode.BLOCKSTMT) {
this.jsdc.append('{');
this.jsdc.appendBefore('{');
this.assign(parent);

@@ -92,3 +113,7 @@ }

}
this.jsdc.append(k + '=' + k + '.value;');
//先存下临时引用
var o = this.hash[node.nid()];
o.id = k;
this.jsdc.append('var ' + o.temp + '=' + k + ';');
this.jsdc.append(k + '=' + o.temp + '.value;');
}

@@ -98,3 +123,2 @@ });

module.exports = Forof;
});

@@ -147,3 +147,23 @@ define(function(require, exports, module) {

else {
self.jsdc.appendBefore(',done:' + (o.index == o.count) + '}');
var belong = self.belong(node);
if(belong.length) {
var done = belong.map(function(o) {
return o.done || 0;
});
done.push((o.index == o.count) ? 1 : 0);
done.push((o.return) ? 0 : 1);
if(done.indexOf(0) > -1) {
done = [false];
}
var hash = {};
done = done.filter(function(o) {
var res = !hash.hasOwnProperty(o);
hash[o] = true;
return res;
});
self.jsdc.appendBefore(',done:' + done.join('&&') + '}');
}
else {
self.jsdc.appendBefore(',done:' + (o.index == o.count && !o.return) + '}');
}
o.yield.push({

@@ -178,3 +198,10 @@ i: self.jsdc.i

if(o.count) {
this.jsdc.appendBefore(';' + o.state + '=-1');
if(!this.jsdc.endsWith(';')
&& !this.jsdc.endsWith(':')
&& !this.jsdc.endsWith('{')
&& !this.jsdc.endsWith('}')
&& !this.jsdc.endsWith('\n')) {
this.jsdc.appendBefore(';');
}
this.jsdc.appendBefore(o.state + '=-1');
this.jsdc.appendBefore(';default:return{done:true}}}');

@@ -238,10 +265,26 @@ }

});
eventbus.on(node.leaf(1).nid(), function(node, start) {
if(start) {
self.jsdc.append('{value:');
}
else {
self.jsdc.appendBefore(',done:true}');
}
});
//无return内容分开侦听
if(node.leaf(1).name() == JsNode.TOKEN) {
eventbus.on(node.leaf(0).nid(), function(node, start) {
if(!start) {
self.jsdc.append('{value:');
}
});
eventbus.on(node.nid(), function(node, start) {
if(!start) {
self.jsdc.appendBefore(',done:true}');
}
});
}
//有则侦听内容
else {
eventbus.on(node.leaf(1).nid(), function(node, start) {
if(start) {
self.jsdc.append('{value:');
}
else {
self.jsdc.appendBefore(',done:true}');
}
});
}
break;

@@ -272,9 +315,10 @@ //忽略这些节点中的yield语句

if(self.stmt.hasOwnProperty(node.nid())) {
var ifstmt = node;
res.index++;
var block = node.leaf(4);
var block = ifstmt.leaf(4);
//改写if语句
self.jsdc.ignore(node.first(), 'gen14');
self.jsdc.ignore(node.leaf(1), 'gen15');
self.jsdc.ignore(node.leaf(2), 'gen16');
self.jsdc.ignore(node.leaf(3), 'gen17');
self.jsdc.ignore(ifstmt.first(), 'gen14');
self.jsdc.ignore(ifstmt.leaf(1), 'gen15');
self.jsdc.ignore(ifstmt.leaf(2), 'gen16');
self.jsdc.ignore(ifstmt.leaf(3), 'gen17');
if(block.name() == JsNode.BLOCKSTMT) {

@@ -284,9 +328,20 @@ self.jsdc.ignore(block.first().first(), 'gen18');

}
var temp;
var elseTemp;
var ifEndTemp;
var top;
var ifstmt = node;
var elset = block.next();
var elseblock;
if(elset && elset.name() == JsNode.TOKEN) {
elseblock = elset.next();
}
//if结束后的状态
eventbus.on(node.nid(), function(node, start) {
eventbus.on(ifstmt.nid(), function(node, start) {
if(!start) {
if(!self.jsdc.endsWith(';')
&& !self.jsdc.endsWith(':')
&& !self.jsdc.endsWith('{')
&& !self.jsdc.endsWith('}')
&& !self.jsdc.endsWith('\n')) {
self.jsdc.appendBefore(';');
}
self.jsdc.append('case ' + ifEndTemp + ':');

@@ -303,8 +358,16 @@ }

self.jsdc.append('?');
self.jsdc.append(++top.index2 + ':' + ++top.index2 + ';break;');
self.jsdc.append('case ' + (top.index2 - 1) + ':');
temp = top.index2;
self.jsdc.append(++top.index2 + ':');
self.jsdc.append((elseblock ? ++top.index2 : top.index2 + 1) + ';break;');
self.jsdc.append('case ' + (elseblock ? top.index2 - 1 : top.index2) + ':');
elseTemp = top.index2;
ifEndTemp = ++top.index2;
}
else {
if(!self.jsdc.endsWith(';')
&& !self.jsdc.endsWith(':')
&& !self.jsdc.endsWith('{')
&& !self.jsdc.endsWith('}')
&& !self.jsdc.endsWith('\n')) {
self.jsdc.appendBefore(';');
}
self.jsdc.appendBefore(top.state + '=');

@@ -316,21 +379,275 @@ self.jsdc.appendBefore(ifEndTemp);

//else语句忽略{}
var elset = block.next();
if(elset && elset.name() == JsNode.TOKEN) {
if(elseblock) {
self.jsdc.ignore(elset, 'gen20');
block = elset.next();
if(elseblock.name() == JsNode.BLOCKSTMT) {
self.jsdc.ignore(elseblock.first().first(), 'gen21');
self.jsdc.ignore(elseblock.first().last(), 'gen22');
}
eventbus.on(elseblock.nid(), function(node, start) {
if(start) {
self.jsdc.append('case ' + elseTemp + ':');
}
else if(elseblock.name() != JsNode.IFSTMT
&& elseblock.parent().name() != JsNode.IFSTMT) {
if(!self.jsdc.endsWith(';')
&& !self.jsdc.endsWith(':')
&& !self.jsdc.endsWith('{')
&& !self.jsdc.endsWith('}')
&& !self.jsdc.endsWith('\n')) {
self.jsdc.appendBefore(';');
}
self.jsdc.appendBefore(top.state + '=');
self.jsdc.appendBefore(ifEndTemp);
self.jsdc.appendBefore(';break;');
}
});
}
}
break;
case JsNode.ITERSTMT:
var itstmt = node;
var first = itstmt.first();
var top;
var loopTemp;
var itTemp;
var itEndTemp;
var endTemp;
switch(first.token().content()) {
case 'for':
var block = itstmt.last();
if(block.name() == JsNode.BLOCKSTMT) {
self.jsdc.ignore(block.first().first(), 'gen21');
self.jsdc.ignore(block.first().last(), 'gen22');
self.jsdc.ignore(block.first().first(), 'gen23');
self.jsdc.ignore(block.first().last(), 'gen24');
}
self.jsdc.ignore(first, 'gen25');
self.jsdc.ignore(node.leaf(1), 'gen26');
self.jsdc.ignore(block.prev(), 'gen27');
switch(itstmt.leaf(3).token().content()) {
case 'in':
var keys = self.jsdc.uid();
var len = self.jsdc.uid();
var index = self.jsdc.uid();
var id;
if(node.leaf(2).name() == JsNode.VARSTMT) {
id = join(node.leaf(2).last());
}
else {
id = join(node.leaf(2));
}
var obj = self.jsdc.uid();
self.jsdc.ignore(node.leaf(2), 'gen28');
self.jsdc.ignore(node.leaf(3), 'gen29');
eventbus.on(itstmt.leaf(4).nid(), function(node, start) {
if(start) {
self.jsdc.append('var ' + obj + '=');
top = self.hash[nid];
}
else {
self.jsdc.appendBefore(',' + keys + '=Object.keys(');
self.jsdc.appendBefore(obj);
self.jsdc.appendBefore('),' + len);
self.jsdc.appendBefore('=' + keys + '.length,');
self.jsdc.appendBefore(index + '=0;');
endTemp = ++top.index2;
self.jsdc.appendBefore('case ' + endTemp + ':');
self.jsdc.appendBefore(top.state + '=');
self.jsdc.appendBefore(index + '++<' + len + '?');
itTemp = ++top.index2;
itEndTemp = ++top.index2;
self.jsdc.appendBefore(itTemp + ':' + itEndTemp + ';break;');
//供yield判断
itstmt.done = index + '<' + len;
}
});
eventbus.on(block.nid(), function(node, start) {
if(start) {
self.jsdc.append('case ' + itTemp + ':');
self.jsdc.append(id + '=');
self.jsdc.append(keys + '[');
self.jsdc.append(index + '];');
}
else {
self.jsdc.appendBefore(top.state + '=' + endTemp);
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':');
}
});
break;
case 'of':
var iterator;
//标记使Forof类处理失效
itstmt.gen = true;
var next = self.jsdc.uid();
var id;
if(node.leaf(2).name() == JsNode.VARSTMT) {
id = join(node.leaf(2).last());
}
else {
id = join(node.leaf(2));
}
var obj = self.jsdc.uid();
self.jsdc.ignore(node.leaf(2), 'gen30');
self.jsdc.ignore(node.leaf(3), 'gen31');
eventbus.on(itstmt.leaf(4).nid(), function(node, start) {
if(start) {
self.jsdc.append('var ' + obj + '=');
top = self.hash[nid];
}
else {
self.jsdc.appendBefore(',' + next);
self.jsdc.appendBefore('=' + obj + '.next();');
endTemp = ++top.index2;
self.jsdc.appendBefore('case ' + endTemp + ':');
self.jsdc.appendBefore(top.state + '=');
self.jsdc.appendBefore(next + '.done' + '?');
iterator = ++top.index2;
itTemp = ++top.index2;
itEndTemp = ++top.index2;
self.jsdc.appendBefore(itTemp + ':' + itEndTemp + ';break;');
//供yield判断
itstmt.done = next + '.done';
}
});
eventbus.on(block.nid(), function(node, start) {
if(start) {
self.jsdc.append('case ' + iterator + ':');
self.jsdc.append(next + '=' + obj + '.next();');
self.jsdc.append(top.state + '=' + endTemp);
self.jsdc.append(';break;case ' + itTemp + ':');
self.jsdc.append(id + '=' + next + '.value;');
}
else {
self.jsdc.appendBefore(top.state + '=' + endTemp);
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':');
}
});
break;
default:
eventbus.on(itstmt.leaf(4).nid(), function(node, start) {
if(start) {
top = self.hash[nid];
loopTemp = ++top.index2;
self.jsdc.append('case ' + loopTemp + ':');
self.jsdc.append(top.state + '=');
//防止优先级错误
if(itstmt.leaf(4).name() == JsNode.ASSIGNEXPR) {
self.jsdc.append('(');
}
itTemp = ++top.index2;
}
else {
if(itstmt.leaf(4).name() == JsNode.ASSIGNEXPR) {
self.jsdc.appendBefore(')');
}
itEndTemp = ++top.index2;
endTemp = ++top.index2;
self.jsdc.appendBefore('?' + itTemp + ':' + itEndTemp);
self.jsdc.appendBefore(';break');
//供yield判断
itstmt.done = join(itstmt.leaf(4));
}
});
eventbus.on(itstmt.leaf(6).nid(), function(node, start) {
if(start) {
self.jsdc.append('case ' + endTemp + ':');
}
else {
self.jsdc.appendBefore(';' + top.state + '=' + loopTemp);
self.jsdc.appendBefore(';break');
}
});
eventbus.on(block.nid(), function(node, start) {
if(start) {
self.jsdc.append(';case ' + itTemp + ':');
}
else {
self.jsdc.appendBefore(top.state + '=' + endTemp);
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':');
}
});
}
break;
case 'while':
self.jsdc.ignore(itstmt.first());
loopTemp = self.jsdc.uid();
var block = itstmt.last();
if(block.name() == JsNode.BLOCKSTMT) {
self.jsdc.ignore(block.first().first(), 'gen32');
self.jsdc.ignore(block.first().last(), 'gen33');
}
eventbus.on(itstmt.nid(), function(node, start) {
if(start) {
self.jsdc.append('var ' + loopTemp + ';');
top = self.hash[nid];
endTemp = ++top.index2;
self.jsdc.append('case ' + endTemp + ':');
self.jsdc.append(top.state + '=');
}
});
eventbus.on(itstmt.leaf(2).nid(), function(node, start) {
if(start) {
self.jsdc.append(loopTemp + '=');
}
});
eventbus.on(block.nid(), function(node, start) {
if(start) {
self.jsdc.append('case ' + temp + ':');
itTemp = ++top.index2;
itEndTemp = ++top.index2;
self.jsdc.append('?' + itTemp + ':' + itEndTemp);
self.jsdc.append(';break;case ' + itTemp + ':');
//供yield判断
itstmt.done = loopTemp;
}
else {
self.jsdc.appendBefore(top.state + '=' + endTemp);
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':');
}
});
break;
case 'do':
loopTemp = self.jsdc.uid();
//供yield判断
itstmt.done = loopTemp;
self.jsdc.ignore(itstmt.first());
self.jsdc.ignore(itstmt.leaf(2));
var block = itstmt.leaf(1);
if(block.name() == JsNode.BLOCKSTMT) {
self.jsdc.ignore(block.first().first(), 'gen34');
self.jsdc.ignore(block.first().last(), 'gen35');
}
eventbus.on(itstmt.nid(), function(node, start) {
if(start) {
self.jsdc.append('var ' + loopTemp + ';');
}
else {
self.jsdc.appendBefore('?' + itTemp + ':' + itEndTemp);
self.jsdc.appendBefore(';break;case ' + itEndTemp + ':');
}
});
eventbus.on(itstmt.leaf(4).nid(), function(node, start) {
if(start) {
self.jsdc.append(loopTemp + '=');
}
});
eventbus.on(block.nid(), function(node, start) {
if(start) {
top = self.hash[nid];
itTemp = ++top.index2;
endTemp = ++top.index2;
self.jsdc.append('case ' + itTemp + ':');
}
else {
if(!self.jsdc.endsWith(';')
&& !self.jsdc.endsWith(':')
&& !self.jsdc.endsWith('{')
&& !self.jsdc.endsWith('}')
&& !self.jsdc.endsWith('\n')) {
self.jsdc.appendBefore(';');
}
self.jsdc.appendBefore(top.state + '=' + endTemp);
itEndTemp = ++top.index2;
self.jsdc.appendBefore(';break;case ' + endTemp + ':');
self.jsdc.appendBefore(top.state + '=');
self.jsdc.appendBefore(ifEndTemp);
self.jsdc.appendBefore('break;');
}
});
}
break;
}

@@ -353,2 +670,16 @@ break;

},
belong: function(node) {
var res = [];
while(node = node.parent()) {
switch(node.name()) {
case JsNode.IFSTMT:
case JsNode.ITERSTMT:
res.push(node);
case JsNode.GENDECL:
case JsNode.GENEXPR:
break;
}
}
return res;
},
getLast: function(node) {

@@ -372,3 +703,3 @@ while(node = node.last()) {

if(token.content() == value) {
this.jsdc.ignore(token, 'gen23');
this.jsdc.ignore(token, 'gen36');
return;

@@ -381,16 +712,2 @@ }

}
},
belong: function(node) {
var res = [];
while(node = node.parent()) {
switch(node.name()) {
case JsNode.IFSTMT:
case JsNode.ITERSTMT:
res.push(node);
case JsNode.GENDECL:
case JsNode.GENEXPR:
break;
}
}
return res;
}

@@ -397,0 +714,0 @@ });

@@ -89,2 +89,3 @@ define(function(require, exports, module) {

append: function(s) {
s = String(s);
this.res += s;

@@ -94,2 +95,3 @@ this.i = this.res.length;

appendBefore: function(s) {
s = String(s);
if(this.i < this.res.length) {

@@ -104,2 +106,3 @@ this.insert(s, this.i);

insert: function(s, i) {
s = String(s);
this.res = this.res.slice(0, i) + s + this.res.slice(i);

@@ -111,2 +114,11 @@ },

},
endsWith: function(s) {
s = String(s);
if(this.i < this.res.length) {
return this.res.slice(this.i - s.length, this.i) == s;
}
else {
return this.res.slice(this.res.length - s.length) == s;
}
},
next: function() {

@@ -156,2 +168,6 @@ var i = ++this.index;

else if(token.type() == Token.KEYWORD
&& content == 'return') {
eventbus.emit(node.nid(), [node, true]);
}
else if(token.type() == Token.KEYWORD
&& content == 'super'){

@@ -186,2 +202,5 @@ this.klass.super(node);

}
else if(content == 'return') {
eventbus.emit(node.nid(), [node]);
}
}

@@ -188,0 +207,0 @@ var ignore = token.ignore;

Sorry, the diff of this file is not supported yet

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