Skip to content

Commit

Permalink
Merge pull request #8210 from alphagov/prettier
Browse files Browse the repository at this point in the history
Format `.js` and `.scss` files using Prettier
  • Loading branch information
dnkrj authored Sep 8, 2023
2 parents 8ef4774 + 823744b commit 81905a6
Show file tree
Hide file tree
Showing 52 changed files with 1,454 additions and 707 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
extends: 'standard',
extends: ['standard', 'prettier'],
ignorePatterns: [
'app/assets/javascripts/vendor/',
'app/assets/javascripts/admin_legacy/**/*',
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ jobs:
name: Lint JavaScript
uses: ./.github/workflows/lintjs.yml

lint-prettier:
name: Prettier
uses: ./.github/workflows/lintprettier.yml

lint-ruby:
name: Lint Ruby
uses: alphagov/govuk-infrastructure/.github/workflows/rubocop.yml@main
Expand Down
17 changes: 17 additions & 0 deletions .github/workflows/lintprettier.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Run lint:prettier

on: workflow_call

jobs:
run-lint-prettier:
name: Run Prettier
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Setup Node
uses: alphagov/govuk-infrastructure/.github/actions/setup-node@main

- name: Run lint:prettier
run: yarn run lint:prettier
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
app/assets/javascripts/vendor/
app/assets/javascripts/admin_legacy
app/assets/stylesheets/vendor/
app/assets/stylesheets/admin_legacy
spec/javascripts/admin_legacy
14 changes: 14 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
semi: false,
singleQuote: true,
trailingComma: 'none',
overrides: [
{
files: '*.scss',
options: {
printWidth: 120,
singleQuote: false
}
}
]
}
2 changes: 1 addition & 1 deletion app/assets/javascripts/admin/analytics.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(function (GOVUK) {
;(function (GOVUK) {
'use strict'

GOVUK.Analytics.load()
Expand Down
145 changes: 87 additions & 58 deletions app/assets/javascripts/admin/modules/add-another.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {};

(function (Modules) {
function AddAnother (module) {
window.GOVUK.Modules = window.GOVUK.Modules || {}
;(function (Modules) {
function AddAnother(module) {
this.module = module
this.addText = this.module.dataset.addText || 'Add another'
}
Expand All @@ -15,39 +14,52 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
AddAnother.prototype.addButton = function () {
const buttonAdd = document.createElement('button')

buttonAdd.classList.add('govuk-button', 'govuk-!-margin-bottom-0', 'govuk-button--secondary', 'add-another__add-button')
buttonAdd.classList.add(
'govuk-button',
'govuk-!-margin-bottom-0',
'govuk-button--secondary',
'add-another__add-button'
)
buttonAdd.setAttribute('type', 'submit')
buttonAdd.textContent = this.addText

this.module.append(buttonAdd)

// Add event listeners for "add" and "remove" buttons
this.module.addEventListener('click', function (e) {
if (e.target.classList.contains('add-another__remove-button')) {
this.removeFields(e.target)
} else if (e.target.classList.contains('add-another__add-button')) {
e.preventDefault()
this.addFields(e.target)
}
}.bind(this))
this.module.addEventListener(
'click',
function (e) {
if (e.target.classList.contains('add-another__remove-button')) {
this.removeFields(e.target)
} else if (e.target.classList.contains('add-another__add-button')) {
e.preventDefault()
this.addFields(e.target)
}
}.bind(this)
)
}

AddAnother.prototype.addFields = function (button) {
const allFields = button.parentNode.querySelectorAll('.js-duplicate-fields-set')
const allFields = button.parentNode.querySelectorAll(
'.js-duplicate-fields-set'
)
const fields = allFields[allFields.length - 1]

// Show hidden "Remove" button
if (fields.querySelector('.add-another__remove-button')) {
fields.querySelector('.add-another__remove-button').style.display = 'inline-block'
fields.querySelector('.add-another__remove-button').style.display =
'inline-block'
}

// Clone the markup of the previous set of fields
const newFields = fields.cloneNode(true)

// Reset values of cloned fields
newFields.querySelectorAll('input, textarea, select').forEach(function (element) {
element.value = ''
})
newFields
.querySelectorAll('input, textarea, select')
.forEach(function (element) {
element.value = ''
})

// Increment values for id, for and name of cloned fields
this.setValues(newFields, null)
Expand All @@ -59,7 +71,9 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
this.removeButton()

// Move focus to first visible field in new set
newFields.querySelectorAll('input:not([type="hidden"]), select, textarea')[0].focus()
newFields
.querySelectorAll('input:not([type="hidden"]), select, textarea')[0]
.focus()
}

AddAnother.prototype.removeButton = function () {
Expand All @@ -70,7 +84,12 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
fields.forEach(function (field) {
if (!field.querySelector('.add-another__remove-button')) {
buttonRemove = document.createElement('button')
buttonRemove.classList.add('govuk-button', 'govuk-button--warning', 'govuk-!-margin-bottom-6', 'add-another__remove-button')
buttonRemove.classList.add(
'govuk-button',
'govuk-button--warning',
'govuk-!-margin-bottom-6',
'add-another__remove-button'
)
buttonRemove.setAttribute('type', 'button')
buttonRemove.textContent = 'Delete'

Expand All @@ -82,7 +101,9 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};

AddAnother.prototype.removeFields = function (button) {
const set = button.parentNode
const input = set.querySelectorAll('input:not([type="hidden"]), select, textarea')[0]
const input = set.querySelectorAll(
'input:not([type="hidden"]), select, textarea'
)[0]
const baseId = input.id
const baseName = input.name

Expand All @@ -101,61 +122,69 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};

// Hide "Remove" button if only first set displayed
if (sets.length === 1) {
sets[0].querySelector('.add-another__remove-button').style.display = 'none'
sets[0].querySelector('.add-another__remove-button').style.display =
'none'
}

// Move focus to first visible field
sets[0].querySelectorAll('input:not([type="hidden"]), select, textarea')[0].focus()
sets[0]
.querySelectorAll('input:not([type="hidden"]), select, textarea')[0]
.focus()
}

// Set values for index, for and name of supplied fields
AddAnother.prototype.setValues = function (set, index) {
let num = 0

set.querySelectorAll('label, input, select, textarea').forEach(function (element) {
const currentName = element.getAttribute('name') || null
const currentId = element.getAttribute('id') || null
const currentFor = element.getAttribute('for') || null
const arrayMatcher = /(.*)\[([0-9]+)\](.*?)$/
const underscoreMatcher = /(.*)_([0-9]+)_(.*?)$/
let matched

if (currentName && arrayMatcher.exec(currentName)) {
matched = arrayMatcher.exec(currentName)

if (index === null) {
num = parseInt(matched[2], 10) + 1
} else {
num = index
set
.querySelectorAll('label, input, select, textarea')
.forEach(function (element) {
const currentName = element.getAttribute('name') || null
const currentId = element.getAttribute('id') || null
const currentFor = element.getAttribute('for') || null
const arrayMatcher = /(.*)\[([0-9]+)\](.*?)$/
const underscoreMatcher = /(.*)_([0-9]+)_(.*?)$/
let matched

if (currentName && arrayMatcher.exec(currentName)) {
matched = arrayMatcher.exec(currentName)

if (index === null) {
num = parseInt(matched[2], 10) + 1
} else {
num = index
}

element.setAttribute(
'name',
matched[1] + '[' + num + ']' + matched[3]
)
}

element.setAttribute('name', matched[1] + '[' + num + ']' + matched[3])
}
if (currentId && underscoreMatcher.exec(currentId)) {
matched = underscoreMatcher.exec(currentId)

if (currentId && underscoreMatcher.exec(currentId)) {
matched = underscoreMatcher.exec(currentId)
if (index === null) {
num = parseInt(matched[2], 10) + 1
} else {
num = index
}

if (index === null) {
num = parseInt(matched[2], 10) + 1
} else {
num = index
element.setAttribute('id', matched[1] + '_' + num + '_' + matched[3])
}

element.setAttribute('id', matched[1] + '_' + num + '_' + matched[3])
}
if (currentFor && underscoreMatcher.exec(currentFor)) {
matched = underscoreMatcher.exec(currentFor)

if (currentFor && underscoreMatcher.exec(currentFor)) {
matched = underscoreMatcher.exec(currentFor)
if (index === null) {
num = parseInt(matched[2], 10) + 1
} else {
num = index
}

if (index === null) {
num = parseInt(matched[2], 10) + 1
} else {
num = index
element.setAttribute('for', matched[1] + '_' + num + '_' + matched[3])
}

element.setAttribute('for', matched[1] + '_' + num + '_' + matched[3])
}
})
})
}

Modules.AddAnother = AddAnother
Expand Down
11 changes: 6 additions & 5 deletions app/assets/javascripts/admin/modules/app-analytics.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {};

(function (Modules) {
function AppAnalytics (module) { }
window.GOVUK.Modules = window.GOVUK.Modules || {}
;(function (Modules) {
function AppAnalytics(module) {}

AppAnalytics.prototype.init = function () {
this.setCustomDimensionsFromMetaTags()
Expand All @@ -23,7 +22,9 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};

for (let i = 0; i < metas.length; i++) {
const meta = metas[i]
const dimensionId = parseInt(meta.getAttribute('name').split('custom-dimension:')[1])
const dimensionId = parseInt(
meta.getAttribute('name').split('custom-dimension:')[1]
)
const content = meta.getAttribute('content')

GOVUK.analytics.setDimension(dimensionId, content)
Expand Down
37 changes: 25 additions & 12 deletions app/assets/javascripts/admin/modules/document-history-paginator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {};

(function (Modules) {
function DocumentHistoryPaginator (module) {
window.GOVUK.Modules = window.GOVUK.Modules || {}
;(function (Modules) {
function DocumentHistoryPaginator(module) {
this.module = module
}

Expand Down Expand Up @@ -30,13 +29,21 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
link.addEventListener('click', function (e) {
e.preventDefault()

window.fetch(new URL(document.location.origin + link.dataset.remotePagination))
.then(function (response) { return response.text() })
.catch(function () { window.location.href = link.href })
window
.fetch(
new URL(document.location.origin + link.dataset.remotePagination)
)
.then(function (response) {
return response.text()
})
.catch(function () {
window.location.href = link.href
})
.then(function (html) {
module.innerHTML = html

const documentHistoryModule = new GOVUK.Modules.DocumentHistoryPaginator(module)
const documentHistoryModule =
new GOVUK.Modules.DocumentHistoryPaginator(module)
documentHistoryModule.init()
})
})
Expand All @@ -58,13 +65,19 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
}

select.addEventListener('change', function () {
window.fetch(form.dataset.remotePagination + '?' + queryParameters())
.then(function (response) { return response.text() })
.catch(function () { window.location.search = queryParameters() })
window
.fetch(form.dataset.remotePagination + '?' + queryParameters())
.then(function (response) {
return response.text()
})
.catch(function () {
window.location.search = queryParameters()
})
.then(function (html) {
module.innerHTML = html

const documentHistoryModule = new GOVUK.Modules.DocumentHistoryPaginator(module)
const documentHistoryModule =
new GOVUK.Modules.DocumentHistoryPaginator(module)
documentHistoryModule.init()
})
})
Expand Down
Loading

0 comments on commit 81905a6

Please sign in to comment.