Skip to content
Open
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
2 changes: 1 addition & 1 deletion packages/ata/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
"jest": "^29.5.0"
},
"peerDependencies": {
"typescript": ">=4.4.4"
"typescript": "^5.9.3"
Copy link
Member

Choose a reason for hiding this comment

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

why is this changing?

Copy link
Author

Choose a reason for hiding this comment

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

This is changed because it gives type error to new projects using new typescript versions. See setupTypeAcquision() in the added screenshots. Should I revert it?

}
}
136 changes: 135 additions & 1 deletion packages/ata/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,78 @@ export const setupTypeAcquisition = (config: ATABootstrapConfig) => {
})
}

function resolveTypesTarget(entry: any): string | null {
if (typeof entry === "string") {
return entry
}
if (entry && typeof entry === "object") {
if (typeof entry.types === "string") return entry.types
if (typeof entry.typings === "string") return entry.typings
if (entry.import) {
const result = resolveTypesTarget(entry.import)
if (result) return result
}
if (entry.require) {
const result = resolveTypesTarget(entry.require)
if (result) return result
}
if (entry.default) {
const result = resolveTypesTarget(entry.default)
if (result) return result
}
}
return null
}

function stripExtension(path: string): string {
return path.replace(/\.(d\.)?(c|m)?tsx?$/, "")
}

function createSubpathModuleDeclarations(moduleName: string, pkg: any): string | null {
const exports = pkg.exports
if (!exports || typeof exports !== "object") {
return null
}

const subpathDeclarations: string[] = []

for (const [key, value] of Object.entries(exports)) {
if (key === "." || key === "./") continue
if (key.startsWith("./") && !key.includes("*")) {
const subpath = key.slice(2)
if (!subpath) continue

const typesTarget = resolveTypesTarget(value)
if (!typesTarget) continue

let targetModule: string
if (typesTarget.startsWith("./")) {
const cleanedTarget = stripExtension(typesTarget.slice(2))
targetModule = `${moduleName}/${cleanedTarget}`
} else if (typesTarget.startsWith("/")) {
const cleanedTarget = stripExtension(typesTarget.slice(1))
targetModule = `${moduleName}/${cleanedTarget}`
} else {
const cleanedTarget = stripExtension(typesTarget)
targetModule = cleanedTarget.startsWith(".")
? `${moduleName}/${cleanedTarget.slice(2)}`
: cleanedTarget
}

const fullModuleName = `${moduleName}/${subpath}`
subpathDeclarations.push(`declare module '${fullModuleName}' {
export * from '${targetModule}';
}`)
}
}

if (!subpathDeclarations.length) {
return null
}

return subpathDeclarations.join("\n\n")
}

async function resolveDeps(initialSourceFile: string, depth: number) {
const depsToGet = getNewDependencies(config, moduleMap, initialSourceFile)

Expand Down Expand Up @@ -101,6 +173,18 @@ export const setupTypeAcquisition = (config: ATABootstrapConfig) => {
if (typeof pkgJSON == "string") {
fsMap.set(path, pkgJSON)
config.delegate.receivedFile?.(pkgJSON, path)

try {
const pkg = JSON.parse(pkgJSON)
const subpathDeclarations = createSubpathModuleDeclarations(tree.moduleName, pkg)
if (subpathDeclarations) {
const subpathPath = prefix + "/subpaths.d.ts"
fsMap.set(subpathPath, subpathDeclarations)
config.delegate.receivedFile?.(subpathDeclarations, subpathPath)
}
} catch (error) {
config.logger?.log(`Could not parse package.json for ${tree.moduleName}`, error)
}
} else {
config.logger?.error(`Could not download package.json for ${tree.moduleName}`)
}
Expand Down Expand Up @@ -171,7 +255,7 @@ export const getReferencesForModule = (ts: typeof import("typescript"), code: st
.concat(meta.importedFiles)
.concat(meta.libReferenceDirectives)
.filter(f => !isDtsFile(f.fileName))
.filter(d => !libMap.has(d.fileName))
.filter(d => !isStandardLibraryReference(d.fileName, libMap))

return references
.map(r => {
Expand Down Expand Up @@ -274,3 +358,53 @@ function getDTName(s: string) {
function isDtsFile(file: string) {
return /\.d\.([^\.]+\.)?[cm]?ts$/i.test(file)
}

const standardLibMatchers = [
/^es\d{1,4}(\.|$)/,
/^esnext(\.|$)/,
/^dom(\.|$)/,
/^scripthost(\.|$)/,
/^webworker(\.|$)/,
/^webworkers?(\.|$)/,
/^typescript\/lib\//,
/^lib\./,
]

const exactStandardLibs = new Set(
[
"es5",
"es6",
"es7",
"es8",
"es9",
"es2015",
"es2016",
"es2017",
"es2018",
"es2019",
"es2020",
"es2021",
"es2022",
"es2023",
"es2024",
"esnext",
"dom",
"dom.iterable",
"dom.asynciterable",
"scripthost",
"webworker",
"webworker.iterable",
"webworker.importscripts",
"webworker.asynciterable",
"webworker.importscripts",
"dom.asynciterable",
"dom.iterable",
].map(l => l.toLowerCase())
)

function isStandardLibraryReference(fileName: string, libMap: Map<string, string>) {
if (libMap.has(fileName)) return true
const normalized = fileName.toLowerCase()
if (exactStandardLibs.has(normalized)) return true
return standardLibMatchers.some(matcher => matcher.test(normalized))
}
5 changes: 5 additions & 0 deletions packages/ata/tests/ata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ describe(getReferencesForModule, () => {
const code = "import 'abc'; import {asda} from 'abc'"
expect(getReferencesForModule(ts, code).map(m => m.module)).toEqual(["abc"])
})

it("keeps scoped subpath imports", () => {
const code = "import '@example/pkg/subpath'"
expect(getReferencesForModule(ts, code).map(m => m.module)).toEqual(["@example/pkg/subpath"])
})
})

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