Skip to content
This repository was archived by the owner on Apr 10, 2023. It is now read-only.

Commit fd6b414

Browse files
committed
feat(execute): ✨Support MemberExpression
1 parent c41c564 commit fd6b414

File tree

10 files changed

+137
-5
lines changed

10 files changed

+137
-5
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ JavaScript parser, executor and serializer.
2323

2424
## Benchmark
2525

26-
@vivaxy/javascript#parse x 6,112 ops/sec ±2.48% (91 runs sampled)
26+
@vivaxy/javascript#parse x 5,761 ops/sec ±0.28% (91 runs sampled)
2727

28-
@babel/parser#parse x 4,067 ops/sec ±2.24% (86 runs sampled)
28+
@babel/parser#parse x 3,674 ops/sec ±2.27% (88 runs sampled)
2929

30-
acorn#parse x 2,119 ops/sec ±3.92% (83 runs sampled)
30+
acorn#parse x 2,224 ops/sec ±1.06% (92 runs sampled)
3131

3232
Fastest is @vivaxy/javascript#parse
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"type": "Program",
3+
"body": [
4+
{
5+
"type": "ExpressionStatement",
6+
"expression": {
7+
"type": "MemberExpression",
8+
"object": {
9+
"type": "Identifier",
10+
"name": "item"
11+
},
12+
"property": {
13+
"type": "Identifier",
14+
"name": "value"
15+
},
16+
"computed": false
17+
}
18+
}
19+
]
20+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @since 20180929 16:19
3+
* @author vivaxy
4+
*/
5+
6+
module.exports = 1;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @since 20180929 16:18
3+
* @author vivaxy
4+
*/
5+
6+
module.exports = {
7+
item: {
8+
value: 1
9+
}
10+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"type": "Program",
3+
"body": [
4+
{
5+
"type": "ExpressionStatement",
6+
"expression": {
7+
"type": "MemberExpression",
8+
"object": {
9+
"type": "Identifier",
10+
"name": "item"
11+
},
12+
"property": {
13+
"type": "Literal",
14+
"value": "value"
15+
},
16+
"computed": true
17+
}
18+
}
19+
]
20+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @since 20180929 16:19
3+
* @author vivaxy
4+
*/
5+
6+
module.exports = 1;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @since 20180929 16:18
3+
* @author vivaxy
4+
*/
5+
6+
module.exports = {
7+
item: {
8+
value: 1
9+
}
10+
};

lib/execute.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,15 @@ module.exports = function execute(ast, scope) {
124124
throw new Error('Unexpected identifier name: ' + ast.name);
125125
}
126126
if (ast.type === astTypes.CONDITIONAL_EXPRESSION) {
127-
return execute(ast.test) ? execute(ast.consequent) : execute(ast.alternate);
127+
return execute(ast.test, scope)
128+
? execute(ast.consequent, scope)
129+
: execute(ast.alternate, scope);
130+
}
131+
if (ast.type === astTypes.MEMBER_EXPRESSION) {
132+
if (ast.computed) {
133+
return execute(ast.object, scope)[execute(ast.property, scope)];
134+
}
135+
return execute(ast.object, scope)[ast.property.name];
128136
}
129137
throw new Error('Unexpected ast type: ' + ast.type);
130138
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"acorn": "^5.7.2",
2727
"ava": "^0.25.0",
2828
"benchmark": "^2.1.4",
29+
"fast-glob": "^2.2.2",
2930
"fs-extra": "^7.0.0",
3031
"husky": "^0.14.3",
3132
"lint-staged": "^7.2.0",

yarn.lock

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@
137137
pretty-ms "^0.2.1"
138138
text-table "^0.2.0"
139139

140+
"@mrmlnc/readdir-enhanced@^2.2.1":
141+
version "2.2.1"
142+
resolved "https://registry.npmjs.org/@mrmlnc/readdir-enhanced/-/readdir-enhanced-2.2.1.tgz#524af240d1a360527b730475ecfa1344aa540dde"
143+
dependencies:
144+
call-me-maybe "^1.0.1"
145+
glob-to-regexp "^0.3.0"
146+
147+
"@nodelib/fs.stat@^1.0.1":
148+
version "1.1.2"
149+
resolved "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-1.1.2.tgz#54c5a964462be3d4d78af631363c18d6fa91ac26"
150+
140151
"@samverschueren/stream-to-observable@^0.3.0":
141152
version "0.3.0"
142153
resolved "https://registry.npmjs.org/@samverschueren/stream-to-observable/-/stream-to-observable-0.3.0.tgz#ecdf48d532c58ea477acfcab80348424f8d0662f"
@@ -832,6 +843,10 @@ call-matcher@^1.0.0:
832843
espurify "^1.6.0"
833844
estraverse "^4.0.0"
834845

846+
call-me-maybe@^1.0.1:
847+
version "1.0.1"
848+
resolved "https://registry.npmjs.org/call-me-maybe/-/call-me-maybe-1.0.1.tgz#26d208ea89e37b5cbde60250a15f031c16a4d66b"
849+
835850
call-signature@0.0.2:
836851
version "0.0.2"
837852
resolved "https://registry.yarnpkg.com/call-signature/-/call-signature-0.0.2.tgz#a84abc825a55ef4cb2b028bd74e205a65b9a4996"
@@ -1585,6 +1600,17 @@ fast-diff@^1.1.1:
15851600
version "1.1.2"
15861601
resolved "https://registry.yarnpkg.com/fast-diff/-/fast-diff-1.1.2.tgz#4b62c42b8e03de3f848460b639079920695d0154"
15871602

1603+
fast-glob@^2.2.2:
1604+
version "2.2.2"
1605+
resolved "https://registry.npmjs.org/fast-glob/-/fast-glob-2.2.2.tgz#71723338ac9b4e0e2fff1d6748a2a13d5ed352bf"
1606+
dependencies:
1607+
"@mrmlnc/readdir-enhanced" "^2.2.1"
1608+
"@nodelib/fs.stat" "^1.0.1"
1609+
glob-parent "^3.1.0"
1610+
is-glob "^4.0.0"
1611+
merge2 "^1.2.1"
1612+
micromatch "^3.1.10"
1613+
15881614
figures@^1.5.0, figures@^1.7.0:
15891615
version "1.7.0"
15901616
resolved "https://registry.yarnpkg.com/figures/-/figures-1.7.0.tgz#cbe1e3affcf1cd44b80cadfed28dc793a9701d2e"
@@ -1806,6 +1832,17 @@ glob-parent@^2.0.0:
18061832
dependencies:
18071833
is-glob "^2.0.0"
18081834

1835+
glob-parent@^3.1.0:
1836+
version "3.1.0"
1837+
resolved "https://registry.npmjs.org/glob-parent/-/glob-parent-3.1.0.tgz#9e6af6299d8d3bd2bd40430832bd113df906c5ae"
1838+
dependencies:
1839+
is-glob "^3.1.0"
1840+
path-dirname "^1.0.0"
1841+
1842+
glob-to-regexp@^0.3.0:
1843+
version "0.3.0"
1844+
resolved "https://registry.npmjs.org/glob-to-regexp/-/glob-to-regexp-0.3.0.tgz#8c5a1494d2066c570cc3bfe4496175acc4d502ab"
1845+
18091846
glob@^7.0.3, glob@^7.0.5, glob@^7.0.6:
18101847
version "7.1.2"
18111848
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15"
@@ -2134,7 +2171,7 @@ is-extglob@^1.0.0:
21342171
version "1.0.0"
21352172
resolved "https://registry.yarnpkg.com/is-extglob/-/is-extglob-1.0.0.tgz#ac468177c4943405a092fc8f29760c6ffc6206c0"
21362173

2137-
is-extglob@^2.1.1:
2174+
is-extglob@^2.1.0, is-extglob@^2.1.1:
21382175
version "2.1.1"
21392176
resolved "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz#a88c02535791f02ed37c76a1b9ea9773c833f8c2"
21402177

@@ -2164,6 +2201,12 @@ is-glob@^2.0.0, is-glob@^2.0.1:
21642201
dependencies:
21652202
is-extglob "^1.0.0"
21662203

2204+
is-glob@^3.1.0:
2205+
version "3.1.0"
2206+
resolved "https://registry.npmjs.org/is-glob/-/is-glob-3.1.0.tgz#7ba5ae24217804ac70707b96922567486cc3e84a"
2207+
dependencies:
2208+
is-extglob "^2.1.0"
2209+
21672210
is-glob@^4.0.0:
21682211
version "4.0.0"
21692212
resolved "https://registry.npmjs.org/is-glob/-/is-glob-4.0.0.tgz#9521c76845cc2610a85203ddf080a958c2ffabc0"
@@ -2761,6 +2804,10 @@ merge-source-map@^1.1.0:
27612804
dependencies:
27622805
source-map "^0.6.1"
27632806

2807+
merge2@^1.2.1:
2808+
version "1.2.2"
2809+
resolved "https://registry.npmjs.org/merge2/-/merge2-1.2.2.tgz#03212e3da8d86c4d8523cebd6318193414f94e34"
2810+
27642811
micromatch@^2.1.5:
27652812
version "2.3.11"
27662813
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-2.3.11.tgz#86677c97d1720b363431d04d0d15293bd38c1565"
@@ -3205,6 +3252,10 @@ pascalcase@^0.1.1:
32053252
version "0.1.1"
32063253
resolved "https://registry.npmjs.org/pascalcase/-/pascalcase-0.1.1.tgz#b363e55e8006ca6fe21784d2db22bd15d7917f14"
32073254

3255+
path-dirname@^1.0.0:
3256+
version "1.0.2"
3257+
resolved "https://registry.npmjs.org/path-dirname/-/path-dirname-1.0.2.tgz#cc33d24d525e099a5388c0336c6e32b9160609e0"
3258+
32083259
path-exists@^2.0.0:
32093260
version "2.1.0"
32103261
resolved "https://registry.yarnpkg.com/path-exists/-/path-exists-2.1.0.tgz#0feb6c64f0fc518d9a754dd5efb62c7022761f4b"

0 commit comments

Comments
 (0)