-
Notifications
You must be signed in to change notification settings - Fork 365
[eslint] no-conflicting-props rule #1381
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
Open
henryqdineen
wants to merge
4
commits into
facebook:main
Choose a base branch
from
henryqdineen:hqd-no-conflicting-props
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+481
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
441c624
[eslint] no-conflicting-props rule
henryqdineen 14089df
Update packages/@stylexjs/eslint-plugin/src/stylex-no-conflicting-pro…
henryqdineen 1da2a50
[eslint] update README for no-conflicting-props rule
henryqdineen e6fa4b8
[eslint] check for conflict with inline spread
henryqdineen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
308 changes: 308 additions & 0 deletions
308
packages/@stylexjs/eslint-plugin/__tests__/stylex-no-conflicting-props-test.js
This file contains hidden or 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,308 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| 'use strict'; | ||
|
|
||
| jest.disableAutomock(); | ||
|
|
||
| const { RuleTester: ESLintTester } = require('eslint'); | ||
| const rule = require('../src/stylex-no-conflicting-props'); | ||
|
|
||
| const eslintTester = new ESLintTester({ | ||
| parser: require.resolve('hermes-eslint'), | ||
| parserOptions: { | ||
| ecmaVersion: 6, | ||
| sourceType: 'module', | ||
| ecmaFeatures: { | ||
| jsx: true, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| eslintTester.run('stylex-no-conflicting-props', rule.default, { | ||
| valid: [ | ||
| { | ||
| code: ` | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| const styles = stylex.create({ | ||
| main: { color: 'red' }, | ||
| }); | ||
| function Component() { | ||
| return <div {...stylex.props(styles.main)} />; | ||
| } | ||
| `, | ||
| }, | ||
| { | ||
| code: ` | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| function Component() { | ||
| return <div className="foo" />; | ||
| } | ||
| `, | ||
| }, | ||
| { | ||
| code: ` | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| function Component() { | ||
| return <div style={{ color: 'red' }} />; | ||
| } | ||
| `, | ||
| }, | ||
| { | ||
| code: ` | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| const styles = stylex.create({ | ||
| main: { color: 'red' }, | ||
| }); | ||
| function Component() { | ||
| return <div {...stylex.props(styles.main)} data-testid="test" />; | ||
| } | ||
| `, | ||
| }, | ||
| { | ||
| code: ` | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| const styles = stylex.create({ | ||
| main: { color: 'red' }, | ||
| }); | ||
| function Component() { | ||
| return <div {...otherProps} className="foo" />; | ||
| } | ||
| `, | ||
| }, | ||
| { | ||
| code: ` | ||
| import { props, create } from '@stylexjs/stylex'; | ||
| const styles = create({ | ||
| main: { color: 'red' }, | ||
| }); | ||
| function Component() { | ||
| return <div {...props(styles.main)} />; | ||
| } | ||
| `, | ||
| }, | ||
| { | ||
| options: [{ validImports: ['custom-stylex'] }], | ||
| code: ` | ||
| import * as stylex from 'custom-stylex'; | ||
| const styles = stylex.create({ | ||
| main: { color: 'red' }, | ||
| }); | ||
| function Component() { | ||
| return <div {...stylex.props(styles.main)} />; | ||
| } | ||
| `, | ||
| }, | ||
| { | ||
| options: [{ validImports: [{ from: 'a', as: 'css' }] }], | ||
| code: ` | ||
| import { css } from 'a'; | ||
| const styles = css.create({ | ||
| main: { color: 'red' }, | ||
| }); | ||
| function Component() { | ||
| return <div {...css.props(styles.main)} />; | ||
| } | ||
| `, | ||
| }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: ` | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| const styles = stylex.create({ | ||
| main: { color: 'red' }, | ||
| }); | ||
| function Component() { | ||
| return <div {...stylex.props(styles.main)} className="foo" />; | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'The `className` prop should not be used when spreading `stylex.props()` to avoid conflicts.', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: ` | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| const styles = stylex.create({ | ||
| main: { color: 'red' }, | ||
| }); | ||
| function Component() { | ||
| return <div {...stylex.props(styles.main)} style={{ margin: 10 }} />; | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'The `style` prop should not be used when spreading `stylex.props()` to avoid conflicts.', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: ` | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| const styles = stylex.create({ | ||
| main: { color: 'red' }, | ||
| }); | ||
| function Component() { | ||
| return <div className="foo" {...stylex.props(styles.main)} />; | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'The `className` prop should not be used when spreading `stylex.props()` to avoid conflicts.', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: ` | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| const styles = stylex.create({ | ||
| main: { color: 'red' }, | ||
| }); | ||
| function Component() { | ||
| return <div style={{ margin: 10 }} {...stylex.props(styles.main)} />; | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'The `style` prop should not be used when spreading `stylex.props()` to avoid conflicts.', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: ` | ||
| import { props, create } from '@stylexjs/stylex'; | ||
| const styles = create({ | ||
| main: { color: 'red' }, | ||
| }); | ||
| function Component() { | ||
| return <div {...props(styles.main)} className="foo" />; | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'The `className` prop should not be used when spreading `stylex.props()` to avoid conflicts.', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: ` | ||
| import { props as p, create } from '@stylexjs/stylex'; | ||
| const styles = create({ | ||
| main: { color: 'red' }, | ||
| }); | ||
| function Component() { | ||
| return <div {...p(styles.main)} className="foo" />; | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'The `className` prop should not be used when spreading `stylex.props()` to avoid conflicts.', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| options: [{ validImports: ['custom-stylex'] }], | ||
| code: ` | ||
| import * as stylex from 'custom-stylex'; | ||
| const styles = stylex.create({ | ||
| main: { color: 'red' }, | ||
| }); | ||
| function Component() { | ||
| return <div {...stylex.props(styles.main)} className="foo" />; | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'The `className` prop should not be used when spreading `stylex.props()` to avoid conflicts.', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| options: [{ validImports: [{ from: 'a', as: 'css' }] }], | ||
| code: ` | ||
| import { css } from 'a'; | ||
| const styles = css.create({ | ||
| main: { color: 'red' }, | ||
| }); | ||
| function Component() { | ||
| return <div {...css.props(styles.main)} className="foo" />; | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'The `className` prop should not be used when spreading `stylex.props()` to avoid conflicts.', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: ` | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| const styles = stylex.create({ | ||
| main: { color: 'red' }, | ||
| }); | ||
| function Component() { | ||
| return <div {...stylex.props(styles.main)} className="foo" style={{ margin: 10 }} />; | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'The `className` prop should not be used when spreading `stylex.props()` to avoid conflicts.', | ||
| }, | ||
| { | ||
| message: | ||
| 'The `style` prop should not be used when spreading `stylex.props()` to avoid conflicts.', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: ` | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| const styles = stylex.create({ | ||
| main: { color: 'red' }, | ||
| }); | ||
| function Component() { | ||
| return <div {...stylex.props(styles.main)} {...{ className: 'foo' }} />; | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'The `className` prop should not be used when spreading `stylex.props()` to avoid conflicts.', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: ` | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| const styles = stylex.create({ | ||
| main: { color: 'red' }, | ||
| }); | ||
| function Component() { | ||
| return <div {...stylex.props(styles.main)} {...{ style: { margin: 10 } }} />; | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| message: | ||
| 'The `style` prop should not be used when spreading `stylex.props()` to avoid conflicts.', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a test that checks for a conflict via another spread?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are you thinking by "conflict via another spread"? With the current implementation these would be false negatives:
The first example just seems unlikely and the second one seems difficult to implement without deeper analysis and/or type awareness. The simplest thing I can think of is to enforce that the
stylex.propsspread is the last spread so it overwrites previous conflicting props.Let me know what you were thinking. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The inline spread, the latter is probably overkill. We've seen some ugly jsx before (though uncommon!) and it's an easy check.
We shouldn't enforce
stylex.propsbeing last, since the goal of this guidance is to avoid multiple sources of truth forclassNameandstylealtogether.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might make sense to enforce that it is the last spread?
Separately, I was thinking of an enhancement to Babel to check for cases like:
And logging an error in
devifpropscontainsclassNameorstyle?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I pushed a commit in e6fa4b8 to handle the inline spread (e.g
{...{className: 'foo'}). I considered handling stuff like{...{...{className: 'foo'}}but it seemed like an extreme edge case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, would rather just disallow the pattern altogether than patch over the problem to have StyleX overwrite things when we don't know the author's intent. But no strong opinion.
Yeah good call! We should add more compiler validation in general.