diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..1caa99b --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,31 @@ +const typeScriptExtensions = ['.ts']; +module.exports = { + parser: '@typescript-eslint/parser', + extends: ['plugin:@typescript-eslint/recommended'], + plugins: ['unused-imports','eslint-plugin-import'], + parserOptions: { + ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features + sourceType: 'module', // Allows for the use of imports + }, + settings: { + 'import/extensions': typeScriptExtensions, + 'import/external-module-folders': ['node_modules', 'node_modules/@types'], + 'import/parsers': { + '@typescript-eslint/parser': typeScriptExtensions, + }, + 'import/resolver': { + node: { + extensions: typeScriptExtensions, + }, + }, + }, + rules: { + "unused-imports/no-unused-imports": "error", + "@typescript-eslint/no-var-requires": "off", + '@typescript-eslint/no-unused-vars': 'off', + '@typescript-eslint/no-empty-function': 'off', + '@typescript-eslint/no-explicit-any': 'off', + '@typescript-eslint/no-this-alias': 'off', + "@typescript-eslint/quotes": 'off' + }, + }; diff --git a/package.json b/package.json index fe39f2f..4428d21 100755 --- a/package.json +++ b/package.json @@ -506,6 +506,7 @@ }, "scripts": { "dev": "rimraf out && node build.js", + "lint:fix": "eslint src/**/*.ts --fix", "build": "rimraf out && cd packages/zip-viewer && npm run build && cd ../.. && node build.js --mode=production", "package": "vsce package --no-dependencies", "publish": "vsce publish --no-dependencies", @@ -515,8 +516,13 @@ "@types/node": "^17.0.23", "@types/node-fetch": "^2.5.7", "@types/vscode": "^1.64.0", + "@typescript-eslint/eslint-plugin": "^7.1.1", + "@typescript-eslint/parser": "^7.1.1", "esbuild": "^0.14.54", "esbuild-plugin-copy": "^1.3.0", + "eslint": "^8.57.0", + "eslint-plugin-import": "^2.29.1", + "eslint-plugin-unused-imports": "^3.1.0", "rimraf": "^3.0.2" }, "dependencies": { @@ -539,4 +545,4 @@ "puppeteer-core": "^1.20.0", "vscode-html-to-docx": "^1.1.2" } -} \ No newline at end of file +} diff --git a/src/common/fileUtil.ts b/src/common/fileUtil.ts index 8dd5cb4..3b0ad8f 100644 --- a/src/common/fileUtil.ts +++ b/src/common/fileUtil.ts @@ -12,7 +12,7 @@ export function writeFile(path: string, buffer: Buffer) { } export function adjustImgPath(uri: vscode.Uri, withworkspace: boolean = false) { - let imgPath = Global.getConfig("pasterImgPath") + const imgPath = Global.getConfig("pasterImgPath") .replace("${fileName}", parse(uri.fsPath).name.replace(/\s/g, '')) .replace("${now}", new Date().getTime() + "") return { @@ -30,7 +30,7 @@ export function adjustImgPath(uri: vscode.Uri, withworkspace: boolean = false) { export function getWorkspacePath(uri: vscode.Uri): string { const folders = vscode.workspace.workspaceFolders; if (!folders || folders.length == 0) return ''; - let workspacePath = folders[0]?.uri?.fsPath; + const workspacePath = folders[0]?.uri?.fsPath; if (folders.length > 1) { for (const folder of folders) { if (uri.fsPath.includes(folder.uri.fsPath)) { diff --git a/src/provider/markdownEditorProvider.ts b/src/provider/markdownEditorProvider.ts index 1a88c6a..0af9c38 100644 --- a/src/provider/markdownEditorProvider.ts +++ b/src/provider/markdownEditorProvider.ts @@ -26,7 +26,7 @@ export class MarkdownEditorProvider implements vscode.CustomTextEditorProvider { private getFolders(): vscode.Uri[] { const data = []; - for (var i = 65; i <= 90; i++) { + for (let i = 65; i <= 90; i++) { data.push(vscode.Uri.file(`${String.fromCharCode(i)}:/`)) } return data; diff --git a/src/provider/officeViewerProvider.ts b/src/provider/officeViewerProvider.ts index 5433b35..634d4e5 100644 --- a/src/provider/officeViewerProvider.ts +++ b/src/provider/officeViewerProvider.ts @@ -1,17 +1,14 @@ import { ZipService } from '@/service/zip/zipService'; -import axios from 'axios'; import { spawn } from 'child_process'; import { existsSync, mkdirSync, readdirSync, readFileSync } from 'fs'; import { tmpdir } from 'os'; import { basename, extname, parse, resolve } from 'path'; import { TextEncoder } from 'util'; import * as vscode from 'vscode'; -import { workspace } from 'vscode'; import { Handler } from '../common/handler'; import { Output } from '../common/Output'; import { Util } from '../common/util'; import { ViewManager } from '@/common/viewManager'; -import { Global } from '@/common/global'; /** * support view office files diff --git a/src/service/markdownService.ts b/src/service/markdownService.ts index c94be8b..94f6be7 100644 --- a/src/service/markdownService.ts +++ b/src/service/markdownService.ts @@ -101,7 +101,8 @@ export class MarkdownService { } const uri = document.uri; - let { relPath, fullPath } = adjustImgPath(uri) + const info = adjustImgPath(uri), { fullPath } = info; + let { relPath } = info; const imagePath = isAbsolute(fullPath) ? fullPath : `${dirname(uri.fsPath)}/${relPath}`.replace(/\\/g, "/"); this.createImgDir(imagePath); this.saveClipboardImageToFileAndGetPath(imagePath, async (savedImagePath) => { @@ -121,7 +122,7 @@ export class MarkdownService { } if (editor) { editor?.edit(edit => { - let current = editor.selection; + const current = editor.selection; if (current.isEmpty) { edit.insert(current.start, `![${imgName}](${relPath})`); } else { @@ -158,7 +159,7 @@ export class MarkdownService { private saveClipboardImageToFileAndGetPath(imagePath: string, cb: (value: string) => void) { if (!imagePath) return; - let platform = process.platform; + const platform = process.platform; if (platform === 'win32') { // Windows const scriptPath = path.join(this.context.extensionPath, '/lib/pc.ps1'); @@ -179,8 +180,8 @@ export class MarkdownService { }); } else if (platform === 'darwin') { // Mac - let scriptPath = path.join(this.context.extensionPath, './lib/mac.applescript'); - let ascript = spawn('osascript', [scriptPath, imagePath]); + const scriptPath = path.join(this.context.extensionPath, './lib/mac.applescript'); + const ascript = spawn('osascript', [scriptPath, imagePath]); ascript.on('exit', function (code, signal) { }); ascript.stdout.on('data', function (data) { @@ -188,13 +189,13 @@ export class MarkdownService { }); } else { // Linux - let scriptPath = path.join(this.context.extensionPath, './lib/linux.sh'); + const scriptPath = path.join(this.context.extensionPath, './lib/linux.sh'); - let ascript = spawn('sh', [scriptPath, imagePath]); + const ascript = spawn('sh', [scriptPath, imagePath]); ascript.on('exit', function (code, signal) { }); ascript.stdout.on('data', function (data) { - let result = data.toString().trim(); + const result = data.toString().trim(); if (result == "no xclip") { vscode.window.showInformationMessage('You need to install xclip command first.'); return;