new transformation inlineNumberToString

This commit is contained in:
mdgriffith 2020-08-08 13:55:46 -04:00
parent 814af8b5ef
commit bd26863eeb
5 changed files with 81 additions and 33 deletions

View File

@ -25,6 +25,7 @@ import {
import { createRemoveUnusedLocalsTransform } from './experiments/removeUnusedLocals';
import { createPassUnwrappedFunctionsTransformer } from './experiments/passUnwrappedFunctions';
import { replaceVDomNode } from './experiments/correctVirtualDom';
import { inlineNumberToString } from './experiments/inlineNumberToString';
export const compileAndTransform = async (
dir: string,
@ -75,6 +76,7 @@ export const compileAndTransform = async (
let inlineCtx: InlineContext | undefined;
const transformations: any[] = removeDisabled([
// [options.replaceVDomNode, replaceVDomNode()],
[options.variantShapes, normalizeVariantShapes],
[
options.inlineFunctions,
@ -84,6 +86,7 @@ export const compileAndTransform = async (
}),
],
[options.inlineEquality, inlineEquality()],
[options.inlineNumberToString, inlineNumberToString()],
[
options.listLiterals,
createInlineListFromArrayTransformer(
@ -119,6 +122,7 @@ export const compileAndTransform = async (
fs.writeFileSync(pathInOutput('elm.opt.js'), printer.printFile(initialJs));
// Prepack, minify, and gzip
if (options.prepack) {
const { code } = prepackFileSync([pathInOutput('elm.opt.transformed.js')], {
debugNames: true,

View File

@ -0,0 +1,30 @@
/*
If we see `String$fromFloat(val)`, replace it with `val + ""`
*/
import ts from 'typescript';
const FLOAT_2_STRING = '$elm$core$String$fromFloat';
const INT_2_STRING = '$elm$core$String$fromInt';
export const inlineNumberToString = (): ts.TransformerFactory<ts.SourceFile> => context => {
return sourceFile => {
const visitor = (node: ts.Node): ts.VisitResult<ts.Node> => {
if (ts.isCallExpression(node)) {
const expression = node.expression;
if (
ts.isIdentifier(expression) &&
(expression.text === FLOAT_2_STRING ||
expression.text === INT_2_STRING) &&
node.arguments.length == 1
) {
return ts.createAdd(node.arguments[0], ts.createIdentifier('""'));
}
}
return ts.visitEachChild(node, visitor, context);
};
return ts.visitNode(sourceFile, visitor);
};
};

View File

@ -10,27 +10,32 @@ import * as Reporting from './reporting';
const defaultOptions: Transforms = {
prepack: true,
replaceVDomNode: true,
variantShapes: true,
inlineNumberToString: true,
inlineEquality: true,
inlineFunctions: true,
listLiterals: true,
passUnwrappedFunctions: true,
arrowFns: true,
objectUpdate: null,
unusedValues: true,
objectUpdate: ObjectUpdate.InlineSpread,
unusedValues: false,
};
const test: Transforms = {
prepack: false,
replaceVDomNode: true,
variantShapes: false,
inlineNumberToString: true,
inlineEquality: false,
inlineFunctions: false,
listLiterals: false,
passUnwrappedFunctions: false,
arrowFns: false,
objectUpdate: null,
unusedValues: false,
};
// const defaultOptions: Transforms = {
// prepack: false,
// variantShapes: false,
// inlineEquality: false,
// inlineFunctions: true,
// passUnwrappedFunctions: true,
// listLiterals: false,
// arrowFns: false,
// objectUpdate: null,
// unusedValues: false,
// };
async function go() {
const report = await Reporting.run([
// Use `runWithBreakdown` if you want the breakdown
@ -40,30 +45,36 @@ async function go() {
// elmFile: 'main',
// options: defaultOptions,
// },
// {
// name: 'bench',
// dir: 'testcases/bench',
// elmFile: 'Main.elm',
// options: defaultOptions,
// },
// {
// name: 'html',
// dir: 'testcases/html',
// elmFile: 'Main.elm',
// options: defaultOptions,
// },
// {
// name: 'elm-ui',
// dir: 'testcases/elm-ui',
// elmFile: 'Main.elm',
// options: defaultOptions,
// },
{
name: 'bench',
dir: 'testcases/bench',
name: 'elm-ui-2',
dir: 'testcases/elm-ui-2',
elmFile: 'Main.elm',
options: defaultOptions,
},
{
name: 'html',
dir: 'testcases/html',
elmFile: 'Main.elm',
options: defaultOptions,
},
{
name: 'elm-ui',
dir: 'testcases/elm-ui',
elmFile: 'Main.elm',
options: defaultOptions,
},
{
name: 'elm-markdown',
dir: 'testcases/elm-markdown',
elmFile: 'Run.elm',
options: defaultOptions,
},
// {
// name: 'elm-markdown',
// dir: 'testcases/elm-markdown',
// elmFile: 'Run.elm',
// options: defaultOptions,
// },
// {
// name: 'elm-markdown',
// dir: 'testcases/elm-markdown',

View File

@ -202,7 +202,9 @@ export const run = async function(runnable: Testcase[]) {
const emptyOpts: Transforms = {
prepack: false,
replaceVDomNode: false,
variantShapes: false,
inlineNumberToString: false,
inlineFunctions: false,
inlineEquality: false,
listLiterals: false,

View File

@ -16,6 +16,7 @@ export type Transforms = {
prepack: boolean;
replaceVDomNode: boolean;
variantShapes: boolean;
inlineNumberToString: boolean;
inlineEquality: boolean;
inlineFunctions: boolean;
passUnwrappedFunctions: boolean;