chore(eslint): payload-logger proper usage works with template literals (#8660)

Improves this https://github.com/payloadcms/payload/pull/8578 to work
with template literals as well,

This is invalid:
```ts
this.payload.logger.error('Failed to create database', err)
```

But this is valid:
```ts
this.payload.logger.error(`Failed to create database ${dbName}`, err)
```

This PR fixes that

<img width="577" alt="image"
src="https://github.com/user-attachments/assets/b867da53-4f81-4b1b-8423-598e0419946a">
This commit is contained in:
Sasha
2024-10-14 02:10:47 +03:00
committed by GitHub
parent c731940239
commit 6be665f887
7 changed files with 32 additions and 13 deletions

View File

@@ -34,11 +34,11 @@ export const rule = {
if (isPayloadLoggerError(callee)) {
const args = node.arguments
// Case 1: Single string is passed as the argument
// Case 1: Single string / templated string is passed as the argument
if (
args.length === 1 &&
args[0].type === 'Literal' &&
typeof args[0].value === 'string'
((args[0].type === 'Literal' && typeof args[0].value === 'string') ||
args[0].type === 'TemplateLiteral')
) {
return // Valid: single string argument
}
@@ -67,11 +67,11 @@ export const rule = {
return // Valid object, checked for 'err'/'error' keys
}
// Case 3: Improper usage (string + error or additional err/error)
// Case 3: Improper usage (string / templated string + error or additional err/error)
if (
args.length > 1 &&
args[0].type === 'Literal' &&
typeof args[0].value === 'string' &&
((args[0].type === 'Literal' && typeof args[0].value === 'string') ||
args[0].type === 'TemplateLiteral') &&
args[1].type === 'Identifier' &&
(args[1].name === 'err' || args[1].name === 'error')
) {

View File

@@ -14,6 +14,10 @@ ruleTester.run('no-improper-payload-logger-error', rule, {
{
code: "payload.logger.error('Some error message')",
},
// Valid: payload.logger.error with a templated string
{
code: 'payload.logger.error(`Some error message`)',
},
// Valid: *.payload.logger.error with object
{
code: "this.payload.logger.error({ msg: 'another message', err })",
@@ -33,6 +37,15 @@ ruleTester.run('no-improper-payload-logger-error', rule, {
},
],
},
// Invalid: payload.logger.error with both templated string and error
{
code: 'payload.logger.error(`Some error message`, err)',
errors: [
{
messageId: 'improperUsage',
},
],
},
// Invalid: *.payload.logger.error with both string and error
{
code: "this.payload.logger.error('Some error message', error)",