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 support for <script setup lang=ts generic="..."> #182

Merged
merged 9 commits into from May 14, 2023
Merged

Conversation

ota-meshi
Copy link
Member

@ota-meshi ota-meshi commented Mar 30, 2023

This PR adds support for generic attributes.

The value of the generic attribute is parsed as a new node VGenericExpression.

export interface VGenericExpression extends HasLocation, HasParent {
    type: "VGenericExpression"
    parent: VExpressionContainer
    params: TSESTree.TSTypeParameterDeclaration["params"]
    rawParams: string[]
}

This change adds a new trick for resolving typescript type information.

When the generic attribute is specified, before passing the code to the script parser, we add virtual code to parse the type information specified by the generic attribute, and then remove it from the AST and scope manager.

e.g.


input:

<script setup lang="ts" generic="T">
const props = defineProps<{ foo: T }>();
</script>

virtual code:

type T = unknown;
const props = defineProps<{ foo: T }>();

input:

<script setup lang="ts" generic="T extends string">
const props = defineProps<{ foo: T }>();
</script>

virtual code:

type T = string;
const props = defineProps<{ foo: T }>();

input:

<script setup lang="ts" generic="T extends Foo, U extends T">
type Foo = number | string;
const props = defineProps<{ foo: T, bar: U }>();
</script>

virtual code:

type T = Foo;
type U = T;
type Foo = number | string;
const props = defineProps<{ foo: T, bar: U }>();

@richardtallent-erm
Copy link

Happy to see this is in progress! I upgraded to 3.3 this morning and tried to use generics and started immediately hitting no-def errors.

@Noxdor
Copy link

Noxdor commented May 12, 2023

Thank you for your effort, can't wait to have this working and update to 3.3!

@ota-meshi ota-meshi merged commit 195d46d into master May 14, 2023
11 checks passed
@ota-meshi ota-meshi deleted the generic branch May 14, 2023 01:04
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.

None yet

3 participants