From 7bdd989c67eea2c35686be0288c3b93b62555f47 Mon Sep 17 00:00:00 2001 From: Lee Rowlands Date: Tue, 14 Nov 2023 08:42:04 +1000 Subject: [PATCH] fix(core): Fix macro handling --- src/index.js | 2 ++ tests/__snapshots__/smoke.test.js.snap | 17 +++++++++++++++++ tests/fixtures/macro.twig | 12 ++++++++++++ tests/fixtures/menu.twig | 7 +++++++ tests/smoke.test.js | 6 ++++++ vite.config.js | 1 + 6 files changed, 45 insertions(+) create mode 100644 tests/fixtures/macro.twig create mode 100644 tests/fixtures/menu.twig diff --git a/src/index.js b/src/index.js index 85d8664..9416378 100644 --- a/src/index.js +++ b/src/index.js @@ -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) => { @@ -136,6 +137,7 @@ const plugin = (options = {}) => { } includes.forEach(processIncludes) embed = includes + .filter((template) => template !== "_self") .map( (template) => `import '${resolve( diff --git a/tests/__snapshots__/smoke.test.js.snap b/tests/__snapshots__/smoke.test.js.snap index eb0d62a..f4b7f4e 100644 --- a/tests/__snapshots__/smoke.test.js.snap +++ b/tests/__snapshots__/smoke.test.js.snap @@ -23,6 +23,23 @@ exports[`Basic smoke test > Should support includes 1`] = ` " `; +exports[`Basic smoke test > Should support macros 1`] = ` +" +" +`; + exports[`Basic smoke test > Should support variables 1`] = ` "

Include

diff --git a/tests/fixtures/macro.twig b/tests/fixtures/macro.twig new file mode 100644 index 0000000..674a8cd --- /dev/null +++ b/tests/fixtures/macro.twig @@ -0,0 +1,12 @@ +{% macro menu_link(title, href, children = []) %} + {% import _self as navigation %} +
  • {{ title }} + {% if children|length %} +
      + {% for child in children %} + {{ navigation.menu_link(child.title, child.href) }} + {% endfor %} +
    + {% endif %} +
  • +{% endmacro %} diff --git a/tests/fixtures/menu.twig b/tests/fixtures/menu.twig new file mode 100644 index 0000000..6fad467 --- /dev/null +++ b/tests/fixtures/menu.twig @@ -0,0 +1,7 @@ +{% import "@tests/macro.twig" as navigation %} + diff --git a/tests/smoke.test.js b/tests/smoke.test.js index c8dd0c0..ce6f7c7 100644 --- a/tests/smoke.test.js +++ b/tests/smoke.test.js @@ -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", () => { @@ -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() + }) }) diff --git a/vite.config.js b/vite.config.js index 36fc877..a6fa004 100644 --- a/vite.config.js +++ b/vite.config.js @@ -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",