Skip to content

Commit

Permalink
feature: @putout/plugin-generators: add
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed May 10, 2024
1 parent e674127 commit 360f0cc
Show file tree
Hide file tree
Showing 29 changed files with 446 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,7 @@ It has a lot plugins divided by groups:
| [`@putout/plugin-eslint`](/packages/plugin-eslint#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-eslint.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-eslint) |
| [`@putout/plugin-package-json`](/packages/plugin-package-json#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-package-json.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-package-json) |
| [`@putout/plugin-promises`](/packages/plugin-promises#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-promises.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-promises) |
| [`@putout/plugin-generators`](/packages/plugin-generators#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-generators.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-generators) |
| [`@putout/plugin-gitignore`](/packages/plugin-gitignore#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-gitignore.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-gitignore) |
| [`@putout/plugin-npmignore`](/packages/plugin-npmignore#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-npmignore.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-npmignore) |
| [`@putout/plugin-coverage`](/packages/plugin-coverage#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-coverage.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-coverage) |
Expand Down
10 changes: 10 additions & 0 deletions packages/plugin-generators/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": [
"plugin:n/recommended",
"plugin:putout/recommended"
],
"plugins": [
"n",
"putout"
]
}
6 changes: 6 additions & 0 deletions packages/plugin-generators/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
*.swp
yarn-error.log

coverage
.idea
12 changes: 12 additions & 0 deletions packages/plugin-generators/.madrun.mjs
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',
};
9 changes: 9 additions & 0 deletions packages/plugin-generators/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.*
test
yarn-error.log
*.spec.js
fixture


coverage
*.config.*
15 changes: 15 additions & 0 deletions packages/plugin-generators/.nycrc.json
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
}
8 changes: 8 additions & 0 deletions packages/plugin-generators/.putout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"printer": "putout",
"match": {
"*.md": {
"generators": "off"
}
}
}
21 changes: 21 additions & 0 deletions packages/plugin-generators/LICENSE
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.
85 changes: 85 additions & 0 deletions packages/plugin-generators/README.md
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function* x() {
yield m;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function x () {
yield m;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const ast = parse(fixture.yield);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function* func2() {
yield* func1();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function func2() {
yield* func1();
}
47 changes: 47 additions & 0 deletions packages/plugin-generators/lib/add-missing-star/index.js
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 packages/plugin-generators/lib/add-missing-star/index.spec.js
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();
});
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() {};
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() {};
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);
},
});
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();
});
9 changes: 9 additions & 0 deletions packages/plugin-generators/lib/index.js
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,
};
Loading

0 comments on commit 360f0cc

Please sign in to comment.