Skip to content

Commit 4eb7a3f

Browse files
committed
feature: @putout/plugin-apply-arrow: add
1 parent 46d4481 commit 4eb7a3f

File tree

17 files changed

+273
-0
lines changed

17 files changed

+273
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,17 @@ function world(a) {
13081308

13091309
</details>
13101310

1311+
<details><summary>apply arrow</summary>
1312+
1313+
```diff
1314+
-export function hello() {
1315+
- return 'world';
1316+
-}
1317+
+export const hello = () => 'world';
1318+
```
1319+
1320+
</details>
1321+
13111322
<details><summary>apply destructuring</summary>
13121323

13131324
```diff
@@ -2103,6 +2114,7 @@ It has a lot of plugins divided by groups:
21032114

21042115
| Package | Version |
21052116
|--------|-------|
2117+
| [`@putout/plugin-apply-arrow`](/packages/plugin-apply-arrow#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-apply-arrow.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-apply-arrow) |
21062118
| [`@putout/plugin-apply-consistent-blocks`](/packages/plugin-apply-consistent-blocks#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-apply-consistent-blocks.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-apply-consistent-blocks) |
21072119
| [`@putout/plugin-apply-at`](/packages/plugin-apply-at#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-apply-at.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-apply-at) |
21082120
| [`@putout/plugin-apply-dot-notation`](/packages/plugin-apply-dot-notation#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-apply-dot-notation.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-apply-dot-notation) |
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": [
3+
"plugin:n/recommended",
4+
"plugin:putout/recommended"
5+
],
6+
"plugins": [
7+
"n",
8+
"putout"
9+
]
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
*.swp
3+
yarn-error.log
4+
5+
coverage
6+
.idea
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import {run} from 'madrun';
2+
3+
export default {
4+
'test': () => `tape 'lib/**/*.spec.js' 'test/*.js'`,
5+
'watch:test': async () => `nodemon -w lib -w test -x ${await run('test')}`,
6+
'lint': () => `putout .`,
7+
'fresh:lint': () => run('lint', '--fresh'),
8+
'lint:fresh': () => run('lint', '--fresh'),
9+
'fix:lint': () => run('lint', '--fix'),
10+
'coverage': async () => `c8 ${await run('test')}`,
11+
'report': () => 'c8 report --reporter=lcov',
12+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.*
2+
test
3+
yarn-error.log
4+
fixture
5+
*.spec.js
6+
7+
coverage
8+
*.config.*
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"check-coverage": true,
3+
"all": true,
4+
"exclude": [
5+
"**/*.spec.*",
6+
"**/fixture",
7+
"test",
8+
".*.*",
9+
"**/*.config.*"
10+
],
11+
"branches": 100,
12+
"lines": 100,
13+
"functions": 100,
14+
"statements": 100
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"match": {
3+
"README.md": {
4+
"apply-arrow": "off",
5+
"putout/check-replace-code": "off"
6+
}
7+
}
8+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) coderaiser
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# @putout/plugin-apply-arrow [![NPM version][NPMIMGURL]][NPMURL]
2+
3+
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-apply-arrow.svg?style=flarrow&longCache=true
4+
[NPMURL]: https://npmjs.org/package/@putout/plugin-apply-arrow "npm"
5+
6+
> An arrow function expression is a compact alternative to a traditional function expression, with some semantic differences and deliberate limitations in usage:
7+
>
8+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)
9+
10+
🐊[**Putout**](https://github.com/coderaiser/putout) plugin adds ability to convert Function Declaration to Arrow Function. Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/828228d2515805afc682eab0fe0a4105/a2c6507dd197d04cbbd387e4fb414ef6548234cf).
11+
12+
## Install
13+
14+
```
15+
npm i @putout/plugin-apply-arrow
16+
```
17+
18+
## Rule
19+
20+
```json
21+
{
22+
"rules": {
23+
"apply-arrow": "on"
24+
}
25+
}
26+
```
27+
28+
## ❌ Example of incorrect code
29+
30+
```js
31+
export function replace() {
32+
return {
33+
'if __a > __b': 'if (__a > __b)',
34+
};
35+
}
36+
```
37+
38+
## ✅ Example of correct code
39+
40+
```js
41+
export const replace = () => ({
42+
'if __a > __b': 'if (__a > __b)',
43+
});
44+
```
45+
46+
## License
47+
48+
MIT
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
module.exports.report = () => `Use 'Arrow Function' instead of 'Function Declaration`;
4+
5+
module.exports.match = () => ({
6+
'function __a(__args) {return __b}': ({__a}, path) => {
7+
const {name} = __a;
8+
const binding = path.parentPath.scope.bindings[name];
9+
10+
if (path.parentPath.isExportNamedDeclaration())
11+
return binding.references === 1;
12+
13+
return !binding.referenced;
14+
},
15+
});
16+
17+
module.exports.replace = () => ({
18+
'function __a(__args) {return __b}': 'const __a = (__args) => __b',
19+
});

0 commit comments

Comments
 (0)