Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crash in getWellKnownSymbolPropertyOfType for mapped typeof symbol #698

Merged
merged 3 commits into from
Feb 1, 2025

Conversation

JoshuaKGoldberg
Copy link
Owner

PR Checklist

Overview

As seen in typescript-eslint/typescript-eslint#10747, we can't assume that prop's declarations array can be found. If the type being checked is a mapped type over the requested well-known symbol, it'll come up as undefined.

💖

Copy link

codecov bot commented Jan 31, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.20%. Comparing base (07a2d18) to head (b64081a).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #698      +/-   ##
==========================================
+ Coverage   76.98%   77.20%   +0.21%     
==========================================
  Files          50       50              
  Lines        4979     5000      +21     
  Branches      680      687       +7     
==========================================
+ Hits         3833     3860      +27     
+ Misses       1145     1139       -6     
  Partials        1        1              
Flag Coverage Δ
4.8.4 74.56% <100.00%> (+0.22%) ⬆️
latest 76.78% <100.00%> (+0.21%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@ronami ronami left a comment

Choose a reason for hiding this comment

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

👍👍👍

@@ -109,7 +109,7 @@ describe("getWellKnownSymbolPropertyOfType", () => {
const { sourceFile, typeChecker } = createSourceFileAndTypeChecker(`
declare const x: {
[Symbol.asyncIterator](): AsyncIterator<any>;
}>
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wdyt about adding the following (or similar) to createSourceFileAndTypeChecker() so issues like this would be easier to notice (probably on a separate PR, I'll be happy to send one):

export function createSourceFileAndTypeChecker(
	sourceText: string,
	fileName = "file.tsx",
): SourceFileAndTypeChecker {
	const compilerOptions: ts.CompilerOptions = {
		lib: ["ES2018"],
		target: ts.ScriptTarget.ES2018,
	};

	const fsMap = tsvfs.createDefaultMapFromNodeModules(compilerOptions, ts);
	fsMap.set(fileName, sourceText);

	const system = tsvfs.createSystem(fsMap);
	const env = tsvfs.createVirtualTypeScriptEnvironment(
		system,
		[fileName],
		ts,
		compilerOptions,
	);

	const program = env.languageService.getProgram();

	if (program === undefined) {
		throw new Error("Failed to get program.");
	}

+ 	const diagnostics = program.getSyntacticDiagnostics();
+ 
+ 	if (diagnostics.length > 0) {
+ 		throw new Error(
+ 			ts.flattenDiagnosticMessageText(
+ 				diagnostics[0].messageText,
+ 				ts.sys.newLine,
+ 			),
+ 		);
+ 	}

	return {
		sourceFile: program.getSourceFile(fileName)!,
		typeChecker: program.getTypeChecker(),
	};
}

Copy link
Owner Author

Choose a reason for hiding this comment

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

Good idea, I'd like that!

@JoshuaKGoldberg JoshuaKGoldberg merged commit e97334e into main Feb 1, 2025
19 of 21 checks passed
@JoshuaKGoldberg JoshuaKGoldberg deleted the well-known-symbol-mapped-typeof branch February 1, 2025 13:36
JoshuaKGoldberg pushed a commit that referenced this pull request Feb 5, 2025
…as valid syntax (#703)

<!-- 👋 Hi, thanks for sending a PR to ts-api-utils! 💖.
Please fill out all fields below and make sure each item is true and [x]
checked.
Otherwise we may not be able to review your PR. -->

## PR Checklist

- [ ] Addresses an existing open issue: fixes #000
- [ ] That issue was marked as [`status: accepting
prs`](https://github.com/JoshuaKGoldberg/ts-api-utils/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22)
- [x] Steps in
[CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/ts-api-utils/blob/main/.github/CONTRIBUTING.md)
were taken

## Overview

<!-- Description of what is changed and how the code change does that.
-->

This is a small follow-up to
#698 (comment),
and it adjusts the test helper `createSourceFileAndTypeChecker` to check
that passed syntax is valid (to prevent accidental invalid syntax on
test cases).

I've checked locally that this would fail before the change on the
linked comment.

---

Codecov fails on this, but I don't think there's much to do (it's a test
helper, should it even be included in the codecov report?).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants