Skip to content

Commit

Permalink
fix(core): Fix macro handling
Browse files Browse the repository at this point in the history
  • Loading branch information
larowlan committed Nov 13, 2023
1 parent b06e6f7 commit 7bdd989
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const includeTokenTypes = [
"Twig.logic.type.embed",
"Twig.logic.type.include",
"Twig.logic.type.extends",
"Twig.logic.type.import",
]

const pluckIncludes = (tokens) => {
Expand Down Expand Up @@ -136,6 +137,7 @@ const plugin = (options = {}) => {
}
includes.forEach(processIncludes)
embed = includes
.filter((template) => template !== "_self")
.map(
(template) =>
`import '${resolve(
Expand Down
17 changes: 17 additions & 0 deletions tests/__snapshots__/smoke.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ exports[`Basic smoke test > Should support includes 1`] = `
"
`;
exports[`Basic smoke test > Should support macros 1`] = `
"<ul>
<li><a href=\\"/home\\">Home</a>
</li>
<li><a href=\\"/about\\">About</a>
<ul>
<li><a href=\\"/contact\\">Contact</a>
</li>
</ul>
</li>
</ul>
"
`;
exports[`Basic smoke test > Should support variables 1`] = `
"<section>
<h1>Include</h1>
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/macro.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% macro menu_link(title, href, children = []) %}
{% import _self as navigation %}
<li><a href="{{ href }}">{{ title }}</a>
{% if children|length %}
<ul>
{% for child in children %}
{{ navigation.menu_link(child.title, child.href) }}
{% endfor %}
</ul>
{% endif %}
</li>
{% endmacro %}
7 changes: 7 additions & 0 deletions tests/fixtures/menu.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% import "@tests/macro.twig" as navigation %}
<ul>
{{ navigation.menu_link('Home', '/home') }}
{{ navigation.menu_link('About', '/about', [
{ title: 'Contact', href: '/contact'}
]) }}
</ul>
6 changes: 6 additions & 0 deletions tests/smoke.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Markup from "../dist/test.js"
import Error from "../dist/error.js"
import ErrorInclude from "../dist/errorInclude.js"
import Menu from "../dist/menu.js"
import { describe, expect, it } from "vitest"

describe("Basic smoke test", () => {
Expand All @@ -22,4 +23,9 @@ describe("Basic smoke test", () => {
const error = ErrorInclude()
expect(error).toContain("An error occurred")
})
it("Should support macros", () => {
const markup = Menu()
expect(markup).toContain("Contact")
expect(markup).toMatchSnapshot()
})
})
1 change: 1 addition & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default defineConfig({
entry: {
test: resolve(__dirname, "tests/fixtures/mockup.twig"),
error: resolve(__dirname, "tests/fixtures/error.twig"),
menu: resolve(__dirname, "tests/fixtures/menu.twig"),
errorInclude: resolve(__dirname, "tests/fixtures/error-include.twig"),
},
name: "vite-plugin-twig-drupal",
Expand Down

0 comments on commit 7bdd989

Please sign in to comment.