From 6b600858afa3304c44f78a0b11b1198029ba162a Mon Sep 17 00:00:00 2001 From: Max Milton Date: Mon, 15 Jul 2024 19:55:17 +0900 Subject: [PATCH] bug(ekscss): Fix types and lint issues --- packages/ekscss/src/compiler.ts | 8 ++++---- packages/ekscss/src/helpers.ts | 36 ++++++++++++++++----------------- packages/ekscss/src/types.ts | 8 ++++---- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/packages/ekscss/src/compiler.ts b/packages/ekscss/src/compiler.ts index 6d6895ef..3e7df102 100644 --- a/packages/ekscss/src/compiler.ts +++ b/packages/ekscss/src/compiler.ts @@ -1,5 +1,3 @@ -/* eslint-disable no-restricted-syntax, prefer-object-spread */ - import * as stylis from 'stylis'; import { map as _map, @@ -32,7 +30,9 @@ export function onAfterBuild(callback: BuildHookFn): void { // TODO: Write tests that prove this doesn't mutate the original object. // TODO: This is only a shallow clone, should we do a deep clone? Use structuredClone or klona function mergeDefaultGlobals(globals: Partial): XCSSGlobals { + // eslint-disable-next-line prefer-object-spread const newGlobals = Object.assign({}, globals, { + // eslint-disable-next-line prefer-object-spread fn: Object.assign({}, globals.fn), }); // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition @@ -74,11 +74,11 @@ export function compile( for (const fn of afterBuildFns) fn(); - // @ts-expect-error - reset for next compile + // @ts-expect-error - resetting ctx to initial state // eslint-disable-next-line no-multi-assign ctx.dependencies = ctx.from = ctx.rootDir = ctx.warnings = ctx.x = undefined; - let sourceMap; + let sourceMap: ReturnType | undefined; if (map) { if (process.env.BROWSER) { diff --git a/packages/ekscss/src/helpers.ts b/packages/ekscss/src/helpers.ts index f8428db9..092cd40e 100644 --- a/packages/ekscss/src/helpers.ts +++ b/packages/ekscss/src/helpers.ts @@ -1,5 +1,3 @@ -/* eslint-disable no-plusplus, no-restricted-syntax */ - import type { Context, Middleware, @@ -234,7 +232,7 @@ export function xcss( } } - // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing + // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing, @typescript-eslint/restrict-plus-operands out += (val || (val == null || val === false ? '' : val)) + strings[index]; } @@ -244,27 +242,27 @@ export function xcss( /** * Resolve XCSS plugins when specified as a stylis Middleware or string. * - * Itterate over plugins and load plugins specified as a string that denotes + * Iterate over plugins and load plugins specified as a string that denotes * either the name of a package or a file path. Useful when loading XCSS * configuration from a JSON file. */ export function resolvePlugins(plugins: (Middleware | string)[]): Middleware[] { if (process.env.BROWSER) { throw new Error('Browser runtime does not support resolving plugins'); - } else { - return plugins.map((plugin) => { - if (typeof plugin !== 'string') return plugin; - - try { - // eslint-disable-next-line - const mod = require(plugin); - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - return (mod.default || mod) as Middleware; - } catch (error) { - // eslint-disable-next-line no-console - console.error(`Failed to load plugin "${plugin}"; ${String(error)}`); - return noop; - } - }); } + + return plugins.map((plugin) => { + if (typeof plugin !== 'string') return plugin; + + try { + // eslint-disable-next-line + const mod = require(plugin); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + return (mod.default || mod) as Middleware; + } catch (error) { + // eslint-disable-next-line no-console + console.error(`Failed to load plugin "${plugin}":\n ${String(error)}`); + return noop; + } + }); } diff --git a/packages/ekscss/src/types.ts b/packages/ekscss/src/types.ts index 28f643c3..6c3cc701 100644 --- a/packages/ekscss/src/types.ts +++ b/packages/ekscss/src/types.ts @@ -1,6 +1,6 @@ -import type { SourceMapGenerator } from 'source-map'; -import type { Element as _Element, Middleware } from 'stylis'; -import type { xcss as _xcss } from './helpers'; +import type { SourceMapGenerator } from 'source-map-js'; +import type { Middleware, Element as _Element } from 'stylis'; +import type { xcss } from './helpers'; // eslint-disable-next-line unicorn/prefer-export-from export type { Middleware }; @@ -93,7 +93,7 @@ export interface XCSSCompileOptions { export type BuildHookFn = () => void; -export type XCSSTemplateFn = (xcss: typeof _xcss, x: XCSSGlobals) => string; +export type XCSSTemplateFn = (xcss_: typeof xcss, x: XCSSGlobals) => string; export interface XCSSCompileResult { css: string;