@putout/plugin-esm 4.2.1 → 4.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/README.md
CHANGED
|
@@ -23,6 +23,7 @@ npm i putout @putout/plugin-esm -D
|
|
|
23
23
|
- ✅ [declare-imports-first](#declare-imports-first);
|
|
24
24
|
- ✅ [group-imports-by-source](#group-imports-by-source);
|
|
25
25
|
- ✅ [merge-duplicate-imports](#merge-duplicate-imports);
|
|
26
|
+
- ✅ [merge-declaration-with-export](#merge-declaration-with-export);
|
|
26
27
|
- ✅ [remove-quotes-from-import-assertions](#remove-quotes-from-import-assertions);
|
|
27
28
|
- ✅ [remove-empty-import](#remove-empty-import);
|
|
28
29
|
- ✅ [remove-empty-export](#remove-empty-export);
|
|
@@ -43,6 +44,7 @@ npm i putout @putout/plugin-esm -D
|
|
|
43
44
|
"esm/declare-imports-first": "on",
|
|
44
45
|
"esm/group-imports-by-source": "on",
|
|
45
46
|
"esm/merge-duplicate-imports": "on",
|
|
47
|
+
"esm/merge-declaration-with-export": "on",
|
|
46
48
|
"esm/remove-quotes-from-import-assertions": "on",
|
|
47
49
|
"esm/remove-empty-export": "on",
|
|
48
50
|
"esm/remove-empty-import": ["on", {
|
|
@@ -162,6 +164,36 @@ import ss from '../../bb/ss.js';
|
|
|
162
164
|
const c = 5;
|
|
163
165
|
```
|
|
164
166
|
|
|
167
|
+
### merge-declaration-with-export
|
|
168
|
+
|
|
169
|
+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/3fad517d76942d0a1f51d7f58a2799af/7a052ab18a31fa382228d6513c412be37091cfb8).
|
|
170
|
+
|
|
171
|
+
#### ❌ Example of incorrect code
|
|
172
|
+
|
|
173
|
+
```js
|
|
174
|
+
const {
|
|
175
|
+
report,
|
|
176
|
+
fix,
|
|
177
|
+
scan,
|
|
178
|
+
} = createRemoveFiles(['*.swp', '*.swo']);
|
|
179
|
+
|
|
180
|
+
export {
|
|
181
|
+
report,
|
|
182
|
+
fix,
|
|
183
|
+
scan,
|
|
184
|
+
};
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
##### ✅ Example of correct code
|
|
188
|
+
|
|
189
|
+
```js
|
|
190
|
+
export const {
|
|
191
|
+
report,
|
|
192
|
+
fix,
|
|
193
|
+
scan,
|
|
194
|
+
} = createRemoveFiles(['*.swp', '*.swo']);
|
|
195
|
+
```
|
|
196
|
+
|
|
165
197
|
### merge-duplicate-imports
|
|
166
198
|
|
|
167
199
|
#### join
|
|
@@ -315,6 +347,10 @@ import('foo.json', {
|
|
|
315
347
|
|
|
316
348
|
### apply-namespace-import-to-file
|
|
317
349
|
|
|
350
|
+
The rule fixes:
|
|
351
|
+
|
|
352
|
+
> `SyntaxError: The requested module './a.js' does not provide an export named 'default'`
|
|
353
|
+
|
|
318
354
|
Check out in 🐊**Putout Editor**:
|
|
319
355
|
|
|
320
356
|
- ✅ [`apply-namespace-import-to-file`](https://putout.cloudcmd.io/#/gist/1492d584559e5798325047de679222a0/c6a37a803b80823de1b64ab944f2427aecefb51b);
|
|
@@ -345,10 +381,6 @@ import a from './a.js';
|
|
|
345
381
|
import * as a from './a.js';
|
|
346
382
|
```
|
|
347
383
|
|
|
348
|
-
## License
|
|
349
|
-
|
|
350
|
-
MIT
|
|
351
|
-
|
|
352
384
|
### resolve-imported-file
|
|
353
385
|
|
|
354
386
|
Check out in 🐊**Putout Editor**:
|
|
@@ -21,7 +21,11 @@ const getMessage = (a) => a.message;
|
|
|
21
21
|
const isESM = (a) => a.rule === 'is-esm';
|
|
22
22
|
const hasExportDefault = (a) => a.rule === 'has-export-default';
|
|
23
23
|
|
|
24
|
-
export const report = (file, {name, source}) =>
|
|
24
|
+
export const report = (file, {name, source}) => {
|
|
25
|
+
const filename = getFilename(file);
|
|
26
|
+
return `Use 'import * as ${name} from '${source}' in '${filename}'`;
|
|
27
|
+
};
|
|
28
|
+
|
|
25
29
|
export const fix = (file, {name, source, content, ast}) => {
|
|
26
30
|
transform(ast, content, {
|
|
27
31
|
rules: {
|
package/lib/index.js
CHANGED
|
@@ -8,6 +8,7 @@ import * as sortImportsBySpecifiers from './sort-imports-by-specifiers/index.js'
|
|
|
8
8
|
import * as removeEmptyImport from './remove-empty-import/index.js';
|
|
9
9
|
import * as removeEmptyExport from './remove-empty-export/index.js';
|
|
10
10
|
import * as mergeDuplicateImports from './merge-duplicate-imports/index.js';
|
|
11
|
+
import * as mergeDeclarationWithExport from './merge-declaration-with-export/index.js';
|
|
11
12
|
import * as convertAssertToWith from './convert-assert-to-with/index.js';
|
|
12
13
|
import * as applyExportFrom from './apply-export-from/index.js';
|
|
13
14
|
|
|
@@ -24,4 +25,5 @@ export const rules = {
|
|
|
24
25
|
'sort-imports-by-specifiers': sortImportsBySpecifiers,
|
|
25
26
|
'resolve-imported-file': ['off', resolveImportedFile],
|
|
26
27
|
'apply-namespace-import-to-file': ['off', applyNamespaceImportToFile],
|
|
28
|
+
'merge-declaration-with-export': mergeDeclarationWithExport,
|
|
27
29
|
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {operator, types} from 'putout';
|
|
2
|
+
|
|
3
|
+
const {remove, replaceWith} = operator;
|
|
4
|
+
const {
|
|
5
|
+
exportNamedDeclaration,
|
|
6
|
+
isExportSpecifier,
|
|
7
|
+
isVariableDeclarator,
|
|
8
|
+
} = types;
|
|
9
|
+
|
|
10
|
+
export const report = () => `Use 'if condition' instead of 'ternary expression'`;
|
|
11
|
+
|
|
12
|
+
export const fix = ({path, bindingPath}) => {
|
|
13
|
+
const {parentPath} = bindingPath;
|
|
14
|
+
const {node} = parentPath;
|
|
15
|
+
|
|
16
|
+
replaceWith(parentPath, exportNamedDeclaration(node));
|
|
17
|
+
remove(path);
|
|
18
|
+
};
|
|
19
|
+
export const traverse = ({push}) => ({
|
|
20
|
+
ExportNamedDeclaration(path) {
|
|
21
|
+
const {specifiers} = path.node;
|
|
22
|
+
|
|
23
|
+
if (!specifiers.length)
|
|
24
|
+
return;
|
|
25
|
+
|
|
26
|
+
const bindingPaths = [];
|
|
27
|
+
|
|
28
|
+
for (const spec of specifiers) {
|
|
29
|
+
if (!isExportSpecifier(spec))
|
|
30
|
+
return;
|
|
31
|
+
|
|
32
|
+
const {local} = spec;
|
|
33
|
+
|
|
34
|
+
const {name} = local;
|
|
35
|
+
const binding = path.scope.bindings[name];
|
|
36
|
+
|
|
37
|
+
if (!binding)
|
|
38
|
+
return;
|
|
39
|
+
|
|
40
|
+
const {path: bindingPath} = binding;
|
|
41
|
+
|
|
42
|
+
if (!isVariableDeclarator(bindingPath))
|
|
43
|
+
return;
|
|
44
|
+
|
|
45
|
+
if (!bindingPaths.length) {
|
|
46
|
+
bindingPaths.push(bindingPath);
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const [bindingPath] = bindingPaths;
|
|
52
|
+
|
|
53
|
+
push({
|
|
54
|
+
path,
|
|
55
|
+
bindingPath,
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {types, operator} from 'putout';
|
|
2
2
|
|
|
3
|
-
const {remove
|
|
3
|
+
const {remove} = operator;
|
|
4
4
|
const {values} = Object;
|
|
5
5
|
|
|
6
6
|
const {
|
|
@@ -23,9 +23,6 @@ export const fix = ({path, imports}) => {
|
|
|
23
23
|
continue;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
if (compareAny(spec, path.node.specifiers))
|
|
27
|
-
continue;
|
|
28
|
-
|
|
29
26
|
all.push(spec);
|
|
30
27
|
}
|
|
31
28
|
|
package/package.json
CHANGED