@putout/plugin-esm 3.0.0 → 4.0.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 CHANGED
@@ -18,6 +18,7 @@ npm i putout @putout/plugin-esm -D
18
18
 
19
19
  ## Rules
20
20
 
21
+ - ✅ [add-index-to-import](#add-index-to-import);
21
22
  - ✅ [apply-export-from](#apply-export-from);
22
23
  - ✅ [declare-imports-first](#declare-imports-first);
23
24
  - ✅ [group-imports-by-source](#group-imports-by-source);
@@ -32,6 +33,7 @@ npm i putout @putout/plugin-esm -D
32
33
  ```json
33
34
  {
34
35
  "rules": {
36
+ "esm/add-index-to-import": "on",
35
37
  "esm/apply-export-from": "on",
36
38
  "esm/declare-imports-first": "on",
37
39
  "esm/group-imports-by-source": "on",
@@ -46,15 +48,41 @@ npm i putout @putout/plugin-esm -D
46
48
  }
47
49
  ```
48
50
 
49
- ## apply-export-from
51
+ ## Rules
52
+
53
+ ### add-index-to-import
54
+
55
+ ESM doesn't add `index.js`, so it can be left after [`@putout/plugin-convert-esm-to-commonjs`](https://github.com/coderaiser/putout/blob/master/packages/plugin-convert-esm-to-commonjs#readme).
56
+ Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/b7c489710767efee95ecf3dd16e232a2/9f974f0a345ef4d0cb39b011097dff82e6c32b75).
57
+
58
+ #### ❌ Example of incorrect code
59
+
60
+ ```js
61
+ import insertRust from './insert-rust.js';
62
+ import addAction from './add-action.js';
63
+
64
+ export const rules = {};
65
+ ```
66
+
67
+ #### ✅ Example of correct code
68
+
69
+ ```js
70
+ import insertRust from './insert-rust/index.js';
71
+ import addAction from './add-action/index.js';
72
+
73
+ export const rules = {};
74
+ ```
75
+
76
+
77
+ ### apply-export-from
50
78
 
51
79
  > The `export` declaration is used to export values from a JavaScript module.
52
80
  >
53
81
  > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export)
54
82
 
55
- Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/c9a3983d269745da89c1c7560f3b7fac/3ecb9aa6b910ce3816605bae11c8dd86bdc457e5).
83
+ Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/##/gist/c9a3983d269745da89c1c7560f3b7fac/3ecb9aa6b910ce3816605bae11c8dd86bdc457e5).
56
84
 
57
- ## ❌ Example of incorrect code
85
+ #### ❌ Example of incorrect code
58
86
 
59
87
  ```js
60
88
  import * as ns_1 from 'x';
@@ -64,24 +92,24 @@ export {
64
92
  };
65
93
  ```
66
94
 
67
- ## ✅ Example of correct code
95
+ #### ✅ Example of correct code
68
96
 
69
97
  ```js
70
98
  export * as ns from 'x';
71
99
  ```
72
100
 
73
- ## declare-imports-first
101
+ ### declare-imports-first
74
102
 
75
103
  Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/b1c18e5d726afe4ebb69d6b7a7dda82b/8189590815a1b8adb35bb8a846e28228e3c7fadf). For **CommonJS** use [nodejs/declare-after-require](https://github.com/coderaiser/putout/tree/master/packages/plugin-nodejs#declare-after-require).
76
104
 
77
- ## ❌ Example of incorrect code
105
+ #### ❌ Example of incorrect code
78
106
 
79
107
  ```js
80
108
  const [arg] = process.argv;
81
109
  import esbuild from 'esbuild';
82
110
  ```
83
111
 
84
- ## ✅ Example of correct code
112
+ #### ✅ Example of correct code
85
113
 
86
114
  ```js
87
115
  import esbuild from 'esbuild';
@@ -89,7 +117,7 @@ import esbuild from 'esbuild';
89
117
  const [arg] = process.argv;
90
118
  ```
91
119
 
92
- ## group-imports-by-source
120
+ ### group-imports-by-source
93
121
 
94
122
  Group order:
95
123
 
@@ -100,7 +128,7 @@ Group order:
100
128
 
101
129
  Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/3cc782acf95211f9d456d63a99032ee1/0674223d050bba572f5271ffdccf8616cb441af5).
