-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
There was a problem hiding this 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>; | |||
}> | |||
}; |
There was a problem hiding this comment.
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(),
};
}
There was a problem hiding this comment.
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!
…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?).
PR Checklist
status: accepting prs
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
.💖