From b3fab602a1e519a3aa712f9203bb848593d39bdb Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Mon, 6 Dec 2021 16:41:59 +0800 Subject: [PATCH] chore: fix some lint errors --- package.json | 1 + pnpm-lock.yaml | 31 +++++++++++++++++++++++++++++++ src/cli-main.ts | 2 +- src/esbuild/external.ts | 2 +- src/esbuild/index.ts | 21 +++++++++++++++------ src/esbuild/svelte.ts | 11 ++++++----- src/esbuild/swc.ts | 2 +- src/index.ts | 6 +++--- src/load.ts | 18 +++++++++++------- src/rollup.ts | 2 +- src/rollup/ts-resolve.ts | 2 +- 11 files changed, 72 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index 9f005df..ef6f550 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "@babel/core": "^7.13.15", "@rollup/plugin-json": "^4.1.0", "@swc/core": "^1.2.112", + "@types/babel__core": "^7.1.16", "@types/buble": "^0.19.2", "@types/debug": "^4.1.5", "@types/flat": "^5.0.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2bf5881..c2bae53 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,6 +4,7 @@ specifiers: '@babel/core': ^7.13.15 '@rollup/plugin-json': ^4.1.0 '@swc/core': ^1.2.112 + '@types/babel__core': ^7.1.16 '@types/buble': ^0.19.2 '@types/debug': ^4.1.5 '@types/flat': ^5.0.2 @@ -62,6 +63,7 @@ devDependencies: '@babel/core': 7.16.0 '@rollup/plugin-json': 4.1.0_rollup@2.60.1 '@swc/core': 1.2.112 + '@types/babel__core': 7.1.16 '@types/buble': 0.19.2 '@types/debug': 4.1.7 '@types/flat': 5.0.2 @@ -480,6 +482,35 @@ packages: '@swc/core-win32-x64-msvc': 1.2.112 dev: true + /@types/babel__core/7.1.16: + resolution: {integrity: sha512-EAEHtisTMM+KaKwfWdC3oyllIqswlznXCIVCt7/oRNrh+DhgT4UEBNC/jlADNjvw7UnfbcdkGQcPVZ1xYiLcrQ==} + dependencies: + '@babel/parser': 7.16.4 + '@babel/types': 7.16.0 + '@types/babel__generator': 7.6.3 + '@types/babel__template': 7.4.1 + '@types/babel__traverse': 7.14.2 + dev: true + + /@types/babel__generator/7.6.3: + resolution: {integrity: sha512-/GWCmzJWqV7diQW54smJZzWbSFf4QYtF71WCKhcx6Ru/tFyQIY2eiiITcCAeuPbNSvT9YCGkVMqqvSk2Z0mXiA==} + dependencies: + '@babel/types': 7.16.0 + dev: true + + /@types/babel__template/7.4.1: + resolution: {integrity: sha512-azBFKemX6kMg5Io+/rdGT0dkGreboUVR0Cdm3fz9QJWpaQGJRQXl7C+6hOTCZcMll7KFyEQpgbYI2lHdsS4U7g==} + dependencies: + '@babel/parser': 7.16.4 + '@babel/types': 7.16.0 + dev: true + + /@types/babel__traverse/7.14.2: + resolution: {integrity: sha512-K2waXdXBi2302XUdcHcR1jCeU0LL4TD9HRs/gk0N2Xvrht+G/BfJa4QObBQZfhMdxiCpV3COl5Nfq4uKTeTnJA==} + dependencies: + '@babel/types': 7.16.0 + dev: true + /@types/buble/0.19.2: resolution: {integrity: sha512-uUD8zIfXMKThmFkahTXDGI3CthFH1kMg2dOm3KLi4GlC5cbARA64bEcUMbbWdWdE73eoc/iBB9PiTMqH0dNS2Q==} dependencies: diff --git a/src/cli-main.ts b/src/cli-main.ts index ea0c8d4..6beb664 100644 --- a/src/cli-main.ts +++ b/src/cli-main.ts @@ -117,7 +117,7 @@ export async function main(options: Options = {}) { options.inject = inject } if (flags.define) { - const define: any = flat(flags.define) + const define: Record = flat(flags.define) options.define = define } if (flags.loader) { diff --git a/src/esbuild/external.ts b/src/esbuild/external.ts index 1120ca8..4c57479 100644 --- a/src/esbuild/external.ts +++ b/src/esbuild/external.ts @@ -13,7 +13,7 @@ export const externalPlugin = ({ external?: (string | RegExp)[] noExternal?: (string | RegExp)[] skipNodeModulesBundle?: boolean - tsconfigResolvePaths?: Record + tsconfigResolvePaths?: Record }): Plugin => { return { name: `external`, diff --git a/src/esbuild/index.ts b/src/esbuild/index.ts index c491cd8..82fbca5 100644 --- a/src/esbuild/index.ts +++ b/src/esbuild/index.ts @@ -26,7 +26,7 @@ const getOutputExtensionMap = ( format: Format ) => { const isModule = pkgTypeField === 'module' - const map: any = {} + const map: Record = {} if (isModule && format === 'cjs') { map['.js'] = '.cjs' } @@ -172,7 +172,9 @@ export async function runEsbuild( }, inject: [ format === 'cjs' ? path.join(__dirname, '../assets/cjs_shims.js') : '', - format === 'esm' && platform === 'node' ? path.join(__dirname, '../assets/esm_shims.js') : '', + format === 'esm' && platform === 'node' + ? path.join(__dirname, '../assets/esm_shims.js') + : '', ...(options.inject || []), ].filter(Boolean), outdir: @@ -262,10 +264,17 @@ export async function runEsbuild( spreadRest: true, }, }).code - } catch (error: any) { - throw new PrettyError( - `Error compiling to es5 target:\n${error.snippet}` - ) + } catch (error) { + if (error instanceof Error) { + throw new PrettyError( + `Error compiling to es5 target:\n${ + // @ts-expect-error not sure how to type error.snippet + error.snippet || error.message + }` + ) + } else { + throw error + } } } // Workaround to enable code splitting for cjs format diff --git a/src/esbuild/svelte.ts b/src/esbuild/svelte.ts index c900fbc..3d7679e 100644 --- a/src/esbuild/svelte.ts +++ b/src/esbuild/svelte.ts @@ -37,11 +37,12 @@ export const sveltePlugin = ({ } // This converts a message in Svelte's format to esbuild's format - let convertMessage = ({ message, start, end }: any) => { + const convertMessage = ({ message, start, end }: any) => { let location if (start && end) { - let lineText = source.split(/\r\n|\r|\n/g)[start.line - 1] - let lineEnd = start.line === end.line ? end.column : lineText.length + const lineText = source.split(/\r\n|\r|\n/g)[start.line - 1] + const lineEnd = + start.line === end.line ? end.column : lineText.length location = { file: filename, line: start.line, @@ -54,8 +55,8 @@ export const sveltePlugin = ({ } // Load the file from the file system - let source = await fs.promises.readFile(args.path, 'utf8') - let filename = path.relative(process.cwd(), args.path) + const source = await fs.promises.readFile(args.path, 'utf8') + const filename = path.relative(process.cwd(), args.path) // Convert Svelte syntax to JavaScript try { diff --git a/src/esbuild/swc.ts b/src/esbuild/swc.ts index 8f373a5..4d4dd54 100644 --- a/src/esbuild/swc.ts +++ b/src/esbuild/swc.ts @@ -9,7 +9,7 @@ export const swcPlugin = (): Plugin => { return { name: 'swc', - async setup(build) { + setup(build) { const swc: typeof import('@swc/core') = localRequire('@swc/core') if (!swc) { diff --git a/src/index.ts b/src/index.ts index 132086d..9681bee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,7 +24,7 @@ export type NormalizedOptions = Omit< 'dts' > & { dts?: DtsConfig - tsconfigResolvePaths: Record + tsconfigResolvePaths: Record tsconfigDecoratorMetadata?: boolean } @@ -144,7 +144,7 @@ export async function build(_options: Options) { /** Files imported by the entry */ const buildDependencies: Set = new Set() - async function killPreviousProcess() { + const killPreviousProcess = async () => { if (existingOnSuccess) { await killProcess({ pid: existingOnSuccess.pid, @@ -244,7 +244,7 @@ export async function build(_options: Options) { ignorePermissionErrors: true, ignored, }) - watcher.on('all', async (type, file) => { + watcher.on('all', (type, file) => { file = slash(file) if (!buildDependencies.has(file)) return diff --git a/src/load.ts b/src/load.ts index 991b42a..7221262 100644 --- a/src/load.ts +++ b/src/load.ts @@ -9,18 +9,22 @@ const joycon = new JoyCon() const loadJson = async (filepath: string) => { try { return jsoncParse(await fs.promises.readFile(filepath, 'utf8')) - } catch (error: any) { - throw new Error( - `Failed to parse ${path.relative(process.cwd(), filepath)}: ${ - error.message - }` - ) + } catch (error) { + if (error instanceof Error) { + throw new Error( + `Failed to parse ${path.relative(process.cwd(), filepath)}: ${ + error.message + }` + ) + } else { + throw error + } } } const jsonLoader = { test: /\.json$/, - async load(filepath: string) { + load(filepath: string) { return loadJson(filepath) }, } diff --git a/src/rollup.ts b/src/rollup.ts index 2e1aa9b..505febc 100644 --- a/src/rollup.ts +++ b/src/rollup.ts @@ -196,7 +196,7 @@ async function watchRollup(options: { ...options.inputConfig, plugins: options.inputConfig.plugins, output: options.outputConfig, - }).on('event', async (event) => { + }).on('event', (event) => { if (event.code === 'START') { logger.info('dts', 'Build start') } else if (event.code === 'BUNDLE_END') { diff --git a/src/rollup/ts-resolve.ts b/src/rollup/ts-resolve.ts index 3dd9c4e..bf3dd28 100644 --- a/src/rollup/ts-resolve.ts +++ b/src/rollup/ts-resolve.ts @@ -13,7 +13,7 @@ const resolveModule = ( ): Promise => new Promise((resolve, reject) => { _resolve(id, opts, (err, res) => { - // @ts-expect-error + // @ts-expect-error error code is not typed if (err?.code === 'MODULE_NOT_FOUND') return resolve(null) if (err) return reject(err) resolve(res || null)