From 7b374b21d2ffca19b277f61b0efcad6c97eacc65 Mon Sep 17 00:00:00 2001 From: double beep <38133098+double-beep@users.noreply.github.com> Date: Tue, 25 Jun 2024 20:23:40 +0000 Subject: [PATCH] fix: only use `??` when appropriate --- eslint.config.js | 7 ++++++- src/Configuration.ts | 4 ++-- src/UserscriptTools/MetaSmokeAPI.ts | 2 +- src/UserscriptTools/Post.ts | 4 ++-- src/modals/comments/submit.ts | 6 +++--- src/popover.ts | 8 ++++---- src/review.ts | 4 ++-- 7 files changed, 20 insertions(+), 15 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index e78d9b7..5603b5d 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -43,7 +43,12 @@ export default tseslint.config({ '@typescript-eslint/prefer-promise-reject-errors': 'off', '@typescript-eslint/no-extraneous-class': 'off', '@typescript-eslint/non-nullable-type-assertion-style': 'off', - '@typescript-eslint/prefer-nullish-coalescing': ['error', { ignorePrimitives: { boolean: true } }], + '@typescript-eslint/prefer-nullish-coalescing': ['error', { + ignorePrimitives: { + boolean: true, + string: true + } + }], '@typescript-eslint/no-misused-promises': ['error', { checksVoidReturn: false }], '@stylistic/arrow-parens': ['warn', 'as-needed'], diff --git a/src/Configuration.ts b/src/Configuration.ts index 7319665..77aa599 100644 --- a/src/Configuration.ts +++ b/src/Configuration.ts @@ -6,7 +6,7 @@ import { } from './UserscriptTools/Store'; import { Flags, flagCategories } from './FlagTypes'; -import { FlagNames, displayStacksToast } from './shared'; +import { FlagNames, displayStacksToast, getFlagTypeFromFlagId } from './shared'; import { buildConfigurationOverlay } from './modals/config'; import { setupCommentsAndFlagsModal } from './modals/comments/main'; @@ -81,7 +81,7 @@ function setupDefaults(): void { cacheCategories(); // update default link-only comment! - const linkOnly = Store.flagTypes.find(({ id }) => id === 6); + const linkOnly = getFlagTypeFromFlagId(6); const defaultComment = flagCategories[2].FlagTypes[0].comments?.low; if (linkOnly // link only flag type has not been removed && defaultComment diff --git a/src/UserscriptTools/MetaSmokeAPI.ts b/src/UserscriptTools/MetaSmokeAPI.ts index a291f0c..9fde907 100644 --- a/src/UserscriptTools/MetaSmokeAPI.ts +++ b/src/UserscriptTools/MetaSmokeAPI.ts @@ -35,7 +35,7 @@ interface MetasmokeWsMessage { export class MetaSmokeAPI extends Reporter { public static accessToken: string; - public static isDisabled: boolean = Store.get(Cached.Metasmoke.disabled) ?? false; + public static isDisabled: boolean = Store.get(Cached.Metasmoke.disabled) || false; public smokeyId: number; diff --git a/src/UserscriptTools/Post.ts b/src/UserscriptTools/Post.ts index 792fc17..f29d7a0 100644 --- a/src/UserscriptTools/Post.ts +++ b/src/UserscriptTools/Post.ts @@ -115,7 +115,7 @@ export default class Post { const url = `/flags/posts/${this.id}/add/${flagName}`; const data = { fkey: StackExchange.options.user.fkey, - otherText: text ?? '', + otherText: text || '', // plagiarism flag: fill "Link(s) to original content" // note wrt link: site will always be Stack Overflow, // post will always be an answer. @@ -501,7 +501,7 @@ export default class Post { this.element.dataset.questionid ?? this.element.dataset.answerid ) ?? ( this.type === 'Answer'// flags/NATO/search page: parse the post URL - ? new URL(href ?? '').pathname.split('/').pop() + ? new URL(href || '').pathname.split('/').pop() : href?.split('/')[4] ); diff --git a/src/modals/comments/submit.ts b/src/modals/comments/submit.ts index 5e49ff7..67f5a0f 100644 --- a/src/modals/comments/submit.ts +++ b/src/modals/comments/submit.ts @@ -13,7 +13,7 @@ function saveName( ): void { const input = card.querySelector('.s-input__md'); - flagType.displayName = input?.value ?? ''; + flagType.displayName = input?.value || ''; } function saveTextareaContent( @@ -44,7 +44,7 @@ function saveSwfr( ): void { // swfr = send when flag raised :) const swfrBox = expandable.querySelector('[id*="-send-when-flag-raised-"'); - const sendFeedback = swfrBox?.checked ?? false; + const sendFeedback = swfrBox?.checked || false; flagType.sendWhenFlagRaised = sendFeedback; @@ -74,7 +74,7 @@ function saveDownvote( ): void { const downvote = expandable.querySelector('[id*="-downvote-post-"'); - flagType.downvote = downvote?.checked ?? false; + flagType.downvote = downvote?.checked || false; } function saveFeedbacks( diff --git a/src/popover.ts b/src/popover.ts index 20783af..3ea2fed 100644 --- a/src/popover.ts +++ b/src/popover.ts @@ -224,7 +224,7 @@ export class Popover { // Comment text: ... const commentText = this.getCommentText(flagType); - const tooltipCommentText = (this.post.deleted ? '' : commentText) ?? ''; + const tooltipCommentText = (this.post.deleted ? '' : commentText) || ''; // if the flag changed from VLQ to NAA, let the user know why const flagName = getFlagToRaise(reportType, this.post.qualifiesForVlq()); @@ -339,7 +339,7 @@ export class Popover { const { addAuthorName } = Store.config; const type = (this.post.opReputation || 0) > 50 ? 'high' : 'low'; - let comment = comments?.[type] ?? comments?.low; + let comment = comments?.[type] || comments?.low; if (comment) { const sitename = StackExchange.options.site.name || ''; const siteurl = window.location.hostname; @@ -354,7 +354,7 @@ export class Popover { comment && addAuthorName ? `${this.post.opName}, ${comment[0].toLowerCase()}${comment.slice(1)}` : comment - ) ?? null; + ) || null; } private async handleReportLinkClick( @@ -413,7 +413,7 @@ export class Popover { .map(type => { return dropdown.querySelector( `[id*="-${type}-checkbox-"]` - )?.checked ?? false; + )?.checked || false; }); // comment diff --git a/src/review.ts b/src/review.ts index d0adc1a..cab5854 100644 --- a/src/review.ts +++ b/src/review.ts @@ -1,4 +1,4 @@ -import { addProgress, addXHRListener, appendLabelAndBoxes, delay } from './shared'; +import { addProgress, addXHRListener, appendLabelAndBoxes, delay, getFlagTypeFromFlagId } from './shared'; import { isDone } from './AdvancedFlagging'; import { MetaSmokeAPI } from './UserscriptTools/MetaSmokeAPI'; @@ -118,7 +118,7 @@ export function setupReview(): void { submit.addEventListener('click', async event => { // find the "Not an answer" flag type - const flagType = Store.flagTypes.find(({ id }) => id === 7); + const flagType = getFlagTypeFromFlagId(7); if (!flagType) return; // something went wrong await addProgress(event, flagType);