Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
zyyv committed Oct 22, 2024
1 parent 71e5031 commit 03f4e9f
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 18 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
[![License][license-src]][license-href]

CSS transform to UnoCSS.

https://astexplorer.net/
6 changes: 1 addition & 5 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// @ts-check
import antfu from '@antfu/eslint-config'

export default antfu(
{
type: 'lib',
},
)
export default antfu()
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
"typecheck": "tsc --noEmit",
"prepare": "simple-git-hooks"
},
"dependencies": {
"@types/css-tree": "^2.3.8",
"css-tree": "^3.0.0",
"magic-string": "^0.30.12"
},
"devDependencies": {
"@antfu/eslint-config": "^3.3.2",
"@antfu/ni": "^0.23.0",
Expand Down
61 changes: 51 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 78 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,78 @@
export const one = 1
export const two = 2
import { parse } from 'css-tree'
import MagicString from 'magic-string'
import type { Declaration, Rule, StyleSheet } from 'css-tree'

export function toArray<T>(value: T | T[] = []): T[] {
return Array.isArray(value) ? value : [value]
}

function blockSource(source: string) {
if (source.includes('{') && source.includes('}')) {
return source
}
return `{ ${source} }`
}

export function parseCSS(css: string) {
const source = new MagicString(css)
const ast = parse(blockSource(source.original)) as StyleSheet

for (const node of ast.children) {
if (node.type === 'Rule') {
handleRuleNode(source, node)
}
}
}

function handleRuleNode(source: MagicString, node: Rule) {

Check failure on line 27 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

'source' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 27 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

'node' is defined but never used. Allowed unused args must match /^_/u
// console.dir(node)
// const selector = node.prelude.children
}

interface CssValueParsedMeta {
value: number | string
unit?: string
}

interface CssValueParsed {
prop: string
meta: CssValueParsedMeta[]
}

export function handleDeclarationNode(node: Declaration): CssValueParsed | undefined {
if (node.type !== 'Declaration') {
return
}

const prop = node.property
let meta: CssValueParsedMeta[]
if (node.value.type === 'Raw') {
meta = toArray<CssValueParsedMeta>({ value: node.value.value })
}
else {
meta = node.value.children.map((child) => {

Check failure on line 53 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Array.prototype.map() expects a value to be returned at the end of arrow function
switch (child.type) {
case 'Dimension':
return {
value: child.value,
unit: child.unit,
}

case 'Hash':
return {
value: child.value,
}

case 'Identifier':
return {
value: child.name,
}
}
}) as unknown as CssValueParsedMeta[]
}

return {
prop,
meta,
}
}
6 changes: 6 additions & 0 deletions src/maps/border.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { positions, positionShort } from './position'

Check failure on line 1 in src/maps/border.ts

View workflow job for this annotation

GitHub Actions / lint

'positions' is defined but never used

Check failure on line 1 in src/maps/border.ts

View workflow job for this annotation

GitHub Actions / lint

'positionShort' is defined but never used

export const border = [
['border', 'border'],
['b', 'b'],
]
2 changes: 2 additions & 0 deletions src/maps/position.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const positions = ['top', 'bottom', 'left', 'right']
export const positionShort = ['t', 'b', 'l', 'r']
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* [match property, uno rule starter]
*
* @example
* ['color', 'c']
* ['background-color', 'bg']
*/
export type KeyToUno = [string, string]
51 changes: 50 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,56 @@
import { parse } from 'css-tree'
import { describe, expect, it } from 'vitest'
import type { Declaration } from 'css-tree'
import { handleDeclarationNode, parseCSS } from '../src'

Check failure on line 4 in test/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

'parseCSS' is defined but never used

const source = `/**

Check failure on line 6 in test/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

'source' is assigned a value but never used. Allowed unused vars must match /^_/u
* Paste or drop some CSS here and explore
* the syntax tree created by chosen parser.
* Enjoy!
*/
@media screen and (min-width: 480px) {
body {
background-color: lightgreen;
}
}
#main {
border: 1px solid black;
}
ul li {
padding: 5px;

Check failure on line 23 in test/index.test.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected tab character
color: #fff;
}
`

describe('should', () => {
it('exported', () => {
expect(1).toEqual(1)
// const css = 'color: red'
const css = 'border: 1px solid #eee'
const ast = parse(css, {
context: 'declaration',
positions: true,
}) as Declaration
const result = handleDeclarationNode(ast)

expect(result).toMatchInlineSnapshot(`
{
"meta": [
{
"unit": "px",
"value": "1",
},
{
"value": "solid",
},
{
"value": "eee",
},
],
"prop": "border",
}
`)
})
})

0 comments on commit 03f4e9f

Please sign in to comment.