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

WIP: feat: adds frag snippet #738

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions src/markup/format/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ function element(node: AbbreviationNode, index: number, items: AbbreviationNode[
pushString(out, `</${name}>`);
commentNodeAfter(node, state);
}
} else if (config.options['jsx.enabled'] && !node.name) {
// Handle JSX fragment
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That defineitely should be tweaked, but Im not sure yet if its a valid way to handle jsx fragment in general

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, nodes without name could be a (group), e.g. (ul>.item*4)+footer

pushString(out, '<>');
if (!pushSnippet(node, state, next) && node.value) {
pushTokens(node.value, state);
node.children.forEach(next);
}
pushString(out, '</>');
} else if (!pushSnippet(node, state, next) && node.value) {
// A text-only node (snippet)
pushTokens(node.value, state);
Expand Down
3 changes: 2 additions & 1 deletion src/snippets/html.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,6 @@

"c": "{<!-- ${0} -->}",
"cc:ie": "{<!--[if IE]>${0}<![endif]-->}",
"cc:noie": "{<!--[if !IE]><!-->${0}<!--<![endif]-->}"
"cc:noie": "{<!--[if !IE]><!-->${0}<!--<![endif]-->}",
"frag": "{<>${0}</>}"
}
57 changes: 57 additions & 0 deletions test/fragment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, it } from "node:test";
import { strictEqual as equal } from "node:assert";
import expand, { UserConfig } from "../src";

describe("frag", () => {
const config: UserConfig = { options: { "jsx.enabled": true } };

it("should expand fragment", () => {
// should it work without explicit jsx.enabled ? It's not a valid html tag afaik
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the question i had. Should the result actually be expanded to <></> ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it’s OK to expand to <>\n\t${0}\n</> since its use case is to insert multiple nodes which will be formatted anyways

equal(expand("frag"), "<></>");
// invalid result, adds one more fragment
equal(expand("frag", config), "<></>");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: actual result is invalid (<><></></>)

});

it("should wrap any content with fragment", () => {
equal(
expand("frag", { ...config, text: "frag test" }),
"<>frag test</>"
);
});

it("should preserve formatting", () => {
equal(
expand("frag", {
...config,
text: "test\n\ttext\twith\nsome\t\tformatting",
}),
"<>test\n\ttext\twith\nsome\t\tformatting</>"
);
});

it("should wrap jsx with fragment", () => {
equal(
expand("frag", { ...config, text: "<span>foo</span>" }),
"<><span>foo</span></>"
);
equal(
expand("frag", { ...config, text: "foo<div>foo</div>" }),
"<>foo<div>foo</div></>"
);
});

it("should wrap fragment with fragment", () => {
equal(
expand("frag", { ...config, text: "<>nested</>" }),
"<><>nested</></>"
);
});

it("should wrap snippets with fragment", () => {
// invalid result, adds one more fragment at start
equal(
expand("frag>ul>li*4", config),
"<><ul>\n\t<li></li>\n\t<li></li>\n\t<li></li>\n\t<li></li>\n</ul></>"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: actual result is invalid (<><></><ul>\n\t<li></li>\n\t<li></li>\n\t<li></li>\n\t<li></li>\n</ul></>)

);
});
});