Skip to content

Commit

Permalink
Use TypeScript check for the Accordion's defaults immutability
Browse files Browse the repository at this point in the history
`Object.freeze` only does a shallow freeze of the object, meaning we have nothing checking our code
against something setting one of the keys of `i18n`.

The `ReadonlyDeep` type from `type-fest` allows TypeScript to check that even deep properties cannot be updated by our code.
  • Loading branch information
romaricpascal committed Nov 11, 2024
1 parent 9189857 commit 3042948
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
23 changes: 19 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"stylelint": "^16.9.0",
"stylelint-config-gds": "^2.0.0",
"stylelint-order": "^6.0.4",
"type-fest": "^4.26.1",
"typescript": "^5.5.4"
},
"optionalDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,9 @@ export class Accordion extends GOVUKFrontendComponentConfigurable {
*
* @see {@link AccordionConfig}
* @constant
* @type {AccordionConfig}
* @type {ReadonlyDeep<AccordionConfig>}
*/
static defaults = Object.freeze({
static defaults = {
i18n: {
hideAllSections: 'Hide all sections',
hideSection: 'Hide',
Expand All @@ -589,7 +589,7 @@ export class Accordion extends GOVUKFrontendComponentConfigurable {
showSectionAriaLabel: 'Show this section'
},
rememberExpanded: true
})
}

/**
* Accordion config schema
Expand All @@ -605,6 +605,11 @@ export class Accordion extends GOVUKFrontendComponentConfigurable {
})
}

Accordion.defaults.rememberExpanded = false

Check failure on line 608 in packages/govuk-frontend/src/govuk/components/accordion/accordion.mjs

View workflow job for this annotation

GitHub Actions / TypeScript compiler (windows-latest)

Cannot assign to 'rememberExpanded' because it is a read-only property.

Check failure on line 608 in packages/govuk-frontend/src/govuk/components/accordion/accordion.mjs

View workflow job for this annotation

GitHub Actions / TypeScript compiler (windows-latest)

Cannot assign to 'rememberExpanded' because it is a read-only property.

Check failure on line 608 in packages/govuk-frontend/src/govuk/components/accordion/accordion.mjs

View workflow job for this annotation

GitHub Actions / TypeScript compiler (windows-latest)

Cannot assign to 'rememberExpanded' because it is a read-only property.
if (Accordion.defaults.i18n?.hideSection) {
Accordion.defaults.i18n.hideSection = 'Some new string'

Check failure on line 610 in packages/govuk-frontend/src/govuk/components/accordion/accordion.mjs

View workflow job for this annotation

GitHub Actions / TypeScript compiler (windows-latest)

Cannot assign to 'hideSection' because it is a read-only property.

Check failure on line 610 in packages/govuk-frontend/src/govuk/components/accordion/accordion.mjs

View workflow job for this annotation

GitHub Actions / TypeScript compiler (windows-latest)

Cannot assign to 'hideSection' because it is a read-only property.

Check failure on line 610 in packages/govuk-frontend/src/govuk/components/accordion/accordion.mjs

View workflow job for this annotation

GitHub Actions / TypeScript compiler (windows-latest)

Cannot assign to 'hideSection' because it is a read-only property.
}

/**
* Accordion config
*
Expand Down Expand Up @@ -641,3 +646,8 @@ export class Accordion extends GOVUKFrontendComponentConfigurable {
/**
* @typedef {import('../../common/configuration.mjs').Schema} Schema
*/

/**
* @template T
* @typedef {import('type-fest').ReadonlyDeep<T>} ReadonlyDeep<T>
*/

0 comments on commit 3042948

Please sign in to comment.