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
// Dependencies: | |
import { tsquery } from '@phenomnomnominal/tsquery'; | |
import { tstemplate } from '@phenomnomnominal/tstemplate'; | |
import { createIdentifier, createLiteral, createToken, SyntaxKind, visitEachChild, visitNode } from 'typescript'; | |
import { Node, PropertyDeclaration, SourceFile, TransformationContext, Transformer, TransformerFactory } from 'typescript'; | |
// Constants: | |
const CAST_PROPERTIES_QUERY = `PropertyDeclaration:has(Decorator:has(Identifier[name="Value"]))`; | |
const RESULT_QUERY = 'PropertyDeclaration, GetAccessor, SetAccessor'; | |
const OTHER_DECORATORS_QUERY = `Decorator:has(Decorator > CallExpression[expression.name!="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
import { Component, Input } from '@angular/core'; | |
import { Value, isBool, isNotNull } from '@trademe/ensure'; | |
@Component({ | |
selector: 'hello' | |
// ... | |
}) | |
export class HelloComponent { | |
private _primary: boolean; | |
@Input() |
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
// Constants: | |
// ... | |
const CASTERS_QUERY = 'CallExpression[expression.name!="Value"], CallExpression[expression.name="Value"] > Identifier[name!="Value"], CallExpression[expression.name="Value"] > ArrayLiteralExpression > Identifier[name!="Value"]'; | |
const CAST_RESULT_EXPRESSION_QUERY = 'ExpressionStatement'; | |
const GETTER_CAST_QUERY = 'Identifier[name="isNotNull"]'; | |
const CAST_CALL_TEMPLATE = tstemplate.compile(` | |
val = <%= name %>(val, <%= propertyName %>); | |
`); |
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
// Constants: | |
// ... | |
const OTHER_DECORATORS_QUERY = `Decorator:has(Decorator > CallExpression[expression.name!="Value"])`; | |
export function transformer (source: SourceFile): TransformerFactory<Node> { | |
// ... | |
} | |
export function valueDecoratorToGetterAndSetterFactory (nodes: Array<Node>): TransformerFactory<Node> { | |
return function (context: TransformationContext): Transformer<Node> { |
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
import { Component, Input } from '@angular/core'; | |
import { Value, isBool, isNotNull } from '@trademe/ensure'; | |
@Component({ | |
selector: 'hello' | |
// ... | |
}) | |
export class HelloComponent { | |
private _primary: boolean; | |
public get primary (): boolean { |
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
// Dependencies: | |
// ... | |
import { tstemplate } from '@phenomnomnominal/tstemplate'; | |
// Constants: | |
// ... | |
const RESULT_QUERY = 'PropertyDeclaration, GetAccessor, SetAccessor'; | |
const GETTER_SETTER_TEMPLATE = tstemplate.compile(` | |
class Template { |
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
// Dependencies: | |
import { tsquery } from '@phenomnomnominal/tsquery'; | |
import { Node, visitNode, SourceFile, TransformationContext, Transformer, TransformerFactory } from 'typescript'; | |
// Constants: | |
const CAST_PROPERTIES_QUERY = `PropertyDeclaration:has(Decorator:has(Identifier[name="Value"]))`; | |
export function transformer (source: SourceFile): TransformerFactory<Node> { | |
const castProperties = tsquery(source, CAST_PROPERTIES_QUERY); | |
return valueDecoratorToGetterAndSetterFactory(castProperties); |
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
// Dependencies: | |
import { Node, visitNode, SourceFile, TransformationContext, Transformer, TransformerFactory } from 'typescript'; | |
export function transformer (source: SourceFile): TransformerFactory<Node> { | |
return function (context: TransformationContext): Transformer<Node> { | |
return function (node: Node): Node { | |
return visitNode(node, visit); | |
}; | |
function visit (node: Node): Node | Array<Node> { |
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
import { createPrinter, transform, SourceFile} from 'typescript'; | |
import { transformer } from './transformer'; | |
function transformFile (file: SourceFile): string { | |
const result = transform(file, [transformer(file)]); | |
const [transformed] = result.transformed; | |
const printer = createPrinter(); | |
return printer.printFile(transformed as SourceFile); | |
} |
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
import * as tslib_1 from "tslib"; | |
import { Component, Input } from "@angular/core"; | |
import { Value, isBool, isNotNull } from "@trademe/ensure"; | |
var HelloComponent = (function () { | |
function HelloComponent() { } | |
HelloComponent.decorators = [{ | |
type: Component, | |
args: [{ | |
selector: "hello" |