Skip to content

Commit

Permalink
chore: fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sparten11740 committed Oct 24, 2024
1 parent 4f3ef2a commit a2c0fce
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 49 deletions.
12 changes: 5 additions & 7 deletions benchmark/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ suite.add('Styled Components', () => {

const results = {}

afterAll(() => {
const result = `const results = ${JSON.stringify(results, null, 2)};\n`
fs.writeFileSync(`${__dirname}/results.js`, result)
})

it('runs', () => {
suite
.on('complete', (e) => {
Expand All @@ -44,7 +49,6 @@ it('runs', () => {
let max = first

let minFunc = firstFunc
let maxFunc = firstFunc

let output = ''

Expand All @@ -56,7 +60,6 @@ it('runs', () => {

if (mean > max) {
max = mean
maxFunc = name
}

const result = mean * 1000
Expand All @@ -73,8 +76,3 @@ it('runs', () => {
})
.run()
})

afterAll(() => {
const result = `const results = ${JSON.stringify(results, null, 2)};\n`
fs.writeFileSync(`${__dirname}/results.js`, result)
})
1 change: 1 addition & 0 deletions benchmark/results.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line no-unused-vars
const results = {
Shakl: 0.029_811_424_241_520_11,
Emotion: 0.037_209_380_871_224_86,
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type StyledComponent<P extends object, S extends object> = React.ForwardR
}

const styled =
<P extends object, S extends object = object, A extends Partial<P> = {}>(
<P extends object, S extends object = object, A extends Partial<P> = {}>( // eslint-disable-line @typescript-eslint/no-empty-object-type
Comp: React.ComponentType<P>,
config: Config<P, A> = {}
) =>
Expand Down
121 changes: 80 additions & 41 deletions test/index.type-test.tsx
Original file line number Diff line number Diff line change
@@ -1,88 +1,127 @@
import React, {Text, ViewStyle} from 'react-native'
import type { ViewStyle } from 'react-native'
import React, { Text } from 'react-native'
import styled from '../src/index'
import extendedStyled from '../src/rn'
import {useRef} from "react";
import LinearGradient, { LinearGradientProps } from 'react-native-linear-gradient'
import { useRef } from 'react'
import type { LinearGradientProps } from 'react-native-linear-gradient'
import LinearGradient from 'react-native-linear-gradient'

const StyledText = styled(Text)(({ transparent }: { transparent?: boolean; color: string }) => ({ flex: 1, opacity: transparent ? 0.5 : 1 }))
const StyledText = styled(Text)(({ transparent }: { transparent?: boolean; color: string }) => ({
flex: 1,
opacity: transparent ? 0.5 : 1,
}))
const StyledTextWithAttrs = StyledText.attrs({ color: 'black' })
const StyledTextWithAttrsFromConfig = styled(StyledText, { attrs: { color: 'black' } })()
const StyledScalableText = StyledText.extend(({ big }: { big?: boolean }) => ({ fontSize: big ? 20 : 10 }))
const StyledScalableText = StyledText.extend(({ big }: { big?: boolean }) => ({
fontSize: big ? 20 : 10,
}))
const StyledTextWithObjectProps = styled(Text)({ flex: 1, opacity: 1 })
const ExtendedStyledText = extendedStyled.Text(({ transparent }: { transparent?: boolean; }) => ({ flex: 1, opacity: transparent ? 0.5 : 1 }))
const ExtendedStyledTextWithStyle = extendedStyled(Text)((props: {specificColor: string}) => ({color: props.specificColor}))
const ExtenedStyledTextWithAttrs = extendedStyled.Text(({ transparent }: { transparent?: boolean; big?:boolean }) => ({ flex: 1, opacity: transparent ? 0.5 : 1 })).attrs(({ big}) => ({
ellipsizeMode: big ? 'middle' : 'head',
const ExtendedStyledText = extendedStyled.Text(({ transparent }: { transparent?: boolean }) => ({
flex: 1,
opacity: transparent ? 0.5 : 1,
}))
const ExtendedStyledTextWithStyle = extendedStyled(Text)((props: { specificColor: string }) => ({
color: props.specificColor,
}))
const ExtenedStyledTextWithAttrs = extendedStyled
.Text(({ transparent }: { transparent?: boolean; big?: boolean }) => ({
flex: 1,
opacity: transparent ? 0.5 : 1,
}))
.attrs(({ big }) => ({
ellipsizeMode: big ? 'middle' : 'head',
}))

const StyledView = extendedStyled.View({width: 100})
const StyledViewWithDynamicProps = extendedStyled.View((props: {active: boolean}) => ({width: props.active ? 100 : 50}))
const StyledView = extendedStyled.View({ width: 100 })
const StyledViewWithDynamicProps = extendedStyled.View((props: { active: boolean }) => ({
width: props.active ? 100 : 50,
}))
const Row = extendedStyled.View((props: { spaced?: boolean; full?: boolean }) => ({
alignItems: 'center',
flexDirection: 'row',
justifyContent: props.spaced ? 'space-between' : undefined,
width: props.full ? '100%' : undefined,
alignItems: 'center',
flexDirection: 'row',
justifyContent: props.spaced ? 'space-between' : undefined,
width: props.full ? '100%' : undefined,
}))

const StyledImage = extendedStyled.Image({ width: 100 })
const StyledImageWithDynamicProps = extendedStyled.Image((props: { active: boolean }) => ({
width: props.active ? 100 : 50,
}))

const StyledImage = extendedStyled.Image({width: 100})
const StyledImageWithDynamicProps = extendedStyled.Image((props: {active: boolean}) => ({width: props.active ? 100 : 50}))

const StyledTouchable = extendedStyled.Touchable({width: 100})
const StyledTouchableWithDynamicProps = extendedStyled.Image((props: {active: boolean}) => ({width: props.active ? 100 : 50}))
const StyledTouchable = extendedStyled.Touchable({ width: 100 })
const StyledTouchableWithDynamicProps = extendedStyled.Image((props: { active: boolean }) => ({
width: props.active ? 100 : 50,
}))
const ViewWithText = extendedStyled.View({}).withChild(StyledText)

const extendedWithLinear = Object.assign(extendedStyled, {
LinearGradient: styled<LinearGradientProps, ViewStyle>(LinearGradient, {
name: 'styled(LinearGradient)',
})
LinearGradient: styled<LinearGradientProps, ViewStyle>(LinearGradient, {
name: 'styled(LinearGradient)',
}),
})

const DangerGradient = styled(LinearGradient)().attrs({ colors: ['orange', 'red']})
const DefaultGradient = extendedWithLinear.LinearGradient({ }).attrs({
colors: ['blue', 'green'],
const DangerGradient = styled(LinearGradient)().attrs({ colors: ['orange', 'red'] })
const DefaultGradient = extendedWithLinear.LinearGradient({}).attrs({
colors: ['blue', 'green'],
})

const MyScreen = () => {
const ref = useRef()
const ref = useRef()
return (
<>
<StyledText color="red" transparent>Hello Transparent World</StyledText>
<StyledText ref={ref} color="red">Hello Transparent World with ref</StyledText>
<StyledTextWithAttrs>Color set through .attrs() and therefore not required</StyledTextWithAttrs>
<StyledTextWithAttrsFromConfig>Color is set through config.attrs and therefore not required</StyledTextWithAttrsFromConfig>
<StyledText color="red" transparent>
Hello Transparent World
</StyledText>
<StyledText ref={ref} color="red">
Hello Transparent World with ref
</StyledText>
<StyledTextWithAttrs>
Color set through .attrs() and therefore not required
</StyledTextWithAttrs>
<StyledTextWithAttrsFromConfig>
Color is set through config.attrs and therefore not required
</StyledTextWithAttrsFromConfig>
{/* should also work without transparent prop */}
<StyledText color="red">Hello World</StyledText>
{/* @ts-expect-error -- should not work without required color prop */}
<StyledText>Hello World</StyledText>
{/* @ts-expect-error -- big is only available on StyledScalableText */}
<StyledText color="red" big>Hello World</StyledText>
<StyledScalableText color="red" transparent big>Hi</StyledScalableText>
<StyledText color="red" big>
Hello World
</StyledText>
<StyledScalableText color="red" transparent big>
Hi
</StyledScalableText>
{/* @ts-expect-error -- transparent prop is not allowed */}
<StyledTextWithObjectProps transparent>Hi</StyledTextWithObjectProps>
<ExtendedStyledText transparent>Hello Extended World</ExtendedStyledText>
<ExtenedStyledTextWithAttrs transparent big>Hello Extended World</ExtenedStyledTextWithAttrs>
<ExtendedStyledTextWithStyle specificColor="red" style={{fontSize: 10}}>Text with inline style</ExtendedStyledTextWithStyle>
<ExtenedStyledTextWithAttrs transparent big>
Hello Extended World
</ExtenedStyledTextWithAttrs>
<ExtendedStyledTextWithStyle specificColor="red" style={{ fontSize: 10 }}>
Text with inline style
</ExtendedStyledTextWithStyle>

<StyledView>Just a View</StyledView>

<Row style={{ justifyContent: 'center' }} full spaced={false}>
<ExtendedStyledText>some text</ExtendedStyledText>
</Row>

<Row style={{ justifyContent: 'center' }} full spaced={false}>
<ExtendedStyledText>some text</ExtendedStyledText>
</Row>

<StyledViewWithDynamicProps active>View with dynamic style</StyledViewWithDynamicProps>

<StyledImage>Image</StyledImage>
<StyledImageWithDynamicProps active>Image with dynamic style</StyledImageWithDynamicProps>

<StyledTouchable>Touchable</StyledTouchable>
<StyledTouchableWithDynamicProps active>Touchable with dynamic style</StyledTouchableWithDynamicProps>
<StyledTouchableWithDynamicProps active>
Touchable with dynamic style
</StyledTouchableWithDynamicProps>

<ViewWithText />

{/* @ts-expect-error -- missing "colors" */}
<LinearGradient/>
<LinearGradient />
{/* doesn't require optional props */}
<DefaultGradient />
<DangerGradient />
Expand Down

0 comments on commit a2c0fce

Please sign in to comment.