Skip to content

Commit

Permalink
feature: @putout/plugin-putout: apply-declare: add
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Mar 10, 2023
1 parent ec12ee7 commit d289312
Show file tree
Hide file tree
Showing 27 changed files with 119 additions and 74 deletions.
5 changes: 0 additions & 5 deletions packages/plugin-madrun/lib/declare/declarations.js

This file was deleted.

9 changes: 3 additions & 6 deletions packages/plugin-madrun/lib/declare/index.js
@@ -1,8 +1,5 @@
'use strict';

const {operator} = require('putout');
const declarations = require('./declarations');

const {declare} = operator;

module.exports = declare(declarations);
module.exports.declare = () => ({
cutEnv: 'import {cutEnv} from "madrun"',
});
2 changes: 1 addition & 1 deletion packages/plugin-madrun/lib/declare/index.spec.js
Expand Up @@ -8,7 +8,7 @@ const test = createTest(__dirname, {
});

test('plugin-madrun: declare: report', (t) => {
t.report('cut-env', `Declare 'cutEnv'`);
t.report('cut-env', `Declare 'cutEnv', it referenced but not defined`);
t.end();
});

Expand Down
7 changes: 0 additions & 7 deletions packages/plugin-maybe/lib/declare/declarations.js

This file was deleted.

11 changes: 5 additions & 6 deletions packages/plugin-maybe/lib/declare/index.js
@@ -1,8 +1,7 @@
'use strict';

const {operator} = require('putout');
const declarations = require('./declarations');

const {declare} = operator;

module.exports = declare(declarations);
module.exports.declare = () => ({
maybeArray: 'const maybeArray = (a) => isArray(a) ? a : [a]',
maybeEmptyArray: 'const maybeEmptyArray = (a) => !a ? [] : a',
maybeFn: 'const maybeFn = (a) => isFn(a) ? a : noop',
});
2 changes: 1 addition & 1 deletion packages/plugin-maybe/lib/declare/index.spec.js
Expand Up @@ -8,7 +8,7 @@ const test = createTest(__dirname, {
});

test('plugin-maybe: declare: report', (t) => {
t.report('declare', `Declare 'maybeArray'`);
t.report('declare', `Declare 'maybeArray', it referenced but not defined`);
t.end();
});

Expand Down
5 changes: 1 addition & 4 deletions packages/plugin-montag/lib/declare/index.js
@@ -1,9 +1,6 @@
'use strict';

const {operator} = require('putout');
const {declare} = operator;

