Skip to content

Commit

Permalink
refactor: improve types for visitor exports
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Oct 23, 2024
1 parent ecd3e51 commit 49f905f
Show file tree
Hide file tree
Showing 28 changed files with 143 additions and 102 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-badgers-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/translator-tags": patch
---

XML Declarations are now a compile error (like cdata).
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

at packages/translator-tags/src/__tests__/fixtures/declaration/template.marko:1:1
> 1 | <?xml version="1.0" encoding="utf-8"?>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ XML declarations sections are not supported in Marko.
2 | <contact-info>
3 | <name>Hello World</name>
4 | </contact-info>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

at packages/translator-tags/src/__tests__/fixtures/declaration/template.marko:1:1
> 1 | <?xml version="1.0" encoding="utf-8"?>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ XML declarations sections are not supported in Marko.
2 | <contact-info>
3 | <name>Hello World</name>
4 | </contact-info>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export const skip_csr = true;
export const skip_ssr = true;
export const error_compiler = true;
24 changes: 6 additions & 18 deletions packages/translator-tags/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Config, types as t } from "@marko/compiler";
import type { Config } from "@marko/compiler";

import coreTagLib from "./core";
import { extractVisitors } from "./util/visitors";
import AssignmentExpression from "./visitors/assignment-expression";
import MarkoCDATA from "./visitors/cdata";
import MarkoComment from "./visitors/comment";
Expand All @@ -16,7 +17,7 @@ import MarkoTag from "./visitors/tag";
import MarkoText from "./visitors/text";
import UpdateExpression from "./visitors/update-expression";

const visitors = {
const visitors = extractVisitors({
Program,
Function,
AssignmentExpression,
Expand All @@ -31,29 +32,16 @@ const visitors = {
MarkoPlaceholder,
MarkoScriptlet,
MarkoComment,
};

const getVisitorOfType = (
typename: "migrate" | "transform" | "analyze" | "translate",
): t.Visitor =>
Object.entries(visitors).reduce((visitor, [name, value]) => {
if (typename in value) {
visitor[name as any] = (value as any)[typename];
}
return visitor;
}, {} as t.Visitor);
});

export { default as internalEntryBuilder } from "./util/entry-builder";

export const transform = getVisitorOfType("transform");
export const analyze = getVisitorOfType("analyze");
export const translate = getVisitorOfType("translate");
export const { transform, analyze, translate } = visitors;
export const taglibs = [
[
__dirname,
{
...coreTagLib,
migrate: getVisitorOfType("migrate"),
migrate: visitors.migrate,
},
],
];
Expand Down
27 changes: 27 additions & 0 deletions packages/translator-tags/src/util/visitors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { types as t } from "@marko/compiler";

export type VisitorType = "migrate" | "transform" | "analyze" | "translate";
export type TemplateVisitor<T extends t.Node> = Partial<
Record<VisitorType, t.VisitNode<unknown, T>>
>;

export function extractVisitors<
Visitors extends Partial<Record<keyof t.Visitor, TemplateVisitor<any>>>,
>(visitors: Visitors) {
const result: Record<VisitorType, t.Visitor> = {
migrate: {},
transform: {},
analyze: {},
translate: {},
};

for (const _name in visitors) {
const name = _name as any;
const value = visitors[name]!;
if (value.migrate) result.migrate[name] = value.migrate;
if (value.transform) result.transform[name] = value.transform;
if (value.analyze) result.analyze[name] = value.analyze;
if (value.translate) result.translate[name] = value.translate;
}
return result;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import { types as t } from "@marko/compiler";

import { isOutputDOM } from "../util/marko-config";
import { getSection } from "../util/sections";
import type { TemplateVisitor } from "../util/visitors";
import { currentProgramPath } from "./program";

export default {
translate: {
exit(assignment: t.NodePath<t.AssignmentExpression>) {
exit(assignment) {
if (isOutputDOM()) {
handleDestructure(assignment, assignment.node.left);
}
},
},
};
} satisfies TemplateVisitor<t.AssignmentExpression>;

function handleDestructure(
assignment: t.NodePath<t.AssignmentExpression>,
Expand Down
8 changes: 5 additions & 3 deletions packages/translator-tags/src/visitors/cdata.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { types as t } from "@marko/compiler";

import type { TemplateVisitor } from "../util/visitors";

export default {
translate: {
enter(path: t.NodePath<t.MarkoCDATA>) {
throw path.buildCodeFrameError(
enter(cdata) {
throw cdata.buildCodeFrameError(
"CDATA sections are not supported in Marko.",
);
},
},
};
} satisfies TemplateVisitor<t.MarkoCDATA>;
6 changes: 4 additions & 2 deletions packages/translator-tags/src/visitors/comment.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { types as t } from "@marko/compiler";

import type { TemplateVisitor } from "../util/visitors";

export default {
translate: {
exit(comment: t.NodePath<t.MarkoComment>) {
exit(comment) {
comment.remove();
},
},
};
} satisfies TemplateVisitor<t.MarkoComment>;
15 changes: 6 additions & 9 deletions packages/translator-tags/src/visitors/declaration.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import type { types as t } from "@marko/compiler";

import { isOutputHTML } from "../util/marko-config";
import * as writer from "../util/writer";
import type { TemplateVisitor } from "../util/visitors";

export default {
translate: {
exit(declaration: t.NodePath<t.MarkoDeclaration>) {
if (isOutputHTML()) {
writer.writeTo(declaration)`<?${declaration.node.value}?>`;
}

declaration.remove();
enter(decl) {
throw decl.buildCodeFrameError(
"XML declarations sections are not supported in Marko.",
);
},
},
};
} satisfies TemplateVisitor<t.MarkoDeclaration>;
5 changes: 3 additions & 2 deletions packages/translator-tags/src/visitors/document-type.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { types as t } from "@marko/compiler";

import { isOutputHTML } from "../util/marko-config";
import type { TemplateVisitor } from "../util/visitors";
import * as writer from "../util/writer";

export default {
translate: {
exit(documentType: t.NodePath<t.MarkoDocumentType>) {
exit(documentType) {
if (isOutputHTML()) {
writer.writeTo(documentType)`<!${documentType.node.value}>`;
}
documentType.remove();
},
},
};
} satisfies TemplateVisitor<t.MarkoDocumentType>;
7 changes: 4 additions & 3 deletions packages/translator-tags/src/visitors/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
type Section,
} from "../util/sections";
import { getSerializedScopeProperties } from "../util/signals";
import type { TemplateVisitor } from "../util/visitors";
import { currentProgramPath, scopeIdentifier } from "./program";
const functionIdsBySection = new WeakMap<Section, Map<string, number>>();
const registeredFunctions = new WeakSet<t.Function>();
Expand All @@ -36,7 +37,7 @@ declare module "@marko/compiler/dist/types" {
}

export default {
analyze(fn: t.NodePath<t.Function>) {
analyze(fn) {
const markoRoot = getMarkoRoot(fn);
const isStatic = !markoRoot || markoRoot.isMarkoScriptlet({ static: true });
if (!isFunction(fn, isStatic)) return;
Expand Down Expand Up @@ -103,7 +104,7 @@ export default {
);
},
translate: {
exit(fn: t.NodePath<t.Function>) {
exit(fn) {
const markoRoot = getMarkoRoot(fn);
const isStatic =
!markoRoot || markoRoot.isMarkoScriptlet({ static: true });
Expand Down Expand Up @@ -226,7 +227,7 @@ export default {
}
},
},
};
} satisfies TemplateVisitor<t.Function>;

function isFunction(
fn: t.NodePath<t.Node>,
Expand Down
8 changes: 5 additions & 3 deletions packages/translator-tags/src/visitors/import-declaration.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { resolveTagImport } from "@marko/babel-utils";
import type { types as t } from "@marko/compiler";

import type { TemplateVisitor } from "../util/visitors";

export default {
analyze(importDecl: t.NodePath<t.ImportDeclaration>) {
analyze(importDecl) {
const { node } = importDecl;
const { source } = node;
const { value } = source;
Expand All @@ -18,7 +20,7 @@ export default {
}
},
translate: {
exit(importDecl: t.NodePath<t.ImportDeclaration>) {
exit(importDecl) {
const { node } = importDecl;
const { extra } = node;
const tagImport = extra?.tagImport as string | undefined;
Expand All @@ -27,4 +29,4 @@ export default {
}
},
},
};
} satisfies TemplateVisitor<t.ImportDeclaration>;
7 changes: 4 additions & 3 deletions packages/translator-tags/src/visitors/placeholder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
getSection,
} from "../util/sections";
import { addStatement } from "../util/signals";
import type { TemplateVisitor } from "../util/visitors";
import * as walks from "../util/walks";
import * as writer from "../util/writer";
import { scopeIdentifier } from "./program";
Expand Down Expand Up @@ -46,7 +47,7 @@ type HTMLMethod = "escapeScript" | "escapeStyle" | "escapeXML" | "toString";
type DOMMethod = "html" | "data";

export default {
analyze(placeholder: t.NodePath<t.MarkoPlaceholder>) {
analyze(placeholder) {
const { node } = placeholder;
const { confident, computed } = evaluate(placeholder);

Expand All @@ -62,7 +63,7 @@ export default {
}
},
translate: {
exit(placeholder: t.NodePath<t.MarkoPlaceholder>) {
exit(placeholder) {
const isHTML = isOutputHTML();
const write = writer.writeTo(placeholder);
const { node } = placeholder;
Expand Down Expand Up @@ -132,7 +133,7 @@ export default {
placeholder.remove();
},
},
};
} satisfies TemplateVisitor<t.MarkoPlaceholder>;

function getParentTagName({ parentPath }: t.NodePath<t.MarkoPlaceholder>) {
return (
Expand Down
5 changes: 3 additions & 2 deletions packages/translator-tags/src/visitors/program/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import {
replaceAssignments,
writeSignals,
} from "../../util/signals";
import type { TemplateVisitor } from "../../util/visitors";
import { visit } from "../../util/walks";
import * as writer from "../../util/writer";

export default {
translate: {
exit(program: t.NodePath<t.Program>) {
exit(program) {
visit(program);
const section = getSection(program);
const { walks, writes, setup } = writer.getSectionMeta(section);
Expand Down Expand Up @@ -150,4 +151,4 @@ export default {
);
},
},
};
} satisfies TemplateVisitor<t.Program>;
5 changes: 3 additions & 2 deletions packages/translator-tags/src/visitors/program/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import isStatic from "../../util/is-static";
import { callRuntime } from "../../util/runtime";
import { getSection } from "../../util/sections";
import { renameBindings, writeHTMLResumeStatements } from "../../util/signals";
import type { TemplateVisitor } from "../../util/visitors";
import { flushInto } from "../../util/writer";

export default {
translate: {
exit(program: t.NodePath<t.Program>) {
exit(program) {
const section = getSection(program);
const tagVarIdentifier = program.scope.generateUidIdentifier("tagVar");

Expand Down Expand Up @@ -62,4 +63,4 @@ export default {
]);
},
},
};
} satisfies TemplateVisitor<t.Program>;
Loading

0 comments on commit 49f905f

Please sign in to comment.