dont key by top level type name for parsing types.

This commit is contained in:
mdgriffith 2020-08-03 09:49:15 -04:00
parent 189b559be8
commit 5adb67ee53
3 changed files with 57 additions and 61 deletions

View File

@ -1,7 +1,7 @@
import { compileSync } from 'node-elm-compiler';
import * as fs from 'fs';
import * as path from 'path';
import { parseElm, parseDir } from './parseElm';
import { parseElm, parseDir, primitives } from './parseElm';
import ts from 'typescript';
import { createCustomTypesTransformer } from './experiments/variantShapes';
import { Mode, Transforms, ObjectUpdate } from './types';
@ -42,26 +42,26 @@ export const compileAndTransform = async (
});
const pathInOutput = (p: string) => path.join(dir, 'output', p);
console.log('lets try this again');
const elmSource = fs.readFileSync(path.join(dir, file), 'utf8');
let parsedVariants = parseElm({
author: 'author',
project: 'project',
source: elmSource,
});
Object.assign(parsedVariants, parseDir('elm-packages'));
}).concat(primitives);
let parsed = parseDir('elm-packages');
parsedVariants = parsedVariants.concat(parsed);
const source = ts.createSourceFile(
'elm.js',
fs.readFileSync(pathInOutput('elm.opt.js'), 'utf-8'),
ts.ScriptTarget.ES2018
);
const replacements = Object.values(parsedVariants).flat();
// const replacements = Object.values(parsedVariants).flat();
const normalizeVariantShapes = createCustomTypesTransformer(
replacements,
parsedVariants,
Mode.Prod
);

View File

@ -19,32 +19,38 @@ const defaultOptions = {
};
async function go() {
const report = await Reporting.run([
// Use `runWithBreakdown` if you want the breakdown
// const report = await Reporting.runWithBreakdown([
// const report = await Reporting.run([
// Use `runWithBreakdown` if you want the breakdown
const report = await Reporting.runWithBreakdown([
// { name: 'simple',
// dir: 'testcases/simple',
// 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-markdown',
// dir: 'testcases/elm-markdown',
// elmFile: 'Run.elm',
// 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: 'elm-markdown',
dir: 'testcases/elm-markdown',
elmFile: 'Run.elm',
options: defaultOptions,
},
// This one takes forever
// {
// name: 'elm-obj-file',

View File

@ -51,6 +51,8 @@ const just: ElmVariant = {
};
const maybe = [nothing, just];
export const primitives = listVariants.concat(maybe);
export const parseElm = ({
author,
project,
@ -59,12 +61,9 @@ export const parseElm = ({
author: string;
project: string;
source: string;
}): { [id: string]: ElmVariant[] } => {
}): ElmVariant[] => {
const tree = elmParser.parse(source);
const found: { [id: string]: ElmVariant[] } = {
List: listVariants,
Maybe: maybe,
};
const found: ElmVariant[] = [];
/*
A quick reference for using treesitter.
@ -110,7 +109,8 @@ export const parseElm = ({
for (let variant of child.namedChildren) {
switch (variant.type) {
case 'upper_case_identifier': {
name = variant.text;
name = moduleName + '.' + variant.text;
break;
}
case 'union_variant': {
@ -121,6 +121,7 @@ export const parseElm = ({
switch (detail.type) {
case 'upper_case_identifier': {
foundVariantName = detail.text;
break;
}
@ -155,27 +156,14 @@ export const parseElm = ({
'$' +
foundVariantName;
if (name in found) {
found[name].push({
typeName: name,
name: foundVariantName,
jsName: jsName,
index: index,
slots: slots,
totalTypeSlotCount: totalTypeSlotCount,
});
} else {
found[name] = [
{
typeName: name,
name: foundVariantName,
index: index,
jsName: jsName,
slots: slots,
totalTypeSlotCount: totalTypeSlotCount,
},
];
}
found.push({
typeName: name,
name: foundVariantName,
jsName: jsName,
index: index,
slots: slots,
totalTypeSlotCount: totalTypeSlotCount,
});
index = index + 1;
break;
@ -186,8 +174,10 @@ export const parseElm = ({
}
}
for (let variant of found[name]) {
variant.totalTypeSlotCount = totalTypeSlotCount;
for (let variant of found) {
if (variant.typeName == name) {
variant.totalTypeSlotCount = totalTypeSlotCount;
}
}
break;
}
@ -196,11 +186,12 @@ export const parseElm = ({
}
}
}
return found;
};
export const parseDir = (dir: string): { [id: string]: ElmVariant[] } => {
let variants: { [id: string]: ElmVariant[] } = {};
export const parseDir = (dir: string): ElmVariant[] => {
let variants: ElmVariant[] = [];
const files = fs.readdirSync(dir);
files.forEach(function(author) {
@ -214,12 +205,11 @@ export const parseDir = (dir: string): { [id: string]: ElmVariant[] } => {
project
);
variants = Object.assign(variants, vars);
variants = variants.concat(vars);
}
});
}
});
return variants;
};
@ -227,8 +217,8 @@ const parseElmFilesInDirectory = (
dir: string,
author: string,
project: string
): { [id: string]: ElmVariant[] } => {
let variants: { [id: string]: ElmVariant[] } = {};
): ElmVariant[] => {
let variants: ElmVariant[] = [];
let files = walkSync(dir, []);
@ -241,7 +231,7 @@ const parseElmFilesInDirectory = (
source: source,
});
variants = Object.assign(variants, results);
variants = variants.concat(results);
}
}