mirror of
https://github.com/mdgriffith/elm-optimize-level-2.git
synced 2024-12-01 19:35:08 +03:00
supported prod mode for transforming custom types
This commit is contained in:
parent
e652a7e8b4
commit
d7bb6d943c
4
src/experiments/types.ts
Normal file
4
src/experiments/types.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export enum Mode {
|
||||
Prod = 'prod',
|
||||
Dev = 'dev',
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
import ts from 'typescript';
|
||||
import { Mode } from './types';
|
||||
|
||||
export type VariantReplacement = {
|
||||
symbolName: string;
|
||||
variantName: string;
|
||||
variantIndex: number;
|
||||
maximumNumberOfArgs: number;
|
||||
numberOfArgs: number;
|
||||
};
|
||||
@ -10,17 +12,22 @@ export type VariantReplacement = {
|
||||
// TODO fill a proper array
|
||||
const argNames = ['a', 'b', 'c', 'd', 'e'];
|
||||
|
||||
const createVariantObjectLiteral = ({
|
||||
const createVariantObjectLiteral = (
|
||||
{
|
||||
variantName,
|
||||
maximumNumberOfArgs,
|
||||
numberOfArgs,
|
||||
}: {
|
||||
variantName: string;
|
||||
maximumNumberOfArgs: number;
|
||||
numberOfArgs: number;
|
||||
}): ts.ObjectLiteralExpression => {
|
||||
variantIndex,
|
||||
}: VariantReplacement,
|
||||
mode: Mode
|
||||
): ts.ObjectLiteralExpression => {
|
||||
return ts.createObjectLiteral([
|
||||
ts.createPropertyAssignment('$', ts.createStringLiteral(variantName)),
|
||||
ts.createPropertyAssignment(
|
||||
'$',
|
||||
mode === Mode.Dev
|
||||
? ts.createStringLiteral(variantName)
|
||||
: ts.createNumericLiteral(variantIndex.toString())
|
||||
),
|
||||
// existing arguments
|
||||
...argNames
|
||||
.slice(0, numberOfArgs)
|
||||
@ -32,11 +39,11 @@ const createVariantObjectLiteral = ({
|
||||
]);
|
||||
};
|
||||
|
||||
const createCtorVariant = ({
|
||||
variantName,
|
||||
maximumNumberOfArgs,
|
||||
numberOfArgs,
|
||||
}: VariantReplacement): ts.Expression => {
|
||||
const createCtorVariant = (
|
||||
replacement: VariantReplacement,
|
||||
mode: Mode
|
||||
): ts.Expression => {
|
||||
const { numberOfArgs } = replacement;
|
||||
const funcExpression = ts.createArrowFunction(
|
||||
undefined,
|
||||
undefined,
|
||||
@ -55,11 +62,7 @@ const createCtorVariant = ({
|
||||
),
|
||||
undefined,
|
||||
undefined,
|
||||
createVariantObjectLiteral({
|
||||
variantName,
|
||||
maximumNumberOfArgs,
|
||||
numberOfArgs,
|
||||
})
|
||||
createVariantObjectLiteral(replacement, mode)
|
||||
);
|
||||
|
||||
if (numberOfArgs > 1) {
|
||||
@ -75,7 +78,8 @@ const createCtorVariant = ({
|
||||
};
|
||||
|
||||
export const createCustomTypesTransformer = (
|
||||
replacements: VariantReplacement[]
|
||||
replacements: VariantReplacement[],
|
||||
mode: Mode
|
||||
): ts.TransformerFactory<ts.SourceFile> => context => {
|
||||
return sourceFile => {
|
||||
const visitor = (node: ts.Node): ts.Node => {
|
||||
@ -83,16 +87,11 @@ export const createCustomTypesTransformer = (
|
||||
for (const replacement of replacements) {
|
||||
if (node.name.text === replacement.symbolName) {
|
||||
if (replacement.numberOfArgs === 0) {
|
||||
const { variantName, maximumNumberOfArgs } = replacement;
|
||||
return ts.updateVariableDeclaration(
|
||||
node,
|
||||
node.name,
|
||||
node.type,
|
||||
createVariantObjectLiteral({
|
||||
variantName,
|
||||
maximumNumberOfArgs,
|
||||
numberOfArgs: 0,
|
||||
})
|
||||
createVariantObjectLiteral(replacement, mode)
|
||||
);
|
||||
}
|
||||
|
||||
@ -100,7 +99,7 @@ export const createCustomTypesTransformer = (
|
||||
node,
|
||||
node.name,
|
||||
node.type,
|
||||
createCtorVariant(replacement)
|
||||
createCtorVariant(replacement, mode)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import {
|
||||
FuncSplit,
|
||||
createFuncInlineTransformer,
|
||||
} from './experiments/inlineWrappedFunctions';
|
||||
import { Mode } from './experiments/types';
|
||||
|
||||
const elmOutput = `
|
||||
var $elm$core$Maybe$Nothing = {$: 'Nothing'};
|
||||
@ -31,6 +32,7 @@ const replacements: VariantReplacement[] = [
|
||||
symbolName: '$elm$core$Maybe$Nothing',
|
||||
variantName: 'Nothing',
|
||||
maximumNumberOfArgs: 1,
|
||||
variantIndex: 1,
|
||||
numberOfArgs: 0,
|
||||
},
|
||||
|
||||
@ -38,17 +40,22 @@ const replacements: VariantReplacement[] = [
|
||||
symbolName: '$elm$core$Maybe$Just',
|
||||
variantName: 'Just',
|
||||
numberOfArgs: 1,
|
||||
variantIndex: 0,
|
||||
maximumNumberOfArgs: 2,
|
||||
},
|
||||
{
|
||||
symbolName: '$author$project$Main$Three',
|
||||
variantName: 'Three',
|
||||
numberOfArgs: 3,
|
||||
variantIndex: 100500,
|
||||
maximumNumberOfArgs: 4,
|
||||
},
|
||||
];
|
||||
|
||||
const customTypeTransformer = createCustomTypesTransformer(replacements);
|
||||
const customTypeTransformer = createCustomTypesTransformer(
|
||||
replacements,
|
||||
Mode.Prod
|
||||
);
|
||||
const [newFile] = ts.transform(source, [customTypeTransformer]).transformed;
|
||||
|
||||
const printer = ts.createPrinter();
|
||||
|
Loading…
Reference in New Issue
Block a user