Skip to content

Commit af285b3

Browse files
committed
feat(convertCssToJss): accept recast options, set defaults for options
1 parent accbe5a commit af285b3

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,15 @@ declare function convertCssToJss(rawCss: string): ObjectProperty[]
162162
import { convertCssToJssString } from 'jss-codemorphs/convertCssToJss'
163163
```
164164

165-
Converts CSS to JSS. Returns the resulting JSS code as a string.
165+
Converts CSS to JSS. Returns the resulting JSS code as a string. You can
166+
pass `recast.Options` as the second argument to customize the code format.
167+
Uses `{ tabWidth: 2, useTabs: false, quote: 'single' }` as default options.
166168

167169
Signature:
168170

169171
```js
170-
declare function convertCssToJssString(rawCss: string): string
172+
declare function convertCssToJssString(
173+
rawCss: string,
174+
options?: recast.Options
175+
): string
171176
```

demo/Root.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,7 @@ export default function Root(): React.ReactNode {
111111
debounce((input: string): void => {
112112
let converted: string | Error
113113
try {
114-
converted = convertCssToJssString(input).replace(/^ {2,}/gm, match =>
115-
match.substring(match.length / 2)
116-
)
114+
converted = convertCssToJssString(input)
117115
} catch (error) {
118116
converted = error
119117
}

src/convertCssToJss.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,21 @@ export default function convertCssToJss(rawCss: string): ObjectProperty[] {
3434
) as ObjectProperty[]
3535
}
3636

37-
export function convertCssToJssString(rawCss: string): string {
38-
return convertCssToJss(rawCss)
39-
.map(p => recast.prettyPrint(p).code)
40-
.join(',\n')
37+
export function convertCssToJssString(
38+
rawCss: string,
39+
options?: recast.Options
40+
): string {
41+
const recastOptions: recast.Options = {
42+
quote: 'single',
43+
tabWidth: 2,
44+
useTabs: false,
45+
...options,
46+
}
47+
return (
48+
convertCssToJss(rawCss)
49+
.map(p => recast.prettyPrint(p, recastOptions).code)
50+
.join(',\n') + ','
51+
)
4152
}
4253

4354
export function collectAnimationNames(nodes: postcss.Node[]): Set<string> {

0 commit comments

Comments
 (0)