-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow to name the constants of a string union by configuration
- Loading branch information
Showing
12 changed files
with
189 additions
and
5 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
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
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,56 @@ | ||
import ts, {Node} from "typescript"; | ||
import {ConverterPlugin} from "../plugin.js"; | ||
import {ConverterContext} from "../context.js"; | ||
import {Render} from "../render.js"; | ||
import {UnionNameResolver} from "../unionNameResolver.js"; | ||
import {GeneratedFile} from "../generated.js"; | ||
|
||
export const unionNameResolverServiceKey = Symbol() | ||
|
||
export class UnionNameResolverService { | ||
private readonly unionNameResolvers: UnionNameResolver[] | ||
private readonly resolvedNodes = new Map<Node, string>() | ||
|
||
constructor(unionNameResolvers: UnionNameResolver[]) { | ||
this.unionNameResolvers = unionNameResolvers | ||
} | ||
|
||
resolveUnionName(node: Node, context: ConverterContext): string | null { | ||
const resolvedName = this.resolvedNodes.get(node) | ||
if (resolvedName) return resolvedName | ||
|
||
for (const unionNameResolver of this.unionNameResolvers) { | ||
const result = unionNameResolver(node, context) | ||
|
||
if (result !== null) { | ||
this.resolvedNodes.set(node, result) | ||
return result | ||
} | ||
} | ||
|
||
return null | ||
} | ||
} | ||
|
||
export class UnionNameResolverPlugin implements ConverterPlugin { | ||
private readonly unionNameResolverService: UnionNameResolverService; | ||
|
||
constructor(unionNameResolvers: UnionNameResolver[]) { | ||
this.unionNameResolverService = new UnionNameResolverService(unionNameResolvers) | ||
} | ||
|
||
generate(): GeneratedFile[] { | ||
return []; | ||
} | ||
|
||
render(node: Node, context: ConverterContext, next: Render): string | null { | ||
return null; | ||
} | ||
|
||
traverse(node: ts.Node, context: ConverterContext): void { | ||
} | ||
|
||
setup(context: ConverterContext): void { | ||
context.registerService(unionNameResolverServiceKey, this.unionNameResolverService) | ||
} | ||
} |
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,5 @@ | ||
import {ConverterContext} from "./context.js"; | ||
import {Node} from "typescript" | ||
|
||
export type UnionNameResolver<TNode extends Node = Node> = | ||
(node: TNode, context: ConverterContext) => string | null |
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
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
33 changes: 33 additions & 0 deletions
33
test/functional/base/karakum/unionNameResolvers/resolveOperatorNames.js
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,33 @@ | ||
import ts from "typescript"; | ||
|
||
export default (node) => { | ||
if (!ts.isStringLiteral(node)) return null | ||
if (!ts.isLiteralTypeNode(node.parent)) return null | ||
if (!ts.isUnionTypeNode(node.parent.parent)) return null | ||
if (!ts.isTypeAliasDeclaration(node.parent.parent.parent)) return null | ||
if (!ts.isIdentifier(node.parent.parent.parent.name)) return null | ||
|
||
const name = node.parent.parent.parent.name.text | ||
if (name === "Operator2") { | ||
switch (node.text) { | ||
case "": | ||
return "EMPTY" | ||
case "=": | ||
return "EQUAL" | ||
case "<": | ||
return "LT" | ||
case ">": | ||
return "GT" | ||
case "<=": | ||
return "LTE" | ||
} | ||
} else if (name === "Operator3") { | ||
return "OPERATOR" | ||
} | ||
|
||
if (name.startsWith("Operator") && (node.text === ">=")) { | ||
return "GTE" | ||
} | ||
|
||
return null | ||
} |
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