diff --git a/main.js b/main.js index 9c13e96..12fbc03 100644 --- a/main.js +++ b/main.js @@ -200,17 +200,18 @@ var AttachmentCleanPasteModule = class extends BaseModule { const arrayBuffer = await file.arrayBuffer(); const filename = file.name; const nameWithoutExt = filename.replace(/\.[^/.]+$/, ""); - let attachmentFolder = this.app.vault.getConfig("attachmentFolderPath"); + const vault = this.app.vault; + let attachmentFolder = vault.getConfig("attachmentFolderPath"); if (!attachmentFolder) attachmentFolder = "attachments"; const folderPath = (0, import_obsidian.normalizePath)(attachmentFolder); - if (!await this.app.vault.adapter.exists(folderPath)) { - await this.app.vault.createFolder(folderPath); + if (!await vault.adapter.exists(folderPath)) { + await vault.createFolder(folderPath); } const ext = filename.slice(filename.lastIndexOf(".") + 1); const basePath = (0, import_obsidian.normalizePath)(`${folderPath}/${nameWithoutExt}`); - const availablePath = await this.app.vault.getAvailablePath(basePath, ext); - await this.app.vault.createBinary(availablePath, arrayBuffer); + const availablePath = await vault.getAvailablePath(basePath, ext); + await vault.createBinary(availablePath, arrayBuffer); return `[[${availablePath}|${nameWithoutExt}]]`; } catch (e) { new import_obsidian.Notice(`Clean Paste error: ${e.message}`); diff --git a/package-lock.json b/package-lock.json index b1165a6..cba334a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.1.5", "license": "MIT", "devDependencies": { + "@types/node": "^20.19.43", "@typescript-eslint/eslint-plugin": "^7.18.0", "@typescript-eslint/parser": "^7.18.0", "builtin-modules": "^3.3.0", @@ -745,9 +746,6 @@ "arm" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -762,9 +760,6 @@ "arm" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -779,9 +774,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -796,9 +788,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -813,9 +802,6 @@ "loong64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -830,9 +816,6 @@ "loong64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -847,9 +830,6 @@ "ppc64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -864,9 +844,6 @@ "ppc64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -881,9 +858,6 @@ "riscv64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -898,9 +872,6 @@ "riscv64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -915,9 +886,6 @@ "s390x" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -932,9 +900,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -949,9 +914,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -1066,6 +1028,16 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/node": { + "version": "20.19.43", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.19.43.tgz", + "integrity": "sha512-6oYBAi5ikg4Pl+kGsoYtawUMBT2zZMCvPNF7pVLnHZfd1zf38DRiWn/gT01RYCdUqkv7Fhr+C9ot4/tb+2sVvA==", + "dev": true, + "license": "MIT", + "dependencies": { + "undici-types": "~6.21.0" + } + }, "node_modules/@types/tern": { "version": "0.23.9", "resolved": "https://registry.npmjs.org/@types/tern/-/tern-0.23.9.tgz", @@ -3416,6 +3388,13 @@ "dev": true, "license": "MIT" }, + "node_modules/undici-types": { + "version": "6.21.0", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz", + "integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==", + "dev": true, + "license": "MIT" + }, "node_modules/uri-js": { "version": "4.4.1", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", diff --git a/package.json b/package.json index 0a72109..51d476e 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "dev": "node esbuild.config.mjs", "build": "node esbuild.config.mjs production", "test": "vitest run", + "typecheck": "tsc --noEmit", "lint": "eslint src/", "format": "prettier --check src/", "format:fix": "prettier --write src/", @@ -19,6 +20,7 @@ "author": "", "license": "MIT", "devDependencies": { + "@types/node": "^20.19.43", "@typescript-eslint/eslint-plugin": "^7.18.0", "@typescript-eslint/parser": "^7.18.0", "builtin-modules": "^3.3.0", diff --git a/src/modules/attachment-clean-paste/index.ts b/src/modules/attachment-clean-paste/index.ts index 92f0f2b..3bc45e0 100644 --- a/src/modules/attachment-clean-paste/index.ts +++ b/src/modules/attachment-clean-paste/index.ts @@ -32,7 +32,7 @@ export class AttachmentCleanPasteModule extends BaseModule { onload(plugin: ZeroQoLModulesPlugin, moduleSettings: Record): void { this.app = plugin.app; - const rawSettings = moduleSettings as AttachmentCleanPasteSettings; + const rawSettings = moduleSettings as unknown as AttachmentCleanPasteSettings; const pasteRef = plugin.app.workspace.on( 'editor-paste', @@ -141,20 +141,27 @@ export class AttachmentCleanPasteModule extends BaseModule { const filename = file.name; const nameWithoutExt = filename.replace(/\.[^/.]+$/, ''); - let attachmentFolder = this.app.vault.getConfig('attachmentFolderPath') as string; + // Legacy/undocumented Vault surface: `getConfig` and `getAvailablePath` + // exist at runtime but are absent from the public typings. + const vault = this.app.vault as typeof this.app.vault & { + getConfig(key: string): string; + getAvailablePath(basePath: string, extension: string): Promise; + }; + + let attachmentFolder = vault.getConfig('attachmentFolderPath'); if (!attachmentFolder) attachmentFolder = 'attachments'; const folderPath = normalizePath(attachmentFolder); - if (!(await this.app.vault.adapter.exists(folderPath))) { - await this.app.vault.createFolder(folderPath); + if (!(await vault.adapter.exists(folderPath))) { + await vault.createFolder(folderPath); } const ext = filename.slice(filename.lastIndexOf('.') + 1); const basePath = normalizePath(`${folderPath}/${nameWithoutExt}`); - const availablePath = await this.app.vault.getAvailablePath(basePath, ext); + const availablePath = await vault.getAvailablePath(basePath, ext); - await this.app.vault.createBinary(availablePath, arrayBuffer); + await vault.createBinary(availablePath, arrayBuffer); return `[[${availablePath}|${nameWithoutExt}]]`; } catch (e) { diff --git a/src/modules/preserve-link-aliases/index.ts b/src/modules/preserve-link-aliases/index.ts index e7d7152..4526681 100644 --- a/src/modules/preserve-link-aliases/index.ts +++ b/src/modules/preserve-link-aliases/index.ts @@ -26,7 +26,7 @@ export class PreserveLinkAliasesModule extends BaseModule { onload(plugin: ZeroQoLModulesPlugin, moduleSettings: Record): void { this.vault = plugin.app.vault; - const rawSettings = moduleSettings as PreserveLinkAliasesSettings; + const rawSettings = moduleSettings as unknown as PreserveLinkAliasesSettings; const renameRef = plugin.app.vault.on('rename', (file: TFile, oldPath: string) => this.handleRename(file, oldPath, rawSettings), diff --git a/tests/modules/attachment-clean-paste.test.ts b/tests/modules/attachment-clean-paste.test.ts index 37d1dbe..f8c1cec 100644 --- a/tests/modules/attachment-clean-paste.test.ts +++ b/tests/modules/attachment-clean-paste.test.ts @@ -27,7 +27,10 @@ vi.mock('obsidian', () => ({ // Minimal normalizePath: collapses backslashes, collapses duplicate slashes, // strips leading/trailing slash. Sufficient for the inputs in these tests. normalizePath: (p: string): string => - p.replace(/\\/g, '/').replace(/\/+/g, '/').replace(/^\/|\/$/g, ''), + p + .replace(/\\/g, '/') + .replace(/\/+/g, '/') + .replace(/^\/|\/$/g, ''), Plugin: class {}, })); @@ -38,8 +41,7 @@ import { AttachmentCleanPasteModule } from '../../src/modules/attachment-clean-p function createFile(name: string, content: string = 'data'): File { return { name, - arrayBuffer: async () => - new TextEncoder().encode(content).buffer as ArrayBuffer, + arrayBuffer: async () => new TextEncoder().encode(content).buffer as ArrayBuffer, } as unknown as File; } @@ -58,11 +60,8 @@ describe('AttachmentCleanPasteModule', () => { createBinary: ReturnType; }; let pasteHandler: - | ((evt: ClipboardEvent, editor: unknown, info: unknown) => Promise) - | null; - let dropHandler: - | ((evt: DragEvent, editor: unknown, info: unknown) => Promise) - | null; + ((evt: ClipboardEvent, editor: unknown, info: unknown) => Promise) | null; + let dropHandler: ((evt: DragEvent, editor: unknown, info: unknown) => Promise) | null; let settings: { extensions: string }; beforeEach(() => { @@ -92,9 +91,7 @@ describe('AttachmentCleanPasteModule', () => { adapter: mockAdapter, createFolder: vi.fn(async () => undefined), // Mirrors the real Obsidian contract: returns ".". - getAvailablePath: vi.fn( - async (basePath: string, ext: string) => `${basePath}.${ext}`, - ), + getAvailablePath: vi.fn(async (basePath: string, ext: string) => `${basePath}.${ext}`), createBinary: vi.fn(async () => undefined), }; @@ -109,9 +106,7 @@ describe('AttachmentCleanPasteModule', () => { module = new AttachmentCleanPasteModule(); module.onload( - mockPlugin as unknown as Parameters< - AttachmentCleanPasteModule['onload'] - >[0], + mockPlugin as unknown as Parameters[0], settings, ); }); @@ -124,10 +119,7 @@ describe('AttachmentCleanPasteModule', () => { ): boolean => ( module as unknown as { - shouldProcess: ( - f: string, - s: { extensions: string }, - ) => boolean; + shouldProcess: (f: string, s: { extensions: string }) => boolean; } ).shouldProcess(filename, cfg); @@ -182,9 +174,7 @@ describe('AttachmentCleanPasteModule', () => { expect(stopPropagation).toHaveBeenCalledTimes(1); // The link was inserted into the editor. expect(editor.replaceSelection).toHaveBeenCalledTimes(1); - expect(editor.replaceSelection).toHaveBeenCalledWith( - '[[attachments/image.png|image]]', - ); + expect(editor.replaceSelection).toHaveBeenCalledWith('[[attachments/image.png|image]]'); }); it('НЕ вызывает preventDefault, если ни один файл не подходит по расширению', async () => { @@ -214,19 +204,14 @@ describe('AttachmentCleanPasteModule', () => { const result = await ( module as unknown as { - processFile: ( - f: File, - s: { extensions: string }, - ) => Promise; + processFile: (f: File, s: { extensions: string }) => Promise; } ).processFile(file, settings); expect(result).toBe('[[attachments/image.png|image]]'); expect(mockVault.createBinary).toHaveBeenCalledTimes(1); // The binary payload was passed to the vault. - const [, payload] = ( - mockVault.createBinary.mock.calls[0] as [string, ArrayBuffer] - ); + const [, payload] = mockVault.createBinary.mock.calls[0] as [string, ArrayBuffer]; expect(payload).toBeInstanceOf(ArrayBuffer); }); @@ -235,10 +220,7 @@ describe('AttachmentCleanPasteModule', () => { const result = await ( module as unknown as { - processFile: ( - f: File, - s: { extensions: string }, - ) => Promise; + processFile: (f: File, s: { extensions: string }) => Promise; } ).processFile(file, settings); @@ -256,21 +238,13 @@ describe('AttachmentCleanPasteModule', () => { describe('onload lifecycle', () => { it('регистрирует оба обработчика (editor-paste и editor-drop) в workspace', () => { - expect(mockWorkspace.on).toHaveBeenCalledWith( - 'editor-paste', - expect.any(Function), - ); - expect(mockWorkspace.on).toHaveBeenCalledWith( - 'editor-drop', - expect.any(Function), - ); + expect(mockWorkspace.on).toHaveBeenCalledWith('editor-paste', expect.any(Function)); + expect(mockWorkspace.on).toHaveBeenCalledWith('editor-drop', expect.any(Function)); }); it('onunload отключает оба зарегистрированных обработчика', () => { module.onunload( - mockPlugin as unknown as Parameters< - AttachmentCleanPasteModule['onunload'] - >[0], + mockPlugin as unknown as Parameters[0], ); expect(mockWorkspace.offref).toHaveBeenCalledTimes(2); diff --git a/tests/modules/preserve-link-aliases.test.ts b/tests/modules/preserve-link-aliases.test.ts index fa256ca..3c4f46d 100644 --- a/tests/modules/preserve-link-aliases.test.ts +++ b/tests/modules/preserve-link-aliases.test.ts @@ -7,6 +7,21 @@ interface PreserveLinkAliasesSettings { processCanvas: boolean; } +// `obsidian` is aliased to __mocks__/obsidian.ts at runtime, but tsc types the +// import against the real package. These helpers keep the mock's extra surface +// visible without weakening the types of the module under test. +type VaultMock = Vault & { + mdFiles: TFile[]; + allFiles: TFile[]; + storage: Map; + readCalls: TFile[]; + modifyCalls: Array<{ file: TFile; content: string }>; +}; + +function makeFile(path: string): TFile { + return new (TFile as unknown as new (path: string) => TFile)(path); +} + // TestablePreserveLinkAliasesModule: surface the private members so tests // can invoke processContent / getFilesToProcess / handleRename directly. class TestablePreserveLinkAliasesModule extends PreserveLinkAliasesModule { @@ -17,21 +32,25 @@ class TestablePreserveLinkAliasesModule extends PreserveLinkAliasesModule { oldBasename: string, settings: PreserveLinkAliasesSettings, ): string { - return (this as unknown as { - processContent: ( - c: string, - nf: string, - nb: string, - ob: string, - s: PreserveLinkAliasesSettings, - ) => string; - }).processContent(content, newFileName, newBasename, oldBasename, settings); + return ( + this as unknown as { + processContent: ( + c: string, + nf: string, + nb: string, + ob: string, + s: PreserveLinkAliasesSettings, + ) => string; + } + ).processContent(content, newFileName, newBasename, oldBasename, settings); } public callGetFilesToProcess(settings: PreserveLinkAliasesSettings): TFile[] { - return (this as unknown as { - getFilesToProcess: (s: PreserveLinkAliasesSettings) => TFile[]; - }).getFilesToProcess(settings); + return ( + this as unknown as { + getFilesToProcess: (s: PreserveLinkAliasesSettings) => TFile[]; + } + ).getFilesToProcess(settings); } public async callHandleRename( @@ -39,9 +58,15 @@ class TestablePreserveLinkAliasesModule extends PreserveLinkAliasesModule { oldPath: string, settings: PreserveLinkAliasesSettings, ): Promise { - await (this as unknown as { - handleRename: (f: TFile, op: string, s: PreserveLinkAliasesSettings) => Promise; - }).handleRename(file, oldPath, settings); + await ( + this as unknown as { + handleRename: ( + f: TFile, + op: string, + s: PreserveLinkAliasesSettings, + ) => Promise; + } + ).handleRename(file, oldPath, settings); } public setVault(vault: Vault): void { @@ -57,12 +82,14 @@ function makeModule(vault?: Vault): TestablePreserveLinkAliasesModule { return module; } -function makeVault(opts: { - mdFiles?: TFile[]; - allFiles?: TFile[]; - storage?: Map; -} = {}): Vault { - const vault = new Vault(); +function makeVault( + opts: { + mdFiles?: TFile[]; + allFiles?: TFile[]; + storage?: Map; + } = {}, +): VaultMock { + const vault = new Vault() as VaultMock; vault.mdFiles = opts.mdFiles ?? []; vault.allFiles = opts.allFiles ?? []; if (opts.storage) { @@ -178,10 +205,10 @@ describe('PreserveLinkAliasesModule', () => { // 5. getFilesToProcess включает canvas-файлы при processCanvas=true // 6. getFilesToProcess НЕ включает canvas-файлы при processCanvas=false describe('getFilesToProcess', () => { - const mdA = new TFile('notes/a.md'); - const mdB = new TFile('notes/b.md'); - const canvasX = new TFile('boards/x.canvas'); - const pdfF = new TFile('papers/f.pdf'); + const mdA = makeFile('notes/a.md'); + const mdB = makeFile('notes/b.md'); + const canvasX = makeFile('boards/x.canvas'); + const pdfF = makeFile('papers/f.pdf'); it('включает canvas-файлы при processCanvas=true', () => { const vault = makeVault({ @@ -217,7 +244,7 @@ describe('PreserveLinkAliasesModule', () => { // 7. handleRename не вызывает processContent если oldBasename === newBasename describe('handleRename', () => { it('не вызывает processContent если oldBasename === newBasename', async () => { - const file = new TFile('notes/note.md'); + const file = makeFile('notes/note.md'); const vault = makeVault({ mdFiles: [file], storage: new Map([[file, 'See [[other.md|note]] here.']]), @@ -233,4 +260,4 @@ describe('PreserveLinkAliasesModule', () => { expect(vault.modifyCalls).toEqual([]); }); }); -}); \ No newline at end of file +}); diff --git a/tests/setup.ts b/tests/setup.ts index e9fab62..1939c6f 100644 --- a/tests/setup.ts +++ b/tests/setup.ts @@ -10,4 +10,4 @@ if (typeof (globalThis as { document?: unknown }).document === 'undefined') { writable: true, configurable: true, }); -} \ No newline at end of file +} diff --git a/tsconfig.json b/tsconfig.json index 24ec216..37a8fbf 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,6 +12,7 @@ "allowSyntheticDefaultImports": true, "strictNullChecks": true, "esModuleInterop": true, + "skipLibCheck": true, "types": ["vitest/globals", "node"] }, "include": ["src/**/*.ts", "tests/**/*.ts", "vitest.config.ts"] diff --git a/vitest.config.ts b/vitest.config.ts index 77e8e83..19a399a 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -15,9 +15,6 @@ export default defineConfig({ environment: 'node', include: ['tests/**/*.test.ts'], setupFiles: ['tests/setup.ts'], - transformMode: { - web: [/\.[jt]sx?$/], - }, }, resolve: { alias: {