Skip to content

Commit

Permalink
Improve JSDoc checks with inline @type
Browse files Browse the repository at this point in the history
Where empty objects are initially assigned (or merged) the compiler needs to know the final “shape” up front

In editors and IDEs this also helps populate tooltip messages during loops etc
  • Loading branch information
colinrotherham committed Feb 10, 2023
1 parent 7565ff3 commit 0d8c0d6
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/govuk/common/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export function mergeConfigs (/* configObject1, configObject2, ...configObjects
*/
var flattenObject = function (configObject) {
// Prepare an empty return object
/** @type {Object<string, unknown>} */
var flattenedObject = {}

/**
Expand Down Expand Up @@ -103,6 +104,7 @@ export function mergeConfigs (/* configObject1, configObject2, ...configObjects
}

// Start with an empty object as our base
/** @type {Object<string, unknown>} */
var formattedConfigObject = {}

// Loop through each of the remaining passed objects and push their keys
Expand Down Expand Up @@ -140,6 +142,7 @@ export function extractConfigByNamespace (configObject, namespace) {
throw new Error('Provide a `namespace` of type "string" to filter the `configObject` by.')
}

/** @type {Object<string, unknown>} */
var newObject = {}

for (var key in configObject) {
Expand Down
1 change: 1 addition & 0 deletions src/govuk/common/normalise-dataset.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function normaliseString (value) {
* @returns {Object<string, unknown>} Normalised dataset
*/
export function normaliseDataset (dataset) {
/** @type {Object<string, unknown>} */
var out = {}

for (var key in dataset) {
Expand Down
1 change: 1 addition & 0 deletions src/govuk/components/accordion/accordion.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function Accordion ($module, config) {
i18n: ACCORDION_TRANSLATIONS
}

/** @type {AccordionConfig} */
this.config = mergeConfigs(
defaultConfig,
config || {},
Expand Down
1 change: 1 addition & 0 deletions src/govuk/components/button/button.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function Button ($module, config) {
preventDoubleClick: false
}

/** @type {ButtonConfig} */
this.config = mergeConfigs(
defaultConfig,
config || {},
Expand Down
1 change: 1 addition & 0 deletions src/govuk/components/character-count/character-count.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ function CharacterCount ($module, config) {
}
}

/** @type {CharacterCountConfig} */
this.config = mergeConfigs(
defaultConfig,
config || {},
Expand Down
2 changes: 2 additions & 0 deletions src/govuk/components/checkboxes/checkboxes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Checkboxes.prototype.syncConditionalRevealWithInputState = function ($input) {
Checkboxes.prototype.unCheckAllInputsExcept = function ($input) {
var $component = this

/** @type {NodeListOf<HTMLInputElement>} */
var allInputsWithSameName = document.querySelectorAll(
'input[type="checkbox"][name="' + $input.name + '"]'
)
Expand All @@ -147,6 +148,7 @@ Checkboxes.prototype.unCheckAllInputsExcept = function ($input) {
Checkboxes.prototype.unCheckExclusiveInputs = function ($input) {
var $component = this

/** @type {NodeListOf<HTMLInputElement>} */
var allInputsWithSameNameAndExclusiveBehaviour = document.querySelectorAll(
'input[data-behaviour="exclusive"][type="checkbox"][name="' + $input.name + '"]'
)
Expand Down
1 change: 1 addition & 0 deletions src/govuk/components/error-summary/error-summary.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function ErrorSummary ($module, config) {
disableAutoFocus: false
}

/** @type {ErrorSummaryConfig} */
this.config = mergeConfigs(
defaultConfig,
config || {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function NotificationBanner ($module, config) {
disableAutoFocus: false
}

/** @type {NotificationBannerConfig} */
this.config = mergeConfigs(
defaultConfig,
config || {},
Expand Down
1 change: 1 addition & 0 deletions src/govuk/i18n.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ I18n.prototype.t = function (lookupKey, options) {
* @returns {string} The translation string to output, with ${} placeholders replaced
*/
I18n.prototype.replacePlaceholders = function (translationString, options) {
/** @type {Intl.NumberFormat | undefined} */
var formatter

if (this.hasIntlNumberFormatSupport()) {
Expand Down

0 comments on commit 0d8c0d6

Please sign in to comment.