-
Notifications
You must be signed in to change notification settings - Fork 858
[ESLint] Warn about static z-index #9236
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
Merged
weronikaolejniczak
merged 4 commits into
elastic:main
from
weronikaolejniczak:feat/eslint-z-index-warning
Jan 13, 2026
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8b8590a
feat(eslint-plugin): add a no-static-z-index rule
weronikaolejniczak 7b8869b
chore: add changelog
weronikaolejniczak 4f3ba2b
Update packages/eslint-plugin/changelogs/upcoming/9236.md
weronikaolejniczak 15d9d74
feat: add new cases and fix syntax
weronikaolejniczak 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Add a `no-static-z-index` rule | ||
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
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
242 changes: 242 additions & 0 deletions
242
packages/eslint-plugin/src/rules/no_static_z_index.test.ts
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,242 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import dedent from 'dedent'; | ||
| import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
|
|
||
| import { NoStaticZIndex } from './no_static_z_index'; | ||
|
|
||
| const languageOptions = { | ||
| parserOptions: { | ||
| ecmaFeatures: { | ||
| jsx: true, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const ruleTester = new RuleTester(); | ||
|
|
||
| ruleTester.run('no-static-z-index', NoStaticZIndex, { | ||
| valid: [ | ||
| { | ||
| // Valid: Using euiTheme variable in inline style | ||
| filename: 'test.tsx', | ||
| code: dedent` | ||
| import React from 'react'; | ||
| import { EuiCode } from '@elastic/eui'; | ||
|
|
||
| function TestComponent() { | ||
| const euiTheme = { levels: { mask: 1000 } }; | ||
| return ( | ||
| <EuiCode style={{ zIndex: euiTheme.levels.mask }}>test</EuiCode> | ||
| ) | ||
| }`, | ||
| languageOptions, | ||
| }, | ||
| { | ||
| // Valid: CSS keyword 'auto' | ||
| filename: 'test.tsx', | ||
| code: dedent` | ||
| import React from 'react'; | ||
|
|
||
| function TestComponent() { | ||
| return ( | ||
| <div style={{ zIndex: 'auto' }}>test</div> | ||
| ) | ||
| }`, | ||
| languageOptions, | ||
| }, | ||
| { | ||
| // Valid: Emotion css with euiTheme variable | ||
| filename: 'test.tsx', | ||
| code: dedent` | ||
| import React from 'react'; | ||
| import { css } from '@emotion/react'; | ||
|
|
||
| function TestComponent() { | ||
| const theme = { zIndex: { modal: 2000 } }; | ||
| return ( | ||
| <div css={css\` | ||
| z-index: \${theme.zIndex.modal}; | ||
| \`}>test</div> | ||
| ) | ||
| }`, | ||
| languageOptions, | ||
| }, | ||
| { | ||
| // Valid: Object style with variable | ||
| filename: 'test.tsx', | ||
| code: dedent` | ||
| import React from 'react'; | ||
|
|
||
| function TestComponent() { | ||
| const zIndexValue = someDynamicValue; | ||
| const style = { zIndex: zIndexValue }; | ||
| return <div style={style}>test</div> | ||
| }`, | ||
| languageOptions, | ||
| }, | ||
| { | ||
| // Valid: Commented out z-index in css template literal | ||
| filename: 'test.tsx', | ||
| code: dedent` | ||
| import React from 'react'; | ||
| import { css } from '@emotion/react'; | ||
|
|
||
| function TestComponent() { | ||
| return ( | ||
| <div css={css\` | ||
| /* z-index: 50; */ | ||
| \`}>test</div> | ||
| ) | ||
| }`, | ||
| languageOptions, | ||
| }, | ||
| ], | ||
|
|
||
| invalid: [ | ||
| { | ||
| // Invalid: Inline style with static number | ||
| filename: 'test.tsx', | ||
| code: dedent` | ||
| import React from 'react'; | ||
|
|
||
| function TestComponent() { | ||
| return ( | ||
| <div style={{ zIndex: 100 }}>test</div> | ||
| ) | ||
| }`, | ||
| languageOptions, | ||
| errors: [{ messageId: 'noStaticZIndexSpecific' }], | ||
| }, | ||
| { | ||
| // Invalid: Inline style with static string number | ||
| filename: 'test.tsx', | ||
| code: dedent` | ||
| import React from 'react'; | ||
|
|
||
| function TestComponent() { | ||
| return ( | ||
| <div style={{ zIndex: '999' }}>test</div> | ||
| ) | ||
| }`, | ||
| languageOptions, | ||
| errors: [{ messageId: 'noStaticZIndexSpecific' }], | ||
| }, | ||
| { | ||
| // Invalid: Emotion css prop with static value | ||
| filename: 'test.tsx', | ||
| code: dedent` | ||
| import React from 'react'; | ||
| import { css } from '@emotion/react'; | ||
|
|
||
| function TestComponent() { | ||
| return ( | ||
| <div css={css\` | ||
| z-index: 50; | ||
| \`}>test</div> | ||
| ) | ||
| }`, | ||
| languageOptions, | ||
| errors: [{ messageId: 'noStaticZIndex' }], | ||
| }, | ||
| { | ||
| // Invalid: Variable with static value used in style | ||
| filename: 'test.tsx', | ||
| code: dedent` | ||
| import React from 'react'; | ||
|
|
||
| function TestComponent() { | ||
| const myStyle = { zIndex: 10 }; | ||
| return <div style={myStyle}>test</div> | ||
| }`, | ||
| languageOptions, | ||
| errors: [{ messageId: 'noStaticZIndexSpecificDeclaredVariable' }], | ||
| }, | ||
| { | ||
| // Invalid: Variable with static value used in css prop (object style) | ||
| filename: 'test.tsx', | ||
| code: dedent` | ||
| import React from 'react'; | ||
| import { css } from '@emotion/react'; | ||
|
|
||
| const myCss = css({ zIndex: 100 }); | ||
|
|
||
| function TestComponent() { | ||
| return <div css={myCss}>test</div> | ||
| }`, | ||
| languageOptions, | ||
| errors: [{ messageId: 'noStaticZIndexSpecificDeclaredVariable' }], | ||
| }, | ||
| { | ||
| // Invalid: css template literal with static z-index | ||
| filename: 'test.tsx', | ||
| code: dedent` | ||
| import { css } from '@emotion/css'; | ||
|
|
||
| const codeCss = css\` z-index: 10; \` | ||
| `, | ||
| languageOptions, | ||
| errors: [{ messageId: 'noStaticZIndex' }], | ||
| }, | ||
| { | ||
| // Invalid: css template literal with nested static z-index | ||
| filename: 'test.tsx', | ||
| code: dedent` | ||
| import { css } from '@emotion/react'; | ||
|
|
||
| const codeCss = css\` | ||
| &:hover { | ||
| z-index: 10; | ||
| } | ||
| \` | ||
| `, | ||
| languageOptions, | ||
| errors: [{ messageId: 'noStaticZIndex' }], | ||
| }, | ||
| { | ||
| // Invalid: css object with static z-index | ||
| filename: 'test.tsx', | ||
| code: dedent` | ||
| import { css } from '@emotion/react'; | ||
|
|
||
| function TestComponent() { | ||
| return <div css={css({ zIndex: 5 })}>test</div> | ||
| } | ||
| `, | ||
| languageOptions, | ||
| errors: [{ messageId: 'noStaticZIndexSpecific' }], | ||
| }, | ||
| { | ||
| // Invalid: arrow function returning object with static z-index | ||
| filename: 'test.tsx', | ||
| code: dedent` | ||
| import { css } from '@emotion/react'; | ||
|
|
||
| function TestComponent() { | ||
| return <div css={() => ({ zIndex: 5 })}>test</div> | ||
| } | ||
| `, | ||
| languageOptions, | ||
| errors: [{ messageId: 'noStaticZIndexSpecific' }], | ||
| }, | ||
| { | ||
| // Invalid: css with multiple arguments, one with static z-index | ||
| filename: 'test.tsx', | ||
| code: dedent` | ||
| import { css } from '@emotion/react'; | ||
|
|
||
| function TestComponent() { | ||
| return <div css={css(someStyle, { zIndex: 5 })}>test</div> | ||
| } | ||
| `, | ||
| languageOptions, | ||
| errors: [{ messageId: 'noStaticZIndexSpecific' }], | ||
| }, | ||
weronikaolejniczak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ], | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.