Skip to content

Commit 0f7ee6e

Browse files
committed
Merge pull request #22 from eslint/issue21
Fix: Parsing issues with property methods (fixes #21)
2 parents 4dd236c + 5f6242c commit 0f7ee6e

32 files changed

+2212
-15
lines changed

espree.js

+37-15
Original file line numberDiff line numberDiff line change
@@ -1947,7 +1947,7 @@ function parsePropertyFunction(params, first) {
19471947
body;
19481948

19491949
params = params || [];
1950-
body = parseConciseBody();
1950+
body = parseFunctionSourceElements();
19511951

19521952
if (first && strict && syntax.isRestrictedWord(params[0].name)) {
19531953
throwErrorTolerant(first, Messages.StrictParamName);
@@ -2021,15 +2021,20 @@ function parseObjectProperty() {
20212021

20222022
id = parseObjectPropertyKey();
20232023

2024-
// Property Assignment: Getter and Setter.
2025-
if (token.value === "get" && !match(":")) {
2024+
/*
2025+
* Check for getters and setters. Be careful! "get" and "set" are legal
2026+
* method names. It's only a getter or setter if followed by a space.
2027+
*/
2028+
if (token.value === "get" && !match(":") && !match("(")) {
2029+
computed = (lookahead.value === "[");
20262030
key = parseObjectPropertyKey();
20272031
expect("(");
20282032
expect(")");
20292033
value = parsePropertyFunction([]);
2030-
return delegate.markEnd(delegate.createProperty("get", key, value, false, false, false), startToken);
2034+
return delegate.markEnd(delegate.createProperty("get", key, value, false, false, computed), startToken);
20312035
}
2032-
if (token.value === "set" && !match(":")) {
2036+
if (token.value === "set" && !match(":") && !match("(")) {
2037+
computed = (lookahead.value === "[");
20332038
key = parseObjectPropertyKey();
20342039
expect("(");
20352040
token = lookahead;
@@ -2042,7 +2047,7 @@ function parseObjectProperty() {
20422047
expect(")");
20432048
value = parsePropertyFunction(param, token);
20442049
}
2045-
return delegate.markEnd(delegate.createProperty("set", key, value, false, false, false), startToken);
2050+
return delegate.markEnd(delegate.createProperty("set", key, value, false, false, computed), startToken);
20462051
}
20472052

20482053
// normal property (key:value)
@@ -2073,10 +2078,27 @@ function parseObjectProperty() {
20732078
if (token.type === Token.EOF || token.type === Token.Punctuator) {
20742079
throwUnexpected(token);
20752080
} else {
2081+
2082+
/*
2083+
* If we've made it here, then that means the property name is represented
2084+
* by a string (i.e, { "foo": 2}). The only options here are normal
2085+
* property with a colon or a method.
2086+
*/
20762087
key = parseObjectPropertyKey();
2077-
expect(":");
2078-
value = parseAssignmentExpression();
2079-
return delegate.markEnd(delegate.createProperty("init", key, value, false, false, false), startToken);
2088+
2089+
// check for property value
2090+
if (match(":")) {
2091+
lex();
2092+
return delegate.markEnd(delegate.createProperty("init", key, parseAssignmentExpression(), false, false, false), startToken);
2093+
}
2094+
2095+
// check for method
2096+
if (allowMethod && match("(")) {
2097+
return delegate.markEnd(delegate.createProperty("init", key, parsePropertyMethodFunction(), true, false, false), startToken);
2098+
}
2099+
2100+
// no other options, this is bad
2101+
throwUnexpected(lex());
20802102
}
20812103
}
20822104

@@ -3302,12 +3324,12 @@ function parseStatement() {
33023324

33033325
// 13 Function Definition
33043326

3305-
function parseConciseBody() {
3306-
if (match("{")) {
3307-
return parseFunctionSourceElements();
3308-
}
3309-
return parseAssignmentExpression();
3310-
}
3327+
// function parseConciseBody() {
3328+
// if (match("{")) {
3329+
// return parseFunctionSourceElements();
3330+
// }
3331+
// return parseAssignmentExpression();
3332+
// }
33113333

33123334
function parseFunctionSourceElements() {
33133335
var sourceElement, sourceElements = [], token, directive, firstRestricted,

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"complexity-report": "~0.6.1",
4242
"dateformat": "^1.0.11",
4343
"eslint": "^0.9.1",
44+
"esprima": "git://github.com/ariya/esprima#harmony",
4445
"istanbul": "~0.2.6",
4546
"json-diff": "~0.3.1",
4647
"leche": "^1.0.1",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
module.exports = {
2+
"type": "Program",
3+
"body": [
4+
{
5+
"type": "ExpressionStatement",
6+
"expression": {
7+
"type": "ObjectExpression",
8+
"properties": [
9+
{
10+
"type": "Property",
11+
"key": {
12+
"type": "Identifier",
13+
"name": "x",
14+
"range": [
15+
3,
16+
4
17+
],
18+
"loc": {
19+
"start": {
20+
"line": 1,
21+
"column": 3
22+
},
23+
"end": {
24+
"line": 1,
25+
"column": 4
26+
}
27+
}
28+
},
29+
"value": {
30+
"type": "Literal",
31+
"value": 10,
32+
"raw": "10",
33+
"range": [
34+
7,
35+
9
36+
],
37+
"loc": {
38+
"start": {
39+
"line": 1,
40+
"column": 7
41+
},
42+
"end": {
43+
"line": 1,
44+
"column": 9
45+
}
46+
}
47+
},
48+
"kind": "init",
49+
"method": false,
50+
"shorthand": false,
51+
"computed": true,
52+
"range": [
53+
2,
54+
9
55+
],
56+
"loc": {
57+
"start": {
58+
"line": 1,
59+
"column": 2
60+
},
61+
"end": {
62+
"line": 1,
63+
"column": 9
64+
}
65+
}
66+
},
67+
{
68+
"type": "Property",
69+
"key": {
70+
"type": "Identifier",
71+
"name": "y",
72+
"range": [
73+
11,
74+
12
75+
],
76+
"loc": {
77+
"start": {
78+
"line": 1,
79+
"column": 11
80+
},
81+
"end": {
82+
"line": 1,
83+
"column": 12
84+
}
85+
}
86+
},
87+
"value": {
88+
"type": "Literal",
89+
"value": 20,
90+
"raw": "20",
91+
"range": [
92+
14,
93+
16
94+
],
95+
"loc": {
96+
"start": {
97+
"line": 1,
98+
"column": 14
99+
},
100+
"end": {
101+
"line": 1,
102+
"column": 16
103+
}
104+
}
105+
},
106+
"kind": "init",
107+
"method": false,
108+
"shorthand": false,
109+
"computed": false,
110+
"range": [
111+
11,
112+
16
113+
],
114+
"loc": {
115+
"start": {
116+
"line": 1,
117+
"column": 11
118+
},
119+
"end": {
120+
"line": 1,
121+
"column": 16
122+
}
123+
}
124+
}
125+
],
126+
"range": [
127+
1,
128+
17
129+
],
130+
"loc": {
131+
"start": {
132+
"line": 1,
133+
"column": 1
134+
},
135+
"end": {
136+
"line": 1,
137+
"column": 17
138+
}
139+
}
140+
},
141+
"range": [
142+
0,
143+
19
144+
],
145+
"loc": {
146+
"start": {
147+
"line": 1,
148+
"column": 0
149+
},
150+
"end": {
151+
"line": 1,
152+
"column": 19
153+
}
154+
}
155+
}
156+
],
157+
"range": [
158+
0,
159+
19
160+
],
161+
"loc": {
162+
"start": {
163+
"line": 1,
164+
"column": 0
165+
},
166+
"end": {
167+
"line": 1,
168+
"column": 19
169+
}
170+
}
171+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
({[x]: 10, y: 20});

0 commit comments

Comments
 (0)