-
-
Notifications
You must be signed in to change notification settings - Fork 15
Add support for ES6 function notations #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for ES6 function notations #16
Conversation
3c5d313 to
f766fb7
Compare
javascript-stringify.js
Outdated
| var prefix = isGeneratorFunction(fn) ? '*' : ''; | ||
|
|
||
| // Was this function defined with method notation? | ||
| if (fn.name === key && fnString.startsWith(prefix + key + '(')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a heads up but startsWith is ES2015. Probably not an issue and will look at updating to ES2015 soon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, good catch. (So is repeat, which I also used.)
Ensure that function values produced with the following ES6 notations
are stringified into valid code:
* Arrow functions `(a, b) => a + b`
* Generators `function* (x) { yield x; }`
* Method notation `{ add(a, b) { return a + b; } }`
Remove extra indentation from function bodies before stringifying them, where extra indentation is determined by finding the line in the function body (after the first line) with the fewest initial space characters.
f766fb7 to
6be4cd4
Compare
|
PR updated to not use ES6 String methods. |
|
Hey @blakeembrey, are there any other concerns about this patch? |
|
Sorry for the delay in merging this, I disappeared from OSS for a while. I just tried landing this but it looks like things have changed in serializing function names in the latest node.js release. I'll try tweaking the code a little to rely less on the generated output of |
|
Here are some unfortunate conclusions I've arrived at regarding correctness in different node.js versions. Just read the bolded statements if you hate that I wrote a whole essay. Conclusion 1: For node.js versions between 6 and 8, it is impossible for Consider the following pair of functions. f = ({'()=>function'(){}})['()=>function']
g = ({'()=>function':()=>function(){}})['()=>function']These functions are clearly fundamentally different; Conclusion 2: Any implementation of In other words, there exist two different function-valued expressions Actually constructing Working out a suffix is a relatively simple trick with block comments. Working out a computed property that evaluates to itself plus the suffix is basically constructing a quine, which can be done pretty mechanically although the results aren't pretty. See below: In node.js 10: o = {
[((s,a,b)=>a+s(a)+","+s(b)+b)(JSON.stringify,"[((s,a,b)=>a+s(a)+\",\"+s(b)+b)(JSON.stringify,",")]() { return 0; /*")]() { return 0; /*() {/* */ return 1;}
};
f = o['[((s,a,b)=>a+s(a)+","+s(b)+b)(JSON.stringify,"[((s,a,b)=>a+s(a)+\\",\\"+s(b)+b)(JSON.stringify,",")]() { return 0; /*")]() { return 0; /*'];And in node.js 4: o = {
'[((s,a,b)=>a+s(a)+","+s(b)+b)(JSON.stringify,"[((s,a,b)=>a+s(a)+\\",\\"+s(b)+b)(JSON.stringify,",")]() { return 0; /*")]() { return 0; /*'() {/* */ return 1;}
};
g = o['[((s,a,b)=>a+s(a)+","+s(b)+b)(JSON.stringify,"[((s,a,b)=>a+s(a)+\\",\\"+s(b)+b)(JSON.stringify,",")]() { return 0; /*")]() { return 0; /*'];If you run these snippets in their respective engines, you'll see that both This is especially sad because the only options I can think of for detecting whether methods are
Do you have thoughts on any of the above? |
|
It sounds like the best approach is to substring the prefix matching against I haven't quite grokked the difference between the two |
|
To be continued in #22... |
This PR contains two improvements for stringifying functions. The first is support for ensuring that function values produced with the following ES6 function notations are stringified into valid code:
(a, b) => a + bfunction* (x) { yield x; }{ add(a, b) { return a + b; } }The second, which applies to all functions, improves function body indentation by removing extra indentation from function bodies before stringifying them, where extra indentation is determined by finding the line in the function body (after the first line) with the fewest initial space characters.