Skip to content

Commit c891491

Browse files
Ensure JSDoc blocks for source are complete
All blocks are documented with a description, params and optional return (or throws) types
1 parent a44596f commit c891491

File tree

16 files changed

+259
-76
lines changed

16 files changed

+259
-76
lines changed

src/govuk/common/index.mjs

+17-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* @param {NodeListOf<Element>} nodes - NodeList from querySelectorAll()
1717
* @param {nodeListIterator} callback - Callback function to run for each node
18-
* @returns {undefined}
18+
* @returns {void}
1919
*/
2020
export function nodeListForEach (nodes, callback) {
2121
if (window.NodeList.prototype.forEach) {
@@ -52,7 +52,7 @@ export function generateUniqueID () {
5252
* (e.g. {'i18n.showSection': 'Show section'}) and combines them together, with
5353
* greatest priority on the LAST item passed in.
5454
*
55-
* @returns {object} A flattened object of key-value pairs.
55+
* @returns {Object<string, unknown>} A flattened object of key-value pairs.
5656
*/
5757
export function mergeConfigs (/* configObject1, configObject2, ...configObjects */) {
5858
/**
@@ -61,16 +61,21 @@ export function mergeConfigs (/* configObject1, configObject2, ...configObjects
6161
* each of our objects, nor transform our dataset from a flat list into a
6262
* nested object.
6363
*
64-
* @param {object} configObject - Deeply nested object
65-
* @returns {object} Flattened object with dot-separated keys
64+
* @param {Object<string, unknown>} configObject - Deeply nested object
65+
* @returns {Object<string, unknown>} Flattened object with dot-separated keys
6666
*/
6767
var flattenObject = function (configObject) {
6868
// Prepare an empty return object
6969
var flattenedObject = {}
7070

71-
// Our flattening function, this is called recursively for each level of
72-
// depth in the object. At each level we prepend the previous level names to
73-
// the key using `prefix`.
71+
/**
72+
* Our flattening function, this is called recursively for each level of
73+
* depth in the object. At each level we prepend the previous level names to
74+
* the key using `prefix`.
75+
*
76+
* @param {Partial<Object<string, unknown>>} obj - Object to flatten
77+
* @param {string} [prefix] - Optional dot-separated prefix
78+
*/
7479
var flattenLoop = function (obj, prefix) {
7580
// Loop through keys...
7681
for (var key in obj) {
@@ -118,9 +123,11 @@ export function mergeConfigs (/* configObject1, configObject2, ...configObjects
118123
* Extracts keys starting with a particular namespace from a flattened config
119124
* object, removing the namespace in the process.
120125
*
121-
* @param {object} configObject - The object to extract key-value pairs from.
126+
* @param {Object<string, unknown>} configObject - The object to extract key-value pairs from.
122127
* @param {string} namespace - The namespace to filter keys with.
123-
* @returns {object} Flattened object with dot-separated key namespace removed
128+
* @returns {Object<string, unknown>} Flattened object with dot-separated key namespace removed
129+
* @throws {Error} Config object required
130+
* @throws {Error} Namespace string required
124131
*/
125132
export function extractConfigByNamespace (configObject, namespace) {
126133
// Check we have what we need
@@ -155,5 +162,5 @@ export function extractConfigByNamespace (configObject, namespace) {
155162
* @param {Element} value - The current node being iterated on
156163
* @param {number} index - The current index in the iteration
157164
* @param {NodeListOf<Element>} nodes - NodeList from querySelectorAll()
158-
* @returns {undefined}
165+
* @returns {void}
159166
*/

src/govuk/common/normalise-dataset.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function normaliseString (value) {
4545
* Loop over an object and normalise each value using normaliseData function
4646
*
4747
* @param {DOMStringMap} dataset - HTML element dataset
48-
* @returns {Object<string, string | boolean | number | undefined>} Normalised dataset
48+
* @returns {Object<string, unknown>} Normalised dataset
4949
*/
5050
export function normaliseDataset (dataset) {
5151
var out = {}

src/govuk/components/accordion/accordion.mjs

+67-15
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ function Accordion ($module, config) {
7575
this.sectionContentClass = 'govuk-accordion__section-content'
7676
}
7777

78-
// Initialize component
78+
/**
79+
* Initialise component
80+
*/
7981
Accordion.prototype.init = function () {
8082
// Check for module
8183
if (!this.$module) {
@@ -90,7 +92,9 @@ Accordion.prototype.init = function () {
9092
this.updateShowAllButton(areAllSectionsOpen)
9193
}
9294

93-
// Initialise controls and set attributes
95+
/**
96+
* Initialise controls and set attributes
97+
*/
9498
Accordion.prototype.initControls = function () {
9599
// Create "Show all" button and set attributes
96100
this.$showAllButton = document.createElement('button')
@@ -123,7 +127,9 @@ Accordion.prototype.initControls = function () {
123127
}
124128
}
125129

126-
// Initialise section headers
130+
/**
131+
* Initialise section headers
132+
*/
127133
Accordion.prototype.initSectionHeaders = function () {
128134
// Loop through section headers
129135
nodeListForEach(this.$sections, function ($section, i) {
@@ -141,6 +147,12 @@ Accordion.prototype.initSectionHeaders = function () {
141147
}.bind(this))
142148
}
143149

150+
/**
151+
* Construct section header
152+
*
153+
* @param {HTMLDivElement} $headerWrapper - Section header wrapper
154+
* @param {number} index - Section index
155+
*/
144156
Accordion.prototype.constructHeaderMarkup = function ($headerWrapper, index) {
145157
var $span = $headerWrapper.querySelector('.' + this.sectionButtonClass)
146158
var $heading = $headerWrapper.querySelector('.' + this.sectionHeadingClass)
@@ -237,15 +249,23 @@ Accordion.prototype.constructHeaderMarkup = function ($headerWrapper, index) {
237249
$heading.appendChild($button)
238250
}
239251

240-
// When a section is opened by the user agent via the 'beforematch' event
252+
/**
253+
* When a section is opened by the user agent via the 'beforematch' event
254+
*
255+
* @param {Event} event - Generic event
256+
*/
241257
Accordion.prototype.onBeforeMatch = function (event) {
242258
var $section = event.target.closest('.' + this.sectionClass)
243259
if ($section) {
244260
this.setExpanded(true, $section)
245261
}
246262
}
247263

248-
// When section toggled, set and store state
264+
/**
265+
* When section toggled, set and store state
266+
*
267+
* @param {HTMLElement} $section - Section element
268+
*/
249269
Accordion.prototype.onSectionToggle = function ($section) {
250270
var expanded = this.isExpanded($section)
251271
this.setExpanded(!expanded, $section)
@@ -254,7 +274,9 @@ Accordion.prototype.onSectionToggle = function ($section) {
254274
this.storeState($section)
255275
}
256276

257-
// When Open/Close All toggled, set and store state
277+
/**
278+
* When Open/Close All toggled, set and store state
279+
*/
258280
Accordion.prototype.onShowOrHideAllToggle = function () {
259281
var $module = this
260282
var $sections = this.$sections
@@ -269,7 +291,12 @@ Accordion.prototype.onShowOrHideAllToggle = function () {
269291
$module.updateShowAllButton(nowExpanded)
270292
}
271293

272-
// Set section attributes when opened/closed
294+
/**
295+
* Set section attributes when opened/closed
296+
*
297+
* @param {boolean} expanded - Section expanded
298+
* @param {HTMLElement} $section - Section element
299+
*/
273300
Accordion.prototype.setExpanded = function (expanded, $section) {
274301
var $icon = $section.querySelector('.' + this.upChevronIconClass)
275302
var $showHideText = $section.querySelector('.' + this.sectionShowHideTextClass)
@@ -320,12 +347,21 @@ Accordion.prototype.setExpanded = function (expanded, $section) {
320347
this.updateShowAllButton(areAllSectionsOpen)
321348
}
322349

323-
// Get state of section
350+
/**
351+
* Get state of section
352+
*
353+
* @param {HTMLElement} $section - Section element
354+
* @returns {boolean} True if expanded
355+
*/
324356
Accordion.prototype.isExpanded = function ($section) {
325357
return $section.classList.contains(this.sectionExpandedClass)
326358
}
327359

328-
// Check if all sections are open
360+
/**
361+
* Check if all sections are open
362+
*
363+
* @returns {boolean} True if all sections are open
364+
*/
329365
Accordion.prototype.checkIfAllSectionsOpen = function () {
330366
// Get a count of all the Accordion sections
331367
var sectionsCount = this.$sections.length
@@ -336,7 +372,11 @@ Accordion.prototype.checkIfAllSectionsOpen = function () {
336372
return areAllSectionsOpen
337373
}
338374

339-
// Update "Show all sections" button
375+
/**
376+
* Update "Show all sections" button
377+
*
378+
* @param {boolean} expanded - Section expanded
379+
*/
340380
Accordion.prototype.updateShowAllButton = function (expanded) {
341381
var $showAllIcon = this.$showAllButton.querySelector('.' + this.upChevronIconClass)
342382
var $showAllText = this.$showAllButton.querySelector('.' + this.showAllTextClass)
@@ -354,8 +394,12 @@ Accordion.prototype.updateShowAllButton = function (expanded) {
354394
}
355395
}
356396

357-
// Check for `window.sessionStorage`, and that it actually works.
358397
var helper = {
398+
/**
399+
* Check for `window.sessionStorage`, and that it actually works.
400+
*
401+
* @returns {boolean} True if session storage is available
402+
*/
359403
checkForSessionStorage: function () {
360404
var testString = 'this is the test string'
361405
var result
@@ -370,7 +414,11 @@ var helper = {
370414
}
371415
}
372416

373-
// Set the state of the accordions in sessionStorage
417+
/**
418+
* Set the state of the accordions in sessionStorage
419+
*
420+
* @param {HTMLElement} $section - Section element
421+
*/
374422
Accordion.prototype.storeState = function ($section) {
375423
if (this.browserSupportsSessionStorage) {
376424
// We need a unique way of identifying each content in the Accordion. Since
@@ -390,7 +438,11 @@ Accordion.prototype.storeState = function ($section) {
390438
}
391439
}
392440

393-
// Read the state of the accordions from sessionStorage
441+
/**
442+
* Read the state of the accordions from sessionStorage
443+
*
444+
* @param {HTMLElement} $section - Section element
445+
*/
394446
Accordion.prototype.setInitialState = function ($section) {
395447
if (this.browserSupportsSessionStorage) {
396448
var $button = $section.querySelector('.' + this.sectionButtonClass)
@@ -409,11 +461,11 @@ Accordion.prototype.setInitialState = function ($section) {
409461
/**
410462
* Create an element to improve semantics of the section button with punctuation
411463
*
412-
* @returns {HTMLSpanElement} DOM element
413-
*
414464
* Adding punctuation to the button can also improve its general semantics by dividing its contents
415465
* into thematic chunks.
416466
* See https://github.com/alphagov/govuk-frontend/issues/2327#issuecomment-922957442
467+
*
468+
* @returns {HTMLElement} DOM element
417469
*/
418470
Accordion.prototype.getButtonPunctuationEl = function () {
419471
var $punctuationEl = document.createElement('span')

src/govuk/components/button/button.mjs

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ var DEBOUNCE_TIMEOUT_IN_SECONDS = 1
1010
* JavaScript enhancements for the Button component
1111
*
1212
* @class
13-
* @param {HTMLElement} $module - The element this component controls
14-
* @param {ButtonConfig} config - Button config
13+
* @param {HTMLElement} $module - HTML element to use for button
14+
* @param {ButtonConfig} [config] - Button config
1515
*/
1616
function Button ($module, config) {
1717
if (!$module) {
@@ -51,7 +51,7 @@ Button.prototype.init = function () {
5151
*
5252
* See https://github.com/alphagov/govuk_elements/pull/272#issuecomment-233028270
5353
*
54-
* @param {KeyboardEvent} event
54+
* @param {KeyboardEvent} event - Keydown event
5555
*/
5656
Button.prototype.handleKeyDown = function (event) {
5757
var target = event.target
@@ -69,8 +69,8 @@ Button.prototype.handleKeyDown = function (event) {
6969
* stops people accidentally causing multiple form submissions by double
7070
* clicking buttons.
7171
*
72-
* @param {MouseEvent} event
73-
* @returns {undefined | false} - Returns undefined, or false when debounced
72+
* @param {MouseEvent} event - Mouse click event
73+
* @returns {undefined | false} Returns undefined, or false when debounced
7474
*/
7575
Button.prototype.debounce = function (event) {
7676
// Check the button that was clicked has preventDoubleClick enabled

src/govuk/components/button/button.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('/components/button', () => {
6161
* Examples don't do this and we need it to have something to submit
6262
*
6363
* @param {import('puppeteer').Page} page - Puppeteer page object
64-
* @returns {undefined}
64+
* @returns {Promise<undefined>}
6565
*/
6666
function trackClicks (page) {
6767
return page.evaluate(() => {
@@ -82,6 +82,7 @@ describe('/components/button', () => {
8282
/**
8383
* Gets the number of times the form was submitted
8484
*
85+
* @param {import('puppeteer').Page} page - Puppeteer page object
8586
* @returns {number} Number of times the form was submitted
8687
*/
8788
function getClicksCount (page) {

src/govuk/components/character-count/character-count.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var CHARACTER_COUNT_TRANSLATIONS = {
5050
* of the available characters/words has been entered.
5151
*
5252
* @class
53-
* @param {HTMLElement} $module - The element this component controls
53+
* @param {HTMLElement} $module - HTML element to use for character count
5454
* @param {CharacterCountConfig} [config] - Character count config
5555
*/
5656
function CharacterCount ($module, config) {

src/govuk/components/checkboxes/checkboxes.mjs

+8-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function Checkboxes ($module) {
1515
}
1616

1717
/**
18-
* Initialise Checkboxes
18+
* Initialise component
1919
*
2020
* Checkboxes can be associated with a 'conditionally revealed' content block –
2121
* for example, a checkbox for 'Phone' could reveal an additional form field for
@@ -62,11 +62,12 @@ Checkboxes.prototype.init = function () {
6262
// for example if they are added to the page dynamically, so sync now too.
6363
this.syncAllConditionalReveals()
6464

65+
// Handle events
6566
$module.addEventListener('click', this.handleClick.bind(this))
6667
}
6768

6869
/**
69-
* Sync the conditional reveal states for all inputs in this $module.
70+
* Sync the conditional reveal states for all checkboxes in this $module.
7071
*/
7172
Checkboxes.prototype.syncAllConditionalReveals = function () {
7273
nodeListForEach(this.$inputs, this.syncConditionalRevealWithInputState.bind(this))
@@ -96,6 +97,8 @@ Checkboxes.prototype.syncConditionalRevealWithInputState = function ($input) {
9697
*
9798
* Find any other checkbox inputs with the same name value, and uncheck them.
9899
* This is useful for when a “None of these" checkbox is checked.
100+
*
101+
* @param {HTMLElement} $input - Checkbox input
99102
*/
100103
Checkboxes.prototype.unCheckAllInputsExcept = function ($input) {
101104
var allInputsWithSameName = document.querySelectorAll('input[type="checkbox"][name="' + $input.name + '"]')
@@ -110,11 +113,13 @@ Checkboxes.prototype.unCheckAllInputsExcept = function ($input) {
110113
}
111114

112115
/**
113-
* Uncheck exclusive inputs
116+
* Uncheck exclusive checkboxes
114117
*
115118
* Find any checkbox inputs with the same name value and the 'exclusive' behaviour,
116119
* and uncheck them. This helps prevent someone checking both a regular checkbox and a
117120
* "None of these" checkbox in the same fieldset.
121+
*
122+
* @param {HTMLInputElement} $input - Checkbox input
118123
*/
119124
Checkboxes.prototype.unCheckExclusiveInputs = function ($input) {
120125
var allInputsWithSameNameAndExclusiveBehaviour = document.querySelectorAll(

src/govuk/components/details/details.mjs

+8-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ function Details ($module) {
2121
this.$module = $module
2222
}
2323

24+
/**
25+
* Initialise component
26+
*/
2427
Details.prototype.init = function () {
2528
if (!this.$module) {
2629
return
@@ -36,6 +39,9 @@ Details.prototype.init = function () {
3639
this.polyfillDetails()
3740
}
3841

42+
/**
43+
* Polyfill component in older browsers
44+
*/
3945
Details.prototype.polyfillDetails = function () {
4046
var $module = this.$module
4147

@@ -144,6 +150,6 @@ export default Details
144150

145151
/**
146152
* @callback polyfillHandleInputsCallback
147-
* @param {KeyboardEvent} event - Keyboard event
148-
* @returns {undefined}
153+
* @param {UIEvent} event - Keyboard or mouse event
154+
* @returns {void}
149155
*/

0 commit comments

Comments
 (0)