@@ -50,25 +50,47 @@ function initAll(config) {
50
50
const $scope = config . scope ?? document
51
51
52
52
components . forEach ( ( [ Component , config ] ) => {
53
- const $elements = $scope . querySelectorAll (
54
- `[data-module="${ Component . moduleName } "]`
55
- )
53
+ createAll ( Component , config , $scope )
54
+ } )
55
+ }
56
+
57
+ /**
58
+ * @template {CompatibleClass} T
59
+ * @param {T } Component - class of the component to create
60
+ * @param {T["defaults"] } [config] - config for the component
61
+ * @param {Element|Document } [$scope] - scope of the document to search within
62
+ */
63
+ function createAll ( Component , config , $scope = document ) {
64
+ const $elements = $scope . querySelectorAll (
65
+ `[data-module="${ Component . moduleName } "]`
66
+ )
56
67
57
- $elements . forEach ( ( $element ) => {
58
- try {
59
- // Only pass config to components that accept it
60
- 'defaults' in Component
61
- ? new Component ( $element , config )
62
- : new Component ( $element )
63
- } catch ( error ) {
64
- console . log ( error )
65
- }
66
- } )
68
+ $elements . forEach ( ( $element ) => {
69
+ try {
70
+ // Only pass config to components that accept it
71
+ 'defaults' in Component
72
+ ? new Component ( $element , config )
73
+ : new Component ( $element )
74
+ } catch ( error ) {
75
+ console . log ( error )
76
+ }
67
77
} )
68
78
}
69
79
70
80
export { initAll }
71
81
82
+ /* eslint-disable jsdoc/valid-types --
83
+ * `{new(...args: any[] ): object}` is not recognised as valid
84
+ * https://github.com/gajus/eslint-plugin-jsdoc/issues/145#issuecomment-1308722878
85
+ * https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/131
86
+ **/
87
+
88
+ /**
89
+ * @typedef {{new (...args: any[]): unknown, defaults?: object, moduleName: string} } CompatibleClass
90
+ */
91
+
92
+ /* eslint-enable jsdoc/valid-types */
93
+
72
94
/**
73
95
* Config for all components via `initAll()`
74
96
*
0 commit comments