From d244419a5d12b4516c25aea09c7a5a3d6db86f35 Mon Sep 17 00:00:00 2001 From: Pauline Date: Sun, 25 Aug 2024 05:20:38 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feature:=20add=20svg=20formatting?= =?UTF-8?q?=20with=20prettier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pauline --- package.json | 2 +- packages/eslint-config/fixtures/input/svg.svg | 53 +++++++++++ .../fixtures/output/with-formatters/svg.svg | 87 +++++++++++++++++++ packages/eslint-config/package.json | 2 +- .../eslint-config/src/configs/formatters.ts | 28 +++++- packages/eslint-config/src/globs.ts | 1 + packages/eslint-config/src/types/index.ts | 14 ++- packages/utils/src/core/crypto.ts | 41 ++++----- pnpm-lock.yaml | 52 +++++------ 9 files changed, 228 insertions(+), 52 deletions(-) create mode 100644 packages/eslint-config/fixtures/input/svg.svg create mode 100644 packages/eslint-config/fixtures/output/with-formatters/svg.svg diff --git a/package.json b/package.json index d0fc70a..0f3e3dd 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "eslint": "^9.9.1", "jiti": "^1.21.6", "tsup": "^8.2.4", - "tsx": "^4.17.0", + "tsx": "^4.18.0", "typescript": "^5.5.4", "unbuild": "^2.0.0", "vite": "^5.4.2", diff --git a/packages/eslint-config/fixtures/input/svg.svg b/packages/eslint-config/fixtures/input/svg.svg new file mode 100644 index 0000000..355b077 --- /dev/null +++ b/packages/eslint-config/fixtures/input/svg.svg @@ -0,0 +1,53 @@ + + + SVG Logo + Designed for the SVG Logo Contest in 2006 by Harvey Rayner, and adopted by W3C in 2009. It is available under the Creative Commons license for those who have an SVG product or who are using SVG on their site. + + + + + SVG Logo + 14-08-2009 + W3CHarvey Rayner, designer + See document description + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/packages/eslint-config/fixtures/output/with-formatters/svg.svg b/packages/eslint-config/fixtures/output/with-formatters/svg.svg new file mode 100644 index 0000000..94d524e --- /dev/null +++ b/packages/eslint-config/fixtures/output/with-formatters/svg.svg @@ -0,0 +1,87 @@ + + SVG Logo + + Designed for the SVG Logo Contest in 2006 by Harvey Rayner, and adopted by W3C in 2009. It is available under the + Creative Commons license for those who have an SVG product or who are using SVG on their site. + + + + + + SVG Logo + 14-08-2009 + + + W3C + + + Harvey Rayner, designer + + + See document description + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/packages/eslint-config/package.json b/packages/eslint-config/package.json index df0e136..12e4fad 100644 --- a/packages/eslint-config/package.json +++ b/packages/eslint-config/package.json @@ -171,7 +171,7 @@ "@types/eslint-plugin-jsx-a11y": "^6.9.0", "@types/fs-extra": "^11.0.4", "@types/prompts": "^2.4.9", - "@unocss/eslint-plugin": "^0.62.2", + "@unocss/eslint-plugin": "^0.62.3", "astro-eslint-parser": "^1.0.2", "eslint": "^9.9.1", "eslint-plugin-astro": "^1.2.3", diff --git a/packages/eslint-config/src/configs/formatters.ts b/packages/eslint-config/src/configs/formatters.ts index b1bf835..01e7e27 100644 --- a/packages/eslint-config/src/configs/formatters.ts +++ b/packages/eslint-config/src/configs/formatters.ts @@ -1,5 +1,5 @@ import { isPackageExists } from 'local-pkg'; -import { GLOB_ASTRO, GLOB_ASTRO_TS, GLOB_CSS, GLOB_GRAPHQL, GLOB_HTML, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS, GLOB_XML } from '../globs'; +import { GLOB_ASTRO, GLOB_ASTRO_TS, GLOB_CSS, GLOB_GRAPHQL, GLOB_HTML, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS, GLOB_SVG, GLOB_XML } from '../globs'; import { ensurePackages, interopDefault, isPackageInScope, parserPlain } from '../utils'; import type { OptionsFormatters, StylisticConfig, TypedFlatConfigItem, VendoredPrettierOptions } from '../types'; import { StylisticConfigDefaults } from './stylistic'; @@ -7,6 +7,7 @@ import { StylisticConfigDefaults } from './stylistic'; type VendoredPrettierXMLOptions = Pick; export async function formatters(options: OptionsFormatters | true = {}, stylistic: StylisticConfig = {}): Promise { + const isXmlInScope = isPackageInScope('@prettier/plugin-xml'); if (options === true) options = { astro: isPackageInScope('prettier-plugin-astro'), @@ -15,14 +16,15 @@ export async function formatters(options: OptionsFormatters | true = {}, stylist html: true, markdown: true, slidev: isPackageExists('@slidev/cli'), - xml: isPackageInScope('@prettier/plugin-xml'), + svg: isXmlInScope, + xml: isXmlInScope, }; await ensurePackages([ 'eslint-plugin-format', options.markdown && options.slidev ? 'prettier-plugin-slidev' : undefined, options.astro ? 'prettier-plugin-astro' : undefined, - options.xml ? '@prettier/plugin-xml' : undefined, + (options.xml || options.svg) ? '@prettier/plugin-xml' : undefined, ]); if (options.slidev && options.markdown !== true && options.markdown !== 'prettier') @@ -154,6 +156,26 @@ export async function formatters(options: OptionsFormatters | true = {}, stylist }, }); + if (options.svg) + configs.push({ + files: [GLOB_SVG], + languageOptions: { + parser: parserPlain, + }, + name: 'petal/formatter/svg', + rules: { + 'format/prettier': [ + 'error', + { + ...prettierXmlOptions, + ...prettierOptions, + parser: 'xml', + plugins: ['@prettier/plugin-xml'], + }, + ], + }, + }); + if (options.markdown) { const formatter = options.markdown === true ? 'prettier' diff --git a/packages/eslint-config/src/globs.ts b/packages/eslint-config/src/globs.ts index 5056890..19b2f75 100644 --- a/packages/eslint-config/src/globs.ts +++ b/packages/eslint-config/src/globs.ts @@ -34,6 +34,7 @@ export const GLOB_GRAPHQL = '**/*.{g,graph}ql'; export const GLOB_YAML = '**/*.y?(a)ml'; export const GLOB_TOML = '**/*.toml'; export const GLOB_XML = '**/*.xml'; +export const GLOB_SVG = '**/*.svg'; export const GLOB_HTML = '**/*.htm?(l)'; export const GLOB_BIN = '**/bin/**/*'; export const GLOB_BIN_SRC = `**/bin.${GLOB_SRC_EXT}`; diff --git a/packages/eslint-config/src/types/index.ts b/packages/eslint-config/src/types/index.ts index 4e7c453..c465755 100644 --- a/packages/eslint-config/src/types/index.ts +++ b/packages/eslint-config/src/types/index.ts @@ -99,12 +99,24 @@ export interface OptionsFormatters { * Requires installing: * - `@prettier/plugin-xml` * - * Currently only support Prettier. + * Currently only supports Prettier. * * @default auto-detect based on the dependencies */ xml?: 'prettier' | boolean; + /** + * Enable formatting support for SVG. + * + * Requires installing: + * - `@prettier/plugin-xml` + * + * Currently only supports Prettier. + * + * @default auto-detect based on the dependencies + */ + svg?: 'prettier' | boolean; + /** * Enable formatting support for Markdown. * diff --git a/packages/utils/src/core/crypto.ts b/packages/utils/src/core/crypto.ts index f013971..1bec65e 100644 --- a/packages/utils/src/core/crypto.ts +++ b/packages/utils/src/core/crypto.ts @@ -1,4 +1,9 @@ -import * as std from '@std/encoding'; +import { decodeHex, encodeHex } from '@std/encoding/hex'; +import { decodeBase32, encodeBase32 } from '@std/encoding/base32'; +import { decodeBase58, encodeBase58 } from '@std/encoding/base58'; +import { decodeBase64, encodeBase64 } from '@std/encoding/base64'; +import { decodeBase64Url, encodeBase64Url } from '@std/encoding/base64url'; +import { decodeAscii85, encodeAscii85 } from '@std/encoding/ascii85'; import nacl from 'tweetnacl'; export type BufferLike = Uint8Array | ArrayBuffer | string; @@ -13,35 +18,35 @@ const te = new TextDecoder(); type Ascii85Standard = 'Adobe' | 'btoa' | 'RFC 1924' | 'Z85'; type Base64Standard = 'btoa' | 'url' | 'url_padded'; -const encodeBase64UrlPadded = (data: Encodable): string => std.encodeBase64(data).replace(/\+/g, '-').replace(/\//g, '_'); -const decodeBase64UrlPadded = (src: string): Uint8Array => std.decodeBase64(src.replace(/-/g, '+').replace(/_/g, '/')); +const encodeBase64UrlPadded = (data: Encodable): string => encodeBase64(data).replace(/\+/g, '-').replace(/\//g, '_'); +const decodeBase64UrlPadded = (src: string): Uint8Array => decodeBase64(src.replace(/-/g, '+').replace(/_/g, '/')); const enc = { - 16: std.encodeHex, - 32: std.encodeBase32, - 58: std.encodeBase58, + 16: encodeHex, + 32: encodeBase32, + 58: encodeBase58, 64: (data: Encodable, standard: Base64Standard = 'btoa') => ({ - btoa: std.encodeBase64, - url: std.encodeBase64Url, + btoa: encodeBase64, + url: encodeBase64Url, url_padded: encodeBase64UrlPadded, })[standard](data), 85: (data: Encodable, standard?: Ascii85Standard) => - std.encodeAscii85(data, { standard }), + encodeAscii85(data, { standard }), }; const dec = { - 16: std.decodeHex, - 32: std.decodeBase32, - 58: std.decodeBase58, + 16: decodeHex, + 32: decodeBase32, + 58: decodeBase58, 64: (src: string, standard: Base64Standard = 'btoa') => ({ - btoa: std.decodeBase64, - url: std.decodeBase64Url, + btoa: decodeBase64, + url: decodeBase64Url, url_padded: decodeBase64UrlPadded, })[standard](src), 85: (src: string, standard?: Ascii85Standard) => - std.decodeAscii85(src, { standard }), + decodeAscii85(src, { standard }), }; export type Base = Extract; @@ -65,11 +70,7 @@ export default function base(b: T, v?: Variation): BaseProvid export const rand: (n: number) => Uint8Array = nacl.randomBytes; export const x25519: (n: Uint8Array) => Uint8Array = nacl.scalarMult.base; export const sha512 = (msg: BufferLike): Uint8Array => nacl.hash(toUint8s(msg)); -export function sha256(msg: BufferLike): Promise { - return crypto.subtle - .digest('SHA-256', toUint8s(msg)) - .then(x => toUint8s(x)); -} +export const sha256 = async (msg: BufferLike): Promise => toUint8s(await crypto.subtle.digest('SHA-256', toUint8s(msg))); export function pair(pk?: Uint8Array): readonly [Uint8Array, Uint8Array] { const priv = pk ?? rand(32); const pub = x25519(priv); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7e9009b..077171a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -31,10 +31,10 @@ importers: version: 1.21.6 tsup: specifier: ^8.2.4 - version: 8.2.4(jiti@1.21.6)(postcss@8.4.39)(tsx@4.17.0)(typescript@5.5.4)(yaml@2.5.0) + version: 8.2.4(jiti@1.21.6)(postcss@8.4.39)(tsx@4.18.0)(typescript@5.5.4)(yaml@2.5.0) tsx: - specifier: ^4.17.0 - version: 4.17.0 + specifier: ^4.18.0 + version: 4.18.0 typescript: specifier: ^5.5.4 version: 5.5.4 @@ -203,8 +203,8 @@ importers: specifier: ^2.4.9 version: 2.4.9 '@unocss/eslint-plugin': - specifier: ^0.62.2 - version: 0.62.2(eslint@9.9.1(jiti@1.21.6))(typescript@5.5.4) + specifier: ^0.62.3 + version: 0.62.3(eslint@9.9.1(jiti@1.21.6))(typescript@5.5.4) astro-eslint-parser: specifier: ^1.0.2 version: 1.0.2(typescript@5.5.4) @@ -1517,15 +1517,15 @@ packages: resolution: {integrity: sha512-sbgsPMW9yLvS7IhCi8IpuK1oBmtbWUNP+hBdwl/I9nzqVsszGnNGti5r9dUtF5RLivHUFFIdRvLiTsPhzSyJ3Q==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} - '@unocss/config@0.62.2': - resolution: {integrity: sha512-TgWhO0hkTADnuSgcUZvFP3i4AVpaiMvr41hhQqCPQTaiLMRrroXFnqL33cpkEbHLIfbTh74pXrDxnzMLaEKVdQ==} + '@unocss/config@0.62.3': + resolution: {integrity: sha512-zYOvFE0HfGIbnP/AvsbAlJpPRx9CQyXzL11m/8zgsHW5SGlJIYxuTll83l/xu026G5mPiksy7quoEOEgCLslqw==} engines: {node: '>=14'} - '@unocss/core@0.62.2': - resolution: {integrity: sha512-86jEFUJ/PSwdb1qqiEi0lWlewfKLQwiH+JAfnh8c2hLjOPVmCkb0nnsYSMh8drmtN5kpk6E06mN0IrKMO7OnvQ==} + '@unocss/core@0.62.3': + resolution: {integrity: sha512-Pfyrj8S7jq9K1QXD6Z5BCeiQavaHpbMN5q958/kmdbNGp57hOg1e346fMJAvgPjLBR+lE/hgZEsDrijtRiZXnw==} - '@unocss/eslint-plugin@0.62.2': - resolution: {integrity: sha512-y1E3jhRfORBHvhA0/dYK/tVBT5/YtJDrV3Y3ytJ5r8HdPqhDYucKV4Ihk2vDthIxzLlxzzwn2YZeSZEdbt0/IQ==} + '@unocss/eslint-plugin@0.62.3': + resolution: {integrity: sha512-8aAx5QMVLar4kTUikq7XmD3NQs6o/by87PfZNAFxuGMhKQ5RBnUfH1pzpRs+FTHN7L09As0W90cjC97b3biEaA==} engines: {node: '>=14'} '@vitest/eslint-plugin@1.0.3': @@ -4228,8 +4228,8 @@ packages: typescript: optional: true - tsx@4.17.0: - resolution: {integrity: sha512-eN4mnDA5UMKDt4YZixo9tBioibaMBpoxBkD+rIPAjVmYERSG0/dWEY1CEFuV89CgASlKL499q8AhmkMnnjtOJg==} + tsx@4.18.0: + resolution: {integrity: sha512-a1jaKBSVQkd6yEc1/NI7G6yHFfefIcuf3QJST7ZEyn4oQnxLYrZR5uZAM8UrwUa3Ge8suiZHcNS1gNrEvmobqg==} engines: {node: '>=18.0.0'} hasBin: true @@ -5641,20 +5641,20 @@ snapshots: '@typescript-eslint/types': 8.2.0 eslint-visitor-keys: 3.4.3 - '@unocss/config@0.62.2': + '@unocss/config@0.62.3': dependencies: - '@unocss/core': 0.62.2 + '@unocss/core': 0.62.3 unconfig: 0.5.5 transitivePeerDependencies: - supports-color - '@unocss/core@0.62.2': {} + '@unocss/core@0.62.3': {} - '@unocss/eslint-plugin@0.62.2(eslint@9.9.1(jiti@1.21.6))(typescript@5.5.4)': + '@unocss/eslint-plugin@0.62.3(eslint@9.9.1(jiti@1.21.6))(typescript@5.5.4)': dependencies: '@typescript-eslint/utils': 8.2.0(eslint@9.9.1(jiti@1.21.6))(typescript@5.5.4) - '@unocss/config': 0.62.2 - '@unocss/core': 0.62.2 + '@unocss/config': 0.62.3 + '@unocss/core': 0.62.3 magic-string: 0.30.11 synckit: 0.9.1 transitivePeerDependencies: @@ -7371,8 +7371,8 @@ snapshots: jiti: 2.0.0-beta.2 jiti-v1: jiti@1.21.6 pathe: 1.1.2 - pkg-types: 1.1.3 - tsx: 4.17.0 + pkg-types: 1.2.0 + tsx: 4.18.0 transitivePeerDependencies: - supports-color @@ -8115,13 +8115,13 @@ snapshots: optionalDependencies: postcss: 8.4.39 - postcss-load-config@6.0.1(jiti@1.21.6)(postcss@8.4.39)(tsx@4.17.0)(yaml@2.5.0): + postcss-load-config@6.0.1(jiti@1.21.6)(postcss@8.4.39)(tsx@4.18.0)(yaml@2.5.0): dependencies: lilconfig: 3.1.2 optionalDependencies: jiti: 1.21.6 postcss: 8.4.39 - tsx: 4.17.0 + tsx: 4.18.0 yaml: 2.5.0 postcss-merge-longhand@7.0.2(postcss@8.4.39): @@ -8791,7 +8791,7 @@ snapshots: tslib@2.6.3: {} - tsup@8.2.4(jiti@1.21.6)(postcss@8.4.39)(tsx@4.17.0)(typescript@5.5.4)(yaml@2.5.0): + tsup@8.2.4(jiti@1.21.6)(postcss@8.4.39)(tsx@4.18.0)(typescript@5.5.4)(yaml@2.5.0): dependencies: bundle-require: 5.0.0(esbuild@0.23.1) cac: 6.7.14 @@ -8803,7 +8803,7 @@ snapshots: globby: 11.1.0 joycon: 3.1.1 picocolors: 1.0.1 - postcss-load-config: 6.0.1(jiti@1.21.6)(postcss@8.4.39)(tsx@4.17.0)(yaml@2.5.0) + postcss-load-config: 6.0.1(jiti@1.21.6)(postcss@8.4.39)(tsx@4.18.0)(yaml@2.5.0) resolve-from: 5.0.0 rollup: 4.20.0 source-map: 0.8.0-beta.0 @@ -8818,7 +8818,7 @@ snapshots: - tsx - yaml - tsx@4.17.0: + tsx@4.18.0: dependencies: esbuild: 0.23.1 get-tsconfig: 4.7.6