Skip to content

Commit 82dc41f

Browse files
committed
feat: css in js convert features that actually work
1 parent 95db9d3 commit 82dc41f

File tree

3 files changed

+131
-8
lines changed

3 files changed

+131
-8
lines changed

package.json

+13-3
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,16 @@
4949
"title": "Next Letter Swap Case"
5050
},
5151
{
52-
"command": "fixCss",
53-
"title": "Fix CSS: Adopt CSS-IN-JS"
52+
"command": "fixCssInJs",
53+
"title": "Fix CSS: Convert CSS-in-JS..."
54+
},
55+
{
56+
"command": "fixCssToJs",
57+
"title": "Fix CSS: CSS to JS"
58+
},
59+
{
60+
"command": "fixJsToCss",
61+
"title": "Fix CSS: JS to CSS"
5462
},
5563
{
5664
"command": "insertAutoCompletions",
@@ -723,7 +731,7 @@
723731
"@types/minimatch": "^3.0.5",
724732
"@types/pluralize": "^0.0.29",
725733
"@zardoy/utils": "^0.0.9",
726-
"@zardoy/vscode-utils": "^0.0.50",
734+
"@zardoy/vscode-utils": "^0.0.52",
727735
"ansi-escapes": "^6.2.0",
728736
"change-case": "^4.1.2",
729737
"create-index": "^2.6.0",
@@ -740,6 +748,8 @@
740748
"path-browserify": "^1.0.1",
741749
"pid-cwd": "^1.2.0",
742750
"pluralize": "github:plurals/pluralize",
751+
"postcss": "^8.4.33",
752+
"postcss-js": "^4.0.1",
743753
"rambda": "^6.9.0",
744754
"remark": "^14.0.2",
745755
"string-dedent": "^3.0.1",

pnpm-lock.yaml

+45-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/features/cssInJs.ts

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import * as vscode from 'vscode'
2+
import { registerTextEditorCommand } from '@zardoy/vscode-utils/build/commands'
3+
import { noCase } from 'change-case'
4+
import { getExtensionCommandId, registerExtensionCommand, showQuickPick } from 'vscode-framework'
5+
import postcssJs from 'postcss-js'
6+
import postcss from 'postcss'
7+
8+
export default () => {
9+
registerTextEditorCommand('fixCssInJs', async (editor, _, mode) => {
10+
const commands = ['fixCssToJs', 'fixJsToCss']
11+
let command: string | undefined
12+
if (mode === 'auto')
13+
command =
14+
editor.document.languageId === 'css' || editor.document.languageId === 'scss' || editor.document.languageId === 'less'
15+
? 'fixCssToJs'
16+
: 'fixJsToCss'
17+
else
18+
command = await showQuickPick(
19+
commands.map(command => ({
20+
label: noCase(command),
21+
value: command,
22+
})),
23+
)
24+
25+
if (!command) return
26+
await vscode.commands.executeCommand(getExtensionCommandId(command as any))
27+
})
28+
29+
registerTextEditorCommand('fixJsToCss', async editor => {
30+
let text = editor.document.getText(editor.selection)
31+
if (!text.trim()) return
32+
const transformJsPropToCss = (prop: string) => {
33+
const converted = prop.replace(/([A-Z])/g, '-$1').toLowerCase()
34+
return converted
35+
}
36+
37+
text = text.replaceAll(/(\w+)(: )/gi, (_, prop, after) => {
38+
const converted = transformJsPropToCss(prop)
39+
return `${converted}${after}`
40+
})
41+
text = text.replaceAll(/: (.*?),?(\r?\n|$)/g, (_, value, ending) => {
42+
let converted = Number.isNaN(Number(value)) ? value : `${value}px`
43+
if (converted.startsWith("'") || converted.startsWith('"')) converted = converted.slice(1, -1)
44+
return `: ${converted};${ending ?? ''}`
45+
})
46+
await editor.edit(edit => edit.replace(editor.selection, text))
47+
})
48+
registerTextEditorCommand('fixCssToJs', async editor => {
49+
const text = editor.document.getText(editor.selection)
50+
if (!text.trim()) return
51+
const root = postcss.parse(text)
52+
53+
const patch = input => {
54+
if (typeof input === 'object') {
55+
const result = {}
56+
for (const key of Object.keys(input)) result[key] = patch(input[key])
57+
58+
return result
59+
}
60+
61+
if (typeof input === 'string') {
62+
const pxMatch = /^(\d+)px$/.exec(input)
63+
return pxMatch ? +pxMatch[1]! : input
64+
}
65+
66+
return input
67+
}
68+
69+
const transformed = JSON.stringify(patch(postcssJs.objectify(root)), null, '\t')
70+
await editor.edit(edit => edit.replace(editor.selection, transformed.slice(1, -1)))
71+
})
72+
// todo suggest file imports
73+
}

0 commit comments

Comments
 (0)