102
130
 
103
- ## ❌ Example of incorrect code
131
+ #### ❌ Example of incorrect code
104
132
 
105
133
  ```js
106
134
  import fs from 'node:fs';
@@ -114,7 +142,7 @@ import parse from '#parser';
114
142
  const c = 5;
115
143
  ```
116
144
 
117
- ## ✅ Example of correct code
145
+ #### ✅ Example of correct code
118
146
 
119
147
  ```js
120
148
  import fs from 'node:fs';
@@ -128,9 +156,9 @@ import ss from '../../bb/ss.js';
128
156
  const c = 5;
129
157
  ```
130
158
 
131
- ## merge-duplicate-imports
159
+ ### merge-duplicate-imports
132
160
 
133
- ### join
161
+ #### join
134
162
 
135
163
  To disable use:
136
164
 
@@ -142,20 +170,20 @@ To disable use:
142
170
  }
143
171
  ```
144
172
 
145
- #### ❌ Example of incorrect code
173
+ ##### ❌ Example of incorrect code
146
174
 
147
175
  ```js
148
176
  import test from 'supertape';
149
177
  import {stub} from 'supertape';
150
178
  ```
151
179
 
152
- #### ✅ Example of correct code
180
+ ##### ✅ Example of correct code
153
181
 
154
182
  ```js
155
183
  import test, {stub} from 'supertape';
156
184
  ```
157
185
 
158
- ### rename
186
+ #### rename
159
187
 
160
188
  Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/6604936dec6b1eed8ce0d143f2962f15/17b310a6e4d85b0b8615a8b91d0e27414e8af291).
161
189
 
@@ -169,7 +197,7 @@ To disable use:
169
197
  }
170
198
  ```
171
199
 
172
- #### ❌ Example of incorrect code
200
+ ##### ❌ Example of incorrect code
173
201
 
174
202
  ```js
175
203
  import putout from './putout.js';
@@ -180,7 +208,7 @@ console.log(all);
180
208
  console.log(x);
181
209
  ```
182
210
 
183
- #### ✅ Example of correct code
211
+ ##### ✅ Example of correct code
184
212
 
185
213
  ```js
186
214
  import putout from './putout.js';
@@ -189,39 +217,39 @@ console.log(putout);
189
217
  console.log(putout);
190
218
  ```
191
219
 
192
- ## remove-empty-export
220
+ ### remove-empty-export
193
221
 
194
222
  ```diff
195
223
  -export {};
196
224
  ```
197
225
 
198
- ## remove-empty-import
226
+ ### remove-empty-import
199
227
 
200
228
  ```diff
201
229
  -import 'abc';
202
230
  ```
203
231
 
204
- ## remove-quotes-from-import-assertions
232
+ ### remove-quotes-from-import-assertions
205
233
 
206
234
  Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/f9f34acddbefba0ded53225ca10fa44e/7b4dba44602b9b2d28fe3a98989474a4b0d8d73d).
207
235
 
208
- ### ❌ Example of incorrect code
236
+ #### ❌ Example of incorrect code
209
237
 
210
238
  ```js
211
239
  import json from './mod.json' with { type: 'json' };
212
240
  ```
213
241
 
214
- ## ✅ Example of correct code
242
+ #### ✅ Example of correct code
215
243
 
216
244
  ```js
217
245
  import json from './mod.json' with { type: 'json' };
218
246
  ```
219
247
 
220
- ## sort-imports-by-specifiers
248
+ ### sort-imports-by-specifiers
221
249
 
222
250
  Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/521e2ff199243a7ce1f65db7140c272e/28c0588281286f8a6765b8aa2ecabbfcde2973a7).
223
251
 
224
- ### ❌ Example of incorrect code
252
+ #### ❌ Example of incorrect code
225
253
 
226
254
  ```js
227
255
  import {
@@ -233,7 +261,7 @@ import {
233
261
  import a1 from 'a1';
234
262
  ```
235
263
 
236
- ### ✅ Example of correct code
264
+ #### ✅ Example of correct code
237
265
 
