mirror of
https://github.com/mdgriffith/elm-optimize-level-2.git
synced 2024-11-29 12:46:32 +03:00
String.join can be replaced with a faster version
This commit is contained in:
parent
d154f3ddfe
commit
df46a92805
40
src/transforms/replaceStringFunctions.ts
Normal file
40
src/transforms/replaceStringFunctions.ts
Normal file
@ -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<ts.SourceFile> = (
|
||||
context
|
||||
) => (sourceFile) => {
|
||||
const visitor = (node: ts.Node): ts.VisitResult<ts.Node> => {
|
||||
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);
|
||||
};
|
Loading…
Reference in New Issue
Block a user