Skip to content

Commit 7f1072a

Browse files
committed
Handle partial barrel imports
1 parent 28b7cda commit 7f1072a

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ See additional options with the `help` command (or `--help` flag).
1717
# see options for the command
1818
npx @openlayers/codemod@latest help replace-barrel-imports
1919
```
20+
21+
## Development
22+
23+
Run tests with `npm test`. Debug the tests with `npx vitest --inspect-brk --no-file-parallelism` (then visit `chrome://inspect/#devices`).
24+
25+
To run the codemod directly from this repo, so something like `node ./codemod.js replace-barrel-imports --consider-relative-paths ../path/to/other/files`.

src/replace-barrel-imports.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ function replaceBarrelImports(fileInfo, api, {considerRelativePaths}) {
229229
barrelFiles[barrelPath],
230230
);
231231
if (!declaration) {
232+
// not a barrel import, leave as is
233+
if (!(importPath in importDeclarations)) {
234+
importDeclarations[importPath] = [];
235+
}
236+
importDeclarations[importPath].push(specifier);
232237
continue;
233238
}
234239

tests/replace-barrel-imports.test.mjs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,31 @@ const cases = [
9898
import OSM from '../src/ol/source/OSM.js';
9999
`,
100100
},
101+
{
102+
name: 'handle partial barrel file',
103+
options: {considerRelativePaths: true},
104+
input: `
105+
import {createXYZ, TileGrid} from '../src/ol/tilegrid.js';
106+
`,
107+
output: `
108+
import TileGrid from '../src/ol/tilegrid/TileGrid.js';
109+
import {createXYZ} from '../src/ol/tilegrid.js';
110+
`,
111+
},
112+
{
113+
name: 'handle multiple partial barrel files',
114+
options: {considerRelativePaths: true},
115+
input: `
116+
import {get as getProjection} from '../src/ol/proj.js';
117+
import {createXYZ, TileGrid, WMTS} from '../src/ol/tilegrid.js';
118+
`,
119+
output: `
120+
import {get as getProjection} from '../src/ol/proj.js';
121+
import TileGrid from '../src/ol/tilegrid/TileGrid.js';
122+
import WMTS from '../src/ol/tilegrid/WMTS.js';
123+
import {createXYZ} from '../src/ol/tilegrid.js';
124+
`,
125+
},
101126
{
102127
name: 'named imports',
103128
options: {considerRelativePaths: false},
@@ -123,6 +148,11 @@ for (const c of cases) {
123148
c.options,
124149
);
125150

151+
if (!output) {
152+
expect(dedent(c.input)).toBe(dedent(c.output));
153+
return;
154+
}
155+
126156
const o = await format(output);
127157
expect(o).toBe(dedent(c.output));
128158
});

0 commit comments

Comments
 (0)