From 9aa242918dad4d2ce956f2cbb3f70ead734c4a24 Mon Sep 17 00:00:00 2001 From: Nachiket Pusalkar <33430835+nachiket87@users.noreply.github.com> Date: Tue, 7 Nov 2023 14:52:09 -0500 Subject: [PATCH 1/2] fix duplicate controllers in manifest on update --- lib/stimulus/manifest.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/stimulus/manifest.rb b/lib/stimulus/manifest.rb index 3f8d6ec..d944d20 100644 --- a/lib/stimulus/manifest.rb +++ b/lib/stimulus/manifest.rb @@ -2,9 +2,11 @@ module Stimulus::Manifest extend self def generate_from(controllers_path) - extract_controllers_from(controllers_path).collect do |controller_path| + manifest = extract_controllers_from(controllers_path).collect do |controller_path| import_and_register_controller(controllers_path, controller_path) end + + manifest.uniq end def import_and_register_controller(controllers_path, controller_path) From c55a28b7bbdcf45a0443bf97d258a31920b1f8ab Mon Sep 17 00:00:00 2001 From: Nachiket Pusalkar <33430835+nachiket87@users.noreply.github.com> Date: Thu, 9 Nov 2023 11:59:29 -0500 Subject: [PATCH 2/2] add test to verify duplicate controller are not registered --- test/fixtures/controllers/hello_controller.ts | 15 +++++++++++++++ test/manifest_test.rb | 3 +++ 2 files changed, 18 insertions(+) create mode 100644 test/fixtures/controllers/hello_controller.ts diff --git a/test/fixtures/controllers/hello_controller.ts b/test/fixtures/controllers/hello_controller.ts new file mode 100644 index 0000000..9ae5f32 --- /dev/null +++ b/test/fixtures/controllers/hello_controller.ts @@ -0,0 +1,15 @@ +// hello_controller.ts +import { Controller } from "stimulus"; + +export default class extends Controller { + static targets = ["name", "output"]; + + declare readonly nameTarget: HTMLInputElement; + declare readonly outputTarget: HTMLElement; + + greet() { + this.outputTarget.textContent = + `Hello, ${this.nameTarget.value}!` + } + +} diff --git a/test/manifest_test.rb b/test/manifest_test.rb index eb9de3a..f5ef21a 100644 --- a/test/manifest_test.rb +++ b/test/manifest_test.rb @@ -9,6 +9,9 @@ class Stimulus::Manifest::Test < ActiveSupport::TestCase assert_includes manifest, 'import HelloController from "./hello_controller"' assert_includes manifest, 'application.register("hello", HelloController)' + assert_equal 1, manifest.scan('import HelloController from "./hello_controller"').length + assert_equal 1, manifest.scan('application.register("hello", HelloController').length + # CoffeeScript controller assert_includes manifest, 'import CoffeeController from "./coffee_controller"' assert_includes manifest, 'application.register("coffee", CoffeeController)'