238
266
  ```js
239
267
  import a1 from 'a1';
@@ -245,7 +273,7 @@ import {
245
273
  } from 'd';
246
274
  ```
247
275
 
248
- ## convert-assert-to-with
276
+ ### convert-assert-to-with
249
277
 
250
278
  > This feature would ideally use the `with` keyword to denote attributes, but there are existing implementations based on a previous version of the proposal using the `assert` keyword. Due to potential web compatibility risks, the proposal still includes `assert` marked as deprecated. Usage of the old syntax is discouraged, and its removal is being investigated.
251
279
  >
@@ -253,7 +281,7 @@ import {
253
281
 
254
282
  Check out in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/9f85897b998c6458efc19db6a5414b79/57ef7cdd113c7a0087e0f7a6e70522f60baa04f4).
255
283
 
256
- ## ❌ Example of incorrect code
284
+ #### ❌ Example of incorrect code
257
285
 
258
286
  ```js
259
287
  import json from './foo.json' assert { type: 'json' };
@@ -265,7 +293,7 @@ import('foo.json', {
265
293
  });
266
294
  ```
267
295
 
268
- ## ✅ Example of correct code
296
+ #### ✅ Example of correct code
269
297
 
270
298
  ```js
271
299
  import json from './foo.json' with { type: 'json' };
@@ -0,0 +1,31 @@
1
+ import {extname} from 'node:path';
2
+ import {operator} from 'putout';
3
+
4
+ const {setLiteralValue} = operator;
5
+
6
+ export const report = (path) => {
7
+ const {value} = path.node.source;
8
+ return `Add 'index.js' to import: '${value}' -> '${value}/index.js'`;
9
+ };
10
+
11
+ export const fix = (path) => {
12
+ const {source} = path.node;
13
+ const {value} = source;
14
+
15
+ setLiteralValue(source, `${value}/index.js`);
16
+ };
17
+
18
+ export const traverse = ({push}) => ({
19
+ ImportDeclaration: (path) => {
20
+ const {value} = path.node.source;
21
+ const ext = extname(value);
22
+
23
+ if (ext)
24
+ return;
25
+
26
+ if (!value.startsWith('.'))
27
+ return;
28
+
29
+ push(path);
30
+ },
31
+ });
package/lib/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import * as addIndexToImport from './add-index-to-import/index.js';
1
2
  import * as declareImportsFirst from './declare-imports-first/index.js';
2
3
  import * as groupImportsBySource from './group-imports-by-source/index.js';
3
4
  import * as removeQuotesFromImportAssertions from './remove-quotes-from-import-assertions/index.js';
@@ -9,13 +10,14 @@ import * as convertAssertToWith from './convert-assert-to-with/index.js';
9
10
  import * as applyExportFrom from './apply-export-from/index.js';
10
11
 
11
12
  export const rules = {
12
- ...mergeDuplicateImports.rules,
13
+ 'add-index-to-import': addIndexToImport,
14
+ 'apply-export-from': applyExportFrom,
15
+ 'convert-assert-to-with': convertAssertToWith,
13
16
  'declare-imports-first': declareImportsFirst,
14
17
  'group-imports-by-source': groupImportsBySource,
18
+ ...mergeDuplicateImports.rules,
15
19
  'remove-quotes-from-import-assertions': removeQuotesFromImportAssertions,
16
20
  'remove-empty-import': removeEmptyImport,
17
21
  'remove-empty-export': removeEmptyExport,
18
22
  'sort-imports-by-specifiers': sortImportsBySpecifiers,
19
- 'convert-assert-to-with': convertAssertToWith,
20
- 'apply-export-from': applyExportFrom,
21
23
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-esm",
3
- "version": "3.0.0",
3
+ "version": "4.0.0",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "🐊Putout plugin improves ability to transform ESM code",
@@ -56,7 +56,7 @@
56
56
  "supertape": "^11.0.3"
57
57
  },
58
58
  "peerDependencies": {
59
- "putout": ">=39"
59
+ "putout": ">=40"
60
60
  },
61
61
  "license": "MIT",
62
62
  "engines": {