Skip to content

Commit

Permalink
Allow string and numeric literal as getter or setter name.
Browse files Browse the repository at this point in the history
Patch by Yusuke Suzuki <utatane.tea@gmail.com>.

http://code.google.com/p/esprima/issues/detail?id=119
  • Loading branch information
ariya committed Jan 5, 2012
1 parent aa84b0f commit ec37399
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 18 deletions.
54 changes: 36 additions & 18 deletions esprima.js
Expand Up @@ -968,36 +968,54 @@ parseStatement: true, parseSourceElement: true */

if (token.value === 'get' && !match(':')) {
token = lex();
if (!isIdentifierName(token)) {
if (!isIdentifierName(token) &&
token.type !== Token.StringLiteral &&
token.type !== Token.NumericLiteral) {
throwUnexpected(token);
}
expect('(');
expect(')');
property = {
key: {
if (token.type === Token.StringLiteral ||
token.type === Token.NumericLiteral) {
property.key = {
type: Syntax.Literal,
value: token.value
};
} else {
property.key = {
type: Syntax.Identifier,
name: token.value
},
value: {
type: Syntax.FunctionExpression,
id: null,
params: [],
body: parseBlock()
},
kind: 'get'
};
}
expect('(');
expect(')');
property.value = {
type: Syntax.FunctionExpression,
id: null,
params: [],
body: parseBlock()
};
property.kind = 'get';
break;
}

if (token.value === 'set' && !match(':')) {
token = lex();
if (!isIdentifierName(token)) {
if (!isIdentifierName(token) &&
token.type !== Token.StringLiteral &&
token.type !== Token.NumericLiteral) {
throwUnexpected(token);
}
property.key = {
type: Syntax.Identifier,
name: token.value
};
if (token.type === Token.StringLiteral ||
token.type === Token.NumericLiteral) {
property.key = {
type: Syntax.Literal,
value: token.value
};
} else {
property.key = {
type: Syntax.Identifier,
name: token.value
};
}
expect('(');
token = lex();
if (token.type !== Token.Identifier) {
Expand Down
158 changes: 158 additions & 0 deletions test/test.js
Expand Up @@ -682,6 +682,68 @@ data = {
}
},

'x = { get "undef"() {} }': {
type: 'ExpressionStatement',
expression: {
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'Identifier',
name: 'x'
},
right: {
type: 'ObjectExpression',
properties: [{
key: {
type: 'Literal',
value: 'undef'
},
value: {
type: 'FunctionExpression',
id: null,
params: [],
body: {
type: 'BlockStatement',
body: []
}
},
kind: 'get'
}]
}
}
},

'x = { get 10() {} }': {
type: 'ExpressionStatement',
expression: {
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'Identifier',
name: 'x'
},
right: {
type: 'ObjectExpression',
properties: [{
key: {
type: 'Literal',
value: 10
},
value: {
type: 'FunctionExpression',
id: null,
params: [],
body: {
type: 'BlockStatement',
body: []
}
},
kind: 'get'
}]
}
}
},

'x = { set width(w) { m_width = w } }': {
type: 'ExpressionStatement',
expression: {
Expand Down Expand Up @@ -922,6 +984,102 @@ data = {
}
},

'x = { set "null"(w) { m_null = w } }': {
type: 'ExpressionStatement',
expression: {
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'Identifier',
name: 'x'
},
right: {
type: 'ObjectExpression',
properties: [{
key: {
type: 'Literal',
value: 'null'
},
value: {
type: 'FunctionExpression',
id: null,
params: [{
type: 'Identifier',
name: 'w'
}],
body: {
type: 'BlockStatement',
body: [{
type: 'ExpressionStatement',
expression: {
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'Identifier',
name: 'm_null'
},
right: {
type: 'Identifier',
name: 'w'
}
}
}]
}
},
kind: 'set'
}]
}
}
},

'x = { set 10(w) { m_null = w } }': {
type: 'ExpressionStatement',
expression: {
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'Identifier',
name: 'x'
},
right: {
type: 'ObjectExpression',
properties: [{
key: {
type: 'Literal',
value: 10
},
value: {
type: 'FunctionExpression',
id: null,
params: [{
type: 'Identifier',
name: 'w'
}],
body: {
type: 'BlockStatement',
body: [{
type: 'ExpressionStatement',
expression: {
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'Identifier',
name: 'm_null'
},
right: {
type: 'Identifier',
name: 'w'
}
}
}]
}
},
kind: 'set'
}]
}
}
},

// This should not be treated as getter.

'x = { get: 42 }': {
Expand Down

0 comments on commit ec37399

Please sign in to comment.