@putout/engine-loader 4.10.0 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -20
- package/lib/index.js +1 -8
- package/lib/load.js +43 -21
- package/lib/parse-rules.js +1 -1
- package/lib/validate-plugin.js +1 -1
- package/lib/validate-rules.js +1 -1
- package/lib/wrap-plugin.js +0 -8
- package/package.json +5 -6
- package/lib/transforms/jscodeshift.js +0 -42
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/engine-loader.svg?style=flat&longCache=true
|
|
4
4
|
[NPMURL]: https://npmjs.org/package/@putout/engine-loader"npm"
|
|
5
5
|
|
|
6
|
-
Load putout `
|
|
6
|
+
Load 🐊[`Putout`](https://github.com/coderaiser/putout) `Plugins` and `Processors`.
|
|
7
7
|
|
|
8
8
|
## Install
|
|
9
9
|
|
|
@@ -11,6 +11,11 @@ Load putout `plugins`, `processors`.
|
|
|
11
11
|
npm i @putout/engine-loader
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
+
## Env Variables
|
|
15
|
+
|
|
16
|
+
When you need to get things working with Yarn PnP, and using custom `plugins` `formatters` or `processers`, add env variable
|
|
17
|
+
`PUTOUT_YARN_PNP` with name of a package that contains dependencies you need.
|
|
18
|
+
|
|
14
19
|
## Code Example
|
|
15
20
|
|
|
16
21
|
### loadPlugins
|
|
@@ -33,25 +38,6 @@ const plugins = loadPlugins({
|
|
|
33
38
|
});
|
|
34
39
|
```
|
|
35
40
|
|
|
36
|
-
#### JSCodeshift
|
|
37
|
-
|
|
38
|
-
`@putout/engine-loader` supports loading transforms written for [jscodeshift](https://github.com/facebook/jscodeshift) with help of prefix `jscodeshift/`.
|
|
39
|
-
|
|
40
|
-
```js
|
|
41
|
-
const pluginNames = [
|
|
42
|
-
'jscodeshift/async-await-codemod',
|
|
43
|
-
];
|
|
44
|
-
|
|
45
|
-
const rules = {
|
|
46
|
-
'jscodeshift/async-await-codemod': ['on', 'any message you like :)'],
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const plugins = loadPlugins({
|
|
50
|
-
pluginNames,
|
|
51
|
-
rules, // optional
|
|
52
|
-
});
|
|
53
|
-
```
|
|
54
|
-
|
|
55
41
|
#### Babel Plugins
|
|
56
42
|
|
|
57
43
|
You can use `babel plugins` with help of `babel/` prefix.
|
package/lib/index.js
CHANGED
|
@@ -120,8 +120,7 @@ function getLoadedRules(rules) {
|
|
|
120
120
|
|
|
121
121
|
function splitRule(rule) {
|
|
122
122
|
const name = rule
|
|
123
|
-
.replace('babel/', '')
|
|
124
|
-
.replace('jscodeshift/', '');
|
|
123
|
+
.replace('babel/', '');
|
|
125
124
|
|
|
126
125
|
if (/^babel/.test(rule))
|
|
127
126
|
return [
|
|
@@ -129,12 +128,6 @@ function splitRule(rule) {
|
|
|
129
128
|
'babel',
|
|
130
129
|
];
|
|
131
130
|
|
|
132
|
-
if (/^jscodeshift/.test(rule))
|
|
133
|
-
return [
|
|
134
|
-
name,
|
|
135
|
-
'jscodeshift',
|
|
136
|
-
];
|
|
137
|
-
|
|
138
131
|
return [
|
|
139
132
|
name,
|
|
140
133
|
'putout',
|
package/lib/load.js
CHANGED
|
@@ -1,37 +1,19 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {createRequire} = require('module');
|
|
4
|
+
|
|
4
5
|
const tryCatch = require('try-catch');
|
|
6
|
+
const once = require('once');
|
|
5
7
|
|
|
6
8
|
const wrapPlugin = require('./wrap-plugin');
|
|
7
9
|
|
|
8
|
-
// yarn PnP API
|
|
9
|
-
// https://yarnpkg.com/advanced/rulebook#modules-shouldnt-hardcode-node_modules-paths-to-access-other-modules
|
|
10
|
-
const customRequire = createRequire(__filename);
|
|
11
10
|
const bigFirst = (a) => `${a[0].toUpperCase()}${a.slice(1)}`;
|
|
12
11
|
|
|
13
|
-
const getModulePath = (name) => {
|
|
14
|
-
const [, path] = tryCatch(customRequire.resolve, name);
|
|
15
|
-
return path;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
function getPath(namespace, type, name) {
|
|
19
|
-
let path = getModulePath(`@${namespace}/${type}-${name}`);
|
|
20
|
-
|
|
21
|
-
if (!path)
|
|
22
|
-
path = getModulePath(`${namespace}-${type}-${name}`);
|
|
23
|
-
|
|
24
|
-
if (!path)
|
|
25
|
-
path = getModulePath(name);
|
|
26
|
-
|
|
27
|
-
return path;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
12
|
const load = (type) => ({name, namespace}) => {
|
|
31
13
|
if (namespace !== 'putout')
|
|
32
14
|
return wrapPlugin(name, namespace);
|
|
33
15
|
|
|
34
|
-
const pluginPath = getPath(namespace, type, name);
|
|
16
|
+
const [pluginPath, customRequire] = getPath(namespace, type, name);
|
|
35
17
|
|
|
36
18
|
if (!pluginPath)
|
|
37
19
|
throw Error(`${bigFirst(type)} "${namespace}-${type}-${name}" could not be found!`);
|
|
@@ -42,3 +24,43 @@ const load = (type) => ({name, namespace}) => {
|
|
|
42
24
|
module.exports.loadPlugin = load('plugin');
|
|
43
25
|
module.exports.loadProcessor = load('processor');
|
|
44
26
|
|
|
27
|
+
function getPath(namespace, type, name) {
|
|
28
|
+
let [path, customRequire] = getModulePath(`@${namespace}/${type}-${name}`);
|
|
29
|
+
|
|
30
|
+
if (!path)
|
|
31
|
+
[path, customRequire] = getModulePath(`${namespace}-${type}-${name}`);
|
|
32
|
+
|
|
33
|
+
if (!path)
|
|
34
|
+
[path, customRequire] = getModulePath(name);
|
|
35
|
+
|
|
36
|
+
return [path, customRequire];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const {PUTOUT_YARN_PNP = 'putout'} = process.env;
|
|
40
|
+
|
|
41
|
+
const createCustomRequire = once(() => createRequire(require.resolve(PUTOUT_YARN_PNP)));
|
|
42
|
+
const createPutoutRequire = once(() => createRequire(require.resolve('putout')));
|
|
43
|
+
|
|
44
|
+
// That's all for Yarn P'n'P
|
|
45
|
+
//
|
|
46
|
+
// We need to create a couple version of require for plugins, formatters and processors:
|
|
47
|
+
// - declared in 🐊Putout package.json;
|
|
48
|
+
// - declared in module that want to extend 🐊Putout;
|
|
49
|
+
//
|
|
50
|
+
// https://yarnpkg.com/advanced/rulebook#modules-shouldnt-hardcode-node_modules-paths-to-access-other-modules
|
|
51
|
+
function getModulePath(name) {
|
|
52
|
+
let path;
|
|
53
|
+
|
|
54
|
+
const customRequire = createCustomRequire();
|
|
55
|
+
const putoutRequire = createPutoutRequire();
|
|
56
|
+
|
|
57
|
+
[, path] = tryCatch(putoutRequire.resolve, name);
|
|
58
|
+
|
|
59
|
+
if (path)
|
|
60
|
+
return [path, putoutRequire];
|
|
61
|
+
|
|
62
|
+
[, path] = tryCatch(customRequire.resolve, name);
|
|
63
|
+
|
|
64
|
+
return [path, customRequire];
|
|
65
|
+
}
|
|
66
|
+
|
package/lib/parse-rules.js
CHANGED
|
@@ -130,7 +130,7 @@ function validateState(rule, value) {
|
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
const cut = (a) => a.split('/')[0];
|
|
133
|
-
const isExclude = (a) => a.includes('babel')
|
|
133
|
+
const isExclude = (a) => a.includes('babel');
|
|
134
134
|
|
|
135
135
|
function parseSubrules(rules) {
|
|
136
136
|
const newRules = {};
|
package/lib/validate-plugin.js
CHANGED
|
@@ -17,6 +17,6 @@ module.exports = ({plugin, rule}) => {
|
|
|
17
17
|
return;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
throw Error(`☝️
|
|
20
|
+
throw Error(`☝️ Cannot determine type of plugin '${rule}'. Here is list of supported plugins: https://git.io/JqcMn`);
|
|
21
21
|
};
|
|
22
22
|
|
package/lib/validate-rules.js
CHANGED
package/lib/wrap-plugin.js
CHANGED
|
@@ -6,7 +6,6 @@ const {print} = require('@putout/engine-parser');
|
|
|
6
6
|
const getPositions = require('./get-positions-by-diff');
|
|
7
7
|
|
|
8
8
|
const babelTransform = require('./transforms/babel');
|
|
9
|
-
const jscodeshiftTransform = require('./transforms/jscodeshift');
|
|
10
9
|
|
|
11
10
|
const getMessage = (a) => a
|
|
12
11
|
.replace(/@babel\/plugin-|babel-plugin-/, '')
|
|
@@ -27,13 +26,6 @@ module.exports = (name, namespace) => {
|
|
|
27
26
|
transform: babelTransform,
|
|
28
27
|
});
|
|
29
28
|
|
|
30
|
-
if (/jscodeshift/.test(namespace))
|
|
31
|
-
return getPlugin({
|
|
32
|
-
name,
|
|
33
|
-
message,
|
|
34
|
-
transform: jscodeshiftTransform,
|
|
35
|
-
});
|
|
36
|
-
|
|
37
29
|
return null;
|
|
38
30
|
};
|
|
39
31
|
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/engine-loader",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.1.0",
|
|
4
|
+
"type": "commonjs",
|
|
4
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
5
6
|
"description": "load plugins and prepare them to run",
|
|
6
|
-
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/engine-loader",
|
|
7
|
+
"homepage": "https://github.com/coderaiser/putout/tree/master/packages/engine-loader#readme",
|
|
7
8
|
"main": "lib/index.js",
|
|
8
9
|
"release": false,
|
|
9
10
|
"tag": false,
|
|
@@ -26,7 +27,6 @@
|
|
|
26
27
|
"@babel/core": "^7.12.3",
|
|
27
28
|
"@putout/engine-parser": "^4.0.1",
|
|
28
29
|
"diff-match-patch": "^1.0.4",
|
|
29
|
-
"jscodeshift": "^0.13.0",
|
|
30
30
|
"nano-memoize": "^1.1.8",
|
|
31
31
|
"once": "^1.4.0",
|
|
32
32
|
"try-catch": "^3.0.0"
|
|
@@ -41,14 +41,13 @@
|
|
|
41
41
|
"@cloudcmd/stub": "^3.0.0",
|
|
42
42
|
"@putout/plugin-convert-commonjs-to-esm": "*",
|
|
43
43
|
"@putout/plugin-remove-unused-variables": "*",
|
|
44
|
-
"@putout/processor-markdown": "*",
|
|
45
44
|
"@putout/processor-javascript": "*",
|
|
45
|
+
"@putout/processor-markdown": "*",
|
|
46
46
|
"c8": "^7.5.0",
|
|
47
47
|
"eslint": "^8.0.1",
|
|
48
48
|
"eslint-plugin-node": "^11.0.0",
|
|
49
|
-
"eslint-plugin-putout": "^
|
|
49
|
+
"eslint-plugin-putout": "^13.0.0",
|
|
50
50
|
"estrace": "^3.0.2",
|
|
51
|
-
"js-codemod": "^7.0.0",
|
|
52
51
|
"just-camel-case": "^4.0.2",
|
|
53
52
|
"lerna": "^4.0.0",
|
|
54
53
|
"madrun": "^8.0.1",
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const once = require('once');
|
|
4
|
-
|
|
5
|
-
const {print} = require('@putout/engine-parser');
|
|
6
|
-
const {assign} = Object;
|
|
7
|
-
|
|
8
|
-
const isStr = (a) => typeof a === 'string';
|
|
9
|
-
|
|
10
|
-
const printAST = (a) => isStr(a) ? a : print(a);
|
|
11
|
-
const getFn = (fn) => (...args) => {
|
|
12
|
-
return printAST(fn(...args));
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const getJScodeshift = once(() => require('jscodeshift'));
|
|
16
|
-
|
|
17
|
-
const wrapCodeShift = (ast) => {
|
|
18
|
-
const j = getJScodeshift();
|
|
19
|
-
const avoidPrint = {
|
|
20
|
-
toSource() {},
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
let isCalled = false;
|
|
24
|
-
const fixedJscodeshift = (a) => {
|
|
25
|
-
if (isCalled)
|
|
26
|
-
return j(a);
|
|
27
|
-
|
|
28
|
-
isCalled = true;
|
|
29
|
-
return assign(j(ast), avoidPrint);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
return assign(fixedJscodeshift, j);
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
module.exports = (ast, source, pluginName) => {
|
|
36
|
-
const jscodeshift = wrapCodeShift(ast);
|
|
37
|
-
const fn = getFn(require(pluginName));
|
|
38
|
-
const options = {};
|
|
39
|
-
|
|
40
|
-
fn({source}, {jscodeshift}, options);
|
|
41
|
-
};
|
|
42
|
-
|