Skip to content

Commit 99ff391

Browse files
committed
chore: fix lint
1 parent 3ee417a commit 99ff391

File tree

7 files changed

+63
-64
lines changed

7 files changed

+63
-64
lines changed

eslint.config.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

eslint.config.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { sxzz } from '@sxzz/eslint-config'
2+
export default sxzz()

playground/vite.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import path from 'node:path'
2-
import { defineConfig } from 'vite'
32
import Vue from '@vitejs/plugin-vue'
3+
import Unocss from 'unocss/vite'
44
import AutoImport from 'unplugin-auto-import/vite'
55
import Components from 'unplugin-vue-components/vite'
6-
import Unocss from 'unocss/vite'
6+
import { defineConfig } from 'vite'
77

88
const resolve = (...paths: string[]) => path.resolve(__dirname, 'src', ...paths)
99

src/core/convert.ts

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { type ParserPlugin, parse } from '@babel/parser'
2-
import { walk } from 'estree-walker'
3-
import { encode } from 'entities'
4-
import MagicString from 'magic-string'
1+
import { parse, type ParserPlugin } from '@babel/parser'
52
import {
3+
isBooleanLiteral,
4+
isCallExpression,
5+
isFunction,
6+
isIdentifier,
7+
isJSX,
8+
isJSXExpressionContainer,
9+
isLiteral,
10+
isStringLiteral,
611
type ArrayExpression,
712
type Binary,
813
type CallExpression,
@@ -23,21 +28,16 @@ import {
2328
type ObjectExpression,
2429
type TemplateLiteral,
2530
type UnaryExpression,
26-
isBooleanLiteral,
27-
isCallExpression,
28-
isFunction,
29-
isIdentifier,
30-
isJSX,
31-
isJSXExpressionContainer,
32-
isLiteral,
33-
isStringLiteral,
3431
} from '@babel/types'
32+
import { encode } from 'entities'
33+
import { walk } from 'estree-walker'
34+
import MagicString from 'magic-string'
3535
import {
36-
type Primitive,
3736
escapeString,
3837
isPlainObject,
3938
isPrimitive,
4039
styleToString,
40+
type Primitive,
4141
} from './utils'
4242
import type { OptionsResolved } from './options'
4343

@@ -75,15 +75,6 @@ function transformJsx(code: string, node: JSX) {
7575
return toStringExpression(resolveJsx(node))
7676
}
7777

78-
function toStringExpression(expr: EvaluatedValue) {
79-
if (expr instanceof RegExp) {
80-
return expr.toString()
81-
} else if (typeof expr === 'object') {
82-
return JSON.stringify(expr)
83-
}
84-
return String(expr)
85-
}
86-
8778
function toStringJsxChildren(
8879
nodes: Array<
8980
| JSXText
@@ -96,18 +87,6 @@ function transformJsx(code: string, node: JSX) {
9687
return nodes.map((child) => toStringJsx(child)).join('')
9788
}
9889

99-
function toStringJsxText(node: JSXText) {
100-
const texts = node.value.split('\n')
101-
102-
return texts
103-
.map((text, idx) => (idx > 0 ? text.trim() : text))
104-
.filter((line) => {
105-
if (line.trim().length === 0) return false
106-
return true
107-
})
108-
.join(' ')
109-
}
110-
11190
function toStringJsxElement(node: JSXElement) {
11291
if (node.openingElement.selfClosing) {
11392
return toStringOpeningElement(node.openingElement)
@@ -117,28 +96,6 @@ function transformJsx(code: string, node: JSX) {
11796
node.openingElement,
11897
)}${children}</${toStringJsxName(node.closingElement!.name)}>`
11998
}
120-
121-
function toStringOpeningElement(node: JSXOpeningElement) {
122-
let str = `<${toStringJsxName(node.name)}`
123-
124-
const props: string[] = node.attributes
125-
.map((attr) => {
126-
if (attr.type === 'JSXAttribute') {
127-
return toStringJsxAttribute(attr)
128-
} else {
129-
return notSupported(node)
130-
}
131-
})
132-
.filter((x): x is string => x !== undefined)
133-
134-
if (props.length > 0) {
135-
str += ` ${props.join(' ')}`
136-
}
137-
138-
str += node.selfClosing ? '/>' : '>'
139-
140-
return str
141-
}
14299
}
143100

144101
function toStringJsxAttribute(node: JSXAttribute) {
@@ -435,6 +392,49 @@ function transformJsx(code: string, node: JSX) {
435392
function notSupported(node: Node): never {
436393
throw new Error(`not supported ${node.type}: ${getSource(node)}`)
437394
}
395+
396+
function toStringOpeningElement(node: JSXOpeningElement) {
397+
let str = `<${toStringJsxName(node.name)}`
398+
399+
const props: string[] = node.attributes
400+
.map((attr) => {
401+
if (attr.type === 'JSXAttribute') {
402+
return toStringJsxAttribute(attr)
403+
} else {
404+
return notSupported(node)
405+
}
406+
})
407+
.filter((x): x is string => x !== undefined)
408+
409+
if (props.length > 0) {
410+
str += ` ${props.join(' ')}`
411+
}
412+
413+
str += node.selfClosing ? '/>' : '>'
414+
415+
return str
416+
}
417+
}
418+
419+
function toStringExpression(expr: EvaluatedValue) {
420+
if (expr instanceof RegExp) {
421+
return expr.toString()
422+
} else if (typeof expr === 'object') {
423+
return JSON.stringify(expr)
424+
}
425+
return String(expr)
426+
}
427+
428+
function toStringJsxText(node: JSXText) {
429+
const texts = node.value.split('\n')
430+
431+
return texts
432+
.map((text, idx) => (idx > 0 ? text.trim() : text))
433+
.filter((line) => {
434+
if (line.trim().length === 0) return false
435+
return true
436+
})
437+
.join(' ')
438438
}
439439

440440
export function transformJsxToString(

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { createUnplugin } from 'unplugin'
21
import { createFilter } from '@rollup/pluginutils'
2+
import { createUnplugin } from 'unplugin'
33
import { transformJsxToString } from './core/convert'
4-
import { type Options, resolveOption } from './core/options'
4+
import { resolveOption, type Options } from './core/options'
55

66
declare global {
77
const jsxToString: (element: JSX.Element) => string

tests/jsx-raw.test.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/* eslint-disable no-void */
2-
/* eslint-disable no-constant-condition */
31
/* eslint-disable prefer-template */
42
import { describe, expect, test } from 'vitest'
53

tests/jsx-to-string.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-constant-binary-expression */
12
/* eslint-disable no-void */
23
/* eslint-disable no-constant-condition */
34
/* eslint-disable prefer-template */

0 commit comments

Comments
 (0)