Skip to content

Commit c5da21d

Browse files
committed
feature: @putout/plugin-esm: add resolve-imported-file
1 parent f78c3dd commit c5da21d

28 files changed

+400
-7
lines changed

packages/plugin-esm/README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ npm i putout @putout/plugin-esm -D
2727
-[remove-empty-import](#remove-empty-import);
2828
-[remove-empty-export](#remove-empty-export);
2929
-[sort-imports-by-specifiers](#sort-imports-by-specifiers);
30+
-[resolve-imported-file](#resolve-imported-file);
3031

3132
## Config
3233

@@ -43,7 +44,8 @@ npm i putout @putout/plugin-esm -D
4344
"esm/remove-empty-import": ["on", {
4445
"ignore": []
4546
}],
46-
"esm/sort-imports-by-specifiers": "on"
47+
"esm/sort-imports-by-specifiers": "on",
48+
"esm/resolve-imported-file": "on"
4749
}
4850
}
4951
```
@@ -304,6 +306,37 @@ import('foo.json', {
304306
});
305307
```
306308

309+
### resolve-imported-file
310+
311+
Check out in 🐊**Putout Editor**:
312+
313+
-[`resolve-imported-file`](https://putout.cloudcmd.io/#/gist/241489cb2781dd37ec96baf0115cde4e/83c2f2e9f490850b7fda432f8d25ae6a64ed07e3);
314+
-[`get-imports`](https://putout.cloudcmd.io/#/gist/ee10100fed86e4db926885dd54298668/7538bca7a9ae006d976f41261c0ed4c0e1902ace);
315+
-[`change-imports`](https://putout.cloudcmd.io/#/gist/23a6dc6741b772c03fbed95feda2b451/1fbecac6fc40282bcda0593aa666a8c213ef85b7);
316+
317+
Let's consider file structure:
318+
319+
```
320+
/
321+
|-- lib/
322+
| `-- index.js
323+
| `-- a.js
324+
```
325+
326+
In this case `index.js` can be fixed:
327+
328+
#### ❌ Example of incorrect code
329+
330+
```js
331+
import a from './a';
332+
```
333+
334+
#### ✅ Example of correct code
335+
336+
```js
337+
import a from './a.js';
338+
```
339+
307340
## License
308341

309342
MIT

packages/plugin-esm/eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const match = {
66
'*.md{js}': {
77
'n/no-deprecated-api': 'off',
88
'n/no-unsupported-features/node-builtins': 'off',
9+
'putout/no-unresolved': 'off',
910
},
1011
};
1112

packages/plugin-esm/lib/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as resolveImportedFile from './resolve-imported-file/index.js';
12
import * as addIndexToImport from './add-index-to-import/index.js';
23
import * as declareImportsFirst from './declare-imports-first/index.js';
34
import * as groupImportsBySource from './group-imports-by-source/index.js';
@@ -10,7 +11,7 @@ import * as convertAssertToWith from './convert-assert-to-with/index.js';
1011
import * as applyExportFrom from './apply-export-from/index.js';
1112

1213
export const rules = {
13-
'add-index-to-import': addIndexToImport,
14+
'add-index-to-import': ['off', addIndexToImport],
1415
'apply-export-from': applyExportFrom,
1516
'convert-assert-to-with': convertAssertToWith,
1617
'declare-imports-first': declareImportsFirst,
@@ -20,4 +21,5 @@ export const rules = {
2021
'remove-empty-import': removeEmptyImport,
2122
'remove-empty-export': removeEmptyExport,
2223
'sort-imports-by-specifiers': sortImportsBySpecifiers,
24+
'resolve-imported-file': ['off', resolveImportedFile],
2325
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import a from './a.js';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import a from './a';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const report = () => '';
2+
export const replace = ({options}) => {
3+
const {from = '', to = ''} = options;
4+
5+
if (!from || !to)
6+
return {};
7+
8+
return {
9+
[`import __imports from '${from}'`]: `import __imports from '${to}'`,
10+
};
11+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {createTest} from '@putout/test';
2+
import * as plugin from './index.js';
3+
4+
const test = createTest(import.meta.url, {
5+
plugins: [
6+
['change-imports', plugin],
7+
],
8+
});
9+
10+
test('putout: esm: resolve-imported-file: no report: change-imports', (t) => {
11+
t.noReport('change-imports');
12+
t.end();
13+
});
14+
15+
test('putout: esm: resolve-imported-file: report: change-imports', (t) => {
16+
t.reportWithOptions('change-imports', ``, {
17+
from: './a',
18+
to: './a.js',
19+
});
20+
t.end();
21+
});
22+
23+
test('putout: esm: resolve-imported-file: transform: change-imports', (t) => {
24+
t.transformWithOptions('change-imports', {
25+
from: './a',
26+
to: './a.js',
27+
});
28+
t.end();
29+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
__putout_processor_filesystem([
2+
'/',
3+
'/lib/',
4+
['/lib/index.js', `
5+
import dotdot from '..';
6+
`],
7+
]);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
__putout_processor_filesystem([
2+
"/",
3+
"/lib/",
4+
["/lib/index.js", "aW1wb3J0IGEgZnJvbSAnLi9hLmpzJzsKaW1wb3J0IGIgZnJvbSAnLi9iL2luZGV4LmpzJzsKaW1wb3J0IGUgZnJvbSAnLi4vZSc7CmltcG9ydCBmcyBmcm9tICdmcyc7CmltcG9ydCBjIGZyb20gJy4vYy5qcyc7CmltcG9ydCBkIGZyb20gJy4vZC5tanMnOwppbXBvcnQgY2pzIGZyb20gJy4vZS5janMnOwppbXBvcnQgZG90ZG90IGZyb20gJy4uL2xpYi9pbmRleC5qcyc7Cg=="],
5+
["/lib/a.js", "ZXhwb3J0IGRlZmF1bHQgNVxcbg=="],
6+
"/lib/b/",
7+
["/lib/b/index.js", "export default 7\n"],
8+
["/package.json", "{"main": "lib/index.js"}"]
9+
]);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
__putout_processor_filesystem([
2+
'/',
3+
'/lib/',
4+
['/lib/index.js', `
5+
import a from "./a";
6+
import b from "./b";
7+
import e from "../e";
8+
import fs from "fs";
9+
import c from "./c.js";
10+
import d from "./d.mjs";
11+
import cjs from "./e.cjs";
12+
import dotdot from '..';
13+
`],
14+
['/lib/a.js', `export default 5\\n`],
15+
'/lib/b/',
16+
['/lib/b/index.js', 'export default 7\\n'],
17+
['/package.json', '{"main": "lib/index.js"}'],
18+
]);

0 commit comments

Comments
 (0)