Skip to content

Commit 18c666b

Browse files
committed
feature: @putout/plugin-react-hooks: convert to ESM
1 parent 18f2edb commit 18c666b

File tree

27 files changed

+101
-153
lines changed

27 files changed

+101
-153
lines changed

packages/plugin-react-hooks/lib/apply-short-fragment/index.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
const {types, operator} = require('putout');
1+
import {types, operator} from 'putout';
42

53
const {
64
JSXClosingFragment,
@@ -10,20 +8,20 @@ const {
108

119
const {replaceWith} = operator;
1210

13-
module.exports.report = () => `Apply shorthand syntax for 'Fragment'`;
11+
export const report = () => `Apply shorthand syntax for 'Fragment'`;
1412

15-
module.exports.include = () => [
13+
export const include = () => [
1614
'JSXOpeningElement',
1715
];
1816

19-
module.exports.fix = (path) => {
17+
export const fix = (path) => {
2018
const {parentPath} = path;
2119
const {children} = path.parentPath.node;
2220

2321
replaceWith(parentPath, JSXFragment(JSXOpeningFragment(), JSXClosingFragment(), children));
2422
};
2523

26-
module.exports.filter = (path) => {
24+
export const filter = (path) => {
2725
if (path.node.attributes.length)
2826
return false;
2927

packages/plugin-react-hooks/lib/apply-short-fragment/index.spec.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as plugin from './index.js';
23

3-
const {createTest} = require('@putout/test');
4-
const plugin = require('.');
5-
6-
const test = createTest(__dirname, {
4+
const test = createTest(import.meta.url, {
75
plugins: [
86
['react-hooks/apply-short-fragment', plugin],
97
],

packages/plugin-react-hooks/lib/common.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
module.exports.traverseClass = (traverse, ast, visitor) => {
1+
export const traverseClass = (traverse, ast, visitor) => {
42
traverse(ast, {
53
'class __ extends React.Component {}': push(visitor),
64
'class __ extends Component {}': push(visitor),

packages/plugin-react-hooks/lib/convert-class-to-function/class-to-function.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
const {types, operator} = require('putout');
1+
import {types, operator} from 'putout';
42

53
const {replaceWith, remove} = operator;
64

@@ -10,7 +8,7 @@ const {
108
functionDeclaration,
119
} = types;
1210

13-
module.exports = (path) => {
11+
export default (path) => {
1412
const {node} = path;
1513
const {body} = node;
1614

packages/plugin-react-hooks/lib/convert-class-to-function/index.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
'use strict';
2-
3-
const {types} = require('putout');
4-
5-
const classToFunction = require('./class-to-function');
6-
const {traverseClass} = require('../common');
1+
import {types} from 'putout';
2+
import classToFunction from './class-to-function.js';
3+
import {traverseClass} from '../common.js';
74

85
const {isIdentifier, isClassMethod} = types;
96

10-
module.exports.report = ({name}) => {
7+
export const report = ({name}) => {
118
return `class ${name} should be a function`;
129
};
1310

14-
module.exports.fix = ({path}) => {
11+
export const fix = ({path}) => {
1512
classToFunction(path);
1613
};
1714

18-
module.exports.find = (ast, {push, traverse}) => {
15+
export const find = (ast, {push, traverse}) => {
1916
traverseClass(traverse, ast, {
2017
Identifier(path) {
2118
const {name} = path.node;

packages/plugin-react-hooks/lib/convert-class-to-function/index.spec.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as convertClassToFunction from './index.js';
23

3-
const {createTest} = require('@putout/test');
4-
const convertClassToFunction = require('.');
5-
6-
const test = createTest(__dirname, {
4+
const test = createTest(import.meta.url, {
75
plugins: [
86
['convert-class-to-function', convertClassToFunction],
97
],

packages/plugin-react-hooks/lib/convert-component-to-use-state/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
'use strict';
1+
import {types} from 'putout';
22

33
const {
44
isIdentifier,
55
isObjectPattern,
6-
} = require('putout').types;
6+
} = types;
77

8-
module.exports.report = () => 'useState should be used instead of Component';
8+
export const report = () => 'useState should be used instead of Component';
99

10-
module.exports.fix = ({node}) => {
10+
export const fix = ({node}) => {
1111
node.key.name = 'useState';
1212
node.value.name = 'useState';
1313
};
1414

15-
module.exports.find = (ast, {traverse}) => {
15+
export const find = (ast, {traverse}) => {
1616
const places = [];
1717

1818
traverse(ast, {

packages/plugin-react-hooks/lib/convert-component-to-use-state/index.spec.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as convertComponentToUseState from './index.js';
23

3-
const {createTest} = require('@putout/test');
4-
const convertComponentToUseState = require('.');
5-
6-
const test = createTest(__dirname, {
4+
const test = createTest(import.meta.url, {
75
plugins: [
86
['convert-component-to-use-state', convertComponentToUseState],
97
],

packages/plugin-react-hooks/lib/convert-import-component-to-use-state/index.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
'use strict';
1+
export const report = () => 'useState should be used instead of Component';
22

3-
module.exports.report = () => 'useState should be used instead of Component';
4-
5-
module.exports.fix = ({node}) => {
3+
export const fix = ({node}) => {
64
node.imported.name = 'useState';
75
node.local.name = 'useState';
86
};
97

10-
module.exports.traverse = ({push}) => ({
8+
export const traverse = ({push}) => ({
119
ImportDeclaration(path) {
1210
const {source} = path.node;
1311

packages/plugin-react-hooks/lib/convert-import-component-to-use-state/index.spec.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as convertImportComponentToUseState from './index.js';
23

3-
const {createTest} = require('@putout/test');
4-
const convertImportComponentToUseState = require('.');
5-
6-
const test = createTest(__dirname, {
4+
const test = createTest(import.meta.url, {
75
plugins: [
86
['convert-import-component-to-use-state', convertImportComponentToUseState],
97
],

packages/plugin-react-hooks/lib/convert-state-to-hooks/index.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
'use strict';
2-
3-
const {operator, types} = require('putout');
4-
5-
const {traverseClass} = require('../common');
6-
const stateToHooks = require('./state-to-hooks');
7-
const setStateToHooks = require('./set-state-to-hooks');
1+
import {operator, types} from 'putout';
2+
import {traverseClass} from '../common.js';
3+
import stateToHooks from './state-to-hooks.js';
4+
import setStateToHooks from './set-state-to-hooks.js';
85

96
const {
107
isIdentifier,
@@ -15,7 +12,7 @@ const {
1512

1613
const {remove} = operator;
1714

18-
module.exports.report = (path) => {
15+
export const report = (path) => {
1916
if (isAssignmentExpression(path))
2017
return 'hooks should be used instead of this.state';
2118

@@ -25,7 +22,7 @@ module.exports.report = (path) => {
2522
return 'hooks should be used instead of this.setState';
2623
};
2724

28-
module.exports.fix = (path) => {
25+
export const fix = (path) => {
2926
if (isAssignmentExpression(path))
3027
return stateToHooks(path);
3128

@@ -35,7 +32,7 @@ module.exports.fix = (path) => {
3532
return setStateToHooks(path);
3633
};
3734

38-
module.exports.find = (ast, {push, traverse}) => {
35+
export const find = (ast, {push, traverse}) => {
3936
traverseClass(traverse, ast, {
4037
AssignmentExpression: (path) => {
4138
if (!path.get('left').isMemberExpression())

packages/plugin-react-hooks/lib/convert-state-to-hooks/index.spec.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as convertStateToHooks from './index.js';
23

3-
const {createTest} = require('@putout/test');
4-
const convertStateToHooks = require('.');
5-
6-
const test = createTest(__dirname, {
4+
const test = createTest(import.meta.url, {
75
plugins: [
86
['convert-state-to-hooks', convertStateToHooks],
97
],

packages/plugin-react-hooks/lib/convert-state-to-hooks/set-state-to-hooks.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
'use strict';
2-
3-
const {template, operator} = require('putout');
1+
import {template, operator} from 'putout';
42

53
const {replaceWithMultiple} = operator;
64

75
const buildHooks = template(`
86
SETTER(VALUE);
97
`);
108

11-
module.exports = (path) => {
9+
export default (path) => {
1210
const {properties} = path.node.arguments[0];
1311
const nodes = [];
1412

packages/plugin-react-hooks/lib/convert-state-to-hooks/state-to-hooks.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
'use strict';
2-
3-
const {template, operator} = require('putout');
1+
import {template, operator} from 'putout';
42

53
const {replaceWithMultiple} = operator;
64

75
const buildHooks = template(`
86
const [NAME, SETTER] = useState(VALUE);
97
`);
108

11-
module.exports = (path) => {
9+
export default (path) => {
1210
const {right} = path.node;
1311
const nodes = [];
1412

packages/plugin-react-hooks/lib/declare/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
module.exports.declare = () => ({
1+
export const declare = () => ({
42
useState: 'import {useState} from "react"',
53
useEffect: 'import {useEffect} from "react"',
64
useContext: 'import {useContext} from "react"',

packages/plugin-react-hooks/lib/declare/index.spec.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as declare from './index.js';
23

3-
const {createTest} = require('@putout/test');
4-
const declare = require('.');
5-
6-
const test = createTest(__dirname, {
4+
const test = createTest(import.meta.url, {
75
plugins: [
86
['react-hooks/declare', declare],
97
],

packages/plugin-react-hooks/lib/react-hooks.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
'use strict';
1+
import * as applyShortFragment from './apply-short-fragment/index.js';
2+
import * as declare from './declare/index.js';
3+
import * as renameMethodUnderScore from './rename-method-under-score/index.js';
4+
import * as convertStateToHooks from './convert-state-to-hooks/index.js';
5+
import * as removeBind from './remove-bind/index.js';
6+
import * as removeThis from './remove-this/index.js';
7+
import * as removeReact from './remove-react/index.js';
8+
import * as convertClassToFunction from './convert-class-to-function/index.js';
9+
import * as convertComponentToUseState from './convert-component-to-use-state/index.js';
10+
import * as convertImportComponentToUseState from './convert-import-component-to-use-state/index.js';
211

3-
const applyShortFragment = require('./apply-short-fragment');
4-
const declare = require('./declare');
5-
const renameMethodUnderScore = require('./rename-method-under-score');
6-
const convertStateToHooks = require('./convert-state-to-hooks');
7-
const removeBind = require('./remove-bind');
8-
const removeThis = require('./remove-this');
9-
const removeReact = require('./remove-react');
10-
const convertClassToFunction = require('./convert-class-to-function');
11-
const convertComponentToUseState = require('./convert-component-to-use-state');
12-
const convertImportComponentToUseState = require('./convert-import-component-to-use-state');
13-
14-
module.exports.rules = {
12+
export const rules = {
1513
'apply-short-fragment': applyShortFragment,
1614
declare,
1715
'rename-method-under-score': renameMethodUnderScore,

packages/plugin-react-hooks/lib/remove-bind/index.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
'use strict';
2-
3-
const {operator} = require('putout');
4-
const {traverseClass} = require('../common');
1+
import {operator} from 'putout';
2+
import {traverseClass} from '../common.js';
53

64
const {remove} = operator;
75

8-
module.exports.report = () => 'bind should not be used';
6+
export const report = () => 'bind should not be used';
97

10-
module.exports.fix = (path) => {
8+
export const fix = (path) => {
119
remove(path);
1210
};
1311

14-
module.exports.find = (ast, {traverse, push}) => {
12+
export const find = (ast, {traverse, push}) => {
1513
traverseClass(traverse, ast, {
1614
CallExpression(path) {
1715
const isBind = path.get('callee.property').isIdentifier({

packages/plugin-react-hooks/lib/remove-bind/index.spec.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as removeBind from './index.js';
23

3-
const {createTest} = require('@putout/test');
4-
const removeBind = require('.');
5-
6-
const test = createTest(__dirname, {
4+
const test = createTest(import.meta.url, {
75
plugins: [
86
['remove-bind', removeBind],
97
],

packages/plugin-react-hooks/lib/remove-react/index.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
'use strict';
2-
31
const reactNotUsed = (vars, path) => !path.scope.bindings.React?.referencePaths?.length;
42
const isReactUsed = (path) => path.scope.bindings.React?.referencePaths?.length;
53

6-
module.exports.report = () => `Remove unused 'React' variable`;
7-
module.exports.match = () => ({
4+
export const report = () => `Remove unused 'React' variable`;
5+
export const match = () => ({
86
'import * as React from "react"': reactNotUsed,
97
'import React from "react"': reactNotUsed,
108
'import __imports from "react"': ({__imports}, path) => {
@@ -15,7 +13,7 @@ module.exports.match = () => ({
1513
},
1614
});
1715

18-
module.exports.replace = () => ({
16+
export const replace = () => ({
1917
'import * as React from "react"': '',
2018
'import React from "react"': '',
2119
'import __imports from "react"'({__imports}, path) {

packages/plugin-react-hooks/lib/remove-react/index.spec.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import {createTest} from '@putout/test';
2+
import * as remove from './index.js';
23

3-
const {createTest} = require('@putout/test');
4-
const remove = require('.');
5-
6-
const test = createTest(__dirname, {
4+
const test = createTest(import.meta.url, {
75
plugins: [
86
['remove-react', remove],
97
],

0 commit comments

Comments
 (0)