-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from omochi/method-chain-style
パラメータオブジェクトをメソッドチェーンで構築できるようにする
- Loading branch information
Showing
26 changed files
with
7,331 additions
and
403 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ DerivedData/ | |
.netrc | ||
.swiftpm | ||
node_modules | ||
data.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import Collections | ||
|
||
struct Def: Codable { | ||
var tagNames: [String] | ||
var voidElements: [String] | ||
var elementAttributes: [String: [String]] | ||
var eventAttributes: [String] | ||
var cssProperties: [String] | ||
|
||
mutating func fix() { | ||
tagNames.removeAll { (tagName) in | ||
voidElements.contains(tagName) | ||
} | ||
|
||
self.cssProperties = { | ||
var css: OrderedSet<String> = [] | ||
|
||
for x in cssProperties { | ||
if !x.hasPrefix("-") { | ||
css.remove("-" + x) | ||
} | ||
|
||
css.append(x) | ||
} | ||
|
||
return css.elements | ||
}() | ||
} | ||
|
||
var allAttributes: [String] { | ||
var attrs: OrderedSet<String> = [] | ||
attrs.formUnion(elementAttributes["*"] ?? []) | ||
|
||
var dict = elementAttributes | ||
dict["*"] = nil | ||
|
||
for key in dict.keys.sorted() { | ||
attrs.formUnion(dict[key] ?? []) | ||
} | ||
|
||
return attrs.elements | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
Codegen/Sources/SRTCodegen/Renderers/AttributesRenderer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Foundation | ||
import CodegenKit | ||
|
||
struct AttributesRenderer: Renderer { | ||
var def: Def | ||
|
||
func isTarget(file: URL) -> Bool { | ||
file.lastPathComponent == "Attributes.swift" | ||
} | ||
|
||
func render(template: inout CodeTemplateModule.CodeTemplate, file: URL, on runner: CodegenKit.CodegenRunner) throws { | ||
let code = def.allAttributes.map { (attribute) in | ||
renderSetter(attribute: attribute) | ||
}.joined(separator: "\n") | ||
|
||
template["setters"] = code | ||
} | ||
|
||
private func renderSetter(attribute: String) -> String { | ||
let symbol = attribute.kebabToCamel() | ||
|
||
return """ | ||
public func \(renderIdentifier(symbol))(_ value: String) -> Attributes { | ||
set("\(attribute)", to: value) | ||
} | ||
""" | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
Codegen/Sources/SRTCodegen/Renderers/EventListenersRenderer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import Foundation | ||
import CodegenKit | ||
|
||
struct EventListenersRenderer: Renderer { | ||
var def: Def | ||
|
||
func isTarget(file: URL) -> Bool { | ||
file.lastPathComponent == "EventListeners.swift" | ||
} | ||
|
||
func render(template: inout CodeTemplateModule.CodeTemplate, file: URL, on runner: CodegenKit.CodegenRunner) throws { | ||
let code = def.eventAttributes.map { (attribute) in | ||
renderSetter(attribute: attribute) | ||
}.joined(separator: "\n") | ||
|
||
template["setters"] = code | ||
} | ||
|
||
private func renderSetter(attribute: String) -> String { | ||
var attribute = attribute | ||
if attribute.hasPrefix("on") { | ||
attribute.removeFirst(2) | ||
} | ||
|
||
return """ | ||
public func \(renderIdentifier(attribute))(_ value: EventListener) -> EventListeners { | ||
set("\(attribute)", to: value) | ||
} | ||
""" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import Foundation | ||
import CodegenKit | ||
|
||
struct StyleRenderer: Renderer { | ||
var def: Def | ||
|
||
func isTarget(file: URL) -> Bool { | ||
file.lastPathComponent == "Style.swift" | ||
} | ||
|
||
func render(template: inout CodeTemplateModule.CodeTemplate, file: URL, on runner: CodegenKit.CodegenRunner) throws { | ||
let code = def.cssProperties.map { (attribute) in | ||
renderSetter(attribute: attribute) | ||
}.joined(separator: "\n") | ||
|
||
template["setters"] = code | ||
} | ||
|
||
private func renderSetter(attribute: String) -> String { | ||
let symbol = attribute.kebabToCamel() | ||
|
||
return """ | ||
public func \(renderIdentifier(symbol))(_ value: String) -> Style { | ||
set("\(attribute)", to: value) | ||
} | ||
""" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Foundation | ||
|
||
public let keywords = [ | ||
"as", | ||
"class", | ||
"continue", | ||
"default", | ||
"defer", | ||
"for", | ||
"is", | ||
"var", | ||
] | ||
|
||
func renderIdentifier(_ text: String) -> String { | ||
var text = text | ||
if keywords.contains(text) { | ||
text = "`\(text)`" | ||
} | ||
return text | ||
} | ||
|
||
extension String { | ||
func kebabToCamel() -> String { | ||
let strs = self.components(separatedBy: "-") | ||
.filter { !$0.isEmpty } | ||
|
||
var result = "" | ||
for (index, str) in strs.enumerated() { | ||
if index == 0 { | ||
result += str.lowercased() | ||
} else { | ||
result += str.capitalized | ||
} | ||
} | ||
|
||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
import htmlTags from "html-tags/index.js"; | ||
import htmlLeafTags from "html-tags/void.js"; | ||
import { htmlTagNames } from "html-tag-names"; | ||
import { htmlVoidElements } from "html-void-elements"; | ||
import { htmlElementAttributes } from "html-element-attributes"; | ||
import { htmlEventAttributes } from "html-event-attributes"; | ||
import * as kcpModule from "known-css-properties"; | ||
|
||
console.log(htmlTags); | ||
console.log(htmlLeafTags); | ||
const json = { | ||
"tagNames": htmlTagNames, | ||
"voidElements": htmlVoidElements, | ||
"elementAttributes": htmlElementAttributes, | ||
"eventAttributes": htmlEventAttributes, | ||
"cssProperties": kcpModule.all | ||
}; | ||
|
||
const string = JSON.stringify(json, undefined, 2); | ||
|
||
console.log(string); |
Oops, something went wrong.