Last active
May 30, 2019 07:16
Second version of our transform, which adds a filter for only Value decorator nodes!
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); | |
} | |
export function valueDecoratorToGetterAndSetterFactory (nodes: Array<Node>): TransformerFactory<Node> { | |
return function (context: TransformationContext): Transformer<Node> { | |
return function (node: Node): Node { | |
return visitNode(node, visit); | |
}; | |
function visit (node: Node): Node | Array<Node> { | |
node = visitEachChild(node, visit, context); | |
if (nodes.includes(node)) { | |
// do something | |
} | |
return node; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment