efront 4.0.3 → 4.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -121,27 +121,42 @@ var skipAssignment = function (o, cx) {
121
121
  break;
122
122
  case STRAP:
123
123
  if (needpunc) {
124
- if (/^catch$/.test(o.text)) {
125
- next();
126
- next();
127
- needpunc = false;
128
- break;
129
- }
130
- if (!/^(in|instanceof|of|else|as|finally)$/.test(o.text)) break loop;
124
+ if (!/^(in|instanceof|of|else|as)$/.test(o.text)) break loop;
131
125
  if (o.text === 'else') {
132
126
  if (!ifdeep) break loop;
133
127
  ifdeep--;
128
+ needpunc = false;
134
129
  }
135
-
136
130
  next();
137
131
  needpunc = false;
138
132
  }
139
- else if (/^(do|if|while|for|switch|with)$/.test(o.text)) {
133
+ else if (/^(try|catch|finally)$/.test(o.text)) {
134
+ next();
135
+ if (o.entry === "(") next();
136
+ next();
137
+ }
138
+ else if (o.text === 'else') {
139
+ ifdeep--;
140
+ needpunc = false;
141
+ next();
142
+ }
143
+ else if (/^(if|while|with|switch)$/.test(o.text)) {
140
144
  if (o.text === 'if') ifdeep++;
141
145
  next();
142
146
  next();
143
147
  break;
144
148
  }
149
+ else if (o.text === 'do') {
150
+ next();
151
+ next();
152
+ next();
153
+ }
154
+ else if (o.text === 'for') {
155
+ next();
156
+ if (o.type === STRAP && o.text === 'await') next();
157
+ next();
158
+ break;
159
+ }
145
160
  else if (o.text === "class") {
146
161
  next();
147
162
  while (o && !o.isClass) next();
@@ -163,9 +178,12 @@ var skipAssignment = function (o, cx) {
163
178
  needpunc = false;
164
179
  break loop;
165
180
  }
181
+ else if (o.isend && !ifdeep) {
182
+ next();
183
+ break loop;
184
+ }
166
185
  else {
167
186
  next();
168
- needpunc = false;
169
187
  }
170
188
  break;
171
189
  default:
@@ -191,7 +209,7 @@ function snapSentenceHead(o) {
191
209
  p = o.prev;
192
210
  if (!p) break;
193
211
  }
194
- var maybeprop = o.type === SCOPED && o.entry !== "{" || o.type === EXPRESS && /^\./.test(o.text);
212
+ var maybeprop = o.type === SCOPED && o.entry !== "{" || o.type === EXPRESS && /^[\.\[]/.test(o.text);
195
213
  if (p.type === EXPRESS) {
196
214
  if (maybeprop || /\.$/.test(p.text)) {
197
215
  o = p;
@@ -472,7 +490,7 @@ var createScoped = function (parsed, wash) {
472
490
  if (mustyield !== false && o.next) {
473
491
  if (o.next.type === STRAP && !/^(?:instanceof|in|of|from|as)$/.test(o.next.text)
474
492
  || o.next.type === STAMP && /[!~]/.test(o.next.text)
475
- || o.next.type === EXPRESS && /^\./.test(o.next.text)
493
+ || o.next.type === EXPRESS && !/^[\.\[]/.test(o.next.text)
476
494
  || o.next.type & (VALUE | QUOTED | SCOPED)
477
495
  ) {
478
496
  mustyield = true;
@@ -704,7 +722,7 @@ var createScoped = function (parsed, wash) {
704
722
  || (VALUE | QUOTED | SCOPED) & o.type
705
723
  || EXPRESS === o.type && !/\.$/.test(o.text)) {
706
724
  if ((VALUE | QUOTED | PROPERTY | LABEL) & next.type) break;
707
- if (EXPRESS === next.type && !/^\./.test(next.text)) break;
725
+ if (EXPRESS === next.type && !/^[\.\[]/.test(next.text)) break;
708
726
  if (next.type === SCOPED && next.entry === "{") break;
709
727
  if (next.type === STRAP && !next.isExpress) break;
710
728
  }
@@ -1079,7 +1097,7 @@ var createString = function (parsed) {
1079
1097
  break;
1080
1098
  default:
1081
1099
  if (o instanceof Object) {
1082
- if (o.prev && o.prev.type === EXPRESS && o.type === EXPRESS && (/^\./.test(o.text) || /\.$/.test(o.prev.text)));
1100
+ if (o.prev && o.prev.type === EXPRESS && o.type === EXPRESS && (/^[\.\[]/.test(o.text) || /\.$/.test(o.prev.text)));
1083
1101
  else if ((STRAP | EXPRESS | PROPERTY | COMMENT | VALUE) & lasttype && (STRAP | EXPRESS | PROPERTY | VALUE) & o.type) {
1084
1102
  result.push(" ");
1085
1103
  }
@@ -937,7 +937,7 @@ var unforin = function (o, getnewname_, killobj) {
937
937
  );
938
938
  insert1(o.queue, o.prev, ...s);
939
939
  splice(o, 0, o.length, ...scanner2(`${kname}=0;${kname}<${tname}.length&&`));
940
- var c = scanner2(`(${m.text}=${tname}[${kname}]);${kname}++`);
940
+ var c = scanner2(`(${m.text}=${tname}[${kname}],true);${kname}++`);
941
941
  insert1(o, null, ...c);
942
942
  };
943
943
 
@@ -1358,7 +1358,7 @@ var down = function (scoped) {
1358
1358
  if (!hp) break a;
1359
1359
  if (hp.text === 'for') {
1360
1360
  unforof(hp.next, getdeepname, scoped.used);
1361
- killed = unforin(scoped.head, getdeepname, _killobj.bind(null, _getlocal)) !== false;
1361
+ if (funcMark) killed = unforin(scoped.head, getdeepname, _killobj.bind(null, _getlocal)) !== false;
1362
1362
  // unforcx(scoped.head, getdeepname);
1363
1363
  }
1364
1364
  else if (hp.text === 'catch') {
@@ -1383,8 +1383,8 @@ var down = function (scoped) {
1383
1383
  if (funcMark) {
1384
1384
  var argname = _letname("_");
1385
1385
  unstruct.debug = downLevel.debug;
1386
- var code = unawait(scoped.body, _getname, argname);
1387
1386
  var body = scanner2(`return ${funcMark}()`);
1387
+ var code = unawait(scoped.body, _getname, argname);
1388
1388
  code.forEach(function (c) {
1389
1389
  var f = scanner2(`function(${c.awaited ? argname : ''}){\r\n}`);
1390
1390
  if (!c.length) f[2].push(...scanner2('return [1,0]'));
@@ -23,7 +23,7 @@ var _break = function (body, cx, result, iscontinue) {
23
23
  if (b.type === LABEL && b.text === label) {
24
24
  if (!b.breaks) b.breaks = [];
25
25
  var _b = scanner2('return []');
26
- _b.ret_ = true;
26
+ _b.ret_ = -1;
27
27
  if (iscontinue) _b[1].continue = s, s.continue = true;
28
28
  b.breaks.push(_b[1]);
29
29
  pushstep(result, _b);
@@ -40,7 +40,7 @@ var _break = function (body, cx, result, iscontinue) {
40
40
  if (b.type !== LABEL) {
41
41
  if (!b.breaks) b.breaks = [];
42
42
  var _b = scanner2("return []");
43
- _b.ret_ = true;
43
+ _b.ret_ = -1;
44
44
  if (iscontinue) _b[1].continue = b, b.continue = true;
45
45
  b.breaks.push(_b[1]);
46
46
  pushstep(result, _b);
@@ -146,7 +146,7 @@ var _switch = function (body, cx, unblock, result, getname) {
146
146
  for (var q of q) if (q.length) pushstep(result, q);
147
147
  var qe = q;
148
148
  if (qe.name) var case_ = scanner2(`if(${qn}===${qe.name})return[]`);
149
- else case_ = scanner2(`return[]`), case_.ret_ = true;
149
+ else case_ = scanner2(`return[]`), case_.ret_ = -1;
150
150
  pushstep(result, case_);
151
151
  var by = cy;
152
152
  m = o[cy];
@@ -154,7 +154,7 @@ var _switch = function (body, cx, unblock, result, getname) {
154
154
  tmp.push(result.length - 1, case_[case_.length - 1], o.slice(by, cy));
155
155
  }
156
156
  if (!result[result.length - 1].ret_) {
157
- case_ = scanner2(`return[]`), case_.ret_ = true;
157
+ case_ = scanner2(`return[]`), case_.ret_ = -1;
158
158
  pushstep(result, case_);
159
159
  tmp.push(result.length - 1, case_[case_.length - 1], []);
160
160
  }
@@ -284,7 +284,9 @@ var _while = function (body, cx, unblock, result) {
284
284
  return cx;
285
285
  };
286
286
  var pushstep = function (result, step) {
287
- if (!step.length) return;
287
+ if (isempty(step, SPACE)) {
288
+ return;
289
+ }
288
290
  var q = result[result.length - 1];
289
291
  if (!q) {
290
292
  result.push(step);
@@ -302,6 +304,7 @@ var pushstep = function (result, step) {
302
304
  if (needcomma(q)) q.push({ type: STAMP, text: ';' });
303
305
  if (!ishalf(q)) {
304
306
  q.ret_ = step.ret_;
307
+ q.ifbrk = step.ifbrk;
305
308
  }
306
309
  q.push(...step);
307
310
  q.await_ = step.await_;
@@ -346,7 +349,7 @@ var patchstep = function (r, nextindex, h) {
346
349
  relink(r);
347
350
  }
348
351
  };
349
- var flusqueue = function (result, queue) {
352
+ var flushqueue = function (result, queue) {
350
353
  var savedLength = result.length;
351
354
  var savedIndex = savedLength - 1;
352
355
  var prev = result[savedIndex];
@@ -359,21 +362,6 @@ var flusqueue = function (result, queue) {
359
362
  }
360
363
  };
361
364
 
362
- var addresult = function (result, step) {
363
- if (!step.length) return;
364
- var cx, mx = 0, n;
365
- var awaited = step.awaited;
366
- var queue = [];
367
- do {
368
- while (step[mx] && step[mx].type === STAMP && /^[,;]$/.test(step[mx].text)) mx++;
369
- cx = step.indexOf(NEXT, mx);
370
- n = step.slice(mx, mx = cx < 0 ? step.length : cx + 1);
371
- if (awaited) n.awaited = awaited, awaited = false;
372
- n.name = step.name;
373
- queue.push(n);
374
- } while (mx < step.length);
375
- flusqueue(result, queue);
376
- };
377
365
  var _do = function (body, cx, unblock, result) {
378
366
  var o = body[cx];
379
367
  var label = o;
@@ -391,7 +379,7 @@ var _do = function (body, cx, unblock, result) {
391
379
  };
392
380
  var stepReturn = function (value, type, q) {
393
381
  var r = scanner2(`return [${value},${type}]`);
394
- r.ret_ = true;
382
+ r.ret_ = type === 2 ? type : true;
395
383
  if (q && q.length) r.name = q[q.length - 1].name;
396
384
  return r;
397
385
  }
@@ -422,7 +410,10 @@ var _return = function (r, nextindex) {
422
410
  r.await_ = true;
423
411
  }
424
412
  if (needcomma(r)) r.push({ type: STAMP, text: ';' });
425
- else if (r.length) r[r.length - 1].text = ';';
413
+ else if (r.length) {
414
+ var re = r[r.length - 1];
415
+ if (re.type === STAMP && re.text === ',') re.text = ';';
416
+ }
426
417
  r.push(...x);
427
418
  relink(r);
428
419
  };
@@ -463,7 +454,6 @@ var _invoke = function (t, getname) {
463
454
  for (var cx = 0; cx < t.length; cx++) {
464
455
  var o = t[cx];
465
456
  if (needbreak(o)) {
466
- if (needcomma(queue)) queue.push({ type: STAMP, text: ',' });
467
457
  var s = splice(t, bx, cx + 1 - bx);
468
458
  if (cx > 0) s.name = s[0].text;
469
459
  else s.name = qname;
@@ -484,7 +474,7 @@ var _invoke = function (t, getname) {
484
474
  var ey = cy;
485
475
  if (ay === ey || ay >= o.length) continue;
486
476
  var m = o.slice(ay, ey);
487
- if (m.length === 1 && (m[0].type === EXPRESS && !/\./.test(m[0].text) || m[0].type === VALUE || m[0].type === QUOTED && !m[0].length)) {
477
+ if (m.length === 1 && (m[0].type === EXPRESS && !/[\.\[]/.test(m[0].text) || m[0].type === VALUE || m[0].type === QUOTED && !m[0].length)) {
488
478
  continue;
489
479
  }
490
480
  var q = toqueue(m, getdeepname, true);
@@ -505,7 +495,7 @@ var _invoke = function (t, getname) {
505
495
  }
506
496
  nameindex = _nameindex;
507
497
  if (!cache.length) continue;
508
- if (queue.length) flusqueue(result, queue), queue = [];
498
+ if (queue.length) flushqueue(result, queue), queue = [];
509
499
  for (var c of cache) pushstep(result, c);
510
500
  cache = [];
511
501
  var n = o.next;
@@ -524,15 +514,15 @@ var _invoke = function (t, getname) {
524
514
  }
525
515
  if (queue.length) {
526
516
  queue.push(t);
527
- flusqueue(result, queue);
517
+ flushqueue(result, queue);
528
518
  }
529
519
  else if (t.length) {
530
520
  var t0 = t[0];
531
- if (t0.type === EXPRESS && /^\./.test(t0.text) || t0.type & (STAMP | STRAP) && powermap[t0.text] < powermap.new) {
521
+ if (t0.type === EXPRESS && /^[\.\[]/.test(t0.text) || t0.type & (STAMP | STRAP) && powermap[t0.text] < powermap.new) {
532
522
  t.unshift(...scanner2(`${qname}=${qname}`));
533
523
  relink(t);
534
524
  }
535
- addresult(result, t);
525
+ pushstep(result, t);
536
526
  }
537
527
  return result;
538
528
  };
@@ -551,10 +541,18 @@ var ishalf = function (q) {
551
541
  if (!e) return false;
552
542
  return e.type === SCOPED && e.entry === '(' && e.prev && e.prev.type === STRAP && /^if$/.test(e.prev.text) || e.type === STRAP && /^else$/.test(e.text);
553
543
  }
544
+ var isempty = function (q, ignore_types) {
545
+ for (var a of q) {
546
+ if (!(a.type & ignore_types || a.type === STAMP && /^[,;]$/.test(a.text))) return false;
547
+ }
548
+ return true;
549
+ }
554
550
  var needcomma = function (q) {
555
- if (!q.length) return false;
556
- var e = q[q.length - 1];
551
+ if (isempty(q, COMMENT | SPACE)) return false;
557
552
  if (ishalf(q)) return false;
553
+ var i = q.length - 1;
554
+ var e = q[i];
555
+ while (e.type & (SPACE | COMMENT)) e = q[--i];
558
556
  return !needbreak(e);
559
557
  };
560
558
  var patchname = function (d, getname) {
@@ -595,9 +593,9 @@ var ternary = function (body, getname, ret) {
595
593
  patchname(d, getname);
596
594
  pushstep(d, stepReturn(1, 0, d));
597
595
  pushstep(c, stepReturn(d.length + 1, 0, c));
598
- addresult(res, scanner2(`if(${getCondition(b, function (b) {
596
+ pushstep(res, scanner2(`if(${getCondition(b, function (b) {
599
597
  b = ternary(b, getname, true);
600
- for (var b of b) addresult(res, b);
598
+ for (var b of b) pushstep(res, b);
601
599
  return b;
602
600
  }, true)})return [1,0]`));
603
601
  var q = res[res.length - 1];
@@ -888,6 +886,7 @@ var getblock = function (body, cx) {
888
886
  cx++;
889
887
  }
890
888
  var b = body.slice(ax, cx);
889
+ while (o && o.type & (SPACE | COMMENT)) o = body[++cx];
891
890
  b.next = o;
892
891
  return b;
893
892
  };
@@ -903,9 +902,11 @@ var ifpatch = function (result, autoskip) {
903
902
  if (!result.length) return;
904
903
  if (autoskip !== false) {
905
904
  var rs = result[result.length - 1];
906
- if (rs.ret_ && !rs.await_) return;
905
+ if (rs.ret_ !== true && rs.ret_) return;
907
906
  }
908
907
  var re = stepReturn(1, 0);
908
+ re.ret_ = -2;
909
+ re.ifbrk = autoskip !== undefined;
909
910
  pushstep(result, re);
910
911
  };
911
912
  function toqueue(body, getname, ret = false, result = []) {
@@ -916,9 +917,11 @@ function toqueue(body, getname, ret = false, result = []) {
916
917
  if (isbr) continue;
917
918
  var findex = iftop[cx] - 1;
918
919
  var p = result[findex];
919
- p.pop();
920
- p.push(scanner2(`[${result.length - findex},0]`)[0]);
921
- relink(p);
920
+ if (p.ifbrk) {
921
+ p.pop();
922
+ p.push(scanner2(`[${result.length - findex},0]`)[0]);
923
+ relink(p);
924
+ }
922
925
  }
923
926
  for (var cx = 1, dx = iftop.length - 2; cx < dx; cx++) {
924
927
  var isbr = iftop[cx++];
@@ -946,6 +949,7 @@ function toqueue(body, getname, ret = false, result = []) {
946
949
  var r = result[cx];
947
950
  if (r.indexOf(b) >= 0) { break }
948
951
  }
952
+ if (cx < 0) throw console.log(result.map(r => createString(r)), e.text, createString([b.prev, b])), "break语句异常";
949
953
  end.push({ type: VALUE, text: b.continue ? b.continue.contat - cx : result.length - cx }, { type: STAMP, text: "," }, { type: VALUE, text: "0" });
950
954
  relink(end);
951
955
  }
@@ -1047,7 +1051,7 @@ function toqueue(body, getname, ret = false, result = []) {
1047
1051
  while (body[cx] !== o.next) cx++;
1048
1052
  o = o.next;
1049
1053
  isbr = isbreak(o);
1050
- if (!isbr) ifpatch(result);
1054
+ if (!isbr) ifpatch(result, true);
1051
1055
  iftop.push(result.length);
1052
1056
  elseif = true;
1053
1057
  }
@@ -1087,8 +1091,8 @@ function toqueue(body, getname, ret = false, result = []) {
1087
1091
  var bn = body.next;
1088
1092
  if (bn && bn.type === STAMP && bn.text === ';') bn = bn.next;
1089
1093
  var inif = bn && bn.type === STRAP && bn.text === 'else';
1090
- if (inif) ifpatch(result);
1091
- else if (!isbr) ifpatch(result);
1094
+ if (inif) ifpatch(result, true);
1095
+ else if (!isbr) ifpatch(result, true);
1092
1096
  uniftop();
1093
1097
  if (inif) ifpatch(result, false);
1094
1098
  iftop = null;
@@ -58,7 +58,9 @@ test("for(a in b)", "for (a in b)", true);
58
58
  test("for(var a in b) a.push()", "for (var a in b) a.push()", true);
59
59
  test("for(;;)", "return [0, 0]", true);
60
60
  test("for(a = os[Symbol.iterator] || os[Symbol.asyncIterator] || Array.prototype[Symbol.iterator], a = a.call(os), b = a.next(); !b.done && (o = b.value, true); b = a.next())", "_ = Symbol.iterator; _ = os[_]; if (_) return [1, 0]; _ = Symbol.asyncIterator; _ = os[_]; if (_) return [1, 0]; _ = Symbol.iterator; _ = Array.prototype[_];\r\n a = _; _ = a.call(os); a = _; _ = a.next(); b = _; return [1, 0];\r\n _ = !b.done; if (!_) return [1, 0]; _ = b.value; o = _; _ = (true);\r\n if (!_) return [1, 0]; _ = a.next(); b = _; return [-1, 0]", true);
61
- test("for(a=0;a<1;a++)if(a==0)continue", `a = 0; return [1, 0];\r\n _ = a < 1; if (!_) return [2, 0]; _ = a == 0; if (_) return [1, 0]; return [1, 0];\r\n _ = a++; return [-1, 0]`, true);
61
+ test("for(a=0;a<1;a++)if(a==0)continue\r\n else a=1", `a = 0; return [1, 0];\r\n _ = a < 1; if (!_) return [3, 0]; _ = a == 0; if (_) return [2, 0]; return [1, 0];\r\n a = 1; return [1, 0];\r\n _ = a++; return [-2, 0]`, true);
62
+ test("for(a=0;a<1;a++)if(a==0){continue} else {a=1}", `a = 0; return [1, 0];\r\n _ = a < 1; if (!_) return [3, 0]; _ = a == 0; if (!_) return [1, 0]; return [2, 0];\r\n a = 1; return [1, 0];\r\n _ = a++; return [-2, 0]`, true);
63
+ test("for(a=0;a<1;a++){if(a==0){a=2;continue} else if(b) {a=1;continue} c=3}", `a = 0; return [1, 0];\r\n _ = a < 1; if (!_) return [4, 0]; _ = a == 0; if (!_) return [1, 0]; a = 2; return [3, 0];\r\n if (!b) return [1, 0]; a = 1; return [2, 0];\r\n c = 3; return [1, 0];\r\n _ = a++; return [-3, 0]`, true);
62
64
  test("for(a=0;a<10;a++) await a", "a = 0; return [1, 0];\r\n _ = a < 10; if (!_) return [2, 0]; _ = a; return [_, 1];\r\n _ = @; _ = a++; return [-1, 0]", true);
63
65
  test("for(a=0;a<10;a++) await a, await b", "a = 0; return [1, 0];\r\n _ = a < 10; if (!_) return [3, 0]; _ = a; return [_, 1];\r\n _ = @; _ = b; return [_, 1];\r\n _ = @; _ = a++; return [-2, 0]", true);
64
66
  test("for(a=0;b=2,a<10;a++) await a, await b", "a = 0; return [1, 0];\r\n b = 2; _ = a < 10; if (!_) return [3, 0]; _ = a; return [_, 1];\r\n _ = @; _ = b; return [_, 1];\r\n _ = @; _ = a++; return [-2, 0]", true);
@@ -73,6 +75,7 @@ test("switch(a){case 1:case 2:break;}", "if (a === 1) return [1, 0]; if (a === 2
73
75
  test("switch(a){case 1:case 2:x=1;}", "if (a === 1) return [1, 0]; if (a === 2) return [1, 0]; return [2, 0];\r\n x = 1; return [1, 0]", true);
74
76
  test("switch(a){case 1:case 2:x=1;}", "if (a === 1) return [1, 0]; if (a === 2) return [1, 0]; return [2, 0];\r\n x = 1; return [1, 0]", true);
75
77
  test("with(a){ a = 1}", `if (_ = with_("a", [a])) _.a = 1; else a = 1;`, true);
78
+ test("try{a=2+1}catch(e){return;}", 'return [65537, 7];\r\n _ = 2 + 1; a = _; return [0, 9];\r\n e = @; return [undefined, 2];\r\n return [1, 9]', true);
76
79
  test("try{a=2+1}catch(e){}", 'return [1, 7];\r\n _ = 2 + 1; a = _; return [0, 9];\r\n return [1, 9]', true);
77
80
  test("try{a=2+1}catch(e){a=3}", 'return [65537, 7];\r\n _ = 2 + 1; a = _; return [0, 9];\r\n e = @; a = 3; return [0, 9];\r\n return [1, 9]', true);
78
81
  test("(function(){})", '_ = function () {}; (_)', true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "efront",
3
- "version": "4.0.3",
3
+ "version": "4.0.6",
4
4
  "description": "简化前端开发,优化web性能",
5
5
  "main": "public/efront.js",
6
6
  "directories": {