Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b2f9924

Browse files
KazariEXantfu
andauthored
fix: use map instead of plain object for injection usage records (#496)
* fix: use map instead of plain object for injection usage records * test: add * add `injections` and deprecate `injectionUsage` * chore: update * chore: update --------- Co-authored-by: Anthony Fu <[email protected]>
1 parent e908b92 commit b2f9924

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

src/context.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,13 @@ export function createUnimport(opts: Partial<UnimportOptions>): Unimport {
6464
const metadata = ctx.getMetadata()
6565
if (metadata) {
6666
result.imports.forEach((i) => {
67-
metadata.injectionUsage[i.name] = metadata.injectionUsage[i.name] || { import: i, count: 0, moduleIds: [] }
68-
metadata.injectionUsage[i.name].count++
69-
if (id && !metadata.injectionUsage[i.name].moduleIds.includes(id))
70-
metadata.injectionUsage[i.name].moduleIds.push(id)
67+
let record = metadata.injectionsUsageMap.get(i.name)
68+
if (!record) {
69+
metadata.injectionsUsageMap.set(i.name, record = { import: i, count: 0, moduleIds: [] })
70+
}
71+
record.count++
72+
if (id && !record.moduleIds.includes(id))
73+
record.moduleIds.push(id)
7174
})
7275
}
7376

@@ -119,7 +122,13 @@ function createInternalContext(opts: Partial<UnimportOptions>) {
119122

120123
if (opts.collectMeta) {
121124
metadata = {
122-
injectionUsage: {},
125+
injectionsUsageMap: new Map(),
126+
get injectionUsage() {
127+
// eslint-disable-next-line unicorn/error-message
128+
const stack = new Error().stack
129+
console.warn('[unimport] `injectionUsage` is deprecated, use `injectionsUsageMap` instead', stack)
130+
return Object.fromEntries(this.injectionsUsageMap)
131+
},
123132
}
124133
}
125134

src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ export interface InjectionUsageRecord {
153153
}
154154

155155
export interface UnimportMeta {
156-
injectionUsage: Record<string, InjectionUsageRecord>
156+
injectionsUsageMap: Map<string, InjectionUsageRecord>
157+
/**
158+
* @deprecated use `injectionsUsageMap` instead
159+
*/
160+
get injectionUsage(): Record<string, InjectionUsageRecord>
157161
}
158162

159163
export interface AddonsOptions {

test/inject.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('inject import', () => {
2929
{ name: 'import3', from: 'specifier3' },
3030
{ name: 'import4', from: 'specifier4' },
3131
{ name: 'foo', as: 'import5', from: 'specifier5' },
32+
{ name: 'toString', from: 'specifier6' },
3233
{ name: 'import10', from: 'specifier10' },
3334
],
3435
collectMeta: true,
@@ -37,6 +38,7 @@ describe('inject import', () => {
3738
await ctx.injectImports('console.log(import1())', 'foo')
3839
await ctx.injectImports('console.log(import2())', 'bar')
3940
await ctx.injectImports('console.log(import1())', 'gar')
41+
await ctx.injectImports('console.log(toString())')
4042

4143
expect(ctx.getMetadata()).toMatchInlineSnapshot(`
4244
{
@@ -64,6 +66,49 @@ describe('inject import', () => {
6466
"bar",
6567
],
6668
},
69+
"toString": {
70+
"count": 1,
71+
"import": {
72+
"as": "toString",
73+
"from": "specifier6",
74+
"name": "toString",
75+
},
76+
"moduleIds": [],
77+
},
78+
},
79+
"injectionsUsageMap": Map {
80+
"import1" => {
81+
"count": 3,
82+
"import": {
83+
"as": "import1",
84+
"from": "specifier1",
85+
"name": "import1",
86+
},
87+
"moduleIds": [
88+
"foo",
89+
"gar",
90+
],
91+
},
92+
"import2" => {
93+
"count": 1,
94+
"import": {
95+
"as": "import2",
96+
"from": "specifier2",
97+
"name": "import2",
98+
},
99+
"moduleIds": [
100+
"bar",
101+
],
102+
},
103+
"toString" => {
104+
"count": 1,
105+
"import": {
106+
"as": "toString",
107+
"from": "specifier6",
108+
"name": "toString",
109+
},
110+
"moduleIds": [],
111+
},
67112
},
68113
}
69114
`)

0 commit comments

Comments
 (0)