From 914493ce4a88c4ac0abd79cc5a851300c23d7458 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 25 Aug 2023 13:09:28 +0200 Subject: [PATCH] style: format with prettier v3 --- README.md | 18 +++++++++--------- renovate.json | 4 +--- src/_utils.ts | 2 +- src/analyze.ts | 24 ++++++++++++------------ src/eval.ts | 14 +++++++------- src/resolve.ts | 16 ++++++++-------- src/syntax.ts | 2 +- test/exports.test.ts | 12 ++++++------ test/fixture/eval.mjs | 8 ++++---- test/fixture/resolve.mjs | 4 +++- tsconfig.json | 4 +--- 11 files changed, 53 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index cc18ea8..a797050 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ import { resolveImports } from "mlly"; // import foo from 'file:///home/user/project/bar.mjs' console.log( - await resolveImports(`import foo from './bar.mjs'`, { url: import.meta.url }) + await resolveImports(`import foo from './bar.mjs'`, { url: import.meta.url }), ); ``` @@ -216,7 +216,7 @@ console.log( findStaticImports(` // Empty line import foo, { bar /* foo */ } from 'baz' -`) +`), ); ``` @@ -276,7 +276,7 @@ import { findDynamicImports } from "mlly"; console.log( findDynamicImports(` const foo = await import('bar') -`) +`), ); ``` @@ -290,7 +290,7 @@ console.log( export const foo = 'bar' export { bar, baz } export default something -`) +`), ); ``` @@ -331,7 +331,7 @@ console.log( export const foo = 'bar' export { bar, baz } export default something -`) +`), ); ``` @@ -369,7 +369,7 @@ await evalModule( import { reverse } from './utils.mjs' console.log(reverse('!emosewa si sj')) `, - { url: import.meta.url } + { url: import.meta.url }, ); ``` @@ -453,7 +453,7 @@ console.log( toDataURL(` // This is an example console.log('Hello world') -`) +`), ); ``` @@ -508,7 +508,7 @@ import { parseNodeModulePath } from "mlly"; // name: "lib" // subpath: "./dist/index.mjs" const { dir, name, subpath } = parseNodeModulePath( - "/src/a/node_modules/lib/dist/index.mjs" + "/src/a/node_modules/lib/dist/index.mjs", ); ``` @@ -521,7 +521,7 @@ import { lookupNodeModuleSubpath } from "mlly"; // subpath: "./utils" const subpath = lookupNodeModuleSubpath( - "/src/a/node_modules/lib/dist/utils.mjs" + "/src/a/node_modules/lib/dist/utils.mjs", ); ``` diff --git a/renovate.json b/renovate.json index a9971c8..57fe916 100644 --- a/renovate.json +++ b/renovate.json @@ -1,5 +1,3 @@ { - "extends": [ - "github>unjs/renovate-config" - ] + "extends": ["github>unjs/renovate-config"] } diff --git a/src/_utils.ts b/src/_utils.ts index 9b7ccd3..a511257 100644 --- a/src/_utils.ts +++ b/src/_utils.ts @@ -12,7 +12,7 @@ export function pcall any>( ): Promise> { try { return Promise.resolve(function_(...arguments_)).catch((error) => - perr(error) + perr(error), ); } catch (error) { return perr(error); diff --git a/src/analyze.ts b/src/analyze.ts index f499f84..30d74ae 100644 --- a/src/analyze.ts +++ b/src/analyze.ts @@ -95,13 +95,13 @@ export function findTypeImports(code: string): TypeImport[] { return [ ...matchAll(IMPORT_NAMED_TYPE_RE, code, { type: "type" }), ...matchAll(ESM_STATIC_IMPORT_RE, code, { type: "static" }).filter( - (match) => /[^A-Za-z]type\s/.test(match.imports) + (match) => /[^A-Za-z]type\s/.test(match.imports), ), ]; } export function parseStaticImport( - matched: StaticImport | TypeImport + matched: StaticImport | TypeImport, ): ParsedStaticImport { const cleanedImports = clearImports(matched.imports); @@ -126,7 +126,7 @@ export function parseStaticImport( } export function parseTypeImport( - matched: TypeImport | StaticImport + matched: TypeImport | StaticImport, ): ParsedStaticImport { if (matched.type === "type") { return parseStaticImport(matched); @@ -169,13 +169,13 @@ export function findExports(code: string): ESMExport[] { const namedExports: NamedExport[] = normalizeNamedExports( matchAll(EXPORT_NAMED_RE, code, { type: "named", - }) + }), ); const destructuredExports: NamedExport[] = matchAll( EXPORT_NAMED_DESTRUCT, code, - { type: "named" } + { type: "named" }, ); for (const namedExport of destructuredExports) { // @ts-expect-error groups @@ -188,7 +188,7 @@ export function findExports(code: string): ESMExport[] { name .replace(/^.*?\s*:\s*/, "") .replace(/\s*=\s*.*$/, "") - .trim() + .trim(), ); } @@ -226,7 +226,7 @@ export function findExports(code: string): ESMExport[] { exports // Filter false positive export matches .filter( - (exp) => !exportLocations || _isExportStatement(exportLocations, exp) + (exp) => !exportLocations || _isExportStatement(exportLocations, exp), ) // Prevent multiple exports of same function, only keep latest iteration of signatures .filter((exp, index, exports) => { @@ -246,14 +246,14 @@ export function findTypeExports(code: string): ESMExport[] { const declaredExports: DeclarationExport[] = matchAll( EXPORT_DECAL_TYPE_RE, code, - { type: "declaration" } + { type: "declaration" }, ); // Find named exports const namedExports: NamedExport[] = normalizeNamedExports( matchAll(EXPORT_NAMED_TYPE_RE, code, { type: "named", - }) + }), ); // Merge and normalize exports @@ -275,7 +275,7 @@ export function findTypeExports(code: string): ESMExport[] { exports // Filter false positive export matches .filter( - (exp) => !exportLocations || _isExportStatement(exportLocations, exp) + (exp) => !exportLocations || _isExportStatement(exportLocations, exp), ) // Prevent multiple exports of same function, only keep latest iteration of signatures .filter((exp, index, exports) => { @@ -325,7 +325,7 @@ export function findExportNames(code: string): string[] { export async function resolveModuleExportNames( id: string, - options?: ResolveOptions + options?: ResolveOptions, ): Promise { const url = await resolvePath(id, options); const code = await loadURL(url); @@ -333,7 +333,7 @@ export async function resolveModuleExportNames( // Explicit named exports const exportNames = new Set( - exports.flatMap((exp) => exp.names).filter(Boolean) + exports.flatMap((exp) => exp.names).filter(Boolean), ); // Recursive * exports diff --git a/src/eval.ts b/src/eval.ts index fc4cc3d..d1ab5e3 100644 --- a/src/eval.ts +++ b/src/eval.ts @@ -11,7 +11,7 @@ const EVAL_ESM_IMPORT_RE = export async function loadModule( id: string, - options: EvaluateOptions = {} + options: EvaluateOptions = {}, ): Promise { const url = await resolve(id, options); const code = await loadURL(url); @@ -20,14 +20,14 @@ export async function loadModule( export async function evalModule( code: string, - options: EvaluateOptions = {} + options: EvaluateOptions = {}, ): Promise { const transformed = await transformModule(code, options); const dataURL = toDataURL(transformed); return import(dataURL).catch((error) => { error.stack = error.stack.replace( new RegExp(dataURL, "g"), - options.url || "_mlly_eval_" + options.url || "_mlly_eval_", ); throw error; }); @@ -35,7 +35,7 @@ export async function evalModule( export function transformModule( code: string, - options?: EvaluateOptions + options?: EvaluateOptions, ): Promise { // Convert JSON to module if (options.url && options.url.endsWith(".json")) { @@ -52,7 +52,7 @@ export function transformModule( export async function resolveImports( code: string, - options?: EvaluateOptions + options?: EvaluateOptions, ): Promise { const imports = [...code.matchAll(EVAL_ESM_IMPORT_RE)].map((m) => m[0]); if (imports.length === 0) { @@ -69,12 +69,12 @@ export async function resolveImports( url = toDataURL(await transformModule(code, { url })); } resolved.set(id, url); - }) + }), ); const re = new RegExp( uniqueImports.map((index) => `(${index})`).join("|"), - "g" + "g", ); return code.replace(re, (id) => resolved.get(id)); } diff --git a/src/resolve.ts b/src/resolve.ts index ad2f9e0..2029201 100644 --- a/src/resolve.ts +++ b/src/resolve.ts @@ -26,7 +26,7 @@ export interface ResolveOptions { function _tryModuleResolve( id: string, url: URL, - conditions: any + conditions: any, ): any | undefined { try { return moduleResolve(id, url, conditions); @@ -77,7 +77,7 @@ function _resolve(id: string, options: ResolveOptions = {}): string { // If url is directory new URL(joinURL(url.pathname, "_index.js"), url), // TODO: Remove in next major version? - new URL("node_modules", url) + new URL("node_modules", url), ); } } @@ -95,7 +95,7 @@ function _resolve(id: string, options: ResolveOptions = {}): string { resolved = _tryModuleResolve( id + prefix + extension, url, - conditionsSet + conditionsSet, ); if (resolved) { break; @@ -113,7 +113,7 @@ function _resolve(id: string, options: ResolveOptions = {}): string { // Throw error if not found if (!resolved) { const error = new Error( - `Cannot find module ${id} imported from ${urls.join(", ")}` + `Cannot find module ${id} imported from ${urls.join(", ")}`, ); // @ts-ignore error.code = "ERR_MODULE_NOT_FOUND"; @@ -139,7 +139,7 @@ export function resolvePathSync(id: string, options?: ResolveOptions): string { export function resolvePath( id: string, - options?: ResolveOptions + options?: ResolveOptions, ): Promise { return pcall(resolvePathSync, id, options); } @@ -171,7 +171,7 @@ export function parseNodeModulePath(path: string) { /** Reverse engineer a subpath export if possible */ export async function lookupNodeModuleSubpath( - path: string + path: string, ): Promise { path = normalize(fileURLToPath(path)); const { name, subpath } = parseNodeModulePath(path); @@ -216,11 +216,11 @@ function _findSubpath(subpath: string, exports: PackageJson["exports"]) { function _flattenExports( exports: Exclude, - path?: string + path?: string, ) { return Object.entries(exports).flatMap(([key, value]) => typeof value === "string" ? [[path ?? key, value]] - : _flattenExports(value, path ?? key) + : _flattenExports(value, path ?? key), ); } diff --git a/src/syntax.ts b/src/syntax.ts index eb4895b..d0759ba 100644 --- a/src/syntax.ts +++ b/src/syntax.ts @@ -50,7 +50,7 @@ const validNodeImportDefaults: ValidNodeImportOptions = { export async function isValidNodeImport( id: string, - _options: ValidNodeImportOptions = {} + _options: ValidNodeImportOptions = {}, ): Promise { if (isNodeBuiltin(id)) { return true; diff --git a/test/exports.test.ts b/test/exports.test.ts index 4fbba3a..e9d2952 100644 --- a/test/exports.test.ts +++ b/test/exports.test.ts @@ -220,7 +220,7 @@ describe("findExportNames", () => { export const foo = 'bar' export { bar, baz } export default something - `) + `), ).toMatchInlineSnapshot(` [ "foo", @@ -258,8 +258,8 @@ describe("resolveModuleExportNames", () => { it("star exports", async () => { expect( await resolveModuleExportNames( - new URL("fixture/exports.mjs", import.meta.url).toString() - ) + new URL("fixture/exports.mjs", import.meta.url).toString(), + ), ).toMatchInlineSnapshot(` [ "foo", @@ -286,8 +286,8 @@ describe("resolveModuleExportNames", () => { it("star exports with package", async () => { expect( await resolveModuleExportNames( - new URL("fixture/package/exports.mjs", import.meta.url).toString() - ) + new URL("fixture/package/exports.mjs", import.meta.url).toString(), + ), ).toMatchInlineSnapshot(` [ "StaticRouter", @@ -316,7 +316,7 @@ describe("findTypeExports", () => { export type { Qux } export type Bing = Qux export declare function getWidget(n: number): Widget - ` + `, ); expect(matches).toMatchInlineSnapshot(` [ diff --git a/test/fixture/eval.mjs b/test/fixture/eval.mjs index fc74f58..d08a5bd 100644 --- a/test/fixture/eval.mjs +++ b/test/fixture/eval.mjs @@ -9,17 +9,17 @@ await evalModule( `, { url: fileURLToPath(import.meta.url), - } + }, ); await loadModule("./hello.mjs", { url: import.meta.url }); console.log( await loadModule("../../package.json", { url: import.meta.url }).then( - (r) => r.default.name - ) + (r) => r.default.name, + ), ); await loadModule("./eval-err.mjs", { url: import.meta.url }).catch((e) => - console.error(e) + console.error(e), ); diff --git a/test/fixture/resolve.mjs b/test/fixture/resolve.mjs index c3d631c..5804cc1 100644 --- a/test/fixture/resolve.mjs +++ b/test/fixture/resolve.mjs @@ -7,5 +7,7 @@ console.log(await resolvePath("./cjs.mjs", { url: import.meta.url })); console.log(await resolvePath("./foo", { url: import.meta.url })); console.log( - await resolveImports("import foo from './eval.mjs'", { url: import.meta.url }) + await resolveImports("import foo from './eval.mjs'", { + url: import.meta.url, + }), ); diff --git a/tsconfig.json b/tsconfig.json index 93d24e0..f592890 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,8 +9,6 @@ "paths": { "mlly": ["./"] }, - "types": [ - "node" - ] + "types": ["node"] } }