@putout/plugin-esm 9.0.0 → 9.2.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
@@ -19,6 +19,7 @@ npm i putout @putout/plugin-esm -D
19
19
  ## Rules
20
20
 
21
21
  - ✅ [add-index-to-import](#add-index-to-import);
22
+ - ✅ [apply-default-import](#apply-default-import);
22
23
  - ✅ [apply-export-from](#apply-export-from);
23
24
  - ✅ [convert-assert-to-with](#convert-assert-to-with);
24
25
  - ✅ [declare-imports-first](#declare-imports-first);
@@ -47,6 +48,7 @@ npm i putout @putout/plugin-esm -D
47
48
  {
48
49
  "rules": {
49
50
  "esm/add-index-to-import": "on",
51
+ "esm/apply-default-import": "on",
50
52
  "esm/apply-export-from": "on",
51
53
  "esm/declare-imports-first": "on",
52
54
  "esm/group-imports-by-source": "on",
@@ -72,6 +74,26 @@ npm i putout @putout/plugin-esm -D
72
74
 
73
75
  ## Rules
74
76
 
77
+ ### apply-default-import
78
+
79
+ > The static `import` declaration is used to import read-only live bindings which are exported by another module.
80
+ >
81
+ > (c) [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import)
82
+
83
+ Check out 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/83dcc8478ac794cbe11df41a80a08cbe/8270019356b6f8ea21233d6eb1edccec69e708bb).
84
+
85
+ #### ❌ Example of incorrect code
86
+
87
+ ```js
88
+ import {default as a} from 'a';
89
+ ```
90
+
91
+ #### ✅ Example of correct code
92
+
93
+ ```js
94
+ import a from 'a';
95
+ ```
96
+
75
97
  ### apply-export-from
76
98
 
77
99
  > The `export` declaration is used to export values from a JavaScript module.
@@ -0,0 +1,5 @@
1
+ export const report = () => `Use default import`;
2
+
3
+ export const replace = () => ({
4
+ 'import {default as __a} from "__b"': 'import __a from "__b"',
5
+ });
@@ -1,8 +1,26 @@
1
- import {types} from 'putout';
1
+ import {types, operator} from 'putout';
2
2
 
3
- const {isImportDefaultSpecifier} = types;
3
+ const {getTemplateValues} = operator;
4
+ const {
5
+ isImportDefaultSpecifier,
6
+ isVariableDeclaration,
7
+ } = types;
8
+
9
+ const DYNAMIC = 'const __identifier__a = await import(__b)';
4
10
 
5
11
  export const report = (path) => {
12
+ if (isVariableDeclaration(path)) {
13
+ const {
14
+ __identifier__a,
15
+ __b,
16
+ } = getTemplateValues(path, DYNAMIC);
17
+
18
+ const {name} = __identifier__a;
19
+ const source = __b.value;
20
+
21
+ return `'const ${name} = await import("${source}")' -> 'const {${name}} = await import("${source}")'`;
22
+ }
23
+
6
24
  const source = path.node.source.value;
7
25
  const {specifiers} = path.node;
8
26
  const [first] = specifiers;
@@ -24,6 +42,7 @@ export const replace = ({options}) => {
24
42
  const {name, source} = options;
25
43
 
26
44
  return {
45
+ [`const ${name} = await import("${source}")`]: `const {${name}} = await import("${source}")`,
27
46
  [`import ${name} from "${source}"`]: `import {${name}} from "${source}"`,
28
47
  [`export * as ${name} from "${source}"`]: `export {${name}} from "${source}"`,
29
48
  };
@@ -1,10 +1,17 @@
1
- import {types} from 'putout';
1
+ import {types, operator} from 'putout';
2
2
 
3
- const {isImportDefaultSpecifier} = types;
3
+ const {getTemplateValues} = operator;
4
+ const {
5
+ isImportDefaultSpecifier,
6
+ isVariableDeclaration,
7
+ } = types;
8
+
9
+ const DYNAMIC = 'const __identifier__a = await import(__b)';
4
10
 
5
11
  export const include = () => [
6
12
  'import __a from "__b"',
7
13
  'export * as __a from "__b"',
14
+ DYNAMIC,
8
15
  ];
9
16
 
10
17
  export const report = (path) => {
@@ -26,6 +33,19 @@ const CommentLine = (value) => ({
26
33
  });
27
34
 
28
35
  const getImport = (path) => {
36
+ if (isVariableDeclaration(path)) {
37
+ const {
38
+ __identifier__a,
39
+ __b,
40
+ } = getTemplateValues(path, DYNAMIC);
41
+
42
+ return [
43
+ __identifier__a.name,
44
+ __b.value,
45
+ 'dynamic',
46
+ ];
47
+ }
48
+
29
49
  const source = path.node.source.value;
30
50
  const [first] = path.node.specifiers;
31
51
 
@@ -17,11 +17,13 @@ const {
17
17
 
18
18
  export const report = (file, {name, source, type}) => {
19
19
  const filename = getFilename(file);
20
+ const reports = {
21
+ import: `Use \`import {${name}} from '${source}'\` in '${filename}'`,
22
+ dynamic: `Use \`const {${name}} = await import('${source}')\` in '${filename}'`,
23
+ export: `Use \`export {${name}} from '${source}'\` in '${filename}'`,
24
+ };
20
25
 
21
- if (type === 'import')
22
- return `Use \`import {${name}} from '${source}'\` in '${filename}'`;
23
-
24
- return `Use \`export {${name}} from '${source}'\` in '${filename}'`;
26
+ return reports[type];
25
27
  };
26
28
 
27
29
  export const fix = (file, {name, source, content, ast}) => {
package/lib/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ import * as applyDefaultImport from './apply-default-import/index.js';
1
2
  import * as applyExportFrom from './apply-export-from/index.js';
2
3
  import * as applyJsImportedFile from './apply-js-imported-file/index.js';
3
4
  import * as applyNameToImportedFile from './apply-name-to-imported-file/index.js';
@@ -37,4 +38,5 @@ export const rules = {
37
38
  'apply-namespace-to-imported-file': ['off', applyNamespaceToImportedFile],
38
39
  'apply-privately-imported-file': ['off', applyPrivatelyImportedFile],
39
40
  'apply-js-imported-file': ['off', applyJsImportedFile],
41
+ 'apply-default-import': applyDefaultImport,
40
42
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-esm",
3
- "version": "9.0.0",
3
+ "version": "9.2.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",