Created
August 12, 2022 08:35
-
-
Save JoviDeCroock/9022f383e3babe319805a55bee3ed24c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* The transformer signature is based on https://github.com/cevek/ttypescript#program | |
*/ | |
export default function (program) { | |
const checker = program.getTypeChecker(); | |
return (context) => { | |
const { factory } = context; | |
return (sourceFile) => { | |
const visitor = (node) => { | |
if(ts.isArrayBindingPattern(node)) { | |
const elements = node.elements.map((el, i) => { | |
if (!el.getText()) return undefined | |
return { key: '' + i, name: el.getText() } | |
}).filter(Boolean) | |
const objectBindingPattern = factory.createObjectBindingPattern() | |
objectBindingPattern.elements = elements.map(el => { | |
const key = factory.createIdentifier(el.key) | |
const name = factory.createIdentifier(el.name) | |
return factory.createBindingElement(undefined, key, name) | |
}) | |
return objectBindingPattern | |
} | |
return ts.visitEachChild(node, visitor, context); | |
}; | |
return ts.visitNode(sourceFile, visitor); | |
}; | |
}; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment