-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: @putout/plugin-generators: add
- Loading branch information
1 parent
e674127
commit 360f0cc
Showing
29 changed files
with
446 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"extends": [ | ||
"plugin:n/recommended", | ||
"plugin:putout/recommended" | ||
], | ||
"plugins": [ | ||
"n", | ||
"putout" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
node_modules | ||
*.swp | ||
yarn-error.log | ||
|
||
coverage | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {run} from 'madrun'; | ||
|
||
export default { | ||
'test': () => `tape 'test/*.js' 'lib/**/*.spec.js'`, | ||
'watch:test': async () => `nodemon -w lib -w test -x ${await run('test')}`, | ||
'lint': () => `putout .`, | ||
'fresh:lint': () => run('lint', '--fresh'), | ||
'lint:fresh': () => run('lint', '--fresh'), | ||
'fix:lint': () => run('lint', '--fix'), | ||
'coverage': async () => `c8 ${await run('test')}`, | ||
'report': () => 'c8 report --reporter=lcov', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.* | ||
test | ||
yarn-error.log | ||
*.spec.js | ||
fixture | ||
|
||
|
||
coverage | ||
*.config.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"check-coverage": true, | ||
"all": true, | ||
"exclude": [ | ||
"**/*.spec.*", | ||
"**/fixture", | ||
"test", | ||
".*.*", | ||
"**/*.config.*" | ||
], | ||
"branches": 100, | ||
"lines": 100, | ||
"functions": 100, | ||
"statements": 100 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"printer": "putout", | ||
"match": { | ||
"*.md": { | ||
"generators": "off" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) coderaiser | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# @putout/plugin-generators [![NPM version][NPMIMGURL]][NPMURL] | ||
|
||
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-generators.svg?style=flat&longCache=true | ||
[NPMURL]: https://npmjs.org/package/@putout/plugin-generators"npm" | ||
|
||
> The `function*` declaration creates a binding of a new generator function to a given name. A generator function can be exited and later re-entered, with its context (variable bindings) saved across re-entrances. | ||
> | ||
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) | ||
🐊[**Putout**](https://github.com/coderaiser/putout) plugin improves `Generator`-related code. | ||
|
||
## Install | ||
|
||
``` | ||
npm i @putout/plugin-generators -D | ||
``` | ||
|
||
## Rules | ||
|
||
- ✅ [add-missing-star](#add-missing-star); | ||
- ✅ [convert-multiple-to-generator](#convert-multiply-to-generator); | ||
|
||
## Config | ||
|
||
```json | ||
{ | ||
"rules": { | ||
"generators/add-missing-star": "on", | ||
"generators/convert-multiple-to-generator": "on" | ||
} | ||
} | ||
``` | ||
|
||
## add-missing-star | ||
|
||
> The `function*` declaration creates a binding of a new generator function to a given name. | ||
> | ||
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*) | ||
### ❌ Example of incorrect code | ||
|
||
```js | ||
function hello() { | ||
yield; | ||
'world'; | ||
} | ||
|
||
function func2() { | ||
yield * func1(); | ||
} | ||
``` | ||
|
||
### ✅ Example of correct code | ||
|
||
```js | ||
function* hello() { | ||
yield 'world'; | ||
} | ||
|
||
function* func2() { | ||
yield* func1(); | ||
} | ||
``` | ||
|
||
## convert-multiply-to-generator | ||
|
||
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/1c5326b470677a2ab253963fe39971f2/860b7eafb0bdd33ec007e709e7825c47bbc38940). | ||
|
||
### ❌ Example of incorrect code | ||
|
||
```js | ||
const a = 5 * function hello() {}; | ||
``` | ||
|
||
### ✅ Example of correct code | ||
|
||
```js | ||
const a = 5; | ||
|
||
function* hello() {} | ||
``` | ||
|
||
## License | ||
|
||
MIT |
3 changes: 3 additions & 0 deletions
3
packages/plugin-generators/lib/add-missing-star/fixture/add-missing-star-fix.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function* x() { | ||
yield m; | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/plugin-generators/lib/add-missing-star/fixture/add-missing-star.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function x () { | ||
yield m; | ||
} |
1 change: 1 addition & 0 deletions
1
packages/plugin-generators/lib/add-missing-star/fixture/member.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
const ast = parse(fixture.yield); |
3 changes: 3 additions & 0 deletions
3
packages/plugin-generators/lib/add-missing-star/fixture/yield-star-fix.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function* func2() { | ||
yield* func1(); | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/plugin-generators/lib/add-missing-star/fixture/yield-star.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function func2() { | ||
yield* func1(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
|
||
const {types, operator} = require('putout'); | ||
const {remove, replaceWith} = operator; | ||
const {YieldExpression} = types; | ||
|
||
const DELEGATE = true; | ||
|
||
module.exports.report = () => `Add missing '*' in generator function`; | ||
|
||
module.exports.fix = (path) => { | ||
const fnPath = path.getFunctionParent(); | ||
|
||
fnPath.node.generator = true; | ||
|
||
if (path.parentPath.isExpressionStatement()) { | ||
const next = path.parentPath.getNextSibling(); | ||
const {expression} = next.node; | ||
|
||
replaceWith(path, YieldExpression(expression)); | ||
remove(next); | ||
|
||
return; | ||
} | ||
|
||
const {parentPath} = path; | ||
const {right} = parentPath.node; | ||
|
||
replaceWith(parentPath, YieldExpression(right, DELEGATE)); | ||
}; | ||
|
||
module.exports.traverse = ({push}) => ({ | ||
Identifier(path) { | ||
if (path.node.name !== 'yield') | ||
return; | ||
|
||
if (path.parentPath.isBinaryExpression({operator: '*'})) { | ||
push(path); | ||
return; | ||
} | ||
|
||
if (!path.parentPath.isExpressionStatement()) | ||
return; | ||
|
||
push(path); | ||
}, | ||
}); |
31 changes: 31 additions & 0 deletions
31
packages/plugin-generators/lib/add-missing-star/index.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
const {createTest} = require('@putout/test'); | ||
const convert = require('.'); | ||
|
||
const test = createTest(__dirname, { | ||
printer: 'putout', | ||
plugins: [ | ||
['promises/add-missing-star', convert], | ||
], | ||
}); | ||
|
||
test('plugin-add-missing-star: exports: report', (t) => { | ||
t.report('add-missing-star', `Add missing '*' in generator function`); | ||
t.end(); | ||
}); | ||
|
||
test('plugin-add-missing-star: no report: member', (t) => { | ||
t.noReport('member'); | ||
t.end(); | ||
}); | ||
|
||
test('plugin-add-missing-star: transform', (t) => { | ||
t.transform('add-missing-star'); | ||
t.end(); | ||
}); | ||
|
||
test('plugin-add-missing-star: transform: yield-star', (t) => { | ||
t.transform('yield-star'); | ||
t.end(); | ||
}); |
10 changes: 10 additions & 0 deletions
10
...generators/lib/convert-multiply-to-generator/fixture/convert-multiply-to-generator-fix.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const c = 1 * 2; | ||
1 + function() {}; | ||
|
||
const a = 5; | ||
|
||
function* hello() {}; | ||
let b = 5 * function() {}; | ||
|
||
b = 5; | ||
function* world() {}; |
8 changes: 8 additions & 0 deletions
8
...gin-generators/lib/convert-multiply-to-generator/fixture/convert-multiply-to-generator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const c = 1 * 2; | ||
1 + function() {}; | ||
|
||
const a = 5 | ||
*function hello() {}; | ||
|
||
let b = 5 * function() {}; | ||
b = 5 * function world() {}; |
37 changes: 37 additions & 0 deletions
37
packages/plugin-generators/lib/convert-multiply-to-generator/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
const {types, operator} = require('putout'); | ||
|
||
const { | ||
insertAfter, | ||
replaceWith, | ||
getTemplateValues, | ||
} = operator; | ||
|
||
const {isStatement} = types; | ||
const MULTIPLY = '__a * __b'; | ||
|
||
module.exports.report = () => `Use 'if condition' instead of 'ternary expression'`; | ||
|
||
module.exports.fix = (path) => { | ||
const a = path.find(isStatement); | ||
const {__a, __b} = getTemplateValues(path, MULTIPLY); | ||
|
||
replaceWith(path, __a); | ||
insertAfter(a, __b); | ||
__b.generator = true; | ||
}; | ||
|
||
module.exports.traverse = ({push}) => ({ | ||
[MULTIPLY]: (path) => { | ||
const rightPath = path.get('right'); | ||
|
||
if (!rightPath.isFunctionExpression()) | ||
return; | ||
|
||
if (!rightPath.node.id) | ||
return; | ||
|
||
push(path); | ||
}, | ||
}); |
21 changes: 21 additions & 0 deletions
21
packages/plugin-generators/lib/convert-multiply-to-generator/index.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
const {createTest} = require('@putout/test'); | ||
const plugin = require('.'); | ||
|
||
const test = createTest(__dirname, { | ||
printer: 'putout', | ||
plugins: [ | ||
['convert-multiply-to-generator', plugin], | ||
], | ||
}); | ||
|
||
test('generators: convert-multiply-to-generator: report', (t) => { | ||
t.report('convert-multiply-to-generator', `Use 'if condition' instead of 'ternary expression'`); | ||
t.end(); | ||
}); | ||
|
||
test('generators: convert-multiply-to-generator: transform', (t) => { | ||
t.transform('convert-multiply-to-generator'); | ||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
const addMissingStar = require('./add-missing-star'); | ||
const convertMultiplyToGenerator = require('./convert-multiply-to-generator'); | ||
|
||
module.exports.rules = { | ||
'add-missing-star': addMissingStar, | ||
'convert-multiply-to-generator': convertMultiplyToGenerator, | ||
}; |
Oops, something went wrong.