From ff37e410c7d07bc9fa28081c73fc4728d7357b67 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Mon, 28 Feb 2022 12:07:21 +0200 Subject: [PATCH 1/7] Add dist/offline exports to React component --- packages/react/package-lock.json | 4 ++-- packages/react/package.json | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/react/package-lock.json b/packages/react/package-lock.json index d933a3e..ce4476d 100644 --- a/packages/react/package-lock.json +++ b/packages/react/package-lock.json @@ -1,12 +1,12 @@ { "name": "@iconify/react", - "version": "3.1.3", + "version": "3.1.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@iconify/react", - "version": "3.1.3", + "version": "3.1.4", "license": "MIT", "devDependencies": { "@babel/preset-env": "^7.16.11", diff --git a/packages/react/package.json b/packages/react/package.json index a45bb5b..d993218 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -2,7 +2,7 @@ "name": "@iconify/react", "description": "Iconify icon component for React.", "author": "Vjacheslav Trushkin", - "version": "3.1.3", + "version": "3.1.4", "license": "MIT", "bugs": "https://github.com/iconify/iconify/issues", "homepage": "https://iconify.design/", @@ -37,6 +37,11 @@ "import": "./dist/offline.mjs", "types": "./dist/offline.d.ts", "default": "./dist/offline.js" + }, + "./dist/offline": { + "import": "./dist/offline.mjs", + "types": "./dist/offline.d.ts", + "default": "./dist/offline.js" } }, "devDependencies": { From 30826c33e07ef8a49c0a7ac48564dc5a988b0ca0 Mon Sep 17 00:00:00 2001 From: Ramy Melo Date: Fri, 4 Mar 2022 11:32:32 -0500 Subject: [PATCH 2/7] chore: enabled eslint rule - prettier/prettier --- packages/utils/.eslintrc.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/utils/.eslintrc.js b/packages/utils/.eslintrc.js index 4599357..6b73909 100644 --- a/packages/utils/.eslintrc.js +++ b/packages/utils/.eslintrc.js @@ -25,7 +25,6 @@ module.exports = { 'no-mixed-spaces-and-tabs': ['off'], 'no-unused-vars': ['off'], '@typescript-eslint/no-unused-vars-experimental': ['error'], - 'prettier/prettier': ['off'], // TODO: enable and fix prettier errors. }, overrides: [ { From 8f739807d5191a4c2ea9326ac6fee9107c6c2006 Mon Sep 17 00:00:00 2001 From: Ramy Melo Date: Fri, 4 Mar 2022 11:37:21 -0500 Subject: [PATCH 3/7] chore: auto fixed prettier rules in src and tests npx eslint --fix src/**/*.ts npx eslint --fix tests/**/*.ts --- packages/utils/src/loader/custom.ts | 10 ++++-- packages/utils/src/loader/fs.ts | 16 ++++++---- packages/utils/src/loader/loader.ts | 17 +++++----- packages/utils/src/loader/modern.ts | 4 +-- packages/utils/src/loader/types.ts | 16 +++++----- packages/utils/src/loader/utils.ts | 33 +++++++++++++------- packages/utils/src/svg/encode-svg-for-css.ts | 13 ++++++-- packages/utils/tests/get-custom-icon-test.ts | 7 +++-- packages/utils/tests/iconify-icon-test.ts | 32 ++++++++++--------- packages/utils/tests/load-icon-test.ts | 19 ++++++----- 10 files changed, 103 insertions(+), 64 deletions(-) diff --git a/packages/utils/src/loader/custom.ts b/packages/utils/src/loader/custom.ts index 321a6c2..c714bc7 100644 --- a/packages/utils/src/loader/custom.ts +++ b/packages/utils/src/loader/custom.ts @@ -1,5 +1,9 @@ import createDebugger from 'debug'; -import type { CustomIconLoader, IconifyLoaderOptions, InlineCollection } from './types'; +import type { + CustomIconLoader, + IconifyLoaderOptions, + InlineCollection, +} from './types'; import { mergeIconProps } from './utils'; const debug = createDebugger('@iconify-loader:custom'); @@ -11,7 +15,7 @@ export async function getCustomIcon( custom: CustomIconLoader | InlineCollection, collection: string, icon: string, - options?: IconifyLoaderOptions, + options?: IconifyLoaderOptions ): Promise { let result: string | undefined | null; @@ -37,7 +41,7 @@ export async function getCustomIcon( collection, icon, options, - undefined, + undefined ); } } diff --git a/packages/utils/src/loader/fs.ts b/packages/utils/src/loader/fs.ts index 50a12f5..2fe2e58 100644 --- a/packages/utils/src/loader/fs.ts +++ b/packages/utils/src/loader/fs.ts @@ -1,12 +1,15 @@ import { promises as fs, Stats } from 'fs'; -import { isPackageExists, resolveModule } from 'local-pkg' -import type { IconifyJSON } from '@iconify/types' +import { isPackageExists, resolveModule } from 'local-pkg'; +import type { IconifyJSON } from '@iconify/types'; import { tryInstallPkg } from './install-pkg'; const _collections: Record> = {}; const isLegacyExists = isPackageExists('@iconify/json'); -export async function loadCollectionFromFS(name: string, autoInstall = false): Promise { +export async function loadCollectionFromFS( + name: string, + autoInstall = false +): Promise { if (!_collections[name]) { _collections[name] = task(); } @@ -30,9 +33,10 @@ export async function loadCollectionFromFS(name: string, autoInstall = false): P return undefined; } if (stat && stat.isFile()) { - return JSON.parse(await fs.readFile(jsonPath as string, 'utf8')) as IconifyJSON; - } - else { + return JSON.parse( + await fs.readFile(jsonPath as string, 'utf8') + ) as IconifyJSON; + } else { return undefined; } } diff --git a/packages/utils/src/loader/loader.ts b/packages/utils/src/loader/loader.ts index 830a799..3ffed28 100644 --- a/packages/utils/src/loader/loader.ts +++ b/packages/utils/src/loader/loader.ts @@ -3,7 +3,7 @@ import { searchForIcon } from './modern'; import { warnOnce } from './install-pkg'; import type { IconifyLoaderOptions } from './types'; -export const isNode = typeof process < 'u' && typeof process.stdout < 'u' +export const isNode = typeof process < 'u' && typeof process.stdout < 'u'; export async function loadIcon( collection: string, @@ -33,8 +33,7 @@ async function importFsModule(): Promise { try { // cjs environments return require('./fs.js'); - } - catch { + } catch { return undefined; } } @@ -44,12 +43,15 @@ async function loadNodeBuiltinIcon( collection: string, icon: string, options?: IconifyLoaderOptions, - warn = true, + warn = true ): Promise { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const { loadCollectionFromFS } = await importFsModule(); - const iconSet = await loadCollectionFromFS(collection, options?.autoInstall); + const iconSet = await loadCollectionFromFS( + collection, + options?.autoInstall + ); if (iconSet) { // possible icon names const ids = [ @@ -61,7 +63,8 @@ async function loadNodeBuiltinIcon( } if (warn) { - warnOnce(`failed to load \`@iconify-json/${collection}\`, have you installed it?`); + warnOnce( + `failed to load \`@iconify-json/${collection}\`, have you installed it?` + ); } } - diff --git a/packages/utils/src/loader/modern.ts b/packages/utils/src/loader/modern.ts index a5b5f51..36bc4cc 100644 --- a/packages/utils/src/loader/modern.ts +++ b/packages/utils/src/loader/modern.ts @@ -13,7 +13,7 @@ export async function searchForIcon( iconSet: IconifyJSON, collection: string, ids: string[], - options?: IconifyLoaderOptions, + options?: IconifyLoaderOptions ): Promise { let iconData: FullIconifyIcon | null; const { customize } = options?.customizations ?? {}; @@ -34,7 +34,7 @@ export async function searchForIcon( collection, id, options, - () => attributes, + () => attributes ); } } diff --git a/packages/utils/src/loader/types.ts b/packages/utils/src/loader/types.ts index ce0018c..3fb278c 100644 --- a/packages/utils/src/loader/types.ts +++ b/packages/utils/src/loader/types.ts @@ -80,36 +80,36 @@ export type IconifyLoaderOptions = { * * @default false */ - addXmlNs?: boolean + addXmlNs?: boolean; /** * Scale of icons against 1em */ - scale?: number + scale?: number; /** * Style to apply to icons by default * * @default '' */ - defaultStyle?: string + defaultStyle?: string; /** * Class names to apply to icons by default * * @default '' */ - defaultClass?: string + defaultClass?: string; /** * Loader for custom loaders */ - customCollections?: Record + customCollections?: Record; /** * Icon customizer */ - customizations?: IconCustomizations + customizations?: IconCustomizations; /** * Auto install icon sources package when the usages is detected @@ -118,5 +118,5 @@ export type IconifyLoaderOptions = { * * @default false */ - autoInstall?: boolean -} + autoInstall?: boolean; +}; diff --git a/packages/utils/src/loader/utils.ts b/packages/utils/src/loader/utils.ts index fec837b..c1d0f7e 100644 --- a/packages/utils/src/loader/utils.ts +++ b/packages/utils/src/loader/utils.ts @@ -6,16 +6,21 @@ export async function mergeIconProps( collection: string, icon: string, options?: IconifyLoaderOptions, - propsProvider?: () => Awaitable>, + propsProvider?: () => Awaitable> ): Promise { - const { scale, addXmlNs = false } = options ?? {} - const { - additionalProps = {}, - iconCustomizer, - } = options?.customizations ?? {}; + const { scale, addXmlNs = false } = options ?? {}; + const { additionalProps = {}, iconCustomizer } = + options?.customizations ?? {}; const props: Record = (await propsProvider?.()) ?? {}; - if (!svg.includes(" width=") && !svg.includes(" height=") && typeof scale === 'number') { - if ((typeof props.width === 'undefined' || props.width === null) && (typeof props.height === 'undefined' || props.height === null)) { + if ( + !svg.includes(' width=') && + !svg.includes(' height=') && + typeof scale === 'number' + ) { + if ( + (typeof props.width === 'undefined' || props.width === null) && + (typeof props.height === 'undefined' || props.height === null) + ) { props.width = `${scale}em`; props.height = `${scale}em`; } @@ -33,18 +38,24 @@ export async function mergeIconProps( props['xmlns'] = 'http://www.w3.org/2000/svg'; } // add xmlns:xlink if xlink present and the xmlns missing - if (!svg.includes(' xmlns:xlink=') && svg.includes('xlink:') && !props['xmlns:xlink']) { + if ( + !svg.includes(' xmlns:xlink=') && + svg.includes('xlink:') && + !props['xmlns:xlink'] + ) { props['xmlns:xlink'] = 'http://www.w3.org/1999/xlink'; } } svg = svg.replace( ' `${p}="${props[p]}"`).join(' ')}` + ` `${p}="${props[p]}"`) + .join(' ')}` ); if (svg && options) { - const { defaultStyle, defaultClass } = options + const { defaultStyle, defaultClass } = options; // additional props and iconCustomizer takes precedence if (defaultClass && !svg.includes(' class=')) { svg = svg.replace('') ? svg.replace('', '') : svg; if (!useSvg.includes(' xmlns:xlink=') && useSvg.includes(' xlink:')) { - useSvg = useSvg.replace(' { const result = await getCustomIcon(() => svg, 'a', 'b', { customizations: { transform(icon) { - return icon.replace(' -1).toBeTruthy(); expect(result && result.indexOf('height="1em"') > -1).toBeTruthy(); diff --git a/packages/utils/tests/iconify-icon-test.ts b/packages/utils/tests/iconify-icon-test.ts index 75a284a..ad702cb 100644 --- a/packages/utils/tests/iconify-icon-test.ts +++ b/packages/utils/tests/iconify-icon-test.ts @@ -1,16 +1,17 @@ import { loadIcon } from '../lib'; describe('Testing loadIcon with @iconify-json/flat-color-icons>', () => { - test('loadIcon works', async () => { const result = await loadIcon('flat-color-icons', 'up-right'); expect(result).toBeTruthy(); }); test('loadIcon adds xmlns:xlink', async () => { - const result = await loadIcon('flat-color-icons', 'up-right', { addXmlNs: true }); + const result = await loadIcon('flat-color-icons', 'up-right', { + addXmlNs: true, + }); expect(result).toBeTruthy(); - expect(result && result.indexOf('xmlns:xlink=') > - 1).toBeTruthy(); + expect(result && result.indexOf('xmlns:xlink=') > -1).toBeTruthy(); }); test('loadIcon with customize with default style and class', async () => { @@ -23,13 +24,13 @@ describe('Testing loadIcon with @iconify-json/flat-color-icons>', () => { props.height = '2em'; return props; }, - } + }, }); expect(result).toBeTruthy(); - expect(result && result.indexOf('margin-top: 1rem;') > - 1).toBeTruthy(); - expect(result && result.indexOf('class="clazz"') > - 1).toBeTruthy(); - expect(result && result.indexOf('width="2em"') > - 1).toBeTruthy(); - expect(result && result.indexOf('height="2em"') > - 1).toBeTruthy(); + expect(result && result.indexOf('margin-top: 1rem;') > -1).toBeTruthy(); + expect(result && result.indexOf('class="clazz"') > -1).toBeTruthy(); + expect(result && result.indexOf('width="2em"') > -1).toBeTruthy(); + expect(result && result.indexOf('height="2em"') > -1).toBeTruthy(); }); test('loadIcon preserves customizations order', async () => { @@ -39,16 +40,19 @@ describe('Testing loadIcon with @iconify-json/flat-color-icons>', () => { defaultClass: 'clazz1', customizations: { additionalProps: { - 'width': '2em', - 'height': '2em', - 'style': 'color: blue;', - 'class': 'clazz2', + width: '2em', + height: '2em', + style: 'color: blue;', + class: 'clazz2', }, // it will never be called, it is not a custom icon transform(icon) { - return icon.replace(' { +const loader: CustomIconLoader = async (name) => { return await fs.readFile(`${fixturesDir}/${name}.svg`, 'utf8'); -} +}; describe('Testing loadIcon', () => { test('CustomCollection', async () => { @@ -13,8 +13,8 @@ describe('Testing loadIcon', () => { expect(svg).toBeTruthy(); const result = await loadIcon('a', 'circle', { customCollections: { - 'a': { - 'circle': svg as string, + a: { + circle: svg as string, }, }, }); @@ -27,13 +27,16 @@ describe('Testing loadIcon', () => { expect(svg).toBeTruthy(); const result = await loadIcon('a', 'circle', { customCollections: { - 'a': { - 'circle': svg as string, + a: { + circle: svg as string, }, }, customizations: { transform(icon) { - return icon.replace(' { const result = await loadIcon('a', '1f3eb', { customCollections: { - 'a': { + a: { '1f3eb': svg as string, }, }, From ff261a0216f6da4d2e2f1789d1c8fe8eeaab3b6e Mon Sep 17 00:00:00 2001 From: Ramy Melo Date: Fri, 4 Mar 2022 15:28:31 -0500 Subject: [PATCH 4/7] chore: enabled extra ts eslint rule @typescript-eslint/recommended-requiring-type-checking --- packages/utils/.eslintrc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utils/.eslintrc.js b/packages/utils/.eslintrc.js index 6b73909..194f68e 100644 --- a/packages/utils/.eslintrc.js +++ b/packages/utils/.eslintrc.js @@ -8,7 +8,7 @@ module.exports = { extends: [ 'eslint:recommended', 'plugin:@typescript-eslint/recommended', - // 'plugin:@typescript-eslint/recommended-requiring-type-checking', + 'plugin:@typescript-eslint/recommended-requiring-type-checking', 'plugin:prettier/recommended', ], globals: { From e5e4ddf49e270079854357b81e4a12cb1c8bf03f Mon Sep 17 00:00:00 2001 From: Ramy Melo Date: Fri, 4 Mar 2022 16:04:14 -0500 Subject: [PATCH 5/7] chore: eslint --fix after enabling ts typed rules. --- packages/utils/src/colors/index.ts | 6 +++--- packages/utils/src/icon-set/convert-info.ts | 4 ++-- packages/utils/src/icon-set/validate.ts | 2 +- packages/utils/src/svg/size.ts | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/utils/src/colors/index.ts b/packages/utils/src/colors/index.ts index f2f4ed4..ce4bf71 100644 --- a/packages/utils/src/colors/index.ts +++ b/packages/utils/src/colors/index.ts @@ -60,8 +60,8 @@ function fromFunction(value: string): Color | null { } // Get function and values - const func = (parts[0] as string).trim(); - const content = (parts[1] as string).trim(); + const func = parts[0].trim(); + const content = parts[1].trim(); // Get alpha and split content let values: string[]; @@ -133,7 +133,7 @@ function fromFunction(value: string): Color | null { const isPercentage: boolean[] = []; const numbers: number[] = []; for (let i = 0; i < 3; i++) { - const colorStr = values[i] as string; + const colorStr = values[i]; const index = colorStr.indexOf('%'); const hasPercentage = index !== -1; if (hasPercentage && index !== colorStr.length - 1) { diff --git a/packages/utils/src/icon-set/convert-info.ts b/packages/utils/src/icon-set/convert-info.ts index cd8e688..30642ba 100644 --- a/packages/utils/src/icon-set/convert-info.ts +++ b/packages/utils/src/icon-set/convert-info.ts @@ -88,9 +88,9 @@ export function convertIconSetInfo( // Get name let name: string; if (typeof source.name === 'string') { - name = source.name as string; + name = source.name; } else if (typeof source.title === 'string') { - name = source.title as string; + name = source.title; } else { return null; } diff --git a/packages/utils/src/icon-set/validate.ts b/packages/utils/src/icon-set/validate.ts index f92a35c..f0eb341 100644 --- a/packages/utils/src/icon-set/validate.ts +++ b/packages/utils/src/icon-set/validate.ts @@ -304,7 +304,7 @@ export function validateIconSet( if (typeof data.chars === 'object') { const chars = data.chars; Object.keys(chars).forEach((char) => { - if (!char.match(matchChar) || typeof chars[char] !== 'string') { + if (!matchChar.exec(char) || typeof chars[char] !== 'string') { if (fix) { delete chars[char]; return; diff --git a/packages/utils/src/svg/size.ts b/packages/utils/src/svg/size.ts index 6a49e5c..a435117 100644 --- a/packages/utils/src/svg/size.ts +++ b/packages/utils/src/svg/size.ts @@ -33,7 +33,7 @@ export function calculateSize( const newParts = []; let code = oldParts.shift() as string; - let isNumber = unitsTest.test(code as string); + let isNumber = unitsTest.test(code); // eslint-disable-next-line no-constant-condition while (true) { From fa01bbe962fc6ef2b2209ecb923aa8442e8aadeb Mon Sep 17 00:00:00 2001 From: Ramy Melo Date: Fri, 4 Mar 2022 16:18:02 -0500 Subject: [PATCH 6/7] refactor(eslint): refactored away mix type errors @typescript-eslint/restrict-plus-operands --- packages/utils/src/colors/index.ts | 14 +++++---- packages/utils/src/svg/build.ts | 36 ++++++++++++++++-------- packages/utils/src/svg/id.ts | 4 ++- packages/utils/tests/replace-ids-test.ts | 12 ++++++-- 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/packages/utils/src/colors/index.ts b/packages/utils/src/colors/index.ts index ce4bf71..84a4725 100644 --- a/packages/utils/src/colors/index.ts +++ b/packages/utils/src/colors/index.ts @@ -435,7 +435,11 @@ export function colorToString(color: Color): string { } case 'hsl': { - const list = [color.h, color.s + '%', color.l + '%']; + const list = [ + color.h, + color.s.toString() + '%', + color.l.toString() + '%', + ]; if (color.alpha !== 1) { list.push(color.alpha); } @@ -445,17 +449,17 @@ export function colorToString(color: Color): string { } case 'lab': { - const list = [color.l + '%', color.a, color.b]; + const list = [color.l.toString() + '%', color.a, color.b]; if (color.alpha !== 1) { - list.push('/ ' + color.alpha); + list.push('/ ' + color.alpha.toString()); } return 'lab(' + list.join(' ') + ')'; } case 'lch': { - const list = [color.l + '%', color.c, color.h]; + const list = [color.l.toString() + '%', color.c, color.h]; if (color.alpha !== 1) { - list.push('/ ' + color.alpha); + list.push('/ ' + color.alpha.toString()); } return 'lch(' + list.join(' ') + ')'; } diff --git a/packages/utils/src/svg/build.ts b/packages/utils/src/svg/build.ts index 79834a4..65744c7 100644 --- a/packages/utils/src/svg/build.ts +++ b/packages/utils/src/svg/build.ts @@ -102,9 +102,9 @@ export function iconToSVG( // Horizontal flip transformations.push( 'translate(' + - (box.width + box.left) + + (box.width + box.left).toString() + ' ' + - (0 - box.top) + + (0 - box.top).toString() + ')' ); transformations.push('scale(-1 1)'); @@ -114,9 +114,9 @@ export function iconToSVG( // Vertical flip transformations.push( 'translate(' + - (0 - box.left) + + (0 - box.left).toString() + ' ' + - (box.height + box.top) + + (box.height + box.top).toString() + ')' ); transformations.push('scale(1 -1)'); @@ -133,7 +133,11 @@ export function iconToSVG( // 90deg tempValue = box.height / 2 + box.top; transformations.unshift( - 'rotate(90 ' + tempValue + ' ' + tempValue + ')' + 'rotate(90 ' + + tempValue.toString() + + ' ' + + tempValue.toString() + + ')' ); break; @@ -141,9 +145,9 @@ export function iconToSVG( // 180deg transformations.unshift( 'rotate(180 ' + - (box.width / 2 + box.left) + + (box.width / 2 + box.left).toString() + ' ' + - (box.height / 2 + box.top) + + (box.height / 2 + box.top).toString() + ')' ); break; @@ -152,7 +156,11 @@ export function iconToSVG( // 270deg tempValue = box.width / 2 + box.left; transformations.unshift( - 'rotate(-90 ' + tempValue + ' ' + tempValue + ')' + 'rotate(-90 ' + + tempValue.toString() + + ' ' + + tempValue.toString() + + ')' ); break; } @@ -214,8 +222,8 @@ export function iconToSVG( } // Convert to string - width = typeof width === 'string' ? width : width + ''; - height = typeof height === 'string' ? height : height + ''; + width = typeof width === 'string' ? width : width.toString() + ''; + height = typeof height === 'string' ? height : height.toString() + ''; // Result const result: IconifyIconBuildResult = { @@ -224,7 +232,13 @@ export function iconToSVG( height, preserveAspectRatio: preserveAspectRatio(customisations), viewBox: - box.left + ' ' + box.top + ' ' + box.width + ' ' + box.height, + box.left.toString() + + ' ' + + box.top.toString() + + ' ' + + box.width.toString() + + ' ' + + box.height.toString(), }, body, }; diff --git a/packages/utils/src/svg/id.ts b/packages/utils/src/svg/id.ts index 0eba6be..26ce358 100644 --- a/packages/utils/src/svg/id.ts +++ b/packages/utils/src/svg/id.ts @@ -52,7 +52,9 @@ export function replaceIDs( // Replace with unique ids ids.forEach((id) => { const newID = - typeof prefix === 'function' ? prefix(id) : prefix + counter++; + typeof prefix === 'function' + ? prefix(id) + : prefix + (counter++).toString(); const escapedID = id.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); diff --git a/packages/utils/tests/replace-ids-test.ts b/packages/utils/tests/replace-ids-test.ts index 897033b..0e44e5e 100644 --- a/packages/utils/tests/replace-ids-test.ts +++ b/packages/utils/tests/replace-ids-test.ts @@ -9,7 +9,9 @@ describe('Testing replaceIDs', () => { // Using callback let counter = 0; - expect(replaceIDs(body, () => 'callback' + counter++)).toBe(expected); + expect( + replaceIDs(body, () => 'callback' + (counter++).toString()) + ).toBe(expected); }); test('Many IDs', () => { @@ -23,7 +25,9 @@ describe('Testing replaceIDs', () => { // Using callback let counter = 0; - expect(replaceIDs(body, () => 'callbackID' + counter++)).toBe( + expect( + replaceIDs(body, () => 'callbackID' + (counter++).toString()) + ).toBe( body .replace(/ssvg-id-1st-place-medala/g, 'callbackID0') .replace(/ssvg-id-1st-place-medalb/g, 'callbackID7') @@ -70,7 +74,9 @@ describe('Testing replaceIDs', () => { // To avoid messing up counter, using custom callback let counter = 0; - expect(replaceIDs(body, () => 'callbackID' + counter++)).toBe( + expect( + replaceIDs(body, () => 'callbackID' + (counter++).toString()) + ).toBe( body .replace(/ssvg-id-compassa/g, 'callbackID2') .replace(/ssvg-id-compassb/g, 'callbackID0') From 30ab5eba2a631cf25bff81fd739a5a11bed53d36 Mon Sep 17 00:00:00 2001 From: Ramy Melo Date: Fri, 4 Mar 2022 17:04:30 -0500 Subject: [PATCH 7/7] refactor(types): refactored away ts errors --- packages/utils/src/icon-set/get-icons.ts | 10 +++++----- packages/utils/src/loader/fs.ts | 2 +- packages/utils/src/loader/loader.ts | 16 ++++++++++++++-- packages/utils/tests/validate-icon-test.ts | 9 ++++++++- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/packages/utils/src/icon-set/get-icons.ts b/packages/utils/src/icon-set/get-icons.ts index 25df213..f29c218 100644 --- a/packages/utils/src/icon-set/get-icons.ts +++ b/packages/utils/src/icon-set/get-icons.ts @@ -19,7 +19,7 @@ export function getIcons( ): IconifyJSON | null { const result: IconifyJSON = { prefix: data.prefix, - icons: Object.create(null), + icons: Object.create(null) as never, }; const tested: Set = new Set(); let empty = true; @@ -43,9 +43,9 @@ export function getIcons( const copied = copy(data.aliases[name].parent, iteration + 1); if (copied) { if (result.aliases === void 0) { - result.aliases = Object.create(null); + result.aliases = Object.create(null) as never; } - result.aliases![name] = { ...data.aliases[name] }; + result.aliases[name] = { ...data.aliases[name] }; } return copied; } @@ -56,9 +56,9 @@ export function getIcons( const copied = copy(parent, iteration + 1); if (copied) { if (result.aliases === void 0) { - result.aliases = Object.create(null); + result.aliases = Object.create(null) as never; } - result.aliases![name] = { + result.aliases[name] = { parent, }; } diff --git a/packages/utils/src/loader/fs.ts b/packages/utils/src/loader/fs.ts index 2fe2e58..bd906e1 100644 --- a/packages/utils/src/loader/fs.ts +++ b/packages/utils/src/loader/fs.ts @@ -10,7 +10,7 @@ export async function loadCollectionFromFS( name: string, autoInstall = false ): Promise { - if (!_collections[name]) { + if (!(await _collections[name])) { _collections[name] = task(); } return _collections[name]; diff --git a/packages/utils/src/loader/loader.ts b/packages/utils/src/loader/loader.ts index 3ffed28..70703fc 100644 --- a/packages/utils/src/loader/loader.ts +++ b/packages/utils/src/loader/loader.ts @@ -32,7 +32,10 @@ async function importFsModule(): Promise { } catch { try { // cjs environments - return require('./fs.js'); + // eslint-disable-next-line @typescript-eslint/no-var-requires + return require('./fs.js') as Promise< + typeof import('./fs') | undefined + >; } catch { return undefined; } @@ -45,9 +48,18 @@ async function loadNodeBuiltinIcon( options?: IconifyLoaderOptions, warn = true ): Promise { + type IconifyJSON = Parameters[0]; + type LoadCollectionFromFS = ( + collection: string, + autoInstall?: boolean + ) => Promise; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - const { loadCollectionFromFS } = await importFsModule(); + const { + loadCollectionFromFS, + }: { + loadCollectionFromFS: LoadCollectionFromFS; + } = await importFsModule(); const iconSet = await loadCollectionFromFS( collection, options?.autoInstall diff --git a/packages/utils/tests/validate-icon-test.ts b/packages/utils/tests/validate-icon-test.ts index 0f4b6f0..777533c 100644 --- a/packages/utils/tests/validate-icon-test.ts +++ b/packages/utils/tests/validate-icon-test.ts @@ -2,7 +2,14 @@ import { validateIconSet } from '../lib/icon-set/validate'; describe('Testing validating icon', () => { // Add various types for testing - const validationValues = new Map(); + const validationValues = new Map< + | boolean + | Record + | [] + | number + | string, + { text: string; type: string } + >(); beforeAll(() => { validationValues.set(true, {