putout 31.2.4 → 31.3.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/ChangeLog CHANGED
@@ -1,3 +1,14 @@
1
+ 2023.08.23, v31.3.0
2
+
3
+ feature:
4
+ - 783861f83 package: @putout/plugin-apply-entries v2.0.0
5
+ - 137145031 putout: add putoutAsync
6
+ - 347e572ad @putout/plugin-apply-entries: ESM
7
+ - 66aecbf55 @putout/plugin-apply-namespace-specifier: create-test
8
+ - e0be94d96 @putout/plugin-putout: apply-namespace-specifier: rules
9
+ - 66434fb93 @putout/engine-loader: add loadPluginsAsync
10
+ - 890558e38 eslint-plugin-putout: putout: esm
11
+
1
12
  2023.08.22, v31.2.4
2
13
 
3
14
  feature:
package/README.md CHANGED
@@ -291,6 +291,28 @@ console.log(t);
291
291
  `;
292
292
  ```
293
293
 
294
+ ### putoutAsync(source, options)
295
+
296
+ ```js
297
+ const {putoutAsync} = require('putout');
298
+
299
+ const source = `
300
+ const t = 'hello';
301
+ const m = t + '!';
302
+ console.log(t);
303
+ `;
304
+
305
+ await putoutAsync(source, {
306
+ plugins: ['remove-unused-variables'],
307
+ });
308
+
309
+ // returns
310
+ `
311
+ const t = 'hello';
312
+ console.log(t);
313
+ `;
314
+ ```
315
+
294
316
  ## License
295
317
 
296
318
  MIT
@@ -0,0 +1,160 @@
1
+ 'use strict';
2
+
3
+ const {traverse, types} = require('@putout/babel');
4
+
5
+ const loader = require('@putout/engine-loader');
6
+ const runner = require('@putout/engine-runner');
7
+
8
+ const {
9
+ parse,
10
+ print,
11
+ generate,
12
+ template,
13
+ } = require('@putout/engine-parser');
14
+
15
+ const {cutShebang, mergeShebang} = require('./shebang');
16
+
17
+ const isString = (a) => typeof a === 'string';
18
+
19
+ const defaultOpts = (opts = {}) => {
20
+ const {
21
+ parser = 'babel',
22
+ printer = opts.printer || 'putout',
23
+ fix = true,
24
+ fixCount = 2,
25
+ loadPlugins = loader.loadPlugins,
26
+ loadPluginsAsync = loader.loadPluginsAsync,
27
+ runPlugins = runner.runPlugins,
28
+ } = opts;
29
+
30
+ return {
31
+ ...opts,
32
+ parser,
33
+ printer,
34
+ fix,
35
+ fixCount,
36
+ loadPlugins,
37
+ loadPluginsAsync,
38
+ runPlugins,
39
+ };
40
+ };
41
+
42
+ module.exports.putoutAsync = async (source, opts) => {
43
+ check(source);
44
+ opts = defaultOpts(opts);
45
+
46
+ const {
47
+ parser,
48
+ isTS,
49
+ isFlow,
50
+ isJSX,
51
+ sourceFileName,
52
+ sourceMapName,
53
+ printer,
54
+ } = opts;
55
+
56
+ const [clearSource, shebang] = cutShebang(source);
57
+
58
+ const ast = parse(clearSource, {
59
+ sourceFileName,
60
+ parser,
61
+ isTS,
62
+ isFlow,
63
+ isJSX,
64
+ printer,
65
+ });
66
+
67
+ const places = await transform(ast, source, opts);
68
+
69
+ if (!opts.fix)
70
+ return {
71
+ code: source,
72
+ places,
73
+ };
74
+
75
+ const printed = print(ast, {
76
+ sourceMapName,
77
+ printer,
78
+ });
79
+
80
+ const code = mergeShebang(shebang, printed);
81
+
82
+ return {
83
+ code,
84
+ places,
85
+ };
86
+ };
87
+
88
+ module.exports.findPlaces = async (ast, source, opts) => {
89
+ return await transform(ast, source, {
90
+ ...opts,
91
+ fix: false,
92
+ });
93
+ };
94
+
95
+ // why we pass 'source' to 'transform()'?
96
+ // because we need to calculate position in a right way
97
+ // and determine is shebang is exists
98
+ //
99
+ // 25 return {¬
100
+ // 26 line: shebang ? line + 1 : line,¬
101
+ // 27 column,¬
102
+ // 28 };¬
103
+ //
104
+ module.exports.transform = transform;
105
+ async function transform(ast, source, opts) {
106
+ opts = defaultOpts(opts);
107
+
108
+ const {
109
+ plugins: pluginNames,
110
+ cache,
111
+ rules,
112
+ fix,
113
+ fixCount,
114
+ loadPluginsAsync,
115
+ runPlugins,
116
+ } = opts;
117
+
118
+ const [, shebang] = cutShebang(source);
119
+
120
+ const plugins = await loadPluginsAsync({
121
+ pluginNames,
122
+ cache,
123
+ rules,
124
+ });
125
+
126
+ const places = runPlugins({
127
+ ast,
128
+ shebang,
129
+ fix,
130
+ fixCount,
131
+ plugins,
132
+ });
133
+
134
+ return places;
135
+ }
136
+
137
+ module.exports.parse = parse;
138
+ module.exports.print = print;
139
+ module.exports.traverse = traverse;
140
+ module.exports.types = types;
141
+ module.exports.template = template;
142
+ module.exports.generate = generate;
143
+ module.exports.initReport = require('./cli/report');
144
+
145
+ module.exports.operator = {
146
+ ...require('@putout/operate'),
147
+ ...require('@putout/compare'),
148
+ ...require('@putout/traverse'),
149
+ ...require('@putout/operator-declare'),
150
+ ...require('@putout/operator-regexp'),
151
+ ...require('@putout/operator-add-args'),
152
+ };
153
+
154
+ module.exports.ignores = require('./ignores');
155
+ module.exports.codeframe = require('./codeframe');
156
+
157
+ function check(source) {
158
+ if (!isString(source))
159
+ throw Error(`☝️ Looks like 'source' has type '${typeof source}', expected: 'string'`);
160
+ }
package/lib/putout.js CHANGED
@@ -13,6 +13,7 @@ const {
13
13
  } = require('@putout/engine-parser');
14
14
 
15
15
  const {cutShebang, mergeShebang} = require('./shebang');
16
+ const {putoutAsync} = require('./putout-async');
16
17
 
17
18
  const isString = (a) => typeof a === 'string';
18
19
 
@@ -83,6 +84,8 @@ module.exports = (source, opts) => {
83
84
  };
84
85
  };
85
86
 
87
+ module.exports.putoutAsync = putoutAsync;
88
+
86
89
  module.exports.findPlaces = (ast, source, opts) => {
87
90
  return transform(ast, source, {
88
91
  ...opts,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "putout",
3
- "version": "31.2.4",
3
+ "version": "31.3.0",
4
4
  "type": "commonjs",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊 Pluggable and configurable code transformer with built-in ESLint, Babel and support of js, jsx, typescript, flow, markdown, yaml and json",
@@ -201,6 +201,7 @@
201
201
  "unused"
202
202
  ],
203
203
  "devDependencies": {
204
+ "@putout/plugin-apply-entries": "^2.0.0",
204
205
  "c8": "^8.0.0",
205
206
  "currify": "^4.0.0",
206
207
  "escover": "^3.2.2",