From de330ffa955955e3ce10e543b0cf017652816fa7 Mon Sep 17 00:00:00 2001 From: await-ovo <13152410380@163.com> Date: Thu, 17 Mar 2022 18:05:19 +0800 Subject: [PATCH] chore: explicitly specify error type as any --- package.json | 2 +- src/swc/compile.ts | 44 ++++++++++++++++++++++++-------------------- src/swc/dir.ts | 10 +++++----- src/swc/file.ts | 2 +- src/swc/util.ts | 18 +++++++++--------- 5 files changed, 40 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index 9b00764..b3bb85d 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,7 @@ "prettier": "^2.5.1", "simple-git-hooks": "^2.7.0", "ts-jest": "^27.0.4", - "typescript": "~4.3.2" + "typescript": "~4.6.2" }, "peerDependencies": { "@swc/core": "^1.2.66", diff --git a/src/swc/compile.ts b/src/swc/compile.ts index c0cb7b4..097c3c5 100644 --- a/src/swc/compile.ts +++ b/src/swc/compile.ts @@ -1,39 +1,42 @@ -import slash from 'slash'; +import slash from "slash"; import { promises } from "fs"; import { dirname, relative } from "path"; import { transformFile, transformFileSync } from "@swc/core"; import type { Options, Output } from "@swc/core"; -const { - mkdir, - stat, - writeFile -} = promises; +const { mkdir, stat, writeFile } = promises; -function withSourceMap(output: Output, options: Options, destFile: string, destDir: string) { +function withSourceMap( + output: Output, + options: Options, + destFile: string, + destDir: string +) { if (!output.map || options.sourceMaps === "inline") { return { sourceCode: output.code, - } + }; } // TODO: remove once fixed in core https://github.com/swc-project/swc/issues/1388 const sourceMap = JSON.parse(output.map); if (options.sourceFileName) { - sourceMap['sources'][0] = options.sourceFileName; + sourceMap["sources"][0] = options.sourceFileName; } if (options.sourceRoot) { - sourceMap['sourceRoot'] = options.sourceRoot; + sourceMap["sourceRoot"] = options.sourceRoot; } output.map = JSON.stringify(sourceMap); const sourceMapPath = destFile + ".map"; - output.code += `\n//# sourceMappingURL=${slash(relative(destDir, sourceMapPath))}`; + output.code += `\n//# sourceMappingURL=${slash( + relative(destDir, sourceMapPath) + )}`; return { sourceMap: output.map, sourceMapPath, - sourceCode: output.code - } + sourceCode: output.code, + }; } export async function outputResult( @@ -44,11 +47,12 @@ export async function outputResult( ) { const destDir = dirname(destFile); - const { - sourceMap, - sourceMapPath, - sourceCode - } = withSourceMap(output, options, destFile, destDir); + const { sourceMap, sourceMapPath, sourceCode } = withSourceMap( + output, + options, + destFile, + destDir + ); await mkdir(destDir, { recursive: true }); const { mode } = await stat(sourceFile); @@ -58,7 +62,7 @@ export async function outputResult( } else { await Promise.all([ writeFile(destFile, sourceCode, { mode }), - writeFile(sourceMapPath, sourceMap, { mode }) + writeFile(sourceMapPath, sourceMap, { mode }), ]); } } @@ -80,7 +84,7 @@ export async function compile( : await transformFile(filename, options); return result; - } catch (err) { + } catch (err: any) { if (!err.message.includes("ignored by .swcrc")) { throw err; } diff --git a/src/swc/dir.ts b/src/swc/dir.ts index 0568d5a..de838a2 100644 --- a/src/swc/dir.ts +++ b/src/swc/dir.ts @@ -122,7 +122,7 @@ async function initialCompilation(cliOptions: CliOptions, swcOptions: Options) { try { const result = await handleCompile(filename, outDir, sync, swcOptions); results.set(filename, result); - } catch (err) { + } catch (err: any) { console.error(err.message); results.set(filename, CompileStatus.Failed); } @@ -131,7 +131,7 @@ async function initialCompilation(cliOptions: CliOptions, swcOptions: Options) { try { const result = await handleCopy(filename, outDir); results.set(filename, result); - } catch (err) { + } catch (err: any) { console.error(err.message); results.set(filename, CompileStatus.Failed); } @@ -238,7 +238,7 @@ async function watchCompilation(cliOptions: CliOptions, swcOptions: Options) { } else if (copyFiles) { await unlink(getDest(filename, outDir)); } - } catch (err) { + } catch (err: any) { if (err.code !== "ENOENT") { console.error(err.stack); } @@ -262,7 +262,7 @@ async function watchCompilation(cliOptions: CliOptions, swcOptions: Options) { (end[1] / 1000000).toFixed(2) ); } - } catch (err) { + } catch (err: any) { console.error(err.message); } } else if (copyFiles) { @@ -276,7 +276,7 @@ async function watchCompilation(cliOptions: CliOptions, swcOptions: Options) { (end[1] / 1000000).toFixed(2) ); } - } catch (err) { + } catch (err: any) { console.error(`Failed to copy ${filename}`); console.error(err.message); } diff --git a/src/swc/file.ts b/src/swc/file.ts index 50de19e..35eeac8 100644 --- a/src/swc/file.ts +++ b/src/swc/file.ts @@ -128,7 +128,7 @@ export default async function ({ } else { results.delete(filename); } - } catch (err) { + } catch (err: any) { console.error(err.message); results.set(filename, err); } diff --git a/src/swc/util.ts b/src/swc/util.ts index e4473f6..8b602f6 100644 --- a/src/swc/util.ts +++ b/src/swc/util.ts @@ -3,7 +3,6 @@ import slash from "slash"; import { mkdirSync, writeFileSync } from "fs"; import { dirname, relative } from "path"; - export async function transform( filename: string, code: string, @@ -34,7 +33,7 @@ export async function compile( outputPath: string | undefined ): Promise { opts = { - ...opts + ...opts, }; if (outputPath) { opts.outputPath = outputPath; @@ -50,15 +49,15 @@ export async function compile( // https://github.com/swc-project/swc/issues/1388 const sourceMap = JSON.parse(result.map); if (opts.sourceFileName) { - sourceMap['sources'][0] = opts.sourceFileName; + sourceMap["sources"][0] = opts.sourceFileName; } if (opts.sourceRoot) { - sourceMap['sourceRoot'] = opts.sourceRoot; + sourceMap["sourceRoot"] = opts.sourceRoot; } result.map = JSON.stringify(sourceMap); } return result; - } catch (err) { + } catch (err: any) { if (!err.message.includes("ignored by .swcrc")) { throw err; } @@ -68,7 +67,7 @@ export async function compile( export function outputFile( output: swc.Output, filename: string, - sourceMaps: undefined | swc.Options['sourceMaps'] + sourceMaps: undefined | swc.Options["sourceMaps"] ) { const destDir = dirname(filename); mkdirSync(destDir, { recursive: true }); @@ -85,7 +84,6 @@ export function outputFile( writeFileSync(filename, code); } - export function assertCompilationResult( result: Map, quiet = false @@ -96,7 +94,7 @@ export function assertCompilationResult( for (const value of result.values()) { if (value instanceof Error) { failed++; - } else if (value as unknown === 'copied') { + } else if ((value as unknown) === "copied") { copied++; } else if (value) { compiled++; @@ -105,7 +103,9 @@ export function assertCompilationResult( if (!quiet && compiled + copied > 0) { const copyResult = copied === 0 ? " " : ` (copied ${copied}) `; console.info( - `Successfully compiled ${compiled} ${compiled !== 1 ? "files" : "file"}${copyResult}with swc.` + `Successfully compiled ${compiled} ${ + compiled !== 1 ? "files" : "file" + }${copyResult}with swc.` ); }