Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/khaki-spoons-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@typescript/ata": patch
---

Remove duplicate modules from getReferencesForModule
26 changes: 14 additions & 12 deletions packages/ata/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,21 @@ export const getReferencesForModule = (ts: typeof import("typescript"), code: st
.filter(f => !isDtsFile(f.fileName))
.filter(d => !libMap.has(d.fileName))

return references.map(r => {
let version = undefined
if (!r.fileName.startsWith(".")) {
version = "latest"
const line = code.slice(r.end).split("\n")[0]!
if (line.includes("// types:")) version = line.split("// types: ")[1]!.trim()
}
return references
.map(r => {
let version = undefined
if (!r.fileName.startsWith(".")) {
version = "latest"
const line = code.slice(r.end).split("\n")[0]!
if (line.includes("// types:")) version = line.split("// types: ")[1]!.trim()
}

return {
module: r.fileName,
version,
}
})
return {
module: r.fileName,
version,
}
})
.filter((r, index, self) => self.findIndex(m => m.module === r.module && m.version === r.version) === index)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quadradic; how big are these inputs typically?

Copy link
Contributor Author

@ChaseMalik ChaseMalik May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a scientific answer, but I would think most files don't import more than on the order of 10s of packages - but in the case of @modelcontextprotocol it is >1000. (I also don't think one would ever specify a different module versions for the same module? but just did the safest option for now, but happy to update) - I can also switch to a Set if preferred.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, you had said there were 1000s of calls; does that occur without 1000s of entries in this list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I updated my response (probably while you were responding, sorry about that), but I would not expect more than 10s of unique packages. So the array would not actually loop through 1000s of entries, but yes it can have 1000s.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is just in a single file. Gotcha.

}

/** A list of modules from the current sourcefile which we don't have existing files for */
Expand Down
5 changes: 5 additions & 0 deletions packages/ata/tests/ata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ describe(getReferencesForModule, () => {
const code = "import {asda} from '123' // types: 1.2.3"
expect(getReferencesForModule(ts, code)[0]).toEqual({ module: "123", version: "1.2.3" })
})

it("removes duplicate imports", () => {
const code = "import 'abc'; import {asda} from 'abc'"
expect(getReferencesForModule(ts, code).map(m => m.module)).toEqual(["abc"])
})
})

describe("ignores lib references", () => {
Expand Down