-
Notifications
You must be signed in to change notification settings - Fork 373
[eslint-plugin] Add transform-media-query rule and soft validation config #1301
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
base: main
Are you sure you want to change the base?
Changes from all commits
698e908
3abe112
004f228
2b1ecd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,7 +31,17 @@ export function flattenRawStyleObject( | |
| ? lastMediaQueryWinsTransform(style) | ||
| : style; | ||
| } catch (error) { | ||
| throw new Error(messages.INVALID_MEDIA_QUERY_SYNTAX); | ||
| if (options.softMediaQueryValidation) { | ||
| console.warn( | ||
| `StyleX: ${messages.INVALID_MEDIA_QUERY_SYNTAX}\n` + | ||
| 'Media query order will not be respected. ' + | ||
| 'This could be due to invalid media query syntax or unsupported edge cases in style-value-parser.\n' + | ||
| `Error: ${error.message || error}`, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we validate that this error message contains the erroring media query? |
||
| ); | ||
| processedStyle = style; | ||
| } else { | ||
| throw new Error(messages.INVALID_MEDIA_QUERY_SYNTAX); | ||
| } | ||
| } | ||
| return _flattenRawStyleObject(processedStyle, [], options); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,6 +97,7 @@ export type StyleXOptions = $ReadOnly<{ | |
| enableDevClassNames?: boolean, | ||
| enableInlinedConditionalMerge?: boolean, | ||
| enableMediaQueryOrder?: boolean, | ||
| softMediaQueryValidation?: boolean, | ||
| enableLegacyValueFlipping?: boolean, | ||
| enableLogicalStylesPolyfill?: boolean, | ||
| enableLTRRTLComments?: boolean, | ||
|
|
@@ -254,6 +255,14 @@ export default class StateManager { | |
| 'options.enableMediaQueryOrder', | ||
| ); | ||
|
|
||
| const softMediaQueryValidation: StyleXStateOptions['softMediaQueryValidation'] = | ||
| z.logAndDefault( | ||
| z.boolean(), | ||
| options.softMediaQueryValidation ?? false, | ||
| false, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's make this true for now |
||
| 'options.softMediaQueryValidation', | ||
| ); | ||
|
|
||
| const enableLegacyValueFlipping: StyleXStateOptions['enableLegacyValueFlipping'] = | ||
| z.logAndDefault( | ||
| z.boolean(), | ||
|
|
@@ -388,6 +397,7 @@ export default class StateManager { | |
| enableInlinedConditionalMerge, | ||
| enableMinifiedKeys, | ||
| enableMediaQueryOrder, | ||
| softMediaQueryValidation, | ||
| enableLegacyValueFlipping, | ||
| enableLogicalStylesPolyfill, | ||
| enableLTRRTLComments, | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,83 @@ | ||||||||
| /** | ||||||||
| * 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-transform-media-queries'); | ||||||||
|
|
||||||||
| const eslintTester = new ESLintTester({ | ||||||||
| parser: require.resolve('hermes-eslint'), | ||||||||
| parserOptions: { | ||||||||
| ecmaVersion: 6, | ||||||||
| sourceType: 'module', | ||||||||
| }, | ||||||||
| }); | ||||||||
|
|
||||||||
| eslintTester.run('stylex-transform-media-queries', rule.default, { | ||||||||
| valid: [ | ||||||||
| { | ||||||||
| code: ` | ||||||||
| import * as stylex from '@stylexjs/stylex'; | ||||||||
| const styles = stylex.create({ | ||||||||
| main: { | ||||||||
| color: 'red', | ||||||||
| }, | ||||||||
| }) | ||||||||
| `, | ||||||||
| }, | ||||||||
| { | ||||||||
| code: ` | ||||||||
| import * as stylex from '@stylexjs/stylex'; | ||||||||
| const styles = stylex.create({ | ||||||||
| main: { | ||||||||
| color: { | ||||||||
| default: 'yellow', | ||||||||
| '@media (min-width: 768px)': 'blue', | ||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we require a
Suggested change
|
||||||||
| '@media (min-width: 1024px)': 'green', | ||||||||
| }, | ||||||||
| }, | ||||||||
| }) | ||||||||
| `, | ||||||||
| }, | ||||||||
| { | ||||||||
| code: ` | ||||||||
| import * as stylex from '@stylexjs/stylex'; | ||||||||
| const styles = stylex.create({ | ||||||||
| main: { | ||||||||
| color: { | ||||||||
| '@media (max-width: 768px)': 'red', | ||||||||
| default: 'blue', | ||||||||
| }, | ||||||||
| }, | ||||||||
| }) | ||||||||
| `, | ||||||||
| }, | ||||||||
| ], | ||||||||
| invalid: [ | ||||||||
| { | ||||||||
| code: ` | ||||||||
| import * as stylex from '@stylexjs/stylex'; | ||||||||
| const styles = stylex.create({ | ||||||||
| main: { | ||||||||
| color: { | ||||||||
| '@media not ((not (min-width: 400px))': 'blue', | ||||||||
| }, | ||||||||
| }, | ||||||||
| }) | ||||||||
| `, | ||||||||
| errors: [ | ||||||||
| { | ||||||||
| message: | ||||||||
| /Media query order may not be respected\. Invalid media query syntax or unsupported edge cases in style-value-parser\. Error:/, | ||||||||
| }, | ||||||||
| ], | ||||||||
| }, | ||||||||
| ], | ||||||||
| }); | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,8 @@ | |
| "dependencies": { | ||
| "css-shorthand-expand": "^1.2.0", | ||
| "micromatch": "^4.0.5", | ||
| "postcss-value-parser": "^4.2.0" | ||
| "postcss-value-parser": "^4.2.0", | ||
| "style-value-parser": "*" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe |
||
| }, | ||
| "files": [ | ||
| "flow_modules/*", | ||
|
|
||
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.
probably don't need to "StyleX: " prefix here