From 02ac9729aeb90137758c271250b594d40ea8f557 Mon Sep 17 00:00:00 2001 From: "Nanashi." Date: Wed, 4 Dec 2024 02:57:19 +0900 Subject: [PATCH] =?UTF-8?q?Migrate:=20optional=20chain=E3=81=AB=E6=9B=B8?= =?UTF-8?q?=E3=81=8D=E6=8F=9B=E3=81=88=E3=82=8B=20(#2387)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Hiroshiba --- .eslintrc.js | 17 +----- .../HelpDialog/HelpLibraryPolicySection.vue | 22 ++++--- src/helpers/map.ts | 39 ------------- tests/unit/lib/map.spec.ts | 58 ------------------- 4 files changed, 11 insertions(+), 125 deletions(-) delete mode 100644 src/helpers/map.ts delete mode 100644 tests/unit/lib/map.spec.ts diff --git a/.eslintrc.js b/.eslintrc.js index 1477adfb4e..d395941f2a 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -44,11 +44,7 @@ module.exports = { plugins: ["import"], parser: vueEslintParser, parserOptions: vueEslintParserOptions, - ignorePatterns: [ - "dist/**/*", - "dist_*/**/*", - "node_modules/**/*", - ], + ignorePatterns: ["dist/**/*", "dist_*/**/*", "node_modules/**/*"], rules: { "linebreak-style": process.env.NODE_ENV === "production" && process.platform !== "win32" @@ -63,17 +59,6 @@ module.exports = { endOfLine: "auto", }, ], - "vue/no-restricted-syntax": [ - "error", - { - selector: "LogicalExpression[operator=??]", - message: `template内で"??"を使うとgithubのsyntax highlightが崩れるので\n三項演算子等を使って書き換えてください`, - }, - { - selector: "MemberExpression[optional=true]", - message: `template内で"?."を使うとgithubのsyntax highlightが崩れるので\n三項演算子等を使って書き換えてください`, - }, - ], "@typescript-eslint/no-unused-vars": [ process.env.NODE_ENV === "development" ? "warn" : "error", // 開発時のみwarn { diff --git a/src/components/Dialog/HelpDialog/HelpLibraryPolicySection.vue b/src/components/Dialog/HelpDialog/HelpLibraryPolicySection.vue index 87675f6f9f..41d5e7f407 100644 --- a/src/components/Dialog/HelpDialog/HelpLibraryPolicySection.vue +++ b/src/components/Dialog/HelpDialog/HelpLibraryPolicySection.vue @@ -12,13 +12,13 @@ >

- {{ mapNullablePipe(engineInfos.get(engineId), (v) => v.name) }} + {{ engineInfos.get(engineId)?.name }}

{{ - mapNullablePipe( - engineInfos.get(selectedInfo.engine), - (v) => v.characterInfos, - (v) => mapNullablePipe(selectedInfo, (i) => v.get(i.character)), - (v) => v.metas.speakerName, - ) + selectedInfo && + engineInfos + .get(selectedInfo.engine) + ?.characterInfos.get(selectedInfo.character)?.metas.speakerName }}

@@ -75,7 +73,7 @@ import BaseDocumentView from "@/components/Base/BaseDocumentView.vue"; import { useStore } from "@/store"; import { useMarkdownIt } from "@/plugins/markdownItPlugin"; import { EngineId, SpeakerId } from "@/type/preload"; -import { mapNullablePipe } from "@/helpers/map"; +import { getOrThrow } from "@/helpers/mapHelper"; type DetailKey = { engine: EngineId; character: SpeakerId }; diff --git a/src/helpers/map.ts b/src/helpers/map.ts deleted file mode 100644 index def8bbf1cc..0000000000 --- a/src/helpers/map.ts +++ /dev/null @@ -1,39 +0,0 @@ -export function mapNullablePipe( - source: T | undefined, - fn1: (_: NonNullable) => U1 | undefined, -): U1 | undefined; -export function mapNullablePipe( - source: T | undefined, - fn1: (_: NonNullable) => U1 | undefined, - fn2: (_: NonNullable) => U2 | undefined, -): U2 | undefined; -export function mapNullablePipe( - source: T | undefined, - fn1: (_: NonNullable) => U1 | undefined, - fn2: (_: NonNullable) => U2 | undefined, - fn3: (_: NonNullable) => U3 | undefined, -): U3 | undefined; -/** - * 一連の関数を実行する。途中でundefinedかnullを返すとその後undefinedを返す。 - */ -// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any -export function mapNullablePipe(source: any, ...fn: Function[]) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return fn.reduce((prev, curr) => { - if (prev == undefined) { - return undefined; - } - // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return curr(prev); - }, source); -} - -export const nullableToDefault = ( - defaultValue: T, - maybeValue: T | undefined, -): T => { - if (maybeValue == undefined) { - return defaultValue; - } - return maybeValue; -}; diff --git a/tests/unit/lib/map.spec.ts b/tests/unit/lib/map.spec.ts deleted file mode 100644 index 5f891c28c2..0000000000 --- a/tests/unit/lib/map.spec.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { describe, expect, it } from "vitest"; - -import { mapNullablePipe, nullableToDefault } from "@/helpers/map"; - -type DummyType = { - outerValue?: { - innerValue?: string; - }; -}; - -describe("mapUndefinedPipe", () => { - it("値をunwrap出来る", () => { - const key = "test"; - const innerValue = "value"; - const value: DummyType = { - outerValue: { - innerValue, - }, - }; - const map = new Map([[key, value]]); - expect( - mapNullablePipe( - map.get(key), - (v) => v.outerValue, - (v) => v.innerValue, - ), - ).toEqual(innerValue); - }); - - it("途中でundefinedを返すとその後undefinedを返す", () => { - const key = "test"; - const value: DummyType = { - outerValue: { - innerValue: undefined, - }, - }; - const map = new Map([[key, value]]); - expect( - mapNullablePipe( - map.get(key), - (v) => v.outerValue, - (v) => v.innerValue, - ), - ).toBeUndefined(); - }); -}); - -describe("undefinedToDefault", () => { - it("値がある時はそのまま返す", () => { - const actualValue = "value"; - expect(nullableToDefault("test", actualValue)).toEqual(actualValue); - }); - - it("値がない時はdefaultValueを返す", () => { - const defaultValue = "test"; - expect(nullableToDefault(defaultValue, undefined)).toEqual(defaultValue); - }); -});