Skip to content

Commit

Permalink
Update to TS 5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Nov 24, 2023
1 parent 8c86774 commit 5fb6b2a
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 44 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"devDependencies": {
"@changesets/cli": "^2.26.1",
"prettier": "^3.0.3",
"typescript": "^5.2.2"
"typescript": "5.3.2"
},
"engines": {
"node": ">=18",
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"tsc": "tsc -b",
"local:install": "npm install -g .",
"local:uninstall": "npm uninstall -g @arethetypeswrong/cli",
"test": "tsc -b test && node --test test/dist",
"test": "tsc -b test && node --test 'test/dist/**/*.test.js'",
"prepack": "pnpm tsc"
},
"type": "module",
Expand All @@ -42,8 +42,8 @@
"@types/marked-terminal": "^3.1.3",
"@types/node": "^20.2.5",
"@types/semver": "^7.5.3",
"@types/ts-expose-internals": "npm:ts-expose-internals@5.2.2",
"typescript": "^5.2.2"
"ts-expose-internals": "5.3.2",
"typescript": "5.3.2"
},
"dependencies": {
"@arethetypeswrong/core": "0.13.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/snapshots/Babel@0.0.1.tgz.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
$ attw Babel@0.0.1.tgz -f table-flipped
error while checking file:
Expected double-quoted property name in JSON at position 450
Expected double-quoted property name in JSON at position 450 (line 17 column 1)
```
Expand Down
8 changes: 4 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"scripts": {
"tsc": "tsc",
"test": "tsc -b test && node --test test/dist",
"test": "tsc -b test && node --test 'test/dist/**/*.test.js'",
"snapshot": "node scripts/createSnapshotFixture.js",
"prepack": "pnpm tsc"
},
Expand Down Expand Up @@ -50,14 +50,14 @@
"@andrewbranch/untar.js": "^1.0.3",
"fflate": "^0.7.4",
"semver": "^7.5.4",
"typescript": "^5.2.2",
"typescript": "5.3.2",
"validate-npm-package-name": "^5.0.0"
},
"devDependencies": {
"@types/node": "^20.8.6",
"@types/semver": "^7.5.0",
"@types/ts-expose-internals": "npm:ts-expose-internals@5.2.2",
"@types/validate-npm-package-name": "^4.0.0"
"@types/validate-npm-package-name": "^4.0.0",
"ts-expose-internals": "5.3.2"
},
"engines": {
"node": ">=18"
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/checkPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,17 @@ function getEntrypointResolution(
}
const moduleSpecifier = packageName + entrypoint.substring(1); // remove leading . before slash
const importingFileName = resolutionKind === "node16-esm" ? "/index.mts" : "/index.ts";
const resolutionMode = resolutionKind === "node16-esm" ? ts.ModuleKind.ESNext : ts.ModuleKind.CommonJS;
const resolutionMode =
resolutionKind === "node16-esm"
? ts.ModuleKind.ESNext
: resolutionKind === "node16-cjs"
? ts.ModuleKind.CommonJS
: undefined;
const resolution = tryResolve();
const implementationResolution = tryResolve(/*noDtsResolution*/ true);
const files = resolution
? host
.createProgram([resolution.fileName])
.createPrimaryProgram(resolution.fileName)
.getSourceFiles()
.map((f) => f.fileName)
: undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default defineCheck({
implExports.has(ts.InternalSymbolName.ExportEquals)
) {
if (!implExports.has(ts.InternalSymbolName.Default)) {
const checker = host.createProgram([implementationFileName], bindOptions).getTypeChecker();
const checker = host.createAuxiliaryProgram([implementationFileName], bindOptions).getTypeChecker();
if (
!checker.getExportsAndPropertiesOfModule(implementationSourceFile.symbol).some((s) => s.name === "default")
) {
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/internal/checks/internalResolutionError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export default defineCheck({
enumerateFiles: true,
dependencies: ({ resolutionOption, fileName }) => [resolutionOption, fileName],
execute: ([resolutionOption, fileName], context) => {
if (!ts.hasTSFileExtension(fileName)) {
return;
}
const host = context.hosts[resolutionOption];
const sourceFile = host.getSourceFile(fileName);
if (sourceFile?.imports) {
Expand All @@ -25,9 +28,12 @@ export default defineCheck({
continue;
}
const resolutionMode = ts.getModeForUsageLocation(sourceFile, moduleSpecifier);
const resolution = ts.getResolvedModule(sourceFile, moduleSpecifier.text, resolutionMode);

const resolution = host.getResolvedModule(sourceFile, moduleSpecifier.text, resolutionMode);
if (!resolution) {
throw new Error(`Expected resolution for '${moduleSpecifier.text}' in ${fileName}`);
}

if (!resolution.resolvedModule) {
problems.push({
kind: "InternalResolutionError",
resolutionOption,
Expand Down
27 changes: 26 additions & 1 deletion packages/core/src/multiCompilerHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class CompilerHostWrapper {
> = {};
private traceCollector: TraceCollector = new TraceCollector();
private sourceFileCache: Map<ts.Path, ts.SourceFile> = new Map();
private resolvedModules: Exclude<ts.Program["resolvedModules"], undefined> = new Map();
private languageVersion = ts.ScriptTarget.Latest;

constructor(fs: Package, moduleResolution: ts.ModuleResolutionKind, moduleKind: ts.ModuleKind) {
Expand Down Expand Up @@ -157,7 +158,27 @@ export class CompilerHostWrapper {
return `${resolutionMode ?? 1}:${+!!noDtsResolution}:${+!!allowJs}:${moduleSpecifier}`;
}

createProgram(rootNames: string[], extraOptions?: ts.CompilerOptions): ts.Program {
createPrimaryProgram(rootName: string) {
const program = ts.createProgram({
rootNames: [rootName],
options: this.compilerOptions,
host: this.compilerHost,
});

program.resolvedModules?.forEach((cache, path) => {
let ownCache = this.resolvedModules.get(path);
if (!ownCache) {
this.resolvedModules.set(path, (ownCache = ts.createModeAwareCache()));
}
cache.forEach((resolution, key, mode) => {
ownCache!.set(key, mode, resolution);
});
});

return program;
}

createAuxiliaryProgram(rootNames: string[], extraOptions?: ts.CompilerOptions): ts.Program {
if (
extraOptions &&
ts.changesAffectModuleResolution(
Expand All @@ -181,6 +202,10 @@ export class CompilerHostWrapper {
});
}

getResolvedModule(sourceFile: ts.SourceFile, moduleName: string, resolutionMode: ts.ResolutionMode) {
return this.resolvedModules.get(sourceFile.path)?.get(moduleName, resolutionMode);
}

private createCompilerHost(fs: Package, sourceFileCache: Map<ts.Path, ts.SourceFile>): ts.CompilerHost {
return {
fileExists: fs.fileExists.bind(fs),
Expand Down
8 changes: 4 additions & 4 deletions packages/core/test/snapshots/moment@2.29.1.tgz.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"File '/node_modules/moment.d.ts' does not exist.",
"'package.json' has a 'typesVersions' field with version-specific path mappings.",
"'package.json' has 'typings' field './moment.d.ts' that references '/node_modules/moment/moment.d.ts'.",
"'package.json' has a 'typesVersions' entry '>=3.1' that matches compiler version '5.2.2', looking for a pattern to match module name 'moment.d.ts'.",
"'package.json' has a 'typesVersions' entry '>=3.1' that matches compiler version '5.3.2', looking for a pattern to match module name 'moment.d.ts'.",
"Module name 'moment.d.ts', matched pattern '*'.",
"Trying substitution 'ts3.1-typings/*', candidate module location: 'ts3.1-typings/moment.d.ts'.",
"File '/node_modules/moment/ts3.1-typings/moment.d.ts' exists - use it as a name resolution result.",
Expand Down Expand Up @@ -63,7 +63,7 @@
"File '/node_modules/moment.d.ts' does not exist.",
"'package.json' has a 'typesVersions' field with version-specific path mappings.",
"'package.json' has 'typings' field './moment.d.ts' that references '/node_modules/moment/moment.d.ts'.",
"'package.json' has a 'typesVersions' entry '>=3.1' that matches compiler version '5.2.2', looking for a pattern to match module name 'moment.d.ts'.",
"'package.json' has a 'typesVersions' entry '>=3.1' that matches compiler version '5.3.2', looking for a pattern to match module name 'moment.d.ts'.",
"Module name 'moment.d.ts', matched pattern '*'.",
"Trying substitution 'ts3.1-typings/*', candidate module location: 'ts3.1-typings/moment.d.ts'.",
"File '/node_modules/moment/ts3.1-typings/moment.d.ts' exists - use it as a name resolution result.",
Expand Down Expand Up @@ -92,7 +92,7 @@
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
"File '/node_modules/moment/package.json' exists according to earlier cached lookups.",
"'package.json' has 'typings' field './moment.d.ts' that references '/node_modules/moment/moment.d.ts'.",
"'package.json' has a 'typesVersions' entry '>=3.1' that matches compiler version '5.2.2', looking for a pattern to match module name 'moment.d.ts'.",
"'package.json' has a 'typesVersions' entry '>=3.1' that matches compiler version '5.3.2', looking for a pattern to match module name 'moment.d.ts'.",
"Module name 'moment.d.ts', matched pattern '*'.",
"Trying substitution 'ts3.1-typings/*', candidate module location: 'ts3.1-typings/moment.d.ts'.",
"File '/node_modules/moment/ts3.1-typings/moment.d.ts' exists - use it as a name resolution result.",
Expand Down Expand Up @@ -125,7 +125,7 @@
"File '/node_modules/moment.d.ts' does not exist.",
"'package.json' has a 'typesVersions' field with version-specific path mappings.",
"'package.json' has 'typings' field './moment.d.ts' that references '/node_modules/moment/moment.d.ts'.",
"'package.json' has a 'typesVersions' entry '>=3.1' that matches compiler version '5.2.2', looking for a pattern to match module name 'moment.d.ts'.",
"'package.json' has a 'typesVersions' entry '>=3.1' that matches compiler version '5.3.2', looking for a pattern to match module name 'moment.d.ts'.",
"Module name 'moment.d.ts', matched pattern '*'.",
"Trying substitution 'ts3.1-typings/*', candidate module location: 'ts3.1-typings/moment.d.ts'.",
"File '/node_modules/moment/ts3.1-typings/moment.d.ts' exists - use it as a name resolution result.",
Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/snapshots/react@18.2.0.tgz.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"Found 'package.json' at '/node_modules/@types/react/package.json'.",
"File '/node_modules/@types/react.d.ts' does not exist.",
"'package.json' has a 'typesVersions' field with version-specific path mappings.",
"'package.json' does not have a 'typesVersions' entry that matches version '5.2'.",
"'package.json' does not have a 'typesVersions' entry that matches version '5.3'.",
"'package.json' does not have a 'typings' field.",
"'package.json' has 'types' field 'index.d.ts' that references '/node_modules/@types/react/index.d.ts'.",
"File '/node_modules/@types/react/index.d.ts' exists - use it as a name resolution result.",
Expand Down Expand Up @@ -558,7 +558,7 @@
"File name '/node_modules/react/package.json/index.js' has a '.js' extension - stripping it.",
"File '/node_modules/@types/react/package.json' exists according to earlier cached lookups.",
"'package.json' has a 'typesVersions' field with version-specific path mappings.",
"'package.json' does not have a 'typesVersions' entry that matches version '5.2'.",
"'package.json' does not have a 'typesVersions' entry that matches version '5.3'.",
"File name '/node_modules/@types/react/package.json' has a '.json' extension - stripping it.",
"File '/node_modules/@types/react/package.d.json.ts' does not exist.",
"'package.json' does not have a 'typings' field.",
Expand Down Expand Up @@ -653,7 +653,7 @@
"File name '/node_modules/react/package.json/index.js' has a '.js' extension - stripping it.",
"File '/node_modules/@types/react/package.json' exists according to earlier cached lookups.",
"'package.json' has a 'typesVersions' field with version-specific path mappings.",
"'package.json' does not have a 'typesVersions' entry that matches version '5.2'.",
"'package.json' does not have a 'typesVersions' entry that matches version '5.3'.",
"File name '/node_modules/@types/react/package.json' has a '.json' extension - stripping it.",
"File '/node_modules/@types/react/package.d.json.ts' does not exist.",
"File '/node_modules/@types/react/package.json.d.ts' does not exist.",
Expand Down
4 changes: 2 additions & 2 deletions packages/history/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
"npm-high-impact": "^1.3.0",
"pacote": "^15.2.0",
"semver": "^7.5.3",
"ts-expose-internals": "^5.2.2",
"ts-expose-internals": "5.3.2",
"tsx": "^3.12.7",
"types-registry": "latest",
"typescript": "^5.2.2"
"typescript": "5.3.2"
},
"engines": {
"node": ">=18"
Expand Down
40 changes: 20 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5fb6b2a

Please sign in to comment.