Skip to content

Commit

Permalink
feat: support arrow functions in Function/#/to-tring-tokens.js
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed May 22, 2017
1 parent 432e290 commit ad3de1e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions .lint
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ sub
./function/#/copy.js
./object/unserialize.js
./test/function/valid-function.js
./test/function/#/to-string-tokens.js
evil

./math/_pack-ieee754.js
Expand Down
15 changes: 11 additions & 4 deletions function/#/to-string-tokens.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
'use strict';

var validFunction = require('../valid-function')

var re = /^\s*function[\0-\'\)-\uffff]*\(([\0-\(\*-\uffff]*)\)\s*\{([\0-\uffff]*)\}\s*$/;
var validFunction = require('../valid-function');

var re1 = /^\s*function[\0-\'\)-\uffff]*\(([\0-\(\*-\uffff]*)\)\s*\{([\0-\uffff]*)\}\s*$/
, re2 = /^\s*\(?([\0-\'\*-\uffff]*)\)?\s*=>\s*(\{?[\0-\uffff]*\}?)\s*$/;

module.exports = function () {
var data = String(validFunction(this)).match(re);
var str = String(validFunction(this))
, data = str.match(re1);
if (!data) {
data = str.match(re2);
if (!data) throw new Error("Unrecognized string format");
data[1] = data[1].trim();
if (data[2][0] === '{') data[2] = data[2].trim().slice(1, -1);
}
return { args: data[1], body: data[2] };
};
12 changes: 12 additions & 0 deletions test/function/#/to-string-tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@ module.exports = function (t, a) {
{ args: 'raz', body: '' });
a.deep(t.call(function () { Object(); }),
{ args: '', body: ' Object(); ' });

try {
eval("(() => {})");
} catch (e) {
// Non ES2015 env
return;
}

a.deep(t.call(eval("(() => {})")), { args: '', body: '' });
a.deep(t.call(eval("((elo) => foo)")), { args: 'elo', body: 'foo' });
a.deep(t.call(eval("(elo => foo)")), { args: 'elo', body: 'foo' });
a.deep(t.call(eval("((elo, bar) => foo())")), { args: 'elo, bar', body: 'foo()' });
};

0 comments on commit ad3de1e

Please sign in to comment.