Skip to content

Commit

Permalink
Merge pull request #3958 from alphagov/rollup-babel-comments
Browse files Browse the repository at this point in the history
Configure Babel to remove non-public comments
  • Loading branch information
colinrotherham authored Aug 11, 2023
2 parents dc1d5c9 + 7f668a5 commit 20ef74b
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 8 deletions.
18 changes: 13 additions & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ module.exports = {
}
],

// JSDoc blocks are optional
'jsdoc/require-jsdoc': 'off',
'jsdoc/require-param-description': 'off',
'jsdoc/require-param': 'off',

// Check for valid formatting
'jsdoc/check-line-alignment': [
'warn',
Expand All @@ -57,6 +52,19 @@ module.exports = {
}
],

// JSDoc blocks can use `@preserve` to prevent removal
'jsdoc/check-tag-names': [
'warn',
{
definedTags: ['preserve']
}
],

// JSDoc blocks are optional
'jsdoc/require-jsdoc': 'off',
'jsdoc/require-param-description': 'off',
'jsdoc/require-param': 'off',

// Require hyphens before param description
// Aligns with TSDoc style: https://tsdoc.org/pages/tags/param/
'jsdoc/require-hyphen-before-param-description': 'warn',
Expand Down
2 changes: 2 additions & 0 deletions docs/contributing/coding-standards/js.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ component
```mjs
/**
* Component name
*
* @preserve
*/
export class Example {
/**
Expand Down
21 changes: 21 additions & 0 deletions packages/govuk-frontend/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ module.exports = function (api) {
const browserslistEnv = isBrowser ? 'javascripts' : 'node'

return {
generatorOpts: {
shouldPrintComment(comment) {
if (!isBrowser || comment.includes('* @preserve')) {
return true
}

// Assume all comments are public unless
// tagged with `@private` or `@internal`
const isPrivate = ['* @internal', '* @private'].some((tag) =>
comment.includes(tag)
)

// Flag any JSDoc comments worth keeping
const isDocumentation = ['* @param', '* @returns', '* @typedef'].some(
(tag) => comment.includes(tag)
)

// Print only public JSDoc comments
return !isPrivate && isDocumentation
}
},
presets: [
[
'@babel/preset-env',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { I18n } from '../../i18n.mjs'
*
* The state of each section is saved to the DOM via the `aria-expanded`
* attribute, which also provides accessibility.
*
* @preserve
*/
export class Accordion extends GOVUKFrontendComponent {
/** @private */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const DEBOUNCE_TIMEOUT_IN_SECONDS = 1

/**
* JavaScript enhancements for the Button component
*
* @preserve
*/
export class Button extends GOVUKFrontendComponent {
/** @private */
Expand All @@ -25,7 +27,6 @@ export class Button extends GOVUKFrontendComponent {
debounceFormSubmitTimer = null

/**
*
* @param {Element} $module - HTML element to use for button
* @param {ButtonConfig} [config] - Button config
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { I18n } from '../../i18n.mjs'
*
* You can configure the message to only appear after a certain percentage
* of the available characters/words has been entered.
*
* @preserve
*/
export class CharacterCount extends GOVUKFrontendComponent {
/** @private */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'

/**
* Checkboxes component
*
* @preserve
*/
export class Checkboxes extends GOVUKFrontendComponent {
/** @private */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'
* Error summary component
*
* Takes focus on initialisation for accessible announcement, unless disabled in configuration.
*
* @preserve
*/
export class ErrorSummary extends GOVUKFrontendComponent {
/** @private */
Expand All @@ -18,7 +20,6 @@ export class ErrorSummary extends GOVUKFrontendComponent {
config

/**
*
* @param {Element} $module - HTML element to use for error summary
* @param {ErrorSummaryConfig} [config] - Error summary config
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { I18n } from '../../i18n.mjs'

/**
* Exit This Page component
*
* @preserve
*/
export class ExitThisPage extends GOVUKFrontendComponent {
/** @private */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'

/**
* Header component
*
* @preserve
*/
export class Header extends GOVUKFrontendComponent {
/** @private */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'

/**
* Notification Banner component
*
* @preserve
*/
export class NotificationBanner extends GOVUKFrontendComponent {
/** @private */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'

/**
* Radios component
*
* @preserve
*/
export class Radios extends GOVUKFrontendComponent {
/** @private */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'

/**
* Skip link component
*
* @preserve
*/
export class SkipLink extends GOVUKFrontendComponent {
/** @private */
Expand All @@ -17,7 +19,6 @@ export class SkipLink extends GOVUKFrontendComponent {
linkedElementListener = false

/**
*
* @param {Element} $module - HTML element to use for skip link
*/
constructor($module) {
Expand Down
2 changes: 2 additions & 0 deletions packages/govuk-frontend/src/govuk/components/tabs/tabs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { GOVUKFrontendComponent } from '../../govuk-frontend-component.mjs'

/**
* Tabs component
*
* @preserve
*/
export class Tabs extends GOVUKFrontendComponent {
/** @private */
Expand Down

0 comments on commit 20ef74b

Please sign in to comment.