Skip to content

Commit

Permalink
refactor: move node deletion to end of translate phase when possible (#…
Browse files Browse the repository at this point in the history
…2088)

* refactor: move node deletion to end of translate phase when possible

* refactor: move _everything_ to end when possible
  • Loading branch information
LuLaValva authored Jan 23, 2024
1 parent e2e3fbd commit 59e73eb
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 54 deletions.
13 changes: 7 additions & 6 deletions packages/translator-tags/src/visitors/cdata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { isOutputHTML } from "../util/marko-config";
import * as writer from "../util/writer";

export default {
translate(cdata: t.NodePath<t.MarkoCDATA>) {
if (isOutputHTML()) {
writer.writeTo(cdata)`<![CDATA[${cdata.node.value}]]>`;
}

cdata.remove();
translate: {
exit(cdata: t.NodePath<t.MarkoCDATA>) {
if (isOutputHTML()) {
writer.writeTo(cdata)`<![CDATA[${cdata.node.value}]]>`;
}
cdata.remove();
},
},
};
16 changes: 9 additions & 7 deletions packages/translator-tags/src/visitors/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import * as writer from "../util/writer";
const ieConditionalCommentRegExp = /^\[if |<!\[endif\]$/;

export default {
translate(comment: t.NodePath<t.MarkoComment>) {
if (isOutputHTML()) {
const { value } = comment.node;
translate: {
exit(comment: t.NodePath<t.MarkoComment>) {
if (isOutputHTML()) {
const { value } = comment.node;

if (ieConditionalCommentRegExp.test(value)) {
writer.writeTo(comment)`<!--${value}-->`;
if (ieConditionalCommentRegExp.test(value)) {
writer.writeTo(comment)`<!--${value}-->`;
}
}
}

comment.remove();
comment.remove();
},
},
};
12 changes: 7 additions & 5 deletions packages/translator-tags/src/visitors/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { isOutputHTML } from "../util/marko-config";
import * as writer from "../util/writer";

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

declaration.remove();
declaration.remove();
},
},
};
13 changes: 7 additions & 6 deletions packages/translator-tags/src/visitors/document-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import { isOutputHTML } from "../util/marko-config";
import * as writer from "../util/writer";

export default {
translate(documentType: t.NodePath<t.MarkoDocumentType>) {
if (isOutputHTML()) {
writer.writeTo(documentType)`<!${documentType.node.value}>`;
}

documentType.remove();
translate: {
exit(documentType: t.NodePath<t.MarkoDocumentType>) {
if (isOutputHTML()) {
writer.writeTo(documentType)`<!${documentType.node.value}>`;
}
documentType.remove();
},
},
};
30 changes: 16 additions & 14 deletions packages/translator-tags/src/visitors/scriptlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@ import { getSection } from "../util/sections";
import { addStatement } from "../util/signals";

export default {
translate(scriptlet: t.NodePath<t.MarkoScriptlet>) {
if (isOutputHTML()) {
if (scriptlet.node.static) return; // handled in program exit for html currently.
scriptlet.replaceWithMultiple(scriptlet.node.body);
} else {
if (scriptlet.node.static) {
translate: {
exit(scriptlet: t.NodePath<t.MarkoScriptlet>) {
if (isOutputHTML()) {
if (scriptlet.node.static) return; // handled in program exit for html currently.
scriptlet.replaceWithMultiple(scriptlet.node.body);
} else {
addStatement(
"render",
getSection(scriptlet),
scriptlet.node.extra?.bodyReferences as References,
scriptlet.node.body,
);
scriptlet.remove();
if (scriptlet.node.static) {
scriptlet.replaceWithMultiple(scriptlet.node.body);
} else {
addStatement(
"render",
getSection(scriptlet),
scriptlet.node.extra?.bodyReferences as References,
scriptlet.node.body,
);
scriptlet.remove();
}
}
}
},
},
};
35 changes: 19 additions & 16 deletions packages/translator-tags/src/visitors/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@ import * as walks from "../util/walks";
import * as writer from "../util/writer";

export default {
translate(text: t.NodePath<t.MarkoText>) {
const followingSiblings = (text.container as t.Statement[]).slice(
(text.key as number) + 1,
);
let needsSeparator = false;
if (isOutputHTML()) {
for (const sibling of followingSiblings) {
if (t.isMarkoPlaceholder(sibling)) {
needsSeparator = true;
break;
} else if (t.isMarkoTag(sibling) || t.isMarkoText(sibling)) {
break;
translate: {
exit(text: t.NodePath<t.MarkoText>) {
const followingSiblings = (text.container as t.Statement[]).slice(
(text.key as number) + 1,
);
let needsSeparator = false;
if (isOutputHTML()) {
for (const sibling of followingSiblings) {
if (t.isMarkoPlaceholder(sibling)) {
needsSeparator = true;
break;
} else if (t.isMarkoTag(sibling) || t.isMarkoText(sibling)) {
break;
}
}
}
}
writer.writeTo(text)`${text.node.value}${needsSeparator ? "<!>" : ""}`;
walks.enterShallow(text);
text.remove();
writer.writeTo(text)`${text.node.value}${needsSeparator ? "<!>" : ""}`;
walks.enterShallow(text);

text.remove();
},
},
};

0 comments on commit 59e73eb

Please sign in to comment.