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

Add twoslash support #47

Merged
merged 35 commits into from
Apr 19, 2022
Merged

Add twoslash support #47

merged 35 commits into from
Apr 19, 2022

Conversation

danvk
Copy link
Collaborator

@danvk danvk commented Apr 8, 2022

PR Checklist

Overview

Adds support for the following syntax:

const square = (x: number) => x * x;
const four = square(2);
//    ^? const four: number

Multiline types look like:

const vector = {
  x: 3,
  y: 4,
};
vector
// ^? const vector: {
//        x: number;
//        y: number;
//    }

A comment line is a continuation of the expected type if it starts with the exact same sequence of spaces and // as the ^? line (with ^? replaced with more spaces).

This also includes a fixer. I've only enabled it for twoslash-style assertions, but enabling it for $ExpectType would be trivial. This might really improve the dev experience for DefinitelyTyped contributors!

Here are some GIFs of it in action.

Single line fix:
expect-type-fix

More complex fix that involves multiple files / a multiline type:
expect-type-multiline-fix

@@ -458,14 +476,36 @@ function getExpectTypeFailures(
const line = lineOfPosition(node.getStart(sourceFile), sourceFile);
const assertion = typeAssertions.get(line);
if (assertion !== undefined) {
const { expected } = assertion;
const { expected, column } = assertion;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have a line and a column. I want to get the node at that position. Surely there's an easier way than this?!

tests/rules/expect-type.test.ts Outdated Show resolved Hide resolved
@danvk danvk marked this pull request as ready for review April 10, 2022 17:43
@danvk danvk changed the title Add twoslash support (WIP) Add twoslash support Apr 10, 2022
@danvk
Copy link
Collaborator Author

danvk commented Apr 10, 2022

This should be good to go! I imagine @JoshuaKGoldberg is busy putting the final touches on his book, so no rush on reviewing. But I would love to start using this in my project: https://github.com/danvk/crudely-typed/blob/main/test/type/select.typetest.ts

@JoshuaKGoldberg
Copy link
Owner

Haha you're right, I am a bit swamped this week (sorry!). But I hope to review as soon as I can!

@JoshuaKGoldberg
Copy link
Owner

We're scheduled to go over this on Twitch tomorrow 😎 https://www.twitch.tv/joshuakgoldberg/schedule?seriesID=c53bbfdd-92bd-487b-bb14-1ffc5965c191

Copy link
Owner

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Looks great, thanks so much for sending this! ✨

Nothing too major, mostly just requesting changes we talked about on the stream.

src/rules/expect.ts Outdated Show resolved Hide resolved
src/rules/expect.ts Outdated Show resolved Hide resolved
src/rules/expect.ts Outdated Show resolved Hide resolved
src/rules/expect.ts Outdated Show resolved Hide resolved
src/rules/expect.ts Outdated Show resolved Hide resolved
src/rules/expect.ts Outdated Show resolved Hide resolved
tests/rules/expect-type.test.ts Outdated Show resolved Hide resolved
tests/rules/twoslash.test.ts Show resolved Hide resolved
tests/rules/twoslash.test.ts Outdated Show resolved Hide resolved
src/rules/expect.ts Outdated Show resolved Hide resolved
Copy link
Collaborator Author

@danvk danvk left a comment

Choose a reason for hiding this comment

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

Thanks for the review, Josh! I've addressed all feedback. I think we should be good to go!

src/rules/expect.ts Outdated Show resolved Hide resolved
src/rules/expect.ts Show resolved Hide resolved
src/rules/expect.ts Outdated Show resolved Hide resolved
src/rules/expect.ts Outdated Show resolved Hide resolved
src/rules/expect.ts Outdated Show resolved Hide resolved
src/rules/expect.ts Outdated Show resolved Hide resolved
tests/rules/expect-type.test.ts Outdated Show resolved Hide resolved
tests/rules/twoslash.test.ts Outdated Show resolved Hide resolved
src/rules/expect.ts Outdated Show resolved Hide resolved
src/rules/expect.ts Outdated Show resolved Hide resolved
Copy link
Owner

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

This is wonderful, thanks @danvk! ✨

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.

Support twoslash syntax (^?)
3 participants