-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert
@emotion/css-prettifier
's source code to TypeScript (#3278)
* Convert `@emotion/css-prettifier`'s source code to TypeScript * use .ts * fixed changeset content
- Loading branch information
Showing
8 changed files
with
81 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@emotion/css-prettifier': minor | ||
--- | ||
|
||
Source code has been migrated to TypeScript. From now on type declarations will be emitted based on that, instead of being hand-written. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import memoize from '@emotion/memoize' | ||
import { | ||
compile, | ||
serialize, | ||
combine, | ||
tokenize, | ||
type Middleware, | ||
type Element | ||
} from 'stylis' | ||
|
||
// adjusted https://github.com/thysultan/stylis.js/blob/68b9043427c177b95a0fd6a2a13f5b636bf80236/src/Serializer.js#L26-L34 | ||
const prettyStringify = memoize( | ||
(indentation): Middleware => | ||
(element, index, children, callback) => { | ||
switch (element.type) { | ||
case '@import': | ||
return (element.return = element.return || element.value) + '\n\n' | ||
case 'decl': | ||
return (element.return = | ||
element.return || `${element.props}: ${element.children};\n`) | ||
case 'comm': | ||
return '' | ||
case '@media': | ||
case '@supports': | ||
element.value = combine( | ||
tokenize(element.value), | ||
(value, index, children) => { | ||
// ( | ||
if (value.charCodeAt(0) === 40 && children[index - 1] !== ' ') { | ||
return ' ' + value | ||
} | ||
return value | ||
} | ||
) | ||
break | ||
case 'rule': | ||
element.value = (element.props as string[]).join( | ||
element.root && | ||
(element.root.type === '@keyframes' || | ||
element.root.type === '@-webkit-keyframes') | ||
? ', ' | ||
: ',\n' | ||
) | ||
} | ||
|
||
const serialized = serialize(element.children as Element[], callback) | ||
return serialized.length | ||
? (element.return = | ||
element.value + | ||
' {\n' + | ||
serialized | ||
.trim() | ||
.replace(/^/gm, indentation) | ||
.replace(/^\s+$/gm, '') + | ||
'\n}\n\n') | ||
: '' | ||
} | ||
) | ||
|
||
export default function prettify(styles: string, indentation = ' ') { | ||
return serialize(compile(styles), prettyStringify(indentation)).trim() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters