@putout/plugin-esm 9.8.0 → 9.10.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,9 +18,9 @@ npm i putout @putout/plugin-esm -D
|
|
|
18
18
|
|
|
19
19
|
## Rules
|
|
20
20
|
|
|
21
|
-
- ✅ [add-index-to-import](#add-index-to-import);
|
|
22
21
|
- ✅ [apply-default-import](#apply-default-import);
|
|
23
22
|
- ✅ [apply-export-from](#apply-export-from);
|
|
23
|
+
- ✅ [apply-import-import](#apply-import-attributes);
|
|
24
24
|
- ✅ [convert-assert-to-with](#convert-assert-to-with);
|
|
25
25
|
- ✅ [declare-imports-first](#declare-imports-first);
|
|
26
26
|
- ✅ [group-imports-by-source](#group-imports-by-source);
|
|
@@ -47,9 +47,9 @@ npm i putout @putout/plugin-esm -D
|
|
|
47
47
|
```json
|
|
48
48
|
{
|
|
49
49
|
"rules": {
|
|
50
|
-
"esm/add-index-to-import": "on",
|
|
51
50
|
"esm/apply-default-import": "on",
|
|
52
51
|
"esm/apply-export-from": "on",
|
|
52
|
+
"esm/apply-import-attirbutes": "on",
|
|
53
53
|
"esm/declare-imports-first": "on",
|
|
54
54
|
"esm/group-imports-by-source": "on",
|
|
55
55
|
"esm/merge-duplicate-imports": "on",
|
|
@@ -124,6 +124,42 @@ export * as ns from 'x';
|
|
|
124
124
|
export {createAsyncLoader} from './load/async-loader.js';
|
|
125
125
|
```
|
|
126
126
|
|
|
127
|
+
### apply-import-attributes
|
|
128
|
+
|
|
129
|
+
> The **import attributes** feature instructs the runtime about how a module should be loaded, including the behavior of module resolution, fetching, parsing, and evaluation.
|
|
130
|
+
>
|
|
131
|
+
> (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import/with)
|
|
132
|
+
|
|
133
|
+
Check out 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/d8d428c2555bacd69d530f84a41ab98e/15d41cf9153b3d2513c2bdf37d36c5276c6f6f95).
|
|
134
|
+
|
|
135
|
+
#### ❌ Example of incorrect code
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
import a from '../package.json';
|
|
139
|
+
|
|
140
|
+
export __export from './package.json';
|
|
141
|
+
|
|
142
|
+
await import('./package.json');
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
#### ✅ Example of correct code
|
|
146
|
+
|
|
147
|
+
```js
|
|
148
|
+
import a from '../package.json' with {
|
|
149
|
+
type: 'json',
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export __export from './package.json' with {
|
|
153
|
+
type: 'json',
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
await import('./package.json', {
|
|
157
|
+
with: {
|
|
158
|
+
type: 'json',
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
127
163
|
### merge-declaration-with-export
|
|
128
164
|
|
|
129
165
|
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/3fad517d76942d0a1f51d7f58a2799af/7a052ab18a31fa382228d6513c412be37091cfb8).
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {types} from 'putout';
|
|
2
|
+
|
|
3
|
+
const {isStringLiteral} = types;
|
|
4
|
+
const TYPES = {
|
|
5
|
+
ImportDeclaration: 'import',
|
|
6
|
+
ExportNamedDeclaration: 'export',
|
|
7
|
+
ImportExpression: 'import',
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const report = (path) => {
|
|
11
|
+
const type = TYPES[path.type];
|
|
12
|
+
return `Use \`${type} with {type: 'json'}\``;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const match = () => ({
|
|
16
|
+
'import __imports from "__a"': ({__a}) => {
|
|
17
|
+
return __a.value.endsWith('.json');
|
|
18
|
+
},
|
|
19
|
+
'export __exports from "__a"': ({__a}) => {
|
|
20
|
+
return __a.value.endsWith('.json');
|
|
21
|
+
},
|
|
22
|
+
'import(__a)': ({__a}) => {
|
|
23
|
+
if (!isStringLiteral(__a))
|
|
24
|
+
return false;
|
|
25
|
+
|
|
26
|
+
return __a.value.endsWith('.json');
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
export const replace = () => ({
|
|
31
|
+
'import __imports from "__a"': `import __imports from "__a" with {
|
|
32
|
+
type: 'json',
|
|
33
|
+
}`,
|
|
34
|
+
'export __exports from "__a"': `export __exports from "__a" with {
|
|
35
|
+
type: 'json',
|
|
36
|
+
}`,
|
|
37
|
+
'import(__a)': `import(__a, {
|
|
38
|
+
with: {
|
|
39
|
+
type: 'json',
|
|
40
|
+
}
|
|
41
|
+
})`,
|
|
42
|
+
});
|
|
@@ -17,7 +17,9 @@ const {
|
|
|
17
17
|
|
|
18
18
|
const getMessage = (a) => a.message;
|
|
19
19
|
|
|
20
|
-
export const report = (file, {from, to}) =>
|
|
20
|
+
export const report = (file, {from, to, filename}) => {
|
|
21
|
+
return `Apply private import: '${from}' -> '${to}' in '${filename}'`;
|
|
22
|
+
};
|
|
21
23
|
export const fix = (file, {content, ast, from, to}) => {
|
|
22
24
|
transform(ast, content, {
|
|
23
25
|
rules: {
|
|
@@ -75,6 +77,7 @@ export const scan = (rootPath, {push, trackFile}) => {
|
|
|
75
77
|
push(file, {
|
|
76
78
|
from,
|
|
77
79
|
to: privateImports.get(to),
|
|
80
|
+
filename,
|
|
78
81
|
content,
|
|
79
82
|
ast,
|
|
80
83
|
});
|
package/lib/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import * as applyImportAttributes from './apply-import-attributes/index.js';
|
|
1
2
|
import * as applyDefaultImport from './apply-default-import/index.js';
|
|
2
3
|
import * as applyExportFrom from './apply-export-from/index.js';
|
|
3
4
|
import * as applyJsImportedFile from './apply-js-imported-file/index.js';
|
|
@@ -39,4 +40,5 @@ export const rules = {
|
|
|
39
40
|
'apply-privately-imported-file': ['off', applyPrivatelyImportedFile],
|
|
40
41
|
'apply-js-imported-file': ['off', applyJsImportedFile],
|
|
41
42
|
'apply-default-import': applyDefaultImport,
|
|
43
|
+
'apply-import-attributes': applyImportAttributes,
|
|
42
44
|
};
|
package/package.json
CHANGED