@putout/plugin-esm 8.5.1 → 8.6.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
@@ -460,8 +460,9 @@ Let's consider file structure:
460
460
  ```
461
461
  /
462
462
  |-- lib/
463
- | `-- index.js "import a from './a.js';"
463
+ | `-- index.js "import a from './a.js';\n export * as b from './b.js'"
464
464
  | `-- a.js "export const a = 2;"
465
+ | `-- b.js "export const b = 2;"
465
466
  ```
466
467
 
467
468
  In this case `index.js` can be fixed:
@@ -470,12 +471,16 @@ In this case `index.js` can be fixed:
470
471
 
471
472
  ```js
472
473
  import a from './a.js';
474
+
475
+ export * as b from './b.js';
473
476
  ```
474
477
 
475
478
  ##### ✅ Example of correct code
476
479
 
477
480
  ```js
478
481
  import {a} from './a.js';
482
+
483
+ export {b} from './b.js';
479
484
  ```
480
485
 
481
486
  ### apply-privately-imported-file
@@ -1,10 +1,23 @@
1
+ import {types} from 'putout';
2
+
3
+ const {isImportDefaultSpecifier} = types;
4
+
1
5
  export const report = (path) => {
2
6
  const source = path.node.source.value;
3
7
  const {specifiers} = path.node;
4
- const [{local}] = specifiers;
5
- const {name} = local;
8
+ const [first] = specifiers;
9
+
10
+ if (isImportDefaultSpecifier(first)) {
11
+ const {local} = first;
12
+ const {name} = local;
13
+
14
+ return `'import ${name} from "${source}"' -> 'import {${name}} from "${source}"'`;
15
+ }
16
+
17
+ const {exported} = first;
18
+ const {name} = exported;
6
19
 
7
- return `'import ${name} from "${source}"' -> 'import {${name}} from "${source}"'`;
20
+ return `'export * as ${name} from "${source}"' -> 'export {${name}} from "${source}"'`;
8
21
  };
9
22
 
10
23
  export const replace = ({options}) => {
@@ -12,5 +25,6 @@ export const replace = ({options}) => {
12
25
 
13
26
  return {
14
27
  [`import ${name} from "${source}"`]: `import {${name}} from "${source}"`,
28
+ [`export * as ${name} from "${source}"`]: `export {${name}} from "${source}"`,
15
29
  };
16
30
  };
@@ -1,10 +1,15 @@
1
+ import {types} from 'putout';
2
+
3
+ const {isImportDefaultSpecifier} = types;
4
+
1
5
  export const include = () => [
2
6
  'import __a from "__b"',
7
+ 'export * as __a from "__b"',
3
8
  ];
4
9
 
5
10
  export const report = (path) => {
6
- const [name, source] = getImport(path);
7
- return `${name} <- ${source}`;
11
+ const [name, source, type] = getImport(path);
12
+ return `${name} <- ${source} <- ${type}`;
8
13
  };
9
14
 
10
15
  export const fix = (path) => {
@@ -23,7 +28,21 @@ const CommentLine = (value) => ({
23
28
  const getImport = (path) => {
24
29
  const source = path.node.source.value;
25
30
  const [first] = path.node.specifiers;
26
- const {name} = first.local;
27
31
 
28
- return [name, source];
32
+ if (isImportDefaultSpecifier(first)) {
33
+ const {name} = first.local;
34
+ return [
35
+ name,
36
+ source,
37
+ 'import',
38
+ ];
39
+ }
40
+
41
+ const {name} = first.exported;
42
+
43
+ return [
44
+ name,
45
+ source,
46
+ 'export',
47
+ ];
29
48
  };
@@ -6,9 +6,6 @@ const {getFilename} = operator;
6
6
  const getMessage = (a) => a.message;
7
7
 
8
8
  export const getImportsTuples = (file, content, ast) => {
9
- if (!content.includes('import'))
10
- return [];
11
-
12
9
  const places = transform(ast, content, {
13
10
  fix: false,
14
11
  plugins: [
@@ -35,10 +32,15 @@ function buildImports(dir, imports) {
35
32
  const list = [];
36
33
 
37
34
  for (const current of imports) {
38
- const [name, source] = current.split(' <- ');
35
+ const [name, source, type] = current.split(' <- ');
39
36
  const full = parseFull(dir, source);
40
37
 
41
- list.push([name, source, full]);
38
+ list.push([
39
+ name,
40
+ source,
41
+ full,
42
+ type,
43
+ ]);
42
44
  }
43
45
 
44
46
  return list;
@@ -16,9 +16,13 @@ const {
16
16
  crawlDirectory,
17
17
  } = operator;
18
18
 
19
- export const report = (file, {name, source}) => {
19
+ export const report = (file, {name, source, type}) => {
20
20
  const filename = getFilename(file);
21
- return `Use \`import {${name}} from '${source}'\` in '${filename}'`;
21
+
22
+ if (type === 'import')
23
+ return `Use \`import {${name}} from '${source}'\` in '${filename}'`;
24
+
25
+ return `Use \`export {${name}} from '${source}'\` in '${filename}'`;
22
26
  };
23
27
 
24
28
  export const fix = (file, {name, source, content, ast}) => {
@@ -55,7 +59,7 @@ export const scan = (rootPath, {push, trackFile}) => {
55
59
  aliasBased: true,
56
60
  });
57
61
 
58
- for (const [name, source, importedFilename] of importsTuples) {
62
+ for (const [name, source, importedFilename, type] of importsTuples) {
59
63
  const importType = determineImportType({
60
64
  name,
61
65
  rootPath,
@@ -66,7 +70,7 @@ export const scan = (rootPath, {push, trackFile}) => {
66
70
 
67
71
  if (importType === 'equal')
68
72
  push(file, {
69
- importType,
73
+ type,
70
74
  name,
71
75
  source,
72
76
  ast,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-esm",
3
- "version": "8.5.1",
3
+ "version": "8.6.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",