module.exports = declare({
module.exports.declare = () => ({
montag: `import montag from 'montag'`,
});

2 changes: 1 addition & 1 deletion packages/plugin-montag/lib/declare/index.spec.js
Expand Up @@ -8,7 +8,7 @@ const test = createTest(__dirname, {
});

test('plugin-montag: declare: report', (t) => {
t.report('montag', `Declare 'montag'`);
t.report('montag', `Declare 'montag', it referenced but not defined`);
t.end();
});

Expand Down
5 changes: 1 addition & 4 deletions packages/plugin-nodejs/lib/declare/index.js
@@ -1,9 +1,6 @@
'use strict';

const {operator} = require('putout');
const {declare} = operator;

module.exports = declare({
module.exports.declare = () => ({
...require('./modules/events'),
...require('./modules/fs'),
...require('./modules/fs-promises'),
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-nodejs/lib/declare/index.spec.js
Expand Up @@ -10,7 +10,7 @@ const test = createTest(__dirname, {
});

test('putout: plugin: nodejs: declare: report: readable-simple', (t) => {
t.report('readable-simple', `Declare 'Readable'`);
t.report('readable-simple', `Declare 'Readable', it referenced but not defined`);
t.end();
});

Expand Down
26 changes: 26 additions & 0 deletions packages/plugin-putout/README.md
Expand Up @@ -20,6 +20,7 @@ npm i @putout/plugin-putout -D
"putout/apply-processors-destructuring": "on",
"putout/apply-async-formatter": "on",
"putout/apply-remove": "on",
"putout/apply-declare": "on",
"putout/add-args": "on",
"putout/add-push": "on",
"putout/convert-putout-test-to-create-test": "on",
Expand Down Expand Up @@ -93,6 +94,31 @@ export const fix = (path) => {
};
```

## apply-declare

Better to use [`Declareator`](https://github.com/coderaiser/putout/tree/master/packages/engine-runner#declarator) instead of `operator.declare()`.

### ❌ Example of incorrect code

```js
const {operator} = require('putout');
const {declare} = operator;

module.exports = declare({
tryCatch: `import tryCatch from 'try-catch'`,
tryToCatch: `import tryToCatch from 'try-to-catch'`,
});
```

### ✅ Example of correct code

```js
module.exports.declare = () => ({
tryCatch: `import tryCatch from 'try-catch'`,
tryToCatch: `import tryToCatch from 'try-to-catch'`,
});
```

## apply-async-formatter

### ❌ Example of incorrect code
Expand Down
@@ -0,0 +1,7 @@
const {operator} = require('putout');
const {declare} = operator;

module.exports.declare = () => ({
tryCatch: `import tryCatch from 'try-catch'`,
tryToCatch: `import tryToCatch from 'try-to-catch'`
});
7 changes: 7 additions & 0 deletions packages/plugin-putout/lib/apply-declare/fixture/declare.js
@@ -0,0 +1,7 @@
const {operator} = require('putout');
const {declare} = operator;

module.exports = declare({
tryCatch: `import tryCatch from 'try-catch'`,
tryToCatch: `import tryToCatch from 'try-to-catch'`,
});
8 changes: 8 additions & 0 deletions packages/plugin-putout/lib/apply-declare/index.js
@@ -0,0 +1,8 @@
'use strict';

module.exports.report = () => `Use 'Declarator' instead of 'operator.declare'`;

module.exports.replace = () => ({
'module.exports = declare(__a)': 'module.exports.declare = () => __a',
});

18 changes: 18 additions & 0 deletions packages/plugin-putout/lib/apply-declare/index.spec.js
@@ -0,0 +1,18 @@
'use strict';

const {createTest} = require('@putout/test');
const plugin = require('.');
const test = createTest(__dirname, {
'tape/apply-declare': plugin,
});

test('plugin-tape: apply-declare: report', (t) => {
t.report('declare', `Use 'Declarator' instead of 'operator.declare'`);
t.end();
});

test('plugin-tape: apply-declare', (t) => {
t.transform('declare');
t.end();
});

12 changes: 0 additions & 12 deletions packages/plugin-putout/lib/declare/declarations.js

This file was deleted.

12 changes: 8 additions & 4 deletions packages/plugin-putout/lib/declare/index.js
@@ -1,8 +1,12 @@
'use strict';

const {operator} = require('putout');
const declarations = require('./declarations');
const {declare} = operator;
const types = require('./types');
const operator = require('./operator');

module.exports = declare(declarations);
module.exports.declare = () => ({
template: `import {template} from 'putout'`,
createTest: `import {createTest} from '@putout/test'`,
...operator,
...types,
});

2 changes: 1 addition & 1 deletion packages/plugin-putout/lib/declare/index.spec.js
Expand Up @@ -10,7 +10,7 @@ const test = createTest(__dirname, {
});

test('plugin-putout: declare: report', (t) => {
t.report('compare', `Declare 'compare'`);
t.report('compare', `Declare 'compare', it referenced but not defined`);
t.end();
});

Expand Down
1 change: 1 addition & 0 deletions packages/plugin-putout/lib/index.js
Expand Up @@ -9,6 +9,7 @@ module.exports.rules = {
...getRule('apply-async-formatter'),
...getRule('apply-create-test'),
...getRule('apply-remove'),
...getRule('apply-declare'),
...getRule('convert-putout-test-to-create-test'),
...getRule('convert-to-no-transform-code'),
...getRule('convert-find-to-traverse'),
Expand Down
7 changes: 7 additions & 0 deletions packages/plugin-putout/test/fixture/apply-declare-fix.js
@@ -0,0 +1,7 @@
const {operator} = require('putout');
const {declare} = operator;

module.exports.declare = () => ({
tryCatch: `import tryCatch from 'try-catch'`,
tryToCatch: `import tryToCatch from 'try-to-catch'`
});
7 changes: 7 additions & 0 deletions packages/plugin-putout/test/fixture/apply-declare.js
@@ -0,0 +1,7 @@
const {operator} = require('putout');
const {declare} = operator;

module.exports = declare({
tryCatch: `import tryCatch from 'try-catch'`,
tryToCatch: `import tryToCatch from 'try-to-catch'`,
});
5 changes: 5 additions & 0 deletions packages/plugin-putout/test/putout.js
Expand Up @@ -147,3 +147,8 @@ test('plugin-putout: transform: convert-number-to-numeric', (t) => {
t.end();
});

test('plugin-putout: transform: apply-declare', (t) => {
t.transform('apply-declare');
t.end();
});

9 changes: 0 additions & 9 deletions packages/plugin-react-hooks/lib/declare/declarations.js

This file was deleted.

13 changes: 7 additions & 6 deletions packages/plugin-react-hooks/lib/declare/index.js
@@ -1,8 +1,9 @@
'use strict';

const {operator} = require('putout');
const declarations = require('./declarations');

const {declare} = operator;

module.exports = declare(declarations);
module.exports.declare = () => ({
useState: 'import {useState} from "react"',
useEffect: 'import {useEffect} from "react"',
useContext: 'import {useContext} from "react"',
useReducer: 'import {useReducer} from "react"',
useCallback: 'import {useCallback} from "react"',
});
2 changes: 1 addition & 1 deletion packages/plugin-react-hooks/lib/declare/index.spec.js
Expand Up @@ -8,7 +8,7 @@ const test = createTest(__dirname, {
});

test('plugin-react-hooks: declare: report', (t) => {
t.report('use-state', `Declare 'useState'`);
t.report('use-state', `Declare 'useState', it referenced but not defined`);
t.end();
});

Expand Down
5 changes: 1 addition & 4 deletions packages/plugin-try-catch/lib/declare/index.js
@@ -1,9 +1,6 @@
'use strict';

const {operator} = require('putout');
const {declare} = operator;

module.exports = declare({
module.exports.declare = () => ({
tryCatch: `import tryCatch from 'try-catch'`,
tryToCatch: `import tryToCatch from 'try-to-catch'`,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-try-catch/lib/declare/index.spec.js
Expand Up @@ -8,7 +8,7 @@ const test = createTest(__dirname, {
});

test('plugin-apply-try-catch: declare: report', (t) => {
t.report('try-catch', `Declare 'tryCatch'`);
t.report('try-catch', `Declare 'tryCatch', it referenced but not defined`);
t.end();
});

Expand Down

0 comments on commit d289312

Please sign in to comment.