Skip to content

Commit

Permalink
feat: update attr tag type generation to align with marko core
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Oct 17, 2024
1 parent 7da97ae commit aff5f68
Show file tree
Hide file tree
Showing 19 changed files with 229 additions and 276 deletions.
8 changes: 8 additions & 0 deletions .changeset/tall-cows-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@marko/language-server": minor
"@marko/language-tools": minor
"@marko/type-check": minor
"marko-vscode": minor
---

Align attribute tag type generation with new types from Marko core.
327 changes: 145 additions & 182 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"devDependencies": {
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.9",
"@types/mocha": "^10.0.8",
"@types/node": "^22.7.4",
"@types/mocha": "^10.0.9",
"@types/node": "^22.7.6",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"esbuild": "^0.24.0",
Expand All @@ -19,11 +19,11 @@
"mocha-snap": "^5.0.0",
"prettier": "^3.3.3",
"tsx": "^4.19.1",
"typescript": "^5.6.2",
"typescript": "^5.6.3",
"@types/jsdom": "^21.1.7"
},
"dependencies": {
"axe-core": "^4.10.0",
"axe-core": "^4.10.1",
"jsdom": "^25.0.1"
},
"private": true,
Expand Down
8 changes: 4 additions & 4 deletions packages/language-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"@luxass/strip-json-comments": "^1.3.2",
"@marko/language-tools": "^2.3.1",
"@marko/babel-utils": "^6.5.7",
"@marko/compiler": "^5.37.15",
"@marko/translator-default": "^6.0.16",
"@marko/compiler": "^5.37.19",
"@marko/translator-default": "^6.0.19",
"htmljs-parser": "^5.5.2",
"marko": "^5.35.20",
"marko": "^5.35.27",
"prettier": "^3.3.3",
"prettier-plugin-marko": "^3.1.4",
"relative-import-path": "^1.0.0",
"typescript": "^5.6.2",
"typescript": "^5.6.3",
"vscode-css-languageservice": "^6.3.1",
"vscode-languageserver": "^9.0.1",
"vscode-languageserver-textdocument": "^1.0.12",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
aria-label attribute does not exist or is empty
aria-labelledby attribute does not exist, references elements that do not exist or references elements that are empty
Element has no title attribute
Element does not have an implicit (wrapped) <label>
Element does not have an explicit <label>
13 |
14 | <object id="object"></object>
15 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
aria-label attribute does not exist or is empty
aria-labelledby attribute does not exist, references elements that do not exist or references elements that are empty
Element has no title attribute
Element does not have an implicit (wrapped) <label>
Element does not have an explicit <label>
Element's default semantics were not overridden with role="none" or role="presentation"
```

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
aria-label attribute does not exist or is empty
aria-labelledby attribute does not exist, references elements that do not exist or references elements that are empty
Element has no title attribute
Element does not have an implicit (wrapped) <label>
Element does not have an explicit <label>
Element's role is not presentational because it is focusable
4 |
5 | <li role="presentation" id="tabindex" tabindex="0"></li>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
## Diagnostics
### Ln 10, Col 4
### Ln 6, Col 4
```marko
8 | </>
9 |
> 10 | <@item>
| ^^^^^ Type '{ renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; }' is not assignable to type 'AttrTag<{ x: number; renderBody?: Body<[], void> | undefined; }>'.
Property 'x' is missing in type '{ renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; }' but required in type '{ x: number; renderBody?: Body<[], void> | undefined; }'.
11 | Hello!
12 | </>
13 | </test-tag>
4 |
5 | <test-tag>
> 6 | <@item x=1>
| ^^^^^ Type '{ x: number; renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; } | { renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; x?: undefined; }' is not assignable to type 'AttrTag<{ x: number; renderBody?: Body<[], void> | undefined; }>'.
Type '{ renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; x?: undefined; }' is not assignable to type 'AttrTag<{ x: number; renderBody?: Body<[], void> | undefined; }>'.
Type '{ renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; x?: undefined; }' is not assignable to type '{ x: number; renderBody?: Body<[], void> | undefined; }'.
Types of property 'x' are incompatible.
Type 'undefined' is not assignable to type 'number'.
7 | Hello!
8 | </>
9 |
```

Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export interface Input {
items: Marko.AttrTag<{
x: number;
renderBody?: Marko.Body;
}>[]
}>
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface Input {
item?: Marko.RepeatableAttrTag<{
item?: Marko.AttrTag<{
size?: "small" | "large"
}>
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
## Hovers
### Ln 3, Col 7
```marko
1 | <comments>
2 | <@comment#a>
> 3 | <@comment#b>
| ^ (property) comment?: Marko.RepeatableAttrTag<Comment> | undefined
4 | // ^?
5 | <let/b = "b" as const/>
6 | </@comment>
```

### Ln 15, Col 3
```marko
13 |
Expand Down Expand Up @@ -43,15 +32,15 @@
```

## Diagnostics
### Ln 9, Col 4
### Ln 2, Col 4
```marko
7 | <let/a = "a" as const/>
8 | </@comment>
> 9 | <@comment>
| ^^^^^^^^ Type '{ renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; }' is not assignable to type 'AttrTag<Comment>'.
Property 'id' is missing in type '{ renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; }' but required in type 'Comment'.
10 | <let/c = "c" as const/>
11 | </@comment>
12 | </comments>
1 | <comments>
> 2 | <@comment#a>
| ^^^^^^^^ Type '{ comment: { id: string; renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; }; renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; id: string; } | { renderBody: () => MarkoReturn<...>; [Symbol.iterator]: any; }' is not assignable to type 'AttrTag<Comment>'.
Type '{ renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; }' is not assignable to type 'AttrTag<Comment>'.
Property 'id' is missing in type '{ renderBody: () => MarkoReturn<void>; [Symbol.iterator]: any; }' but required in type 'Comment'.
3 | <@comment#b>
4 | // ^?
5 | <let/b = "b" as const/>
```

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
static interface Comment {
id: string;
renderBody: Marko.Body;
comment?: Marko.RepeatableAttrTag<Comment>;
comment?: Marko.AttrTag<Comment>;
}
export interface Input {
comment: Marko.RepeatableAttrTag<Comment>;
comment: Marko.AttrTag<Comment>;
}

// Empty
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
aria-label attribute does not exist or is empty
aria-labelledby attribute does not exist, references elements that do not exist or references elements that are empty
Element has no title attribute
Element does not have an implicit (wrapped) <label>
Element does not have an explicit <label>
Element's default semantics were not overridden with role="none" or role="presentation"
3 | value++;
4 | }/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
aria-label attribute does not exist or is empty
aria-labelledby attribute does not exist, references elements that do not exist or references elements that are empty
Element has no title attribute
Element does not have an implicit (wrapped) <label>
Element does not have an explicit <label>
Element's default semantics were not overridden with role="none" or role="presentation"
5 | x = 2;
6 | x++;
Expand Down
62 changes: 21 additions & 41 deletions packages/language-tools/marko.internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ declare global {
override: Override,
): [0] extends [1 & Override] ? Marko.Global : Override;

export function attrTagNames<Input, Keys extends keyof Input>(
export function attrTagNames<Input, Keys extends keyof Input & string>(
input: Input,
): Record<string, never> & {
[Key in Keys as `@${Input[Key] extends infer Value
? Value extends { renderBody?: any }
? Value extends Marko.AttrTag<any>
? Key
: never
: never}`]: Input[Key];
Expand Down Expand Up @@ -261,12 +261,12 @@ declare global {
| readonly (infer Item)[]
| (infer Item extends Record<PropertyKey, any>)
? number extends From | To | Step
? MaybeRepeatable<Item>
? undefined | Marko.AttrTag<Item>
: Step extends 0
? never
: [To] extends [From extends void ? 0 : From]
? undefined
: Repeatable<Item>
: Marko.AttrTag<Item>
: never;
};

Expand All @@ -288,14 +288,18 @@ declare global {
[Key in keyof Return]: Return[Key] extends
| readonly (infer Item)[]
| (infer Item extends Record<PropertyKey, any>)
? MaybeRepeatable<Item>
? undefined | Marko.AttrTag<Item>
: never;
};

export function mergeAttrTags<Attrs extends readonly any[]>(
...attrs: Attrs
): MergeAttrTags<Attrs>;

export function repeatedAttrTag<AttrTag>(
...attrTags: Marko.AttrTag<AttrTag>[]
): AttrTag;

// TODO: this could be improved.
// currently falls back to DefaultRenderer too eagerly.
export type DynamicRenderer<Name> = [0] extends [1 & Name]
Expand Down Expand Up @@ -461,30 +465,25 @@ type MergeAttrTag<A, B> = {
: never;
};

type MergeAttrTagValue<A, B> = A extends readonly any[]
? B extends readonly any[]
? [...A, ...B]
type MergeAttrTagValue<A, B> = A extends readonly (infer AType)[]
? B extends readonly (infer BType)[]
? AType | BType
: B extends void
? A
: [...A, B]
: AType | B
: A extends void
? B
: B extends void
? A
: [A, B];
: A | B;

type AttrTagByListSize<T, Item> = T extends
| readonly [any, any, ...any[]]
| readonly [...any[], any, any]
| readonly [any, ...any[], any]
? Repeated<Item>
: T extends readonly [any]
? Item
: T extends readonly [any, ...any[]] | readonly [...any[], any]
? Repeatable<Item>
: T extends readonly []
? undefined
: MaybeRepeatable<Item>;
| readonly [any, ...any[]]
| readonly [...any[], any]
? Marko.AttrTag<Item>
: T extends readonly []
? undefined
: undefined | Marko.AttrTag<Item>;

type AttrTagByObjectSize<
Value,
Expand All @@ -494,22 +493,7 @@ type AttrTagByObjectSize<
> = CheckNever<
Keys,
undefined,
CheckUnionSize<
KnownKeys,
[Keys] extends [KnownKeys] ? Item : Repeatable<Item>,
Repeated<Item>,
MaybeRepeatable<Item>
>
>;

type CheckUnionSize<T, IfOne, IfMany, Else, Copy = T> = CheckNever<
T,
Else,
T extends T
? (Copy extends T ? never : true) extends never
? IfOne
: IfMany
: never
CheckNever<KnownKeys, Marko.AttrTag<Item>, undefined | Marko.AttrTag<Item>>
>;

type RecordKeys<T> = keyof {
Expand All @@ -534,8 +518,4 @@ type UnionToIntersection<T> = (T extends any ? (_: T) => any : never) extends (
? U
: never;

type Repeated<T> = [T, T, ...T[]];
type Repeatable<T> = T | Repeated<T>;
type MaybeRepeatable<T> = undefined | Repeatable<T>;

export {};
8 changes: 4 additions & 4 deletions packages/language-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
"@marko/compiler": "^5.28.4"
},
"dependencies": {
"@babel/parser": "^7.25.7",
"@babel/parser": "^7.25.8",
"@luxass/strip-json-comments": "^1.3.2",
"htmljs-parser": "^5.5.2",
"relative-import-path": "^1.0.0"
},
"devDependencies": {
"@babel/code-frame": "^7.25.7",
"@marko/compiler": "^5.37.15",
"@marko/translator-default": "^6.0.16",
"@marko/compiler": "^5.37.19",
"@marko/translator-default": "^6.0.19",
"@types/babel__code-frame": "^7.0.6",
"@typescript/vfs": "^1.6.0",
"marko": "^5.35.20",
"marko": "^5.35.27",
"mitata": "^0.1.8",
"tsx": "^4.19.1"
},
Expand Down
12 changes: 6 additions & 6 deletions packages/language-tools/src/extractors/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ constructor(_?: Return) {}
for (const nameText in staticAttrTags) {
const attrTag = staticAttrTags[nameText];
const attrTagDef = this.#lookup.getTag(nameText);
const isRepeated = attrTag.length > 1 ? true : attrTagDef?.isRepeated;
const isRepeated = attrTag.length > 1;
const [firstAttrTag] = attrTag;
const name =
attrTagDef?.targetProperty ||
Expand All @@ -1090,7 +1090,7 @@ constructor(_?: Return) {}
this.#extractor.write("]: ");

if (isRepeated) {
this.#extractor.write("[\n");
this.#extractor.write(`${varShared("repeatedAttrTag")}(\n`);
}

for (const childNode of attrTag) {
Expand All @@ -1099,7 +1099,7 @@ constructor(_?: Return) {}
}

if (isRepeated) {
this.#extractor.write(`]${SEP_COMMA_NEW_LINE}`);
this.#extractor.write(`)${SEP_COMMA_NEW_LINE}`);
}
}
}
Expand Down Expand Up @@ -1169,14 +1169,14 @@ constructor(_?: Return) {}
case "while": {
this.#writeComments(tag);
this.#extractor
.write(`${varShared("mergeAttrTags")}((\n`)
.write("((\n")
.copy(
this.#getRangeWithoutTrailingComma(tag.args?.value) ||
"undefined",
)
.write("\n) ? [");
.write("\n) ? ");
this.#writeDynamicAttrTagBody(tag);
this.#extractor.write("] : [])");
this.#extractor.write(" : {})");
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/type-check/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"@marko/language-tools": "^2.3.1",
"arg": "^5.0.2",
"kleur": "^4.1.5",
"typescript": "^5.6.2"
"typescript": "^5.6.3"
},
"devDependencies": {
"@types/babel__code-frame": "^7.0.6"
Expand Down
Loading

0 comments on commit aff5f68

Please sign in to comment.