From a6c022df6d82de3069d76ef636f8d275a31104c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Mon, 12 Jun 2023 19:45:10 +0800 Subject: [PATCH] chore: upgrade eslint config --- .eslintrc.js | 9 -------- .gitignore | 1 + .vscode/settings.json | 6 ++++- eslint.config.mjs | 3 +++ package.json | 2 +- src/core/convert.ts | 53 ++++++++++++++++++++----------------------- src/core/options.ts | 4 ++-- src/core/utils.ts | 4 ++-- src/index.ts | 3 +-- src/vite.ts | 2 +- 10 files changed, 41 insertions(+), 46 deletions(-) delete mode 100644 .eslintrc.js create mode 100644 eslint.config.mjs diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 818d157..0000000 --- a/.eslintrc.js +++ /dev/null @@ -1,9 +0,0 @@ -const { defineConfig } = require('eslint-define-config') - -module.exports = defineConfig({ - root: true, - extends: ['@sxzz'], - rules: { - 'unicorn/prefer-string-replace-all': 'off', - }, -}) diff --git a/.gitignore b/.gitignore index dbf15ae..bf6f0f8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules dist *.log .vercel +.eslintcache diff --git a/.vscode/settings.json b/.vscode/settings.json index 3da2f47..a6ce123 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,8 @@ { "editor.formatOnSave": true, - "unocss.root": "./playground" + "unocss.root": "./playground", + "eslint.experimental.useFlatConfig": true, + "eslint.options": { + "overrideConfigFile": "./eslint.config.mjs" + } } diff --git a/eslint.config.mjs b/eslint.config.mjs new file mode 100644 index 0000000..b3d5acd --- /dev/null +++ b/eslint.config.mjs @@ -0,0 +1,3 @@ +import { sxzz } from '@sxzz/eslint-config' + +export default sxzz() diff --git a/package.json b/package.json index 1ffbb7a..92c5cba 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,7 @@ "access": "public" }, "scripts": { - "lint": "eslint . --ext .vue,.js,.ts,.jsx,.tsx,.json,.md", + "lint": "ESLINT_USE_FLAT_CONFIG=true eslint -c eslint.config.mjs --cache .", "lint:fix": "pnpm run lint -- --fix", "build": "tsup && tsx scripts/postbuild.mts", "dev": "pnpm run -C playground dev", diff --git a/src/core/convert.ts b/src/core/convert.ts index 68fbba0..1394efe 100644 --- a/src/core/convert.ts +++ b/src/core/convert.ts @@ -1,7 +1,28 @@ -import { parse } from '@babel/parser' +import { type ParserPlugin, parse } from '@babel/parser' import { walk } from 'estree-walker' import { encode } from 'entities' +import MagicString from 'magic-string' import { + type ArrayExpression, + type Binary, + type CallExpression, + type Expression, + type JSX, + type JSXAttribute, + type JSXElement, + type JSXExpressionContainer, + type JSXFragment, + type JSXIdentifier, + type JSXMemberExpression, + type JSXNamespacedName, + type JSXOpeningElement, + type JSXSpreadChild, + type JSXText, + type Literal, + type Node, + type ObjectExpression, + type TemplateLiteral, + type UnaryExpression, isBooleanLiteral, isCallExpression, isFunction, @@ -11,38 +32,14 @@ import { isLiteral, isStringLiteral, } from '@babel/types' -import MagicString from 'magic-string' import { + type Primitive, escapeString, isPlainObject, isPrimitive, styleToString, } from './utils' -import type { ParserPlugin } from '@babel/parser' -import type { Primitive } from './utils' -import type { OptionsResolved } from './options' -import type { - ArrayExpression, - Binary, - CallExpression, - Expression, - JSX, - JSXAttribute, - JSXElement, - JSXExpressionContainer, - JSXFragment, - JSXIdentifier, - JSXMemberExpression, - JSXNamespacedName, - JSXOpeningElement, - JSXSpreadChild, - JSXText, - Literal, - Node, - ObjectExpression, - TemplateLiteral, - UnaryExpression, -} from '@babel/types' +import { type OptionsResolved } from './options' export type EvaluatedValue = | Exclude @@ -249,7 +246,7 @@ function transformJsx(code: string, node: JSX) { const expressions = node.expressions.map((expr) => resolveExpression(expr, node) ) - return expressions.slice(-1)[0] + return expressions.at(-1) } case 'ParenthesizedExpression': // (1) case 'TSNonNullExpression': // 1! diff --git a/src/core/options.ts b/src/core/options.ts index ee8562f..f2b8df7 100644 --- a/src/core/options.ts +++ b/src/core/options.ts @@ -1,5 +1,5 @@ -import type { ParserPlugin } from '@babel/parser' -import type { FilterPattern } from '@rollup/pluginutils' +import { type ParserPlugin } from '@babel/parser' +import { type FilterPattern } from '@rollup/pluginutils' export interface Options { include?: FilterPattern diff --git a/src/core/utils.ts b/src/core/utils.ts index 17f327c..9384643 100644 --- a/src/core/utils.ts +++ b/src/core/utils.ts @@ -11,7 +11,7 @@ export type Primitive = const KEBAB_REGEX = /[A-Z\u00C0-\u00D6\u00D8-\u00DE]/g export function kebabCase(str: string) { - return str.replace(KEBAB_REGEX, (match) => { + return str.replaceAll(KEBAB_REGEX, (match) => { return `-${match.toLowerCase()}` }) } @@ -29,7 +29,7 @@ export const escapeString = (str: string) => { wrap: true, es6: true, }) - return text.replace(RAW_RE, '${$1}') + return text.replaceAll(RAW_RE, '${$1}') } export const isPrimitive = (val: unknown): val is Primitive => { if (typeof val === 'object') return val === null diff --git a/src/index.ts b/src/index.ts index 19de074..9c284a3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,7 @@ import { createUnplugin } from 'unplugin' import { createFilter } from '@rollup/pluginutils' import { transformJsxToString } from './core/convert' -import { resolveOption } from './core/options' -import type { Options } from './core/options' +import { type Options, resolveOption } from './core/options' declare global { const jsxToString: (element: JSX.Element) => string diff --git a/src/vite.ts b/src/vite.ts index 4b357ca..f015a62 100644 --- a/src/vite.ts +++ b/src/vite.ts @@ -1,4 +1,4 @@ import unplugin from '.' -import type {} from 'vite' +import {} from 'vite' export default unplugin.vite