diff --git a/src/transforms/replaceStringFunctions.ts b/src/transforms/replaceStringFunctions.ts new file mode 100644 index 0000000..bd80298 --- /dev/null +++ b/src/transforms/replaceStringFunctions.ts @@ -0,0 +1,40 @@ +import ts, { isIdentifier } from 'typescript'; +import { ast } from './utils/create'; + +const $elm$core$String$join = ` +var $elm$core$String$join = F2(function (sep, strs) { + if (!strs.b) { + return ""; + } + var acc = strs.a; + strs = strs.b; + + for (; strs.b; strs = strs.b) { + acc = acc + sep + strs.a; + } + + return acc; +}; +`; + + +const replacements = { + $elm$core$String$join, +}; + +export const replaceStringFunctions: ts.TransformerFactory = ( + context +) => (sourceFile) => { + const visitor = (node: ts.Node): ts.VisitResult => { + if (ts.isVariableStatement(node)) { + const name = node.declarationList.declarations[0]?.name; + if (isIdentifier(name) && name.text in replacements) { + const key = name.text as keyof typeof replacements; + return ast(replacements[key]); + } + } + return ts.visitEachChild(node, visitor, context); + }; + + return ts.visitNode(sourceFile, visitor); +};