lepu-test-platform-web/node_modules/babel-plugin-syntax-trailin...
zhangbo db3ebd0df0 把之前百度云的代码移植过来 2024-01-03 15:23:47 +08:00
..
lib 把之前百度云的代码移植过来 2024-01-03 15:23:47 +08:00
.npmignore 把之前百度云的代码移植过来 2024-01-03 15:23:47 +08:00
README.md 把之前百度云的代码移植过来 2024-01-03 15:23:47 +08:00
package.json 把之前百度云的代码移植过来 2024-01-03 15:23:47 +08:00

README.md

babel-plugin-syntax-trailing-function-commas

Compile trailing function commas to ES5

function clownPuppiesEverywhere(
  param1,
  param2,
) { /* ... */ }

clownPuppiesEverywhere(
  'foo',
  'bar',
);

Try in REPL

Example

Basic

This is an example from the Proposal.

Let's say you have this function:

function clownPuppiesEverywhere(
  param1,
  param2
) { /* ... */ }

clownPuppiesEverywhere(
  'foo',
  'bar'
);

If you want to have a new parameter called param3, the diff output would be like that:

function clownPuppiesEverywhere(
  param1,
- param2
+ param2, // Change this line to add a comma
+ param3  // Add param3
) { /* ... */ }

clownPuppiesEverywhere(
  'foo',
- 'bar'
+ 'bar', // Change this line to add a comma
+ 'baz'  // Add param3
);

In total, you have to change 2 lines for the function declaration and 2 lines for each usage.

If you had your function defined with trailing commas:

function clownPuppiesEverywhere(
  param1,
  param2,
) { /* ... */ }

clownPuppiesEverywhere(
  'foo',
  'bar',
);

Adding a new parameter would only change one line in the function declaration and one line for each usage:

function clownPuppiesEverywhere(
  param1,
  param2,
+ param3, // Add param3
) { /* ... */ }

clownPuppiesEverywhere(
  'foo',
  'bar',
+ 'baz', // Add param3
);

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.

Installation

npm install --save-dev babel-plugin-syntax-trailing-function-commas

Usage

.babelrc

{
  "plugins": ["syntax-trailing-function-commas"]
}

Via CLI

babel --plugins syntax-trailing-function-commas script.js

Via Node API

require("babel-core").transform("code", {
  plugins: ["syntax-trailing-function-commas"]
});

References