Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc cleanup #2332

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brown-glasses-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"marko": patch
---

Fix incorrect AttrTag tag type definition.
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).
6 changes: 6 additions & 0 deletions .changeset/young-mails-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@marko/translator-default": patch
"marko": patch
---

Remove extraneous runtime helper.
2 changes: 1 addition & 1 deletion packages/marko/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ declare global {
}

export type AttrTag<T> = T & {
[Symbol.iterator](): T;
[Symbol.iterator](): Iterator<T>;
};

/**
Expand Down
12 changes: 0 additions & 12 deletions packages/marko/src/runtime/helpers/repeatable.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/translator-default/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,6 @@ export function getRuntimeEntryFiles(output, optimize) {
`${base}runtime/helpers/dynamic-tag.js`,
`${base}runtime/helpers/attr-tag.js`,
`${base}runtime/helpers/merge.js`,
`${base}runtime/helpers/repeatable.js`,
`${base}runtime/helpers/render-tag.js`,
`${base}runtime/helpers/style-value.js`,
`${base}runtime/helpers/to-string.js`,
Expand Down
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
23 changes: 16 additions & 7 deletions packages/translator-tags/src/util/optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class Sorted<T> {

if (b) {
if (Array.isArray(b)) {
return addSorted(this.compare, b, a) as Many<T>;
return addSorted(this.compare, [...b], a) as Many<T>;
}

return joinRepeatable(this.compare, b, a);
Expand Down Expand Up @@ -199,14 +199,23 @@ function unionSortedRepeatable<T>(
let aIndex = 0;
let bIndex = 0;

// Since both sides are Repeated<T> we can safely assume that the first 2 elements are present in either array.
const result: Many<T> = [
compare(a[aIndex], b[bIndex]) <= 0 ? a[aIndex++] : b[bIndex++],
compare(a[aIndex], b[bIndex]) <= 0 ? a[aIndex++] : b[bIndex++],
];
const result = [] as unknown as Many<T>;

while (aIndex < aLen && bIndex < bLen) {
result.push(compare(a[aIndex], b[bIndex]) <= 0 ? a[aIndex++] : b[bIndex++]);
const aValue = a[aIndex];
const bValue = b[bIndex];
const delta = compare(aValue, bValue);
if (delta === 0) {
aIndex++;
bIndex++;
result.push(aValue);
} else if (delta < 0) {
aIndex++;
result.push(aValue);
} else {
bIndex++;
result.push(bValue);
}
}

if (aLen === bLen && aIndex === aLen) {
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
Loading