efront 4.4.1 → 4.4.3

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.
@@ -1,19 +1,72 @@
1
- function createSeek(express) {
2
- var dist;
3
- express.forEach(function (search) {
4
- if (dist) {
5
- if (/[\=]/.test(dist)) dist = `(${dist})`;
6
- var pd = /[\.\[]/.test(dist) ? `${dist}!==void 0` : `typeof ${dist}!=='undefined'`;
7
- dist = `${pd}&&${dist}!==null?${dist}${search}:''`
8
- } else {
9
- dist = search;
1
+ function splitSeek(express) {
2
+ var dist = [];
3
+ var x = 0;
4
+ express.forEach((e, i) => {
5
+ if (i > x && /\?/.test(e)) {
6
+ var exp = express.slice(x, x = i);
7
+ dist.push(exp);
10
8
  }
11
9
  });
10
+ if (x < express.length) dist.push(express.slice(x, express.length));
11
+ dist.forEach(d => d[0] = d[0].replace(/^\?/, ''))
12
12
  return dist;
13
13
  }
14
- function main(express) {
14
+ function getTmpvar(explist) {
15
+ var varset = {};
16
+ explist.forEach(e => {
17
+ e = e[0].replace(/^([^\.\[]]+)[\s\S]*$/, "$1");
18
+ varset[e] = true;
19
+ });
20
+ var tmpvar = 'b', n = 0;
21
+ while (tmpvar in varset) {
22
+ tmpvar = 'b' + n++;
23
+ }
24
+ return tmpvar;
25
+ }
26
+ var autoDefine = false;
27
+ function createSeek(express, split = true) {
28
+ var tmpvar = 'a', undef = '_', _null = '$';
29
+ if (express.length <= 1) return express[0];
30
+ var notmp = !/[\.\[\(\{]/.test(express[0]);
31
+ if (express.length === 2) {
32
+ if (split && /^\?/.test(express[1])) {
33
+ if (notmp) return `typeof ${tmpvar}!=='undefined'&&${tmpvar}!==null?${tmpvar}:${express[1].slice(1)}`;
34
+ return `function(${tmpvar},${undef}){return ${tmpvar}===${undef}||${tmpvar}===null?${express[1].slice(1)}:${tmpvar}}(${express[0]})`;
35
+ }
36
+ if (notmp) {
37
+ tmpvar = express[0];
38
+ if (autoDefine) {
39
+ return `typeof ${tmpvar}==='undefined'||${tmpvar}===null?void 0:${tmpvar}${express[1]}`;
40
+ }
41
+ return `${tmpvar}===void 0||${tmpvar}===null?void 0:${tmpvar}${express[1]}`;
42
+ };
43
+ return `function(${tmpvar},${undef}){return ${tmpvar}===${undef}||${tmpvar}===null?${undef}:${tmpvar}${express[1]}}(${express[0]})`;
44
+ }
45
+ if (!split) {
46
+ var dist = express.slice(1).map((search, i, a) => {
47
+ return `if(${tmpvar}===${undef}||${tmpvar}===${_null})return;${tmpvar}=${tmpvar}${search};`;
48
+ }).join('');
49
+ dist = `function(${tmpvar},${_null},${undef}){${dist}return ${tmpvar}}(${express[0]},null)`;
50
+ if (notmp && autoDefine) {
51
+ dist = `typeof ${express[0]}==='undefined'&&${express[0]}===null?void 0:${dist}`;
52
+ }
53
+ return dist;
54
+ }
55
+ var explist = splitSeek(express);
56
+ if (explist.length === 1) {
57
+ return createSeek(explist[0], false);
58
+ }
59
+ tmpvar = getTmpvar(explist);
60
+ var dist = explist.slice(0, explist.length - 1).map(express => {
61
+ var dist = createSeek(express, false);
62
+ return `${tmpvar}=${dist};if(${tmpvar}!==void 0&&${tmpvar}!==null)return ${tmpvar};`;
63
+ }).join("") + `return ${createSeek(explist[explist.length - 1], false)}`;
64
+ return `function(${tmpvar}){${dist}}()`;
65
+ }
66
+ function main(express, autodef = true) {
67
+ autoDefine = autodef;
15
68
  if (!/\?\s*\.(?=[^\d])|\?\s*[\?\]\}\)\:\,=|%&;\>\<]|\?\s*$/.test(express)) return express;
16
- var reg = /\\[\s\S]|\?\s*(\.(?!\d)|$|(?=[\?\]\}\)\:\,=|%&;\>\<\*\/]))|[\:\,\+\=\-\!%\^\|\/\&\*\!\;\?\>\<~\{\}\[\]\(\)'"`\s]/g;
69
+ var reg = /\\[\s\S]|\?\s*([\.](?!\d)|\?|$|(?=[\?\]\}\)\:\,=|%&;\>\<\*\/]))|[\:\,\+\=\-\!%\^\|\/\&\*\!\;\?\>\<~\{\}\[\]\(\)'"`\s]/g;
17
70
  var cache = [], queue = [];
18
71
  var exp = [];
19
72
  var instr = false;
@@ -22,7 +75,7 @@ function main(express) {
22
75
  if (!exp.length) exp.push(s);
23
76
  else {
24
77
  var e = exp[exp.length - 1];
25
- if (!/[\.]\s*$/.test(e) && !/^\s*[\.'"`\[\(]/.test(s)) {
78
+ if (!/[\.\?]\s*$/.test(e) && !/^\s*[\.'"`\[\(]/.test(s)) {
26
79
  queue.push(createSeek(exp));
27
80
  exp.splice(0, exp.length, '');
28
81
  e = '';
@@ -0,0 +1,11 @@
1
+ assert(renderExpress("[].a"), "[].a")
2
+ assert(renderExpress("a?b:c"), "a?b:c")
3
+ assert(renderExpress("a?"), "(typeof a==='undefined'||a===null?void 0:a)")
4
+ assert(renderExpress("[].a?.()"), "(function(a,_){return a===_||a===null?_:a()}([].a))")// 暂不支持在?.()的函数中访问this,此为临时方案
5
+ assert(renderExpress("[].a?.b?.c?.()"), "(function(a,$,_){if(a===_||a===$)return;a=a.b;if(a===_||a===$)return;a=a.c;if(a===_||a===$)return;a=a();return a}([].a,null))")
6
+ assert(renderExpress("a??b"), "(typeof a!=='undefined'&&a!==null?a:b)")
7
+ assert(renderExpress("a?.b??b?.c", false), "(function(b0){b0=a===void 0||a===null?void 0:a.b;if(b0!==void 0&&b0!==null)return b0;return b===void 0||b===null?void 0:b.c}())")
8
+
9
+ assert(renderExpress("[]?.a"), "(function(a,_){return a===_||a===null?_:a.a}([]))")
10
+ assert(renderExpress("[]?.()"), "(function(a,_){return a===_||a===null?_:a()}([]))")
11
+ assert(renderExpress("[]?.[]"), "(function(a,_){return a===_||a===null?_:a[]}([]))")
@@ -20,8 +20,10 @@ const {
20
20
  getDeclared,
21
21
  createScoped,
22
22
  snapExpressHead,
23
+ snapExpressFoot,
23
24
  splice,
24
25
  relink,
26
+ remove,
25
27
  setqueue,
26
28
  replace,
27
29
  skipAssignment,
@@ -521,7 +523,18 @@ function detour(o, ie) {
521
523
  if (m) { context.avoidMap[m[0]] = true; }
522
524
  }
523
525
  if (/\?\./.test(text)) {
524
- text = renderExpress(text);
526
+ if (/\?\.$/.test(text)) {
527
+ o = snapExpressHead(o);
528
+ var f = snapExpressFoot(o);
529
+ var rest = [o];
530
+ remove(o, f.prev);
531
+ while (o !== f) {
532
+ o = o.next;
533
+ rest.push(o);
534
+ }
535
+ text = createString(rest);
536
+ }
537
+ text = renderExpress(text, false);
525
538
  if (hasdot) text = "..." + text;
526
539
  o = replace(o, ...scan(text));
527
540
  continue;
@@ -125,7 +125,15 @@ var skipAssignment = function (o, cx) {
125
125
  break;
126
126
  case STRAP:
127
127
  if (needpunc) {
128
- if (!/^(in|instanceof|of|else|as|from)$/.test(o.text)) break loop;
128
+ if (!/^(in|instanceof|of|else|as|from|catch|finally)$/.test(o.text)) {
129
+ break loop;
130
+ }
131
+ if (o.text === 'catch') {
132
+ next();
133
+ if (o && o.entry === '(') next();
134
+ needpunc = false;
135
+ break;
136
+ }
129
137
  if (o.text === 'else') {
130
138
  if (!ifdeep) break loop;
131
139
  ifdeep--;
@@ -134,21 +142,16 @@ var skipAssignment = function (o, cx) {
134
142
  next();
135
143
  needpunc = false;
136
144
  }
137
- else if (/^(try|catch|finally)$/.test(o.text)) {
138
- next();
139
- if (o.entry === "(") next();
140
- next();
141
- }
142
145
  else if (o.text === 'else') {
143
146
  if (ifdeep <= 0) break loop;
144
147
  ifdeep--;
145
148
  needpunc = false;
146
149
  next();
147
150
  }
148
- else if (/^(if|while|with|switch)$/.test(o.text)) {
151
+ else if (/^(if|while|with|switch|try)$/.test(o.text)) {
149
152
  if (o.text === 'if') ifdeep++;
150
153
  next();
151
- next();
154
+ if (o.entry === "(") next();
152
155
  }
153
156
  else if (o.text === 'do') {
154
157
  next();
@@ -1323,10 +1326,16 @@ var splice = function (queue, index, size, ...args) {
1323
1326
  setqueue(args, queue);
1324
1327
  return res;
1325
1328
  };
1326
- var remove = function (o) {
1329
+ var remove = function (o, end) {
1327
1330
  var q = o.queue;
1328
1331
  var i = q.indexOf(o);
1329
- if (i >= 0) splice(q, i, 1);
1332
+ var length = 1;
1333
+ if (arguments.length === 2) {
1334
+ end = q.indexOf(end, i) + 1;
1335
+ if (end < 0) end = i;
1336
+ length = end - i;
1337
+ }
1338
+ if (i >= 0) splice(q, i, length);
1330
1339
  };
1331
1340
  var replace = function (o, ...args) {
1332
1341
  var queue = o.queue;
@@ -34,6 +34,10 @@ testPickSentence(`a+b\r\n++c`, 5, "++c")
34
34
  testPickSentence(`a+b++\r\nc`, 5, "c")
35
35
  testPickSentence(`a+b++\r\nc`, 5, "c")
36
36
  testPickSentence(`a+b++\r\n+c`, 6, "a + b++\r\n+ c")
37
+ testPickSentence(`if(a)try{}catch{}a;`, 0, "if (a) try {} catch {}")
38
+ testPickSentence(`if(a)try{}catch(a){}a;`, 0, "if (a) try {} catch (a) {}")
39
+ testPickSentence(`if(a)try{}catch{} else a;`, 0, "if (a) try {} catch {} else a")
40
+ testPickSentence(`if(a)try{}catch{} else if(a);`, 0, "if (a) try {} catch {} else if (a)")
37
41
  assert(common.createString(common.pickArgument(scanner2(`a={a:1,c:d}`)[2][4])), 'c: d')
38
42
  assert(common.createString(common.pickArgument(scanner2(`a=class{a=1\r\nc=d}`)[3][4])), 'c = d')
39
43
  assert(common.createString(common.pickArgument(scanner2(`(a=1,c=d)`)[0][4])), 'c = d')
@@ -125,7 +125,6 @@ test("menus[a+b]()", "_ = a + b; menus[_]()", true);
125
125
  test(`switch(a){default: a;case 1:b;}`, 'if (a === 1) return [2, 0]; return [1, 0];\r\n a; return [1, 0];\r\n b; return [1, 0]', true);
126
126
  test("loop:{a=b;if(a) continue loop}", "a = b; if (a) return [0, 0]", true);
127
127
  test("if(a)try{}finally{}else a;", 'if (!a) return [4, 0]; return [1, 8];\r\n return [0, 9];\r\n return [1, 9];\r\n return [2, 0];\r\n a; return [1, 0]')
128
- test("if(a)try{}finally{}else;", 'if (!a) return [4, 0]; return [1, 8];\r\n return [0, 9];\r\n return [1, 9];\r\n return [1, 0]');
129
128
  test("b:while(t){switch(a){case c:break b;}}", 'if (!t) return [1, 0]; if (a === c) return [1, 0]; return [0, 0]');
130
129
  test("predef[key[1]](r.slice(key[0].length).trim())", '_ = key[1]; _0 = key[0].length; _0 = r.slice(_0); _0 = _0.trim(); predef[_](_0)');
131
130
  test("predef[key[1]](r.slice())(r.slice())", '_ = key[1]; _0 = r.slice(); _ = predef[_](_0); _0 = r.slice(); _(_0)');
@@ -149,7 +148,8 @@ test(`a[0]-- > 1`, "_ = a[0]--, _ > 1");
149
148
  test(`c=b\r\n++a`, "c = b; ++a");
150
149
  test(`c=b+\r\n++a`, "_ =\r\n++a, c = b + _");
151
150
  test(`c=b+ ++a`, "_ = ++a, c = b + _");
152
- unstruct.debug = true; r++;
153
151
  test(`c=b+ +a`, "_ = +a, c = b + _");
154
152
  test(`c=b+ !a`, "_ = !a, c = b + _");
155
153
  test(`do {var loadcount = 0;} while (loadcount !== 0);`, `loadcount = 0; _ = loadcount !== 0; if (_) return [0, 0]`);
154
+ unstruct.debug = true; r++;
155
+ test("if(a)try{a}catch{};a;", 'if (!a) return [4, 0]; return [1, 7];\r\n a; return [0, 9];\r\n return [1, 9];\r\n return [1, 0];\r\n a');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "efront",
3
- "version": "4.4.1",
3
+ "version": "4.4.3",
4
4
  "description": "简化前端开发,优化web性能",
5
5
  "main": "public/efront.js",
6
6
  "directories": {
@@ -10,7 +10,7 @@
10
10
  "efront": "public/efront.js"
11
11
  },
12
12
  "scripts": {
13
- "prepack": "node tools/build-efront.js --uplevel --node --deno --no-target",
13
+ "prepack": "node tools/build-efront.js --node --deno --no-target",
14
14
  "start": "efront ./efront/ --libs=typescript,esprima,escodegen,esmangle,pngjs,less-node"
15
15
  },
16
16
  "repository": {