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 dedicated Key Type #41

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
98 changes: 49 additions & 49 deletions Benchmark/Sources/SwimBenchmark/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,176 +9,176 @@ benchmark("Build a basic page") {
}
}
body {
article {
article(class: "article", id: "main") {
section {
p {
p(class: "lorem-ipsum", id: "p-1") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-2") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-3") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-4") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-5") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-6") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-7") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-8") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}
}

section {
p {
p(class: "lorem-ipsum", id: "p-9") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-10") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-11") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-12") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-13") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-14") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-15") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-16") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}
}

section {
p {
p(class: "lorem-ipsum", id: "p-17") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-18") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-19") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-20") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-21") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-22") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-23") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}

p {
p(class: "lorem-ipsum", id: "p-24") {
"Lorem ipsum, dolor sit amet."
a {
a(href: "https://example.org") {
%"anchor"%
}
}
Expand Down
25 changes: 19 additions & 6 deletions Generator/Sources/Generator/Tag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ extension Tag {

var `public`: TokenSyntax { SyntaxFactory.makePublicKeyword().withTrailingTrivia(.spaces(1)) }

var `static`: TokenSyntax { SyntaxFactory.makeStaticKeyword().withTrailingTrivia(.spaces(1)) }

var `func`: TokenSyntax { SyntaxFactory.makeFuncKeyword().withTrailingTrivia(.spaces(1)) }

var `var`: TokenSyntax { SyntaxFactory.makeVarKeyword().withTrailingTrivia(.spaces(1)) as TokenSyntax }
Expand Down Expand Up @@ -137,6 +139,7 @@ extension Tag {
member.addMember(MemberDeclListItemSyntax {
$0.useDecl(DeclSyntax(VariableDeclSyntax {
$0.addAttribute(Syntax(`public`.withLeadingTrivia(format.leading())))
$0.addAttribute(Syntax(`static`.withLeadingTrivia(format.leading())))
$0.useLetOrVarKeyword(`let`)
$0.addBinding(PatternBindingSyntax {
$0.usePattern(PatternSyntax(SyntaxFactory.makeIdentifierPattern(identifier: elementNameIdentifier)))
Expand Down Expand Up @@ -244,7 +247,7 @@ extension Tag {
$0.usePattern(PatternSyntax(SyntaxFactory.makeIdentifierPattern(identifier: attributesIdentifier)))
$0.useTypeAnnotation(TypeAnnotationSyntax {
$0.useColon(colon)
$0.useType(buildStringsToStringsDicitonaryType(format))
$0.useType(buildAttributeKeyToStringsDicitonaryType(format))
})
$0.useInitializer(InitializerClauseSyntax {
$0.useEqual(equal)
Expand Down Expand Up @@ -364,8 +367,14 @@ extension Tag {
}))
$0.useLeftParen(leftParen)
$0.addArgument(TupleExprElementSyntax {
$0.useExpression(ExprSyntax(IdentifierExprSyntax {
$0.useIdentifier(elementNameIdentifier)
$0.useExpression(ExprSyntax(FunctionCallExprSyntax {
$0.useCalledExpression(ExprSyntax(MemberAccessExprSyntax {
$0.useBase(ExprSyntax(IdentifierExprSyntax {
$0.useIdentifier(SyntaxFactory.makeIdentifier("Self"))
}))
$0.useDot(dot)
$0.useName(elementNameIdentifier)
}))
}))
$0.useTrailingComma(comma)
})
Expand Down Expand Up @@ -455,7 +464,7 @@ extension Tag {
FunctionParameterSyntax {
$0.useSecondName(SyntaxFactory.makeIdentifier("customAttributes"))
$0.useColon(colon)
$0.useType(buildStringsToStringsDicitonaryType(format))
$0.useType(buildAttributeKeyToStringsDicitonaryType(format))
$0.useDefaultArgument(InitializerClauseSyntax{
$0.useEqual(equal)
$0.useValue(buildEmptyDictionaryLiteral(format))
Expand Down Expand Up @@ -508,14 +517,18 @@ extension Tag {
ExprSyntax(SyntaxFactory.makeDictionaryExpr(leftSquare: leftBracket, content: Syntax(SyntaxFactory.makeColonToken()), rightSquare: rightBracket))
}

func buildStringsToStringsDicitonaryType(_ format: Format) -> TypeSyntax {
func buildAttributeKeyToStringsDicitonaryType(_ format: Format) -> TypeSyntax {
let attributeKeyType = TypeSyntax(SimpleTypeIdentifierSyntax {
$0.useName(SyntaxFactory.makeIdentifier("AttributeKey"))
})

let stringType = TypeSyntax(SimpleTypeIdentifierSyntax {
$0.useName(SyntaxFactory.makeIdentifier("String"))
})

return TypeSyntax(SyntaxFactory.makeDictionaryType(
leftSquareBracket: leftBracket,
keyType: stringType,
keyType: attributeKeyType,
colon: colon,
valueType: stringType,
rightSquareBracket: rightBracket
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let myDocument = html(lang: "en-US") {
head {
meta(charset: "utf-8", content: "text/html", httpEquiv: "Content-Type")
}
body(customAttributes: [ "data-foo": "bar" ]) {
body(customAttributes: [ .data("foo"): "bar" ]) {
article(classes: "readme", "modern") {
header {
h1 {
Expand Down
2 changes: 1 addition & 1 deletion Sources/HTML/Tag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import Foundation

public protocol Tag {
/// The name of the element.
var elementName: String { get }
static var elementName: String { get }
}
Loading