Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: explicitly specify error type as any & upgrade typescript to latest #124

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
44 changes: 24 additions & 20 deletions src/swc/compile.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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);
Expand All @@ -58,7 +62,7 @@ export async function outputResult(
} else {
await Promise.all([
writeFile(destFile, sourceCode, { mode }),
writeFile(sourceMapPath, sourceMap, { mode })
writeFile(sourceMapPath, sourceMap, { mode }),
]);
}
}
Expand All @@ -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;
}
Expand Down
10 changes: 5 additions & 5 deletions src/swc/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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) {
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/swc/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
18 changes: 9 additions & 9 deletions src/swc/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -34,7 +33,7 @@ export async function compile(
outputPath: string | undefined
): Promise<swc.Output | void> {
opts = {
...opts
...opts,
};
if (outputPath) {
opts.outputPath = outputPath;
Expand All @@ -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;
}
Expand All @@ -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 });
Expand All @@ -85,7 +84,6 @@ export function outputFile(
writeFileSync(filename, code);
}


export function assertCompilationResult<T>(
result: Map<string, Error | T>,
quiet = false
Expand All @@ -96,7 +94,7 @@ export function assertCompilationResult<T>(
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++;
Expand All @@ -105,7 +103,9 @@ export function assertCompilationResult<T>(
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.`
);
}

Expand Down