@putout/plugin-esm 8.5.1 → 8.7.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
@@ -34,8 +34,8 @@ npm i putout @putout/plugin-esm -D
34
34
 
35
35
  ## File rules
36
36
 
37
- - ✅ [apply-name-to-imported-file](#apply-name-to-imported-file]]);
38
- - ✅ [apply-namespace-to-imported-file](#apply-namespace-to-imported-file]]);
37
+ - ✅ [apply-name-to-imported-file](#apply-name-to-imported-file);
38
+ - ✅ [apply-namespace-to-imported-file](#apply-namespace-to-imported-file);
39
39
  - ✅ [apply-privately-imported-file](#apply-privately-imported-file);
40
40
  - ✅ [apply-js-imported-file](#apply-js-imported-file);
41
41
  - ✅ [resolve-imported-file](#resolve-imported-file);
@@ -428,9 +428,9 @@ Let's consider file structure:
428
428
 
429
429
  ```
430
430
  /
431
- |-- lib/
432
- | `-- index.js "import a from './a.js';"
433
- | `-- a.js "export const x = 2;"
431
+ └── lib/
432
+ ├── index.js "import a from './a.js';"
433
+ └── b.js "export const b = 2;"
434
434
  ```
435
435
 
436
436
  In this case `index.js` can be fixed:
@@ -459,9 +459,10 @@ Let's consider file structure:
459
459
 
460
460
  ```
461
461
  /
462
- |-- lib/
463
- | `-- index.js "import a from './a.js';"
464
- | `-- a.js "export const a = 2;"
462
+ └── lib/
463
+ ├── index.js "import a from './a.js';\n export * as b from './b.js'"
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
@@ -495,12 +500,12 @@ Let's consider file structure:
495
500
 
496
501
  ```
497
502
  /
498
- |-- package.json {"imports": {"#is: {"default": "./lib/tokenize/is.js"}}}
499
- |-- lib/
500
- | `-- tokenize/
501
- | `-- is.js "export const isPrev = () => {}"
502
- | `-- expressions/
503
- `-- spread-element.js "import {isPrev} from '../is.js"
503
+ ├── package.json {"imports": {"#is: {"default": "./lib/tokenize/is.js"}}}
504
+ └── lib/
505
+ └── tokenize/
506
+ ├── is.js "export const isPrev = () => {}"
507
+ └── expressions/
508
+ └── spread-element.js "import {isPrev} from '../is.js"
504
509
  ```
505
510
 
506
511
  In this case `spread-element.js` can be fixed:
@@ -529,9 +534,9 @@ Let's consider file structure:
529
534
 
530
535
  ```
531
536
  /
532
- |-- lib/
533
- | `-- index.js
534
- | `-- a.js
537
+ └── lib/
538
+ ├── index.js
539
+ └── a.js
535
540
  ```
536
541
 
537
542
  In this case `index.js` can be fixed:
@@ -559,9 +564,9 @@ Let's consider file structure:
559
564
 
560
565
  ```
561
566
  /
562
- |-- processors/
563
- | `-- index.js
564
- | `-- parse-processor-names.js
567
+ └── processors/
568
+ ├── index.js
569
+ └── parse-processor-names.js
565
570
  ```
566
571
 
567
572
  In this case `index.js` can be fixed:
@@ -589,9 +594,9 @@ Let's consider file structure:
589
594
 
590
595
  ```
591
596
  /
592
- |-- lib/
593
- | `-- index.js
594
- | `-- a.js
597
+ └── lib/
598
+ ├── index.js
599
+ └── a.js
595
600
  ```
596
601
 
597
602
  In this case `index.js` can be fixed:
@@ -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
  };
@@ -5,20 +5,15 @@ import * as isESMPlugin from '#is-esm';
5
5
  const isESM = (a) => a.rule === 'is-esm';
6
6
  const hasExportDefault = (a) => a.rule === 'has-export-default';
7
7
 
8
- const {
9
- findFile,
10
- readFileContent,
11
- } = operator;
8
+ const {readFileContent} = operator;
12
9
 
13
- export const determineImportType = ({name, rootPath, importedFilename, privateImports, crawled}) => {
10
+ export const determineImportType = ({name, rootPath, importedFilename, privateImports, crawlFile}) => {
14
11
  const parsedName = parseImportedFilename({
15
12
  importedFilename,
16
13
  privateImports,
17
14
  });
18
15
 
19
- const [importedFile] = findFile(rootPath, parsedName, {
20
- crawled,
21
- });
16
+ const [importedFile] = crawlFile(rootPath, parsedName);
22
17
 
23
18
  if (!importedFile)
24
19
  return '';
@@ -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,22 @@ 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
+
35
+ return [
36
+ name,
37
+ source,
38
+ 'import',
39
+ ];
40
+ }
41
+
42
+ const {name} = first.exported;
43
+
44
+ return [
45
+ name,
46
+ source,
47
+ 'export',
48
+ ];
29
49
  };
@@ -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;
@@ -6,19 +6,22 @@ import {
6
6
  } from 'putout';
7
7
  import {createGetPrivateImports} from '#private-imports';
8
8
  import {determineImportType} from '#determine-import-type';
9
+ import {getImportsTuples} from '#get-imports-tuples';
9
10
  import {transformNamedImport} from './transform-named-import.js';
10
- import {getImportsTuples} from './get-imports-tuples.js';
11
11
 
12
12
  const {
13
13
  getFilename,
14
14
  readFileContent,
15
15
  writeFileContent,
16
- crawlDirectory,
17
16
  } = operator;
18
17
 
19
- export const report = (file, {name, source}) => {
18
+ export const report = (file, {name, source, type}) => {
20
19
  const filename = getFilename(file);
21
- return `Use \`import {${name}} from '${source}'\` in '${filename}'`;
20
+
21
+ if (type === 'import')
22
+ return `Use \`import {${name}} from '${source}'\` in '${filename}'`;
23
+
24
+ return `Use \`export {${name}} from '${source}'\` in '${filename}'`;
22
25
  };
23
26
 
24
27
  export const fix = (file, {name, source, content, ast}) => {
@@ -33,14 +36,13 @@ export const fix = (file, {name, source, content, ast}) => {
33
36
  writeFileContent(file, newContent);
34
37
  };
35
38
 
36
- export const scan = (rootPath, {push, trackFile}) => {
39
+ export const scan = (rootPath, {push, trackFile, crawlFile}) => {
37
40
  const mask = [
38
41
  '*.js',
39
42
  '*.mjs',
40
43
  ];
41
44
 
42
45
  const getPrivateImports = createGetPrivateImports();
43
- const crawled = crawlDirectory(rootPath);
44
46
 
45
47
  for (const file of trackFile(rootPath, mask)) {
46
48
  const content = readFileContent(file);
@@ -55,18 +57,18 @@ export const scan = (rootPath, {push, trackFile}) => {
55
57
  aliasBased: true,
56
58
  });
57
59
 
58
- for (const [name, source, importedFilename] of importsTuples) {
60
+ for (const [name, source, importedFilename, type] of importsTuples) {
59
61
  const importType = determineImportType({
60
62
  name,
61
63
  rootPath,
62
64
  importedFilename,
63
65
  privateImports,
64
- crawled,
66
+ crawlFile,
65
67
  });
66
68
 
67
69
  if (importType === 'equal')
68
70
  push(file, {
69
- importType,
71
+ type,
70
72
  name,
71
73
  source,
72
74
  ast,
@@ -6,14 +6,13 @@ import {
6
6
  } from 'putout';
7
7
  import {createGetPrivateImports} from '#private-imports';
8
8
  import {determineImportType} from '#determine-import-type';
9
+ import {getImportsTuples} from '#get-imports-tuples';
9
10
  import {transformNamespaceImport} from './transform-namespace-import.js';
10
- import {getImportsTuples} from '../apply-name-to-imported-file/get-imports-tuples.js';
11
11
 
12
12
  const {
13
13
  getFilename,
14
14
  readFileContent,
15
15
  writeFileContent,
16
- crawlDirectory,
17
16
  } = operator;
18
17
 
19
18
  export const report = (file, {name, source}) => {
@@ -33,14 +32,13 @@ export const fix = (file, {name, source, content, ast}) => {
33
32
  writeFileContent(file, newContent);
34
33
  };
35
34
 
36
- export const scan = (rootPath, {push, trackFile}) => {
35
+ export const scan = (rootPath, {push, trackFile, crawlFile}) => {
37
36
  const mask = [
38
37
  '*.js',
39
38
  '*.mjs',
40
39
  ];
41
40
 
42
41
  const getPrivateImports = createGetPrivateImports();
43
- const crawled = crawlDirectory(rootPath);
44
42
 
45
43
  for (const file of trackFile(rootPath, mask)) {
46
44
  const content = readFileContent(file);
@@ -61,7 +59,7 @@ export const scan = (rootPath, {push, trackFile}) => {
61
59
  rootPath,
62
60
  importedFilename,
63
61
  privateImports,
64
- crawled,
62
+ crawlFile,
65
63
  });
66
64
 
67
65
  if (importType === 'namespace')
@@ -1,6 +1,9 @@
1
1
  export const report = () => '';
2
2
  export const replace = ({options}) => {
3
- const {from = '', to = ''} = options;
3
+ const {
4
+ from = '',
5
+ to = '',
6
+ } = options;
4
7
 
5
8
  if (!from || !to)
6
9
  return {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/plugin-esm",
3
- "version": "8.5.1",
3
+ "version": "8.7.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",
@@ -12,7 +12,7 @@
12
12
  },
13
13
  "imports": {
14
14
  "#get-imports": "./lib/shorten-imported-file/get-imports/index.js",
15
- "#get-imports-tuples": "./lib/shorten-imported-file/get-imports-tuples.js",
15
+ "#get-imports-tuples": "./lib/apply-name-to-imported-file/get-imports-tuples.js",
16
16
  "#get-default-imports": "./lib/apply-name-to-imported-file/get-imports/index.js",
17
17
  "#change-imports": "./lib/resolve-imported-file/change-imports/index.js",
18
18
  "#private-imports": "./lib/apply-privately-imported-file/private-imports.js",