Skip to content

Commit

Permalink
ES6: Default parameter for arrow function expression.
Browse files Browse the repository at this point in the history
  • Loading branch information
ariya committed Apr 27, 2014
1 parent 8274902 commit e49e897
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 4 deletions.
20 changes: 16 additions & 4 deletions esprima.js
Original file line number Diff line number Diff line change
Expand Up @@ -1552,11 +1552,11 @@ parseStatement: true, parseSourceElement: true */
return this;
},

finishArrowFunctionExpression: function (params, body, expression) {
finishArrowFunctionExpression: function (params, defaults, body, expression) {
this.type = Syntax.ArrowFunctionExpression;
this.id = null;
this.params = params;
this.defaults = [];
this.defaults = defaults;
this.body = body;
this.rest = null;
this.generator = false;
Expand Down Expand Up @@ -2635,10 +2635,11 @@ parseStatement: true, parseSourceElement: true */
}

function reinterpretAsCoverFormalsList(expressions) {
var i, len, param, params, defaults, options, rest;
var i, len, param, params, defaults, defaultCount, options, rest;

params = [];
defaults = [];
defaultCount = 0;
rest = null;
options = {
paramSet: {}
Expand All @@ -2650,6 +2651,11 @@ parseStatement: true, parseSourceElement: true */
params.push(param);
defaults.push(null);
validateParam(options, param, param.name);
} else if (param.type === Syntax.AssignmentExpression) {
params.push(param.left);
defaults.push(param.right);
++defaultCount;
validateParam(options, param.left, param.left.name);
} else {
return null;
}
Expand All @@ -2662,6 +2668,10 @@ parseStatement: true, parseSourceElement: true */
);
}

if (defaultCount === 0) {
defaults = [];
}

return {
params: params,
defaults: defaults,
Expand Down Expand Up @@ -2689,7 +2699,7 @@ parseStatement: true, parseSourceElement: true */

strict = previousStrict;

return node.finishArrowFunctionExpression(options.params, body, body.type !== Syntax.BlockStatement);
return node.finishArrowFunctionExpression(options.params, options.defaults, body, body.type !== Syntax.BlockStatement);
}

// 11.13 Assignment Operators
Expand All @@ -2709,6 +2719,8 @@ parseStatement: true, parseSourceElement: true */
state.parenthesisCount === (oldParenthesisCount + 1)) {
if (expr.type === Syntax.Identifier) {
list = reinterpretAsCoverFormalsList([ expr ]);
} else if (expr.type === Syntax.AssignmentExpression) {
list = reinterpretAsCoverFormalsList([ expr ]);
} else if (expr.type === Syntax.SequenceExpression) {
list = reinterpretAsCoverFormalsList(expr.expressions);
} else if (expr === PlaceHolders.ArrowParameterPlaceHolder) {
Expand Down
177 changes: 177 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11355,6 +11355,73 @@ var testFixture = {
}
},

'(x=1) => x * x': {
type: 'ExpressionStatement',
expression: {
type: 'ArrowFunctionExpression',
id: null,
params: [{
type: 'Identifier',
name: 'x',
range: [1, 2],
loc: {
start: { line: 1, column: 1 },
end: { line: 1, column: 2 }
}
}],
defaults: [{
type: 'Literal',
value: 1,
raw: '1',
range: [3, 4],
loc: {
start: { line: 1, column: 3 },
end: { line: 1, column: 4 }
}
}],
body: {
type: 'BinaryExpression',
operator: '*',
left: {
type: 'Identifier',
name: 'x',
range: [9, 10],
loc: {
start: { line: 1, column: 9 },
end: { line: 1, column: 10 }
}
},
right: {
type: 'Identifier',
name: 'x',
range: [13, 14],
loc: {
start: { line: 1, column: 13 },
end: { line: 1, column: 14 }
}
},
range: [9, 14],
loc: {
start: { line: 1, column: 9 },
end: { line: 1, column: 14 }
}
},
rest: null,
generator: false,
expression: true,
range: [0, 14],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 14 }
}
},
range: [0, 14],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 14 }
}
},

// not strict mode, using eval
'eval => 42': {
type: 'ExpressionStatement',
Expand Down Expand Up @@ -11531,6 +11598,116 @@ var testFixture = {
}
},

// not strict mode, assigning to eval
'(eval = 10) => 42': {
type: 'ExpressionStatement',
expression: {
type: 'ArrowFunctionExpression',
id: null,
params: [{
type: 'Identifier',
name: 'eval',
range: [1, 5],
loc: {
start: { line: 1, column: 1 },
end: { line: 1, column: 5 }
}
}],
defaults: [{
type: 'Literal',
value: 10,
raw: '10',
range: [8, 10],
loc: {
start: { line: 1, column: 8 },
end: { line: 1, column: 10 }
}
}],
body: {
type: 'Literal',
value: 42,
raw: '42',
range: [15, 17],
loc: {
start: { line: 1, column: 15 },
end: { line: 1, column: 17 }
}
},
rest: null,
generator: false,
expression: true,
range: [0, 17],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 17 }
}
},
range: [0, 17],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 17 }
}
},

// not strict mode, using eval, IsSimpleParameterList is false
'(eval, a = 10) => 42': {
type: 'ExpressionStatement',
expression: {
type: 'ArrowFunctionExpression',
id: null,
params: [{
type: 'Identifier',
name: 'eval',
range: [1, 5],
loc: {
start: { line: 1, column: 1 },
end: { line: 1, column: 5 }
}
}, {
type: 'Identifier',
name: 'a',
range: [7, 8],
loc: {
start: { line: 1, column: 7 },
end: { line: 1, column: 8 }
}
}],
defaults: [null, {
type: 'Literal',
value: 10,
raw: '10',
range: [11, 13],
loc: {
start: { line: 1, column: 11 },
end: { line: 1, column: 13 }
}
}],
body: {
type: 'Literal',
value: 42,
raw: '42',
range: [18, 20],
loc: {
start: { line: 1, column: 18 },
end: { line: 1, column: 20 }
}
},
rest: null,
generator: false,
expression: true,
range: [0, 20],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 20 }
}
},
range: [0, 20],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 20 }
}
},

'(x => x)': {
type: 'ExpressionStatement',
expression: {
Expand Down

0 comments on commit e49e897

Please sign in to comment.