yahoo / yahoo/serialize-javascript
Shorthand method definition fails to serialize and eval property
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 2.9k
- Forks
- 215
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 2
Description
*properly
When using shorthand to define a function as a method in an object literal, serialization assigns a named function expression, which fails to evaluate.
$ var testSer = { fn1: function(a) { console.log(a); return this; }, fn2: (d) => { console.log(d); return this; }, fn3(r) { console.log(r); return this; } };
Validate the functions first:
test 1
{ fn1: [Function], fn2: [Function], fn3: [Function: fn3] }
$ testSer.fn2('test 2')
test 2
<context>
$ testSer.fn3('test 3')
test 3
{ fn1: [Function], fn2: [Function], fn3: [Function: fn3] }
Do the serialization:
$ ca
'{"fn1":function (a) { console.log(a); return this; },"fn2":(d) => { console.log(d); return this; },"fn3":fn3(r) { console.log(r); return this; }}'
Note how it has created a "fn3" entry as a named function, but has neglected to add the function keyword.
Now try to eval:
$ var c = eval('('+ca+')');
SyntaxError: Unexpected token {
at repl:1:20
at REPLServer.defaultEval (repl.js:262:27)
at bound (domain.js:287:14)
at REPLServer.runBound [as eval] (domain.js:300:12)
at REPLServer.<anonymous> (repl.js:431:12)
at emitOne (events.js:82:20)
at REPLServer.emit (events.js:169:7)
at REPLServer.Interface._onLine (readline.js:211:10)
at REPLServer.Interface._line (readline.js:550:8)
at REPLServer.Interface._ttyWrite (readline.js:827:14)
at ReadStream.onkeypress (readline.js:106:10)
at emitTwo (events.js:87:13)
at ReadStream.emit (events.js:172:7)
at emitKeys (readline.js:1251:14)
at next (native)
at ReadStream.onData (readline.js:919:36)
at emitOne (events.js:77:13)
at ReadStream.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:153:18)
at ReadStream.Readable.push (_stream_readable.js:111:10)
at TTY.onread (net.js:531:20)
All it needs to do is add the function keyword:
'{"fn1":function (a) { console.log(a); return this; },"fn2":(d) => { console.log(d); return this; },"fn3": function fn3(r) { console.log(r); return this; }}'
Node version:
$ node -v
v4.4.3
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the issue with the serialize-javascript call shown in the report, focusing on the fn3 shorthand method output. Compare its serialized form with fn1 and fn2, then evaluate the result to confirm the syntax error is gone. Done means shorthand methods serialize to valid evaluable JavaScript while preserving the method name.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100