From c9cd044e16bbcaa981ffddfcccd443cc8d650d34 Mon Sep 17 00:00:00 2001 From: Roy Ivy III Date: Sat, 18 Nov 2023 22:41:30 -0600 Subject: [PATCH] wip/change ~ fix `biome` linter complaints - lint/complexity/useLiteralKeys - lint/correctness/noConstructorReturn - lint/style/noParameterAssign - lint/style/useTemplate - lint/suspicious/noExplicitAny - lint/suspicious/noRedundantUseStrict --- .commitlint.config.js | 6 ++-- biome.json | 7 +++++ eg/show-paths.cjs.js | 39 ++++++++++++++------------ eg/show-paths.esm.mjs | 46 +++++++++++++++---------------- eg/show-paths.local.deno.ts | 20 ++++++-------- eg/show-paths.remote(CDN).deno.ts | 20 ++++++-------- eg/show-paths.remote.deno.ts | 20 ++++++-------- eg/show-paths.ts | 22 +++++++-------- src/lib/XDGAppPaths.ts | 7 +++-- src/mod.test.ts | 14 ++++++---- src/platform-adapters/node.ts | 6 ++-- 11 files changed, 105 insertions(+), 102 deletions(-) create mode 100644 biome.json diff --git a/.commitlint.config.js b/.commitlint.config.js index 882f850..00376d2 100644 --- a/.commitlint.config.js +++ b/.commitlint.config.js @@ -6,9 +6,9 @@ /* @prettier */ // note: (dprint) {.dprint.json}.prettier.associations should contain the name of this file -const isNPMTestDist = !!process.env['npm_config_test_dist']; -const isTestDist = !!process.env['test_dist']; -const isTestRelease = !!process.env['test_release']; +const isNPMTestDist = !!process.env.npm_config_test_dist; +const isTestDist = !!process.env.test_dist; +const isTestRelease = !!process.env.test_release; /** Relax linting rules/strictures (for development; *not* when submitting for distribution/release). */ const relaxedReview = !(isNPMTestDist || isTestDist || isTestRelease); diff --git a/biome.json b/biome.json new file mode 100644 index 0000000..22d9a15 --- /dev/null +++ b/biome.json @@ -0,0 +1,7 @@ +{ + "json": { + "parser": { + "allowComments": true + } + } +} diff --git a/eg/show-paths.cjs.js b/eg/show-paths.cjs.js index 5db8986..fd7da4d 100644 --- a/eg/show-paths.cjs.js +++ b/eg/show-paths.cjs.js @@ -1,7 +1,7 @@ // deno-fmt-ignore-file ## prefer customized `prettier` formatting // # spell-checker:ignore APPNAME /* eslint-env es6, node */ -'use strict'; +// 'use strict'; const path = require('path'); @@ -20,27 +20,30 @@ xdgAppPaths.log = function (dirOptions) { return typeof x; } - if (typeOf(dirOptions) === 'boolean') { - dirOptions = { isolated: dirOptions }; - } - - if ( - typeOf(dirOptions) !== 'object' || - dirOptions === null || - typeOf(dirOptions.isolated) !== 'boolean' - ) { - dirOptions = { isolated: self.$isolated() }; - } - - return path.join(self.state(dirOptions), (dirOptions.isolated ? '' : self.$name() + '-') + 'log'); + const dirOptions_ = (() => { + if (typeOf(dirOptions) === 'boolean') { + return { isolated: dirOptions }; + } + if ( + typeOf(dirOptions) !== 'object' || + dirOptions == null || + typeOf(dirOptions.isolated) !== 'boolean' + ) { + return { isolated: self.$isolated() }; + } + })(); + + return path.join( + self.state(dirOptions_), + `${dirOptions_.isolated ? '' : `${self.$name()}-`} + 'log'` + ); }; function showObjectEntries(obj) { - var strings = []; - Object.keys(obj).forEach((key) => { + const strings = Object.keys(obj).map((key) => { const value = obj[key]; const val = typeof value === 'function' ? value() : value; - strings.push(key + ' = ' + val); + return `${key} = ${val}`; }); return strings.join('\n'); } @@ -53,7 +56,7 @@ console.log('appPaths.log(false):', xdgAppPaths.log(false)); console.log('appPaths.log(true):', xdgAppPaths.log(true)); // eslint-disable-next-line functional/immutable-data -delete process.env.XDG_CONFIG_HOME; +process.env.XDG_CONFIG_HOME = void 0; // eslint-disable-next-line functional/no-let let p = require(xdgAppPathsModulePath)('dross'); diff --git a/eg/show-paths.esm.mjs b/eg/show-paths.esm.mjs index c553770..df18aa9 100644 --- a/eg/show-paths.esm.mjs +++ b/eg/show-paths.esm.mjs @@ -1,7 +1,7 @@ // deno-fmt-ignore-file ## prefer customized `prettier` formatting // # spell-checker:ignore APPNAME /* eslint-env es6, node */ -'use strict'; +// 'use strict'; import path from 'path'; import { inspect } from 'util'; @@ -11,13 +11,10 @@ import { inspect } from 'util'; import xdgAppPaths from '../dist/cjs/esm-wrapper/mod.esm.js'; function objectEntries(obj) { - const map = {}; - Object.keys(obj).forEach((key) => { + return Object.keys(obj).map((key) => { const value = obj[key]; - const val = typeof value === 'function' ? value() : value; - map[key] = val; + return typeof value === 'function' ? value() : value; }); - return map; } // Extend appPaths with a "log" location function @@ -28,27 +25,30 @@ xdgAppPaths.log = function (dirOptions = null) { return typeof x; } - if (typeOf(dirOptions) === 'boolean') { - dirOptions = { isolated: dirOptions }; - } - - if ( - typeOf(dirOptions) !== 'object' || - dirOptions === null || - typeOf(dirOptions.isolated) !== 'boolean' - ) { - dirOptions = { isolated: self.$isolated() }; - } - - return path.join(self.state(dirOptions), (dirOptions.isolated ? '' : self.$name() + '-') + 'log'); + const dirOptions_ = (() => { + if (typeOf(dirOptions) === 'boolean') { + return { isolated: dirOptions }; + } + if ( + typeOf(dirOptions) !== 'object' || + dirOptions == null || + typeOf(dirOptions.isolated) !== 'boolean' + ) { + return { isolated: self.$isolated() }; + } + })(); + + return path.join( + self.state(dirOptions_), + `${dirOptions_.isolated ? '' : `${self.$name()}-`} + 'log'` + ); }; function showObjectEntries(obj) { - var strings = []; - Object.keys(obj).forEach((key) => { + const strings = Object.keys(obj).map((key) => { const value = obj[key]; const val = typeof value === 'function' ? value() : value; - strings.push(key + ' = ' + val); + return `${key} = ${val}`; }); return strings.join('\n'); } @@ -60,7 +60,7 @@ console.log('appPaths.log():', xdgAppPaths.log()); console.log('appPaths.log(false):', xdgAppPaths.log(false)); console.log('appPaths.log(true):', xdgAppPaths.log(true)); -delete process.env.XDG_CONFIG_HOME; +process.env.XDG_CONFIG_HOME = void 0; // eslint-disable-next-line functional/no-let let p = xdgAppPaths('dross'); diff --git a/eg/show-paths.local.deno.ts b/eg/show-paths.local.deno.ts index 33707ee..b964fba 100644 --- a/eg/show-paths.local.deno.ts +++ b/eg/show-paths.local.deno.ts @@ -14,7 +14,7 @@ const deno = Deno; const inspect = deno.inspect; -/* eslint-disable @typescript-eslint/no-explicit-any , functional/immutable-data , no-console , security-node/detect-crlf , security/detect-object-injection */ +/* eslint-disable functional/immutable-data , no-console , security-node/detect-crlf , security/detect-object-injection */ // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore // deno-type import @@ -23,14 +23,12 @@ import xdgAppPaths from '../src/mod.deno.ts'; // @ts-ignore // deno-type import import type { DirOptions, XDGAppPaths } from '../src/mod.deno.ts'; -function objectEntries(obj: any) { - const map: any = {}; - Object.keys(obj).forEach((key) => { - const value = obj[key]; - const val = typeof value === 'function' ? value() : value; - map[key] = val; +function objectEntries(obj: unknown) { + const obj_ = obj as { readonly [key: string]: unknown }; + return Object.keys(obj_).map((key) => { + const value = obj_[key]; + return typeof value === 'function' ? value() : value; }); - return map; } // eslint-disable-next-line functional/prefer-readonly-type @@ -39,9 +37,9 @@ type XDGAppPathsWithLog = XDGAppPaths & { log: (dirOptions?: DirOptions | boolea // Extend appPaths with a "log" location (xdgAppPaths as XDGAppPathsWithLog).log = function log(dirOptions?: DirOptions | boolean) { const self = xdgAppPaths; - dirOptions = dirOptions ?? { isolated: self.$isolated() }; - const isolated = typeof dirOptions === 'boolean' ? dirOptions : dirOptions.isolated || true; - return path.join(self.state(isolated), (isolated ? '' : self.$name() + '-') + 'log'); + const dirOptions_ = dirOptions ?? { isolated: self.$isolated() }; + const isolated = typeof dirOptions_ === 'boolean' ? dirOptions_ : dirOptions_.isolated || true; + return path.join(self.state(isolated), `${isolated ? '' : `${self.$name()}-`} + 'log'`); }; console.log('appPaths:', inspect(xdgAppPaths)); diff --git a/eg/show-paths.remote(CDN).deno.ts b/eg/show-paths.remote(CDN).deno.ts index 0d818d3..091a3bf 100644 --- a/eg/show-paths.remote(CDN).deno.ts +++ b/eg/show-paths.remote(CDN).deno.ts @@ -4,7 +4,7 @@ // --allow-env (transitive from 'xdg-app-paths') // --allow-read -/* eslint-disable @typescript-eslint/ban-ts-comment , @typescript-eslint/no-explicit-any , functional/immutable-data , import/order , no-console , security-node/detect-crlf , security/detect-object-injection */ +/* eslint-disable @typescript-eslint/ban-ts-comment , functional/immutable-data , import/order , no-console , security-node/detect-crlf , security/detect-object-injection */ // @ts-ignore // deno-type URL import import * as path from 'https://deno.land/std@0.150.0/path/mod.ts'; @@ -21,14 +21,12 @@ import xdgAppPaths from 'https://cdn.jsdelivr.net/gh/rivy/js.xdg-app-paths@v8.1. // @ts-ignore // deno-type import import type { DirOptions, XDGAppPaths } from '../src/mod.deno.ts'; -function objectEntries(obj: any) { - const map: any = {}; - Object.keys(obj).forEach((key) => { - const value = obj[key]; - const val = typeof value === 'function' ? value() : value; - map[key] = val; +function objectEntries(obj: unknown) { + const obj_ = obj as { readonly [key: string]: unknown }; + return Object.keys(obj_).map((key) => { + const value = obj_[key]; + return typeof value === 'function' ? value() : value; }); - return map; } // eslint-disable-next-line functional/prefer-readonly-type @@ -37,9 +35,9 @@ type XDGAppPathsWithLog = XDGAppPaths & { log: (dirOptions?: DirOptions | boolea // Extend appPaths with a "log" location (xdgAppPaths as XDGAppPathsWithLog).log = function log(dirOptions?: DirOptions | boolean) { const self = xdgAppPaths; - dirOptions = dirOptions ?? { isolated: self.$isolated() }; - const isolated = typeof dirOptions === 'boolean' ? dirOptions : dirOptions.isolated || true; - return path.join(self.state(isolated), (isolated ? '' : self.$name() + '-') + 'log'); + const dirOptions_ = dirOptions ?? { isolated: self.$isolated() }; + const isolated = typeof dirOptions_ === 'boolean' ? dirOptions_ : dirOptions_.isolated || true; + return path.join(self.state(isolated), `${isolated ? '' : `${self.$name()}-`}log`); }; console.log('appPaths:', inspect(xdgAppPaths)); diff --git a/eg/show-paths.remote.deno.ts b/eg/show-paths.remote.deno.ts index c3cdc69..6dd70e6 100644 --- a/eg/show-paths.remote.deno.ts +++ b/eg/show-paths.remote.deno.ts @@ -4,7 +4,7 @@ // --allow-env (transitive from 'xdg-app-paths') // --allow-read -/* eslint-disable @typescript-eslint/ban-ts-comment , @typescript-eslint/no-explicit-any , functional/immutable-data , import/order , no-console , security-node/detect-crlf , security/detect-object-injection */ +/* eslint-disable @typescript-eslint/ban-ts-comment , functional/immutable-data , import/order , no-console , security-node/detect-crlf , security/detect-object-injection */ // @ts-ignore // deno-type URL import import * as path from 'https://deno.land/std@0.150.0/path/mod.ts'; @@ -21,14 +21,12 @@ import xdgAppPaths from 'https://deno.land/x/xdg_app_paths@v8.1.0/src/mod.deno.t // @ts-ignore // deno-type import import type { DirOptions, XDGAppPaths } from '../src/mod.deno.ts'; -function objectEntries(obj: any) { - const map: any = {}; - Object.keys(obj).forEach((key) => { - const value = obj[key]; - const val = typeof value === 'function' ? value() : value; - map[key] = val; +function objectEntries(obj: unknown) { + const obj_ = obj as { readonly [key: string]: unknown }; + return Object.keys(obj_).map((key) => { + const value = obj_[key]; + return typeof value === 'function' ? value() : value; }); - return map; } // eslint-disable-next-line functional/prefer-readonly-type @@ -37,9 +35,9 @@ type XDGAppPathsWithLog = XDGAppPaths & { log: (dirOptions?: DirOptions | boolea // Extend appPaths with a "log" location (xdgAppPaths as XDGAppPathsWithLog).log = function log(dirOptions?: DirOptions | boolean) { const self = xdgAppPaths; - dirOptions = dirOptions ?? { isolated: self.$isolated() }; - const isolated = typeof dirOptions === 'boolean' ? dirOptions : dirOptions.isolated || true; - return path.join(self.state(isolated), (isolated ? '' : self.$name() + '-') + 'log'); + const dirOptions_ = dirOptions ?? { isolated: self.$isolated() }; + const isolated = typeof dirOptions_ === 'boolean' ? dirOptions_ : dirOptions_.isolated || true; + return path.join(self.state(isolated), `${isolated ? '' : `${self.$name()}-`}log`); }; console.log('appPaths:', inspect(xdgAppPaths)); diff --git a/eg/show-paths.ts b/eg/show-paths.ts index 0298ca7..e821143 100644 --- a/eg/show-paths.ts +++ b/eg/show-paths.ts @@ -3,19 +3,17 @@ import path from 'path'; import { inspect } from 'util'; -/* eslint-disable @typescript-eslint/no-explicit-any , functional/immutable-data , no-console , security-node/detect-crlf , security/detect-object-injection */ +/* eslint-disable functional/immutable-data , no-console , security-node/detect-crlf , security/detect-object-injection */ import xdgAppPaths from '../dist/cjs/mod.cjs'; import type { DirOptions, XDGAppPaths } from '../src/mod.esm'; -function objectEntries(obj: any) { - const map: any = {}; - Object.keys(obj).forEach((key) => { - const value = obj[key]; - const val = typeof value === 'function' ? value() : value; - map[key] = val; +function objectEntries(obj: unknown) { + const obj_ = obj as { readonly [key: string]: unknown }; + return Object.keys(obj_).map((key) => { + const value = obj_[key]; + return typeof value === 'function' ? value() : value; }); - return map; } // eslint-disable-next-line functional/prefer-readonly-type @@ -24,9 +22,9 @@ type XDGAppPathsWithLog = XDGAppPaths & { log: (dirOptions?: DirOptions | boolea // Extend appPaths with a "log" location (xdgAppPaths as XDGAppPathsWithLog).log = function log(dirOptions?: DirOptions | boolean) { const self = xdgAppPaths; - dirOptions = dirOptions ?? { isolated: self.$isolated() }; - const isolated = typeof dirOptions === 'boolean' ? dirOptions : dirOptions.isolated || true; - return path.join(self.state(isolated), (isolated ? '' : self.$name() + '-') + 'log'); + const dirOptions_ = dirOptions ?? { isolated: self.$isolated() }; + const isolated = typeof dirOptions_ === 'boolean' ? dirOptions_ : dirOptions_.isolated || true; + return path.join(self.state(isolated), `${isolated ? '' : `${self.$name()}-`} + 'log'`); }; console.log('appPaths:', inspect(xdgAppPaths)); @@ -36,7 +34,7 @@ console.log(objectEntries(xdgAppPaths)); console.log('appPaths.log(false):', (xdgAppPaths as XDGAppPathsWithLog).log(false)); console.log('appPaths.log(true):', (xdgAppPaths as XDGAppPathsWithLog).log(true)); -delete process.env.XDG_CONFIG_HOME; +process.env.XDG_CONFIG_HOME = void 0; // eslint-disable-next-line functional/no-let let p = xdgAppPaths('dross'); diff --git a/src/lib/XDGAppPaths.ts b/src/lib/XDGAppPaths.ts index bcb8ec5..96f780c 100644 --- a/src/lib/XDGAppPaths.ts +++ b/src/lib/XDGAppPaths.ts @@ -1,7 +1,7 @@ // deno-fmt-ignore-file ## prefer customized `prettier` formatting // # spell-checker:ignore APPDATA LOCALAPPDATA MacOS tempdir /* eslint-env es6, node */ -'use strict'; +// 'use strict'; import { Platform } from '../platform-adapters/_base.js'; @@ -133,8 +133,8 @@ function Adapt(adapter_: Platform.Adapter): { readonly XDGAppPaths: XDGAppPaths }; function isIsolated(dirOptions?: DirOptions | boolean): boolean { - dirOptions = dirOptions ?? { isolated: isolated_ }; - const isolated = isBoolean(dirOptions) ? dirOptions : dirOptions.isolated ?? isolated_; + const dirOptions_ = dirOptions ?? { isolated: isolated_ }; + const isolated = isBoolean(dirOptions_) ? dirOptions_ : dirOptions_.isolated ?? isolated_; return isolated; } @@ -176,6 +176,7 @@ function Adapt(adapter_: Platform.Adapter): { readonly XDGAppPaths: XDGAppPaths .map((s) => path.join(s, finalPathSegment(dirOptions))) as readonly string[]; }; + // biome-ignore lint/correctness/noConstructorReturn: return class as a callable Function object return XDGAppPaths as XDGAppPaths; } } diff --git a/src/mod.test.ts b/src/mod.test.ts index 755035f..1d42333 100644 --- a/src/mod.test.ts +++ b/src/mod.test.ts @@ -26,11 +26,13 @@ test('CJS <=> ESM', (t) => { t.is(typeof mCJS, typeof mESM); t.is(Object.keys(mCJS).length, api.length); t.is(Object.keys(mCJS).length, Object.keys(mESM).length); - api.forEach((key) => { - /* eslint-disable @typescript-eslint/no-explicit-any , security/detect-object-injection */ + // avoid Array.prototype.forEach() to satisfy linter; also ref: [Avoid forEach()](https://aeflash.com/2014-11/avoid-foreach.html) @@ + api.reduce((_, key) => { + /* eslint-disable security/detect-object-injection */ t.is(typeof mCJS[key], 'function'); - t.is(typeof mCJS[key], typeof (mESM as any)[key]); - t.deepEqual(mCJS[key](), (mESM as any)[key]()); - /* eslint-enable @typescript-eslint/no-explicit-any , security/detect-object-injection */ - }); + t.is(typeof mCJS[key], typeof (mESM as unknown as { readonly [key: string]: unknown })[key]); + t.deepEqual(mCJS[key](), (mESM as typeof mCJS)[key]()); + /* eslint-enable security/detect-object-injection */ + return null; + }, null); }); diff --git a/src/platform-adapters/node.ts b/src/platform-adapters/node.ts index e649440..ecf6166 100644 --- a/src/platform-adapters/node.ts +++ b/src/platform-adapters/node.ts @@ -19,13 +19,11 @@ export const adapter: Platform.Adapter = { // HACK: additional comparison `require?.main?.filename !== process.execArgv[0]` compensates for ESM scripts run via `ts-node` (requireMainFilename !== process.execArgv[0] ? requireMainFilename : void 0) || // HACK: `process._eval` is undocumented; used here (again, for ESM) as evidence of `node -e ...` differentiating between immediate eval vs file-bound scripts - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (typeof (process as any)._eval === 'undefined' ? process.argv[1] : void 0); + ((process as { readonly _eval?: unknown })._eval == null ? process.argv[1] : void 0); return filename; }, pkgMainFilename: () => { - // eslint-disable-next-line @typescript-eslint/no-explicit-any - return (process as any).pkg ? process.execPath : void 0; + return (process as { readonly pkg?: unknown }).pkg ? process.execPath : void 0; }, }, path,