Skip to content

Commit

Permalink
fix: add explicit function return types (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredLunde authored Aug 18, 2022
1 parent df2239e commit d327470
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 18 deletions.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dash-ui/styles",
"version": "1.0.1",
"version": "1.0.0",
"description": "A tiny, powerful, framework-agnostic CSS-in-JS library.",
"keywords": [
"styles",
Expand Down Expand Up @@ -96,6 +96,12 @@
"lunde"
],
"rules": {
"@typescript-eslint/explicit-function-return-type": [
"warn",
{
"allowExpressions": true
}
],
"prefer-const": "off",
"prefer-rest-params": "off"
}
Expand Down
2 changes: 1 addition & 1 deletion server/types/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

25 changes: 14 additions & 11 deletions src/create-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import type {
Pseudos as CSSPseudos,
SvgAttributes as CSSSvgAttributes,
} from "csstype";
import { O } from "ts-toolbelt";
import type { JsonValue, PartialDeep, ValueOf, Primitive } from "type-fest";
import type { JsonValue, PartialDeep, Primitive, ValueOf } from "type-fest";
import { createDash } from "./create-dash";
import type { Dash } from "./create-dash";
import { hash as fnv1aHash, noop, safeHash } from "./utils";
Expand Down Expand Up @@ -69,7 +68,7 @@ export function createStyles<
const defaultStyles = compiledStyleMap.default || "";

// style('text', {})
function style() {
function style(): string {
// eslint-disable-next-line prefer-spread
const css_ = css.apply(null, arguments as any);
if (!css_) return "";
Expand All @@ -81,7 +80,7 @@ export function createStyles<
return className;
}

function css() {
function css(): string {
const args = arguments as unknown as StyleArguments<Variants>;
const numArgs = args.length;

Expand Down Expand Up @@ -141,10 +140,10 @@ export function createStyles<
lazyFn: (
value: Value
) => string | StyleCallback<Tokens, Themes> | StyleObject
) {
): StylesLazy<Value> {
const cache = new Map<string | Value, string>();

function css(value?: Value) {
function css(value?: Value): string {
if (value === void 0) return "";
const key = typeof value === "object" ? JSON.stringify(value) : value;
let css = cache.get(key);
Expand Down Expand Up @@ -759,7 +758,7 @@ export function compileStyles<
((value || "") as string);
}

function stringifyStyleObject(object: StyleObject) {
function stringifyStyleObject(object: StyleObject): string {
let string = "";

for (const key in object) {
Expand All @@ -785,7 +784,7 @@ function stringifyStyleObject(object: StyleObject) {
return string;
}

function compileLiterals(args: IArguments) {
function compileLiterals(args: IArguments): string {
const literals = args[0];
return Array.isArray(literals)
? literals.reduce((curr, next, i) => curr + next + (args[i + 1] || ""), "")
Expand All @@ -799,7 +798,7 @@ const cssDisallowedRe = /[^\w-]/g;
// We cache the case transformations below because the cache
// will grow to a predictable size and the regex is slowwwww
const caseCache: Record<string, string> = {};
function cssCase(string: string) {
function cssCase(string: string): string {
return (
caseCache[string] ??
(caseCache[string] = string.replace(cssCaseRe, "-$&").toLowerCase())
Expand Down Expand Up @@ -850,7 +849,10 @@ type SerializedTokens = {
function mergeTokens<
Tokens extends DashTokens = DashTokens,
Themes extends DashThemes = DashThemes
>(target: Record<string, any>, source: Record<string, any>) {
>(
target: Record<string, any>,
source: Record<string, any>
): TokensUnion<Tokens, Themes> {
for (const key in source) {
const value = source[key];
target[key] =
Expand All @@ -863,11 +865,12 @@ function mergeTokens<
/**
* A utility function that will convert a camel-cased, dot-notation string
* into a dash-cased CSS property variable.
*
* @param path - A dot-notation string that represents the path to a value
*/
export function pathToToken<
Tokens extends Record<string, unknown> = TokensUnion<DashTokens, DashThemes>
>(path: KeysUnion<Tokens>) {
>(path: KeysUnion<Tokens>): string {
return (
"var(--" +
path.replace(/\./g, "-").replace(cssCaseRe, "-$&").toLowerCase() +
Expand Down
2 changes: 1 addition & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import crc from "crc";
import { createDash, createStyles, styles, pathToToken } from "./index";
import { createDash, createStyles, pathToToken, styles } from "./index";

afterEach(() => {
styles.dash.sheet.flush();
Expand Down
9 changes: 6 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ export function hash(string: string): string {
const minL = /(^|[:;,{}\s])\s+|$/g;
const minR = / +{/g;

export function safeHash(key: string, hashFn: typeof hash) {
export function safeHash(
key: string,
hashFn: typeof hash
): (string: string) => string {
const hashCache: Record<string, string> = {};
let value: string | undefined;
return (string: string) => {
return (string: string): string => {
if ((value = hashCache[string])) return value;
value = hashFn(string.replace(minL, "$1").replace(minR, "{"));
// allows class names to start with numbers
Expand All @@ -38,4 +41,4 @@ export function safeHash(key: string, hashFn: typeof hash) {
};
}

export function noop() {}
export function noop(): void {}
2 changes: 1 addition & 1 deletion types/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

0 comments on commit d327470

Please sign in to comment.