Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript migration of @emotion/utils #2359

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/old-years-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@emotion/utils': minor
---

Source code has been migrated to TypeScript. From now on type declarations will be emitted based on that, instead of being hand-written.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ coverage/
node_modules/
stylis.min.js
/demo/dist
/packages/site/build
/packages/site/build
5 changes: 2 additions & 3 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"description": "internal utils for emotion",
"main": "dist/emotion-utils.cjs.js",
"module": "dist/emotion-utils.esm.js",
"types": "dist/emotion-utils.cjs.d.ts",
"browser": {
"./dist/emotion-utils.cjs.js": "./dist/emotion-utils.browser.cjs.js",
"./dist/emotion-utils.esm.js": "./dist/emotion-utils.browser.esm.js"
},
"types": "types/index.d.ts",
"license": "MIT",
"scripts": {
"test:typescript": "dtslint types"
Expand All @@ -19,8 +19,7 @@
},
"files": [
"src",
"dist",
"types/*.d.ts"
"dist"
],
"devDependencies": {
"dtslint": "^0.3.0"
Expand Down
20 changes: 10 additions & 10 deletions packages/utils/src/index.js → packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* import type { RegisteredCache, EmotionCache, SerializedStyles } from './types' */
import { RegisteredCache, EmotionCache, SerializedStyles } from './types'

const isBrowser = typeof document !== 'undefined'

export function getRegisteredStyles(
registered /*: RegisteredCache */,
registeredStyles /*: string[] */,
classNames /*: string */
) {
registered: RegisteredCache,
registeredStyles: string[],
classNames: string
): string {
let rawClassName = ''

classNames.split(' ').forEach(className => {
Expand All @@ -20,10 +20,10 @@ export function getRegisteredStyles(
}

export const insertStyles = (
cache /*: EmotionCache */,
serialized /*: SerializedStyles */,
isStringTag /*: boolean */
) => {
cache: EmotionCache,
serialized: SerializedStyles,
isStringTag: boolean
): string | void => {
let className = `${cache.key}-${serialized.name}`
if (
// we only need to add the styles to the registered cache if the
Expand All @@ -43,7 +43,7 @@ export const insertStyles = (
}
if (cache.inserted[serialized.name] === undefined) {
let stylesForSSR = ''
let current = serialized
let current: SerializedStyles | undefined = serialized

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this even actually possible for serialized to be undefined here? I would've expected if (cache.inserted[serialized.name] === undefined) { to error from accessing undefined.name if so.

If that's actually possible, would it make sense to introduce optional chaining into the if statement's syntax?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is current-specific typing in advance because current.next is possibly undefined and we're checking against it as part of the do/while:

let current: SerializedStyles | undefined = serialized
do {
let maybeStyles = cache.insert(
serialized === current ? `.${className}` : '',
current,
cache.sheet,
true
)
if (!isBrowser && maybeStyles !== undefined) {
stylesForSSR += maybeStyles
}
current = current.next
} while (current !== undefined)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, gotcha, that makes sense. I haven't seen a while loop like this in a while :pun-dog: so it threw me. Thanks for the quick update!

do {
let maybeStyles = cache.insert(
serialized === current ? `.${className}` : '',
Expand Down
29 changes: 0 additions & 29 deletions packages/utils/src/types.js

This file was deleted.

27 changes: 27 additions & 0 deletions packages/utils/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { StyleSheet } from '@emotion/sheet'

export { StyleSheet }

export type RegisteredCache = Record<string, string | undefined>

export type SerializedStyles = {
name: string
styles: string
map?: string
next?: SerializedStyles
}

export type EmotionCache = {
inserted: Record<string, string | true | undefined>
registered: RegisteredCache
sheet: StyleSheet
key: string
compat?: true
nonce?: string
insert: (
selector: string,
serialized: SerializedStyles,
sheet: StyleSheet,
shouldCache: boolean
) => string | void
}
50 changes: 1 addition & 49 deletions packages/utils/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,3 @@
// Definitions by: Junyoung Clare Jang <https://github.com/Ailrun>
// TypeScript Version: 2.2

export interface RegisteredCache {
[key: string]: string
}

export interface StyleSheet {
container: HTMLElement
nonce?: string
key: string
insert(rule: string): void
flush(): void
tags: Array<HTMLStyleElement>
}

export interface EmotionCache {
inserted: {
[key: string]: string | true
}
registered: RegisteredCache
sheet: StyleSheet
key: string
compat?: true
nonce?: string
insert(
selector: string,
serialized: SerializedStyles,
sheet: StyleSheet,
shouldCache: boolean
): string | void
}

export interface SerializedStyles {
name: string
styles: string
map?: string
next?: SerializedStyles
}

export const isBrowser: boolean
export function getRegisteredStyles(
registered: RegisteredCache,
registeredStyles: Array<string>,
classNames: string
): string
export function insertStyles(
cache: EmotionCache,
serialized: SerializedStyles,
isStringTag: boolean
): string | void
export * from '../src'
9 changes: 1 addition & 8 deletions packages/utils/types/tests.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import {
EmotionCache,
RegisteredCache,
SerializedStyles,
StyleSheet,
getRegisteredStyles,
insertStyles,
isBrowser
insertStyles
} from '@emotion/utils'

declare const testCache: EmotionCache
Expand Down Expand Up @@ -39,7 +36,3 @@ insertStyles(testCache, {
name: 'abc',
styles: 'font-size: 18px;'
})

const test0: boolean = isBrowser
// $ExpectError
const test1: number = isBrowser