7da126c0d9cb56e3f2466c0c0747a53312f3f8c7d6e19121a4f0b5926c602ca961fb34eb2cfdd4cdd93cb1667b05b92ec472499fc052ddacef50ef0618a75e 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # babel-plugin-syntax-trailing-function-commas
  2. Compile trailing function commas to ES5
  3. ```js
  4. function clownPuppiesEverywhere(
  5. param1,
  6. param2,
  7. ) { /* ... */ }
  8. clownPuppiesEverywhere(
  9. 'foo',
  10. 'bar',
  11. );
  12. ```
  13. [Try in REPL](http://babeljs.io/repl/#?evaluate=true&presets=es2015%2Cstage-0&code=function%20clownPuppiesEverywhere(%0A%20%20param1%2C%0A%20%20param2%2C%0A)%20%7B%20%2F*%20...%20*%2F%20%7D%0A%0AclownPuppiesEverywhere(%0A%20%20'foo'%2C%0A%20%20'bar'%2C%0A)%3B)
  14. ## Example
  15. ### Basic
  16. This is an example from the [Proposal](https://github.com/jeffmo/es-trailing-function-commas).
  17. Let's say you have this function:
  18. ```js
  19. function clownPuppiesEverywhere(
  20. param1,
  21. param2
  22. ) { /* ... */ }
  23. clownPuppiesEverywhere(
  24. 'foo',
  25. 'bar'
  26. );
  27. ```
  28. If you want to have a new parameter called `param3`, the diff output would be like that:
  29. ```diff
  30. function clownPuppiesEverywhere(
  31. param1,
  32. - param2
  33. + param2, // Change this line to add a comma
  34. + param3 // Add param3
  35. ) { /* ... */ }
  36. clownPuppiesEverywhere(
  37. 'foo',
  38. - 'bar'
  39. + 'bar', // Change this line to add a comma
  40. + 'baz' // Add param3
  41. );
  42. ```
  43. In total, you have to change 2 lines for the function declaration and 2 lines for each usage.
  44. If you had your function defined with trailing commas:
  45. ```js
  46. function clownPuppiesEverywhere(
  47. param1,
  48. param2,
  49. ) { /* ... */ }
  50. clownPuppiesEverywhere(
  51. 'foo',
  52. 'bar',
  53. );
  54. ```
  55. Adding a new parameter would only change one line in the function declaration and one line for each usage:
  56. ```diff
  57. function clownPuppiesEverywhere(
  58. param1,
  59. param2,
  60. + param3, // Add param3
  61. ) { /* ... */ }
  62. clownPuppiesEverywhere(
  63. 'foo',
  64. 'bar',
  65. + 'baz', // Add param3
  66. );
  67. ```
  68. In the end, your diff output will be cleaner and easier to read, it would be much quicker to add a new parameter to your functions, it also makes it easier to copy paste elements and move code around.
  69. ## Installation
  70. ```sh
  71. npm install --save-dev babel-plugin-syntax-trailing-function-commas
  72. ```
  73. ## Usage
  74. ### Via `.babelrc` (Recommended)
  75. **.babelrc**
  76. ```json
  77. {
  78. "plugins": ["syntax-trailing-function-commas"]
  79. }
  80. ```
  81. ### Via CLI
  82. ```sh
  83. babel --plugins syntax-trailing-function-commas script.js
  84. ```
  85. ### Via Node API
  86. ```javascript
  87. require("babel-core").transform("code", {
  88. plugins: ["syntax-trailing-function-commas"]
  89. });
  90. ```
  91. ## References
  92. * [Proposal](https://github.com/jeffmo/es-trailing-function-commas)
  93. * [Spec](http://jeffmo.github.io/es-trailing-function-commas/)
  94. * [Why you should enforce Dangling Commas for Multiline Statements](https://medium.com/@nikgraf/why-you-should-enforce-dangling-commas-for-multiline-statements-d034c98e36f8)