Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "jsdc", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "transform ecmascript6 to ecmascript5", | ||
@@ -5,0 +5,0 @@ "maintainers": [ |
@@ -5,3 +5,2 @@ ##transform ecmascript6 to ecmascript5 | ||
server目录下为nodejs环境的module模块; | ||
java目录为性能考虑后续提供的java版本。 | ||
@@ -18,3 +17,3 @@ 如果想直接用npm安装,请执行npm install jsdc | ||
由于js性能问题,解析大文件会造成耗时,所以良好的做法时解析前先在服务器端检查此js文件有无变更,有之后再调用jsdc,否则读取缓存。 | ||
由于js性能问题,解析大文件会造成耗时,所以良好的做法是解析前先在服务器端检查此js文件有无变更,有之后再调用jsdc,否则读取缓存。并且一些明显的无需转换的全局库文件也应该忽略,如jQuery、YUI。 | ||
@@ -27,8 +26,12 @@ ##API | ||
jsdc.tree():Object | ||
jsdc.tree():Node | ||
获取解析后的语法树。此为内部接口,一般用不到。 | ||
jsdc.token():Array<Token> | ||
获取解析后的词法单元列表。此为内部接口,一般用不到。 | ||
# License | ||
[MIT License] |
@@ -9,2 +9,4 @@ var Lexer = require('./lexer/Lexer'), | ||
temp, | ||
fold, | ||
foldIndex, | ||
bindId, | ||
@@ -18,3 +20,4 @@ bind, | ||
res, | ||
node; | ||
node, | ||
token; | ||
@@ -27,2 +30,4 @@ function init() { | ||
temp = ''; | ||
fold = null; | ||
foldIndex = 0; | ||
bind = ''; | ||
@@ -55,4 +60,4 @@ rest = ''; | ||
//varǰ����������� | ||
if(node.name() == Node.VARSTMT) { | ||
preVar(node); | ||
if(node.name() == Node.VARDECL) { | ||
vardecl(true, node); | ||
} | ||
@@ -123,4 +128,8 @@ //��¼������������ջ����Ĭ�ϲ���ʡ�Ը�ֵ�������� | ||
}); | ||
//�Զ�չ����ֵ | ||
if(node.name() == Node.VARDECL && fold) { | ||
vardecl(false, node); | ||
} | ||
//fnbody�������������ջ | ||
if(node.name() == Node.FNBODY) { | ||
else if(node.name() == Node.FNBODY) { | ||
env.pop(); | ||
@@ -158,7 +167,44 @@ } | ||
} | ||
function preVar(varstmt) { | ||
function vardecl(startOrEnd, vardecl) { | ||
var index = env[env.length - 1]; | ||
preHash[index] = preHash[index] || {}; | ||
for(var i = 1, leaves = varstmt.leaves(), len = leaves.length; i < len; i += 2) { | ||
var vn = leaves[i].leaves()[0].token().content(); | ||
if(startOrEnd) { | ||
preHash[index] = preHash[index] || {}; | ||
var vn = vardecl.leaves()[0]; | ||
if(vn.name() == Node.TOKEN) { | ||
preVar(vn.token().content()); | ||
} | ||
//�Զ�չ����ֵ | ||
else if(vn.name() == Node.ARRLTR) { | ||
fold = []; | ||
for(var prms = vn.leaves(), j = 1, l = prms.length - 1; j < l; j += 2) { | ||
vn = prms[j].leaves()[0].token().content(); | ||
fold.push(vn); | ||
preVar(vn); | ||
} | ||
foldIndex = res.length; | ||
} | ||
} | ||
else { | ||
//ȥ��[]�������ͺ����= | ||
var end = res.indexOf(']', foldIndex), | ||
prefix = res.slice(0, foldIndex), | ||
suffix = res.slice(end + 1); | ||
end = suffix.indexOf('='); | ||
prefix += suffix.slice(0, end); | ||
suffix = suffix.slice(end + 1); | ||
res = prefix + suffix; | ||
//����forEach | ||
end = /([;\s\r\n])*$/.exec(res)[1]; | ||
end = res.length - end.length; | ||
prefix = res.slice(0, end); | ||
suffix = res.slice(end); | ||
prefix += '.forEach(function(o, i) { '; | ||
fold.forEach(function(o, i) { | ||
prefix += 'if(i == ' + i + ') ' + o + ' = o; ' | ||
}); | ||
prefix += '})'; | ||
res = prefix + suffix; | ||
fold = null; | ||
} | ||
function preVar(vn) { | ||
if(preHash[index][vn]) { | ||
@@ -173,2 +219,4 @@ return; | ||
} | ||
function varfold(varstmt) { | ||
} | ||
function block(startOrEnd) { | ||
@@ -325,7 +373,7 @@ var index = startOrEnd ? res.lastIndexOf('{') + 1 : res.lastIndexOf('}'); | ||
exports.parse = function(code) { | ||
var lexer = new Lexer(new EcmascriptRule()); | ||
lexer.parse(code); | ||
var parser = new Parser(lexer), | ||
var lexer = new Lexer(new EcmascriptRule()), | ||
parser = new Parser(lexer), | ||
ignore = {}; | ||
try { | ||
token = lexer.parse(code); | ||
node = parser.program(); | ||
@@ -353,1 +401,4 @@ ignore = parser.ignore(); | ||
}; | ||
exports.token = function() { | ||
return token; | ||
}; |
@@ -53,3 +53,3 @@ var Class = require('../util/Class'), | ||
if(match.match(this.peek, this.code, this.index)) { | ||
var token = new Token(match.tokenType(), match.content(), match.val()), | ||
var token = new Token(match.tokenType(), match.content()), | ||
error = match.error(), | ||
@@ -63,3 +63,3 @@ matchLen = match.content().length; | ||
this.index += matchLen - 1; | ||
var n = character.count(token.val(), character.LINE); | ||
var n = character.count(token.content(), character.LINE); | ||
count += n; | ||
@@ -66,0 +66,0 @@ this.totalLine += n; |
var Class = require('../util/Class'), | ||
character = require('../util/character'), | ||
types, | ||
Token = Class(function(type, content, val) { | ||
Token = Class(function(type, content) { | ||
this.t = type; | ||
this.c = content; | ||
if(character.isUndefined(val)) { | ||
val = content; | ||
} | ||
this.v = val; | ||
}).methods({ | ||
@@ -21,5 +17,2 @@ type: function(t) { | ||
}, | ||
val: function() { | ||
return this.v; | ||
}, | ||
tag: function() { | ||
@@ -26,0 +19,0 @@ return Token.type(this.t); |
@@ -9,3 +9,3 @@ var Class = require('../util/Class'), | ||
this.look = null; | ||
this.tokens = lexer.tokens(); | ||
this.tokens = null; | ||
this.lastLine = 1; | ||
@@ -16,11 +16,13 @@ this.lastCol = 1; | ||
this.index = 0; | ||
this.length = this.tokens.length; | ||
this.length = 0; | ||
this.inFor = false; | ||
this.ignores = {}; | ||
if(this.tokens.length) { | ||
this.move(); | ||
} | ||
this.hasMoveLine = false; | ||
}).methods({ | ||
program: function() { | ||
this.tokens = this.lexer.tokens(); | ||
this.length = this.tokens.length; | ||
if(this.tokens.length) { | ||
this.move(); | ||
} | ||
var node = new Node(Node.PROGRAM); | ||
@@ -169,3 +171,11 @@ while(this.look) { | ||
var node = new Node(Node.VARDECL); | ||
node.add(this.match(Token.ID, 'missing variable name')); | ||
if(!this.look) { | ||
this.error('missing variable name'); | ||
} | ||
if(this.look.content() == '[') { | ||
node.add(this.arrltr()); | ||
} | ||
else { | ||
node.add(this.match(Token.ID, 'missing variable name')); | ||
} | ||
if(this.look && this.look.content() == '=') { | ||
@@ -1039,3 +1049,3 @@ node.add(this.assign()); | ||
case 'false': | ||
node.add(this.match()); | ||
return this.match(); | ||
break; | ||
@@ -1046,6 +1056,6 @@ case '(': | ||
case '[': | ||
node.add(this.arrltr()); | ||
return this.arrltr(); | ||
break; | ||
case '{': | ||
node.add(this.objltr()); | ||
return this.objltr(); | ||
break; | ||
@@ -1052,0 +1062,0 @@ default: |
@@ -1,2 +0,2 @@ | ||
var jsdc = require('./jsdc'); | ||
var jsdc = require('../server/jsdc'); | ||
var fs = require('fs'); | ||
@@ -3,0 +3,0 @@ |
@@ -10,2 +10,4 @@ define(function(require, exports) { | ||
temp, | ||
fold, | ||
foldIndex, | ||
bindId, | ||
@@ -19,3 +21,4 @@ bind, | ||
res, | ||
node; | ||
node, | ||
token; | ||
@@ -28,2 +31,4 @@ function init() { | ||
temp = ''; | ||
fold = null; | ||
foldIndex = 0; | ||
bind = ''; | ||
@@ -56,4 +61,4 @@ rest = ''; | ||
//varǰ����������� | ||
if(node.name() == Node.VARSTMT) { | ||
preVar(node); | ||
if(node.name() == Node.VARDECL) { | ||
vardecl(true, node); | ||
} | ||
@@ -124,4 +129,8 @@ //��¼������������ջ����Ĭ�ϲ���ʡ�Ը�ֵ�������� | ||
}); | ||
//�Զ�չ����ֵ | ||
if(node.name() == Node.VARDECL && fold) { | ||
vardecl(false, node); | ||
} | ||
//fnbody�������������ջ | ||
if(node.name() == Node.FNBODY) { | ||
else if(node.name() == Node.FNBODY) { | ||
env.pop(); | ||
@@ -159,7 +168,44 @@ } | ||
} | ||
function preVar(varstmt) { | ||
function vardecl(startOrEnd, vardecl) { | ||
var index = env[env.length - 1]; | ||
preHash[index] = preHash[index] || {}; | ||
for(var i = 1, leaves = varstmt.leaves(), len = leaves.length; i < len; i += 2) { | ||
var vn = leaves[i].leaves()[0].token().content(); | ||
if(startOrEnd) { | ||
preHash[index] = preHash[index] || {}; | ||
var vn = vardecl.leaves()[0]; | ||
if(vn.name() == Node.TOKEN) { | ||
preVar(vn.token().content()); | ||
} | ||
//�Զ�չ����ֵ | ||
else if(vn.name() == Node.ARRLTR) { | ||
fold = []; | ||
for(var prms = vn.leaves(), j = 1, l = prms.length - 1; j < l; j += 2) { | ||
vn = prms[j].leaves()[0].token().content(); | ||
fold.push(vn); | ||
preVar(vn); | ||
} | ||
foldIndex = res.length; | ||
} | ||
} | ||
else { | ||
//ȥ��[]�������ͺ����= | ||
var end = res.indexOf(']', foldIndex), | ||
prefix = res.slice(0, foldIndex), | ||
suffix = res.slice(end + 1); | ||
end = suffix.indexOf('='); | ||
prefix += suffix.slice(0, end); | ||
suffix = suffix.slice(end + 1); | ||
res = prefix + suffix; | ||
//����forEach | ||
end = /([;\s\r\n])*$/.exec(res)[1]; | ||
end = res.length - end.length; | ||
prefix = res.slice(0, end); | ||
suffix = res.slice(end); | ||
prefix += '.forEach(function(o, i) { '; | ||
fold.forEach(function(o, i) { | ||
prefix += 'if(i == ' + i + ') ' + o + ' = o; ' | ||
}); | ||
prefix += '})'; | ||
res = prefix + suffix; | ||
fold = null; | ||
} | ||
function preVar(vn) { | ||
if(preHash[index][vn]) { | ||
@@ -174,2 +220,4 @@ return; | ||
} | ||
function varfold(varstmt) { | ||
} | ||
function block(startOrEnd) { | ||
@@ -326,7 +374,7 @@ var index = startOrEnd ? res.lastIndexOf('{') + 1 : res.lastIndexOf('}'); | ||
exports.parse = function(code) { | ||
var lexer = new Lexer(new EcmascriptRule()); | ||
lexer.parse(code); | ||
var parser = new Parser(lexer), | ||
var lexer = new Lexer(new EcmascriptRule()), | ||
parser = new Parser(lexer), | ||
ignore = {}; | ||
try { | ||
token = lexer.parse(code); | ||
node = parser.program(); | ||
@@ -354,2 +402,5 @@ ignore = parser.ignore(); | ||
}; | ||
exports.token = function() { | ||
return token; | ||
}; | ||
}); |
@@ -54,3 +54,3 @@ define(function(require, exports, module) { | ||
if(match.match(this.peek, this.code, this.index)) { | ||
var token = new Token(match.tokenType(), match.content(), match.val()), | ||
var token = new Token(match.tokenType(), match.content()), | ||
error = match.error(), | ||
@@ -64,3 +64,3 @@ matchLen = match.content().length; | ||
this.index += matchLen - 1; | ||
var n = character.count(token.val(), character.LINE); | ||
var n = character.count(token.content(), character.LINE); | ||
count += n; | ||
@@ -67,0 +67,0 @@ this.totalLine += n; |
@@ -5,9 +5,5 @@ define(function(require, exports, module) { | ||
types, | ||
Token = Class(function(type, content, val) { | ||
Token = Class(function(type, content) { | ||
this.t = type; | ||
this.c = content; | ||
if(character.isUndefined(val)) { | ||
val = content; | ||
} | ||
this.v = val; | ||
}).methods({ | ||
@@ -23,5 +19,2 @@ type: function(t) { | ||
}, | ||
val: function() { | ||
return this.v; | ||
}, | ||
tag: function() { | ||
@@ -28,0 +21,0 @@ return Token.type(this.t); |
@@ -10,3 +10,3 @@ define(function(require, exports, module) { | ||
this.look = null; | ||
this.tokens = lexer.tokens(); | ||
this.tokens = null; | ||
this.lastLine = 1; | ||
@@ -17,11 +17,13 @@ this.lastCol = 1; | ||
this.index = 0; | ||
this.length = this.tokens.length; | ||
this.length = 0; | ||
this.inFor = false; | ||
this.ignores = {}; | ||
if(this.tokens.length) { | ||
this.move(); | ||
} | ||
this.hasMoveLine = false; | ||
}).methods({ | ||
program: function() { | ||
this.tokens = this.lexer.tokens(); | ||
this.length = this.tokens.length; | ||
if(this.tokens.length) { | ||
this.move(); | ||
} | ||
var node = new Node(Node.PROGRAM); | ||
@@ -170,3 +172,11 @@ while(this.look) { | ||
var node = new Node(Node.VARDECL); | ||
node.add(this.match(Token.ID, 'missing variable name')); | ||
if(!this.look) { | ||
this.error('missing variable name'); | ||
} | ||
if(this.look.content() == '[') { | ||
node.add(this.arrltr()); | ||
} | ||
else { | ||
node.add(this.match(Token.ID, 'missing variable name')); | ||
} | ||
if(this.look && this.look.content() == '=') { | ||
@@ -1040,3 +1050,3 @@ node.add(this.assign()); | ||
case 'false': | ||
node.add(this.match()); | ||
return this.match(); | ||
break; | ||
@@ -1047,6 +1057,6 @@ case '(': | ||
case '[': | ||
node.add(this.arrltr()); | ||
return this.arrltr(); | ||
break; | ||
case '{': | ||
node.add(this.objltr()); | ||
return this.objltr(); | ||
break; | ||
@@ -1053,0 +1063,0 @@ default: |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
525954
49
14400
34
3