Skip to content

Commit

Permalink
feat: add type args and params (#48)
Browse files Browse the repository at this point in the history
* feat: add type args and params

* test: more coverage

* fix: use default prettier formatting
  • Loading branch information
LuLaValva authored Nov 22, 2023
1 parent 266d2eb commit 8f2ea07
Show file tree
Hide file tree
Showing 8 changed files with 1,210 additions and 1,488 deletions.
2,418 changes: 948 additions & 1,470 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"fixpack": "^4.0.0",
"husky": "^8.0.3",
"lint-staged": "^14.0.1",
"marko": "^5.31.11",
"marko": "^5.32.0",
"mocha": "^10.2.0",
"mocha-snap": "^5.0.0",
"nyc": "^15.1.0",
Expand Down
45 changes: 45 additions & 0 deletions src/__tests__/__snapshots__/generic-tag.expected/auto.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<my-tag<MyType, MyOtherType> my-attr="my-string"/>
<my-tag <MyType, MyOtherType>|x|/>
<my-tag <MyType>|x| my-attr="my-string"/>
<my-tag<MyType>|x|/>
<my-tag<MyType1><MyOtherType>|x|/>
<my-tag<
A,
Whole,
Bunch,
Of,
Tag,
Attribute,
Types,
That,
Need,
To,
Wrap,
To,
The,
Next,
Line
><
A,
Whole,
Bunch,
Of,
Tag,
Parameter,
Types,
That,
Need,
To,
Wrap,
To,
The,
Next,
Line,
>|x|/>
<my-tag <
T extends
keyof (typeof SuperLongImportedVariableName)["member1"]["member2"]["member3"],
K extends T extends string
? MyType["member1"]["member2"]["member3"]
: MyOtherType,
>|x|/>
45 changes: 45 additions & 0 deletions src/__tests__/__snapshots__/generic-tag.expected/concise.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
my-tag<MyType, MyOtherType> my-attr="my-string"
my-tag <MyType, MyOtherType>|x|
my-tag <MyType>|x| my-attr="my-string"
my-tag<MyType>|x|
my-tag<MyType1><MyOtherType>|x|
my-tag<
A,
Whole,
Bunch,
Of,
Tag,
Attribute,
Types,
That,
Need,
To,
Wrap,
To,
The,
Next,
Line
><
A,
Whole,
Bunch,
Of,
Tag,
Parameter,
Types,
That,
Need,
To,
Wrap,
To,
The,
Next,
Line,
>|x|
my-tag <
T extends
keyof (typeof SuperLongImportedVariableName)["member1"]["member2"]["member3"],
K extends T extends string
? MyType["member1"]["member2"]["member3"]
: MyOtherType,
>|x|
45 changes: 45 additions & 0 deletions src/__tests__/__snapshots__/generic-tag.expected/html.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<my-tag<MyType, MyOtherType> my-attr="my-string"/>
<my-tag <MyType, MyOtherType>|x|/>
<my-tag <MyType>|x| my-attr="my-string"/>
<my-tag<MyType>|x|/>
<my-tag<MyType1><MyOtherType>|x|/>
<my-tag<
A,
Whole,
Bunch,
Of,
Tag,
Attribute,
Types,
That,
Need,
To,
Wrap,
To,
The,
Next,
Line
><
A,
Whole,
Bunch,
Of,
Tag,
Parameter,
Types,
That,
Need,
To,
Wrap,
To,
The,
Next,
Line,
>|x|/>
<my-tag <
T extends
keyof (typeof SuperLongImportedVariableName)["member1"]["member2"]["member3"],
K extends T extends string
? MyType["member1"]["member2"]["member3"]
: MyOtherType,
>|x|/>
45 changes: 45 additions & 0 deletions src/__tests__/__snapshots__/generic-tag.expected/with-parens.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<my-tag<MyType, MyOtherType> my-attr="my-string"/>
<my-tag <MyType, MyOtherType>|x|/>
<my-tag <MyType>|x| my-attr="my-string"/>
<my-tag<MyType>|x|/>
<my-tag<MyType1><MyOtherType>|x|/>
<my-tag<
A,
Whole,
Bunch,
Of,
Tag,
Attribute,
Types,
That,
Need,
To,
Wrap,
To,
The,
Next,
Line
><
A,
Whole,
Bunch,
Of,
Tag,
Parameter,
Types,
That,
Need,
To,
Wrap,
To,
The,
Next,
Line,
>|x|/>
<my-tag <
T extends
keyof (typeof SuperLongImportedVariableName)["member1"]["member2"]["member3"],
K extends T extends string
? MyType["member1"]["member2"]["member3"]
: MyOtherType,
>|x|/>
11 changes: 11 additions & 0 deletions src/__tests__/fixtures/generic-tag.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<my-tag<MyType, MyOtherType> my-attr="my-string" />
<my-tag <MyType, MyOtherType>|x| />
<my-tag
<MyType>|x|
my-attr="my-string"
/>
<my-tag<MyType>|x|/>
<my-tag<MyType1><MyOtherType>|x| />
<my-tag<A, Whole, Bunch, Of, Tag, Attribute, Types, That, Need, To, Wrap, To, The, Next, Line>
<A, Whole, Bunch, Of, Tag, Parameter, Types, That, Need, To, Wrap, To, The, Next, Line>|x| />
<my-tag <T extends keyof (typeof SuperLongImportedVariableName)["member1"]["member2"]["member3"], K extends (T extends string ? MyType["member1"]["member2"]["member3"] : MyOtherType)>|x| />
87 changes: 70 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import {
} from "./utils/with-parens-if-needed";
import asLiteralTextContent from "./utils/as-literal-text-content";
import { getOriginalCodeForNode } from "./utils/get-original-code";
import {
TSTypeParameterDeclaration,
TSTypeParameterInstantiation,
} from "@marko/compiler/babel-types";
type Node = types.Node;
const defaultFilePath = resolve("index.marko");
const rootRequire = createRequire(defaultFilePath);
Expand Down Expand Up @@ -310,6 +314,34 @@ export const printers: Record<string, Printer<types.Node>> = {
);
}

if (node.typeArguments) {
doc.push(
(
tagPath as AstPath<
types.MarkoTag & {
typeArguments: TSTypeParameterInstantiation;
}
>
).call(print, "typeArguments"),
);
}
if (node.body.typeParameters) {
if (!node.typeArguments) {
doc.push(" ");
}
doc.push(
(
tagPath as AstPath<
types.MarkoTag & {
body: {
typeParameters: TSTypeParameterDeclaration;
};
}
>
).call(print, "body", "typeParameters"),
);
}

const shorthandIndex = doc.push("") - 1;

if (node.var) {
Expand Down Expand Up @@ -434,13 +466,13 @@ export const printers: Record<string, Printer<types.Node>> = {
opts.markoSyntax === "html"
? ""
: isFirst
? b.ifBreak("--", " --", { groupId })
: "--",
? b.ifBreak("--", " --", { groupId })
: "--",
opts.markoSyntax === "html"
? ""
: preserveSpace
? b.hardline
: b.line,
? b.hardline
: b.line,
preserveSpace ? textDocs : b.fill(textDocs),
opts.markoSyntax === "html"
? ""
Expand All @@ -466,8 +498,8 @@ export const printers: Record<string, Printer<types.Node>> = {
node.body.body.some((child) => child.type === "MarkoScriptlet"))
? b.hardline
: preserveSpace
? ""
: b.softline;
? ""
: b.softline;
const wrapSep =
!preserveSpace &&
opts.markoSyntax === "html" &&
Expand Down Expand Up @@ -741,11 +773,11 @@ export const printers: Record<string, Printer<types.Node>> = {
))
? b.hardline
: opts.markoSyntax === "concise" ||
node.body.body.some(
(child) => child.type === "MarkoScriptlet",
)
? b.hardline
: b.softline;
node.body.body.some(
(child) => child.type === "MarkoScriptlet",
)
? b.hardline
: b.softline;

if (opts.markoSyntax === "html") {
doc.push(">");
Expand Down Expand Up @@ -895,11 +927,11 @@ export const printers: Record<string, Printer<types.Node>> = {
))
? b.hardline
: opts.markoSyntax === "concise" ||
node.body.body.some(
(child) => child.type === "MarkoScriptlet",
)
? b.hardline
: b.softline;
node.body.body.some(
(child) => child.type === "MarkoScriptlet",
)
? b.hardline
: b.softline;

if (opts.markoSyntax === "html") {
doc.push(">");
Expand Down Expand Up @@ -992,7 +1024,28 @@ export const printers: Record<string, Printer<types.Node>> = {
} else {
const parent = path.getParentNode() as types.Node | undefined;
const parentType = parent?.type;
if (
if (parentType === "MarkoTag" && path.key === "typeArguments") {
return tryPrintEmbed(
`_${code}`,
scriptParser,
(doc: any) => {
return doc[1].contents;
},
code,
);
} else if (
parentType === "MarkoTagBody" &&
path.key === "typeParameters"
) {
return tryPrintEmbed(
`function _${code}() {}`,
scriptParser,
(doc: any) => {
return doc[1].contents;
},
code,
);
} else if (
parentType === "MarkoTagBody" ||
(parentType === "VariableDeclarator" && path.key === "id") ||
(parentType === "MarkoTag" && path.key === "var")
Expand Down

0 comments on commit 8f2ea07

Please sign in to comment.