Skip to content

Commit

Permalink
Enforce that text is wrapped in <Text>, remaining cases (#3421)
Browse files Browse the repository at this point in the history
* Toggle.Button -> Toggle.ButtonWithText

* Simplify Prompt.Cancel/Action

* Move lines down for better diff

* Remove ButtonWithText

* Simplify types

* Enforce Button/ButtonText nesting

* Add suggested wrapper in linter error

* Check <Trans> ancestry too

* Also check literals

* Rm ts-ignore
  • Loading branch information
gaearon authored Apr 5, 2024
1 parent 49266c3 commit 46c112e
Show file tree
Hide file tree
Showing 15 changed files with 587 additions and 73 deletions.
15 changes: 5 additions & 10 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,12 @@ module.exports = {
'bsky-internal/avoid-unwrapped-text': [
'error',
{
impliedTextComponents: [
'Button', // TODO: Not always safe.
'H1',
'H2',
'H3',
'H4',
'H5',
'H6',
'P',
],
impliedTextComponents: ['H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'P'],
impliedTextProps: [],
suggestedTextWrappers: {
Button: 'ButtonText',
'ToggleButton.Button': 'ToggleButton.ButtonText',
},
},
],
'simple-import-sort/imports': [
Expand Down
339 changes: 338 additions & 1 deletion eslint/__tests__/avoid-unwrapped-text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('avoid-unwrapped-text', () => {

{
code: `
<View prop={
<View propText={
<Trans><Text>foo</Text></Trans>
}>
<Bar />
Expand Down Expand Up @@ -281,6 +281,170 @@ function MyText({ foo }) {
}
`,
},

{
code: `
<View>
<Text>{'foo'}</Text>
</View>
`,
},

{
code: `
<View>
<Text>{foo + 'foo'}</Text>
</View>
`,
},

{
code: `
<View>
<Text><Trans>{'foo'}</Trans></Text>
</View>
`,
},

{
code: `
<View>
{foo['bar'] && <Bar />}
</View>
`,
},

{
code: `
<View>
{(foo === 'bar') && <Bar />}
</View>
`,
},

{
code: `
<View>
{(foo !== 'bar') && <Bar />}
</View>
`,
},

{
code: `
<View>
<Text>{\`foo\`}</Text>
</View>
`,
},

{
code: `
<View>
<Text><Trans>{\`foo\`}</Trans></Text>
</View>
`,
},

{
code: `
<View>
<Text>{_(msg\`foo\`)}</Text>
</View>
`,
},

{
code: `
<View>
<Text><Trans>{_(msg\`foo\`)}</Trans></Text>
</View>
`,
},

{
code: `
<Foo>
<View prop={stuff('foo')}>
<Bar />
</View>
</Foo>
`,
},

{
code: `
<Foo>
<View onClick={() => stuff('foo')}>
<Bar />
</View>
</Foo>
`,
},

{
code: `
<View>
{renderItem('foo')}
</View>
`,
},

{
code: `
<View>
{foo === 'foo' && <Bar />}
</View>
`,
},

{
code: `
<View>
{foo['foo'] && <Bar />}
</View>
`,
},

{
code: `
<View>
{check('foo') && <Bar />}
</View>
`,
},

{
code: `
<View>
{foo.bar && <Bar />}
</View>
`,
},

{
code: `
<Text>
<Trans>{renderItem('foo')}</Trans>
</Text>
`,
},

{
code: `
<View>
{null}
</View>
`,
},

{
code: `
<Text>
<Trans>{null}</Trans>
</Text>
`,
},
],

invalid: [
Expand Down Expand Up @@ -455,6 +619,179 @@ function MyText({ foo }) {
`,
errors: 1,
},

{
code: `
<View>
{'foo'}
</View>
`,
errors: 1,
},

{
code: `
<View>
{foo && 'foo'}
</View>
`,
errors: 1,
},

{
code: `
<View>
<Trans>{'foo'}</Trans>
</View>
`,
errors: 1,
},

{
code: `
<View>
{foo && <Trans>{'foo'}</Trans>}
</View>
`,
errors: 1,
},

{
code: `
<View>
{10}
</View>
`,
errors: 1,
},

{
code: `
<View>
<Trans>{10}</Trans>
</View>
`,
errors: 1,
},

{
code: `
<View>
<Trans>{foo + 10}</Trans>
</View>
`,
errors: 1,
},

{
code: `
<View>
{\`foo\`}
</View>
`,
errors: 1,
},

{
code: `
<View>
<Trans>{\`foo\`}</Trans>
</View>
`,
errors: 1,
},

{
code: `
<View>
<Trans>{foo + \`foo\`}</Trans>
</View>
`,
errors: 1,
},

{
code: `
<View>
{_(msg\`foo\`)}
</View>
`,
errors: 1,
},

{
code: `
<View>
{foo + _(msg\`foo\`)}
</View>
`,
errors: 1,
},

{
code: `
<View>
<Trans>{_(msg\`foo\`)}</Trans>
</View>
`,
errors: 1,
},

{
code: `
<View>
<Trans>{foo + _(msg\`foo\`)}</Trans>
</View>
`,
errors: 1,
},

{
code: `
<View>
<Trans>foo</Trans>
</View>
`,
errors: 1,
},

{
code: `
<View>
<Trans><Trans>foo</Trans></Trans>
</View>
`,
errors: 1,
},

{
code: `
<View>
<Trans>{foo}</Trans>
</View>
`,
errors: 1,
},

{
code: `
<View>
<Trans>{'foo'}</Trans>
</View>
`,
errors: 1,
},

{
code: `
<View prop={
<Trans><Text>foo</Text></Trans>
}>
<Bar />
</View>
`,
errors: 1,
},
],
}

Expand Down
Loading

0 comments on commit 46c112e

Please sign in to comment.