Skip to content

Commit 4b13ed7

Browse files
committed
refactor: adjust code to ESLint rules
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent 1db02fd commit 4b13ed7

File tree

10 files changed

+58
-43
lines changed

10 files changed

+58
-43
lines changed

lib/csp-nonce.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: GPL-3.0-or-later
44
*/
55

6-
import { getRequestToken } from './requesttoken'
6+
import { getRequestToken } from './requesttoken.ts'
77

88
/**
99
* Get the CSP nonce for script loading

lib/eventbus.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: GPL-3.0-or-later
44
*/
5-
import { NextcloudUser } from './user'
5+
6+
import type { NextcloudUser } from './user.ts'
67

78
declare module '@nextcloud/event-bus' {
89
export interface NextcloudEvents {

lib/globals.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*!
2+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
3+
* SPDX-License-Identifier: GPL-3.0-or-later
4+
*/
5+
6+
declare global {
7+
interface Window {
8+
_oc_isadmin?: boolean
9+
}
10+
}
11+
12+
export {}

lib/guest.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
/**
1+
/*!
22
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: GPL-3.0-or-later
44
*/
5+
6+
import type { NextcloudUser } from './user.ts'
7+
58
import { getBuilder } from '@nextcloud/browser-storage'
6-
import { NextcloudUser } from './user'
79
import { emit, subscribe } from '@nextcloud/event-bus'
810

911
const browserStorage = getBuilder('public').persist().build()
1012

1113
class GuestUser implements NextcloudUser {
12-
1314
private _displayName: string | null
1415
readonly uid: string
1516
readonly isAdmin: boolean
@@ -27,7 +28,6 @@ class GuestUser implements NextcloudUser {
2728
this._displayName = guest.displayName
2829
browserStorage.setItem('guestNickname', guest.displayName || '')
2930
})
30-
3131
}
3232

3333
get displayName(): string | null {
@@ -39,7 +39,6 @@ class GuestUser implements NextcloudUser {
3939
browserStorage.setItem('guestNickname', displayName)
4040
emit('user:info:changed', this)
4141
}
42-
4342
}
4443

4544
let currentUser: NextcloudUser | undefined
@@ -64,7 +63,8 @@ export function getGuestNickname(): string | null {
6463

6564
/**
6665
* Set the guest nickname for public pages
67-
* @param nickname The nickname to set
66+
*
67+
* @param nickname - The nickname to set
6868
*/
6969
export function setGuestNickname(nickname: string): void {
7070
if (!nickname || nickname.trim().length === 0) {

lib/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: GPL-3.0-or-later
44
*/
5-
export type { CsrfTokenObserver } from './requesttoken'
6-
export type { NextcloudUser } from './user'
75

8-
export { getCSPNonce } from './csp-nonce'
9-
export { getGuestUser, getGuestNickname, setGuestNickname } from './guest'
10-
export { getRequestToken, onRequestTokenUpdate } from './requesttoken'
11-
export { getCurrentUser } from './user'
6+
export type { CsrfTokenObserver } from './requesttoken.ts'
7+
export type { NextcloudUser } from './user.ts'
8+
9+
export { getCSPNonce } from './csp-nonce.ts'
10+
export { getGuestNickname, getGuestUser, setGuestNickname } from './guest.ts'
11+
export { getRequestToken, onRequestTokenUpdate } from './requesttoken.ts'
12+
export { getCurrentUser } from './user.ts'

lib/requesttoken.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: GPL-3.0-or-later
44
*/
5+
56
import { subscribe } from '@nextcloud/event-bus'
67

78
export interface CsrfTokenObserver {
8-
(token: string): void;
9+
(token: string): void
910
}
1011

1112
let token: string | null | undefined
@@ -14,7 +15,7 @@ const observers: CsrfTokenObserver[] = []
1415
/**
1516
* Get current request token
1617
*
17-
* @return {string|null} Current request token or null if not set
18+
* @return Current request token or null if not set
1819
*/
1920
export function getRequestToken(): string | null {
2021
if (token === undefined) {
@@ -40,8 +41,10 @@ subscribe('csrf-token-update', (e: unknown) => {
4041
observers.forEach((observer) => {
4142
try {
4243
observer(token!)
43-
} catch (e) {
44-
console.error('Error updating CSRF token observer', e)
44+
} catch (error) {
45+
// we cannot use the logger as the logger uses this library = circular dependency
46+
// eslint-disable-next-line no-console
47+
console.error('Error updating CSRF token observer', error)
4548
}
4649
})
4750
})

lib/user.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@
22
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: GPL-3.0-or-later
44
*/
5-
declare global {
6-
interface Window {
7-
_oc_isadmin?: boolean
8-
}
9-
}
105

116
export interface NextcloudUser {
12-
uid: string,
13-
displayName: string | null,
14-
isAdmin: boolean,
7+
uid: string
8+
displayName: string | null
9+
isAdmin: boolean
1510
}
1611

1712
let currentUser: NextcloudUser | null | undefined
1813

19-
const getAttribute = (el: HTMLHeadElement | undefined, attribute: string): string | null => {
14+
/**
15+
* @param el - The element
16+
* @param attribute - The attribute to fetch
17+
*/
18+
function getAttribute(el: HTMLHeadElement | undefined, attribute: string): string | null {
2019
if (el) {
2120
return el.getAttribute(attribute)
2221
}

test/guest.test.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
/**
1+
/*!
22
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: GPL-3.0-or-later
44
*/
5-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
5+
66
import { getBuilder } from '@nextcloud/browser-storage'
77
import { emit } from '@nextcloud/event-bus'
8+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
89

910
// Mock dependencies
1011
vi.mock('@nextcloud/browser-storage')
@@ -20,7 +21,7 @@ const mockBrowserStorage = {
2021
}
2122

2223
// Mock crypto for UUID generation
23-
const originalCrypto = global.crypto
24+
const originalCrypto = globalThis.crypto
2425
const mockCrypto = {
2526
randomUUID: vi.fn(() => 'mock-uuid-' + Math.random().toString(36).slice(2, 10)),
2627
}
@@ -42,16 +43,16 @@ describe('Guest User Module', () => {
4243
}),
4344
})
4445

45-
// Replace global crypto with mock
46-
Object.defineProperty(global, 'crypto', {
46+
// Replace globalThis crypto with mock
47+
Object.defineProperty(globalThis, 'crypto', {
4748
value: mockCrypto,
4849
writable: true,
4950
})
5051
})
5152

5253
afterEach(() => {
5354
// Restore original crypto
54-
Object.defineProperty(global, 'crypto', {
55+
Object.defineProperty(globalThis, 'crypto', {
5556
value: originalCrypto,
5657
writable: true,
5758
})
@@ -122,12 +123,8 @@ describe('Guest User Module', () => {
122123
describe('setGuestNickname', () => {
123124
it('should throw an error if nickname is empty', async () => {
124125
const { setGuestNickname } = await import('../lib')
125-
expect(() => setGuestNickname('')).toThrow(
126-
'Nickname cannot be empty',
127-
)
128-
expect(() => setGuestNickname(' ')).toThrow(
129-
'Nickname cannot be empty',
130-
)
126+
expect(() => setGuestNickname('')).toThrow('Nickname cannot be empty')
127+
expect(() => setGuestNickname(' ')).toThrow('Nickname cannot be empty')
131128
})
132129

133130
it('should set the nickname and store it in browser storage', async () => {
@@ -162,7 +159,6 @@ describe('Guest User Module', () => {
162159

163160
describe('GuestUser class', () => {
164161
it('should update displayName when set through property', async () => {
165-
166162
const { getGuestUser } = await import('../lib')
167163
const guestUser = getGuestUser()
168164
const newName = 'Property Test User'

test/request-token.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: GPL-3.0-or-later
44
*/
5+
56
import { emit } from '@nextcloud/event-bus'
67
import { beforeEach, describe, expect, test, vi } from 'vitest'
78

@@ -48,7 +49,9 @@ describe('request token', () => {
4849
test('handle exception in observer', async () => {
4950
const spy = vi.spyOn(window.console, 'error')
5051
const { onRequestTokenUpdate } = await import('../lib')
51-
const observer = vi.fn(() => { throw new Error('!Error!') })
52+
const observer = vi.fn(() => {
53+
throw new Error('!Error!')
54+
})
5255
// silence the console
5356
spy.mockImplementationOnce(() => {})
5457

vitest.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: GPL-3.0-or-later
44
*/
5+
56
import type { UserConfig } from 'vite'
7+
68
import viteConfig from './vite.config'
79

810
export default async (env) => {
911
const config = typeof viteConfig === 'function' ? await viteConfig(env) : viteConfig
10-
// node-externals conflicts with vitest
11-
config.plugins = config.plugins!.filter((plugin) => plugin && (!('name' in plugin) || plugin?.name !== 'node-externals'))
1212

1313
return {
1414
...config,

0 commit comments

Comments
 (0)