Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3eaeec1
start initial work for design token rules
eokoneyo Nov 18, 2024
e9f0840
add test and implementation for prefer css rule
eokoneyo Nov 18, 2024
10c7dd2
add implementation and test for no css colro value rule
eokoneyo Nov 18, 2024
759409b
start on adding documentation for new eslint rules
eokoneyo Nov 19, 2024
664f991
update regex for matching CSS color values
eokoneyo Nov 19, 2024
13fe975
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine Nov 19, 2024
855ee3e
overhaul package take, config and apply rules to codebase
eokoneyo Nov 19, 2024
a64f092
add support for more css properties that accept color values
eokoneyo Nov 19, 2024
d02fbc4
add improvement
eokoneyo Nov 20, 2024
ac87368
fix codeowners annotation
eokoneyo Nov 20, 2024
7d3f999
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine Nov 20, 2024
279f60a
even more improvements
eokoneyo Nov 26, 2024
02a8966
modify implementation to account for spread properties, and mark any …
eokoneyo Nov 26, 2024
94205ea
start on consideration for evaluating variables defined referenced as…
eokoneyo Nov 28, 2024
ede7c4f
handle cases for member expression values
eokoneyo Nov 29, 2024
4728e03
account for css tagged template literal usage to specify component cl…
eokoneyo Dec 2, 2024
c7357c8
improve README
eokoneyo Dec 2, 2024
ce7f27d
consolidate logic where possible
eokoneyo Dec 2, 2024
fb27e6d
simplify logic for detecting literal values for tagged template funct…
eokoneyo Dec 4, 2024
f943330
account for variables defined outside the scope of the component that…
eokoneyo Dec 5, 2024
0e4d9db
change implementation for discovering identifiers to handle even more…
eokoneyo Dec 5, 2024
6438572
improve implementation to prevent false positives
eokoneyo Dec 10, 2024
dfea8c2
fix issue with resolving variable declaration for spread elements, al…
eokoneyo Dec 12, 2024
219062a
Merge branch 'main' into chore/resolve-kibana-team-1272
elasticmachine Dec 16, 2024
7e3db76
Merge branch 'main' into chore/resolve-kibana-team-1272
elasticmachine Dec 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,6 @@ module.exports = {
],
},
},

/**
* Jest specific rules
*/
Expand Down
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ packages/kbn-es-errors @elastic/kibana-core
packages/kbn-es-query @elastic/kibana-data-discovery
packages/kbn-es-types @elastic/kibana-core @elastic/obs-knowledge-team
packages/kbn-eslint-config @elastic/kibana-operations
packages/kbn-eslint-plugin-css @elastic/appex-sharedux
packages/kbn-eslint-plugin-disable @elastic/kibana-operations
packages/kbn-eslint-plugin-eslint @elastic/kibana-operations
packages/kbn-eslint-plugin-i18n @elastic/obs-knowledge-team @elastic/kibana-operations
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,7 @@
"@kbn/es": "link:packages/kbn-es",
"@kbn/es-archiver": "link:packages/kbn-es-archiver",
"@kbn/eslint-config": "link:packages/kbn-eslint-config",
"@kbn/eslint-plugin-css": "link:packages/kbn-eslint-plugin-css",
"@kbn/eslint-plugin-disable": "link:packages/kbn-eslint-plugin-disable",
"@kbn/eslint-plugin-eslint": "link:packages/kbn-eslint-plugin-eslint",
"@kbn/eslint-plugin-i18n": "link:packages/kbn-eslint-plugin-i18n",
Expand Down Expand Up @@ -1566,6 +1567,7 @@
"@types/classnames": "^2.2.9",
"@types/cli-progress": "^3.11.5",
"@types/color": "^3.0.3",
"@types/cssstyle": "^2.2.4",
"@types/cytoscape": "^3.14.0",
"@types/d3": "^3.5.43",
"@types/d3-array": "^2.12.1",
Expand Down Expand Up @@ -1714,6 +1716,7 @@
"css-loader": "^3.4.2",
"cssnano": "^5.1.12",
"cssnano-preset-default": "^5.2.12",
"cssstyle": "^4.1.0",
"csstype": "^3.0.2",
"cypress": "13.15.2",
"cypress-axe": "^1.5.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/kbn-eslint-config/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
'@kbn/eslint-plugin-imports',
'@kbn/eslint-plugin-telemetry',
'@kbn/eslint-plugin-i18n',
'@kbn/eslint-plugin-css',
'eslint-plugin-depend',
'prettier',
],
Expand Down Expand Up @@ -332,6 +333,7 @@ module.exports = {
'@kbn/imports/no_boundary_crossing': 'error',
'@kbn/imports/no_group_crossing_manifests': 'error',
'@kbn/imports/no_group_crossing_imports': 'error',
'@kbn/css/no_css_color': 'warn',
'no-new-func': 'error',
'no-implied-eval': 'error',
'no-prototype-builtins': 'error',
Expand Down
129 changes: 129 additions & 0 deletions packages/kbn-eslint-plugin-css/README.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
---
id: kibSharedUXEslintPluginCSS
slug: /kibana-dev-docs/shared-ux/packages/kbn-eslint-plugin-css
title: '@kbn/eslint-plugin-design-tokens'
description: Custom ESLint rules to guardrails for using eui in the Kibana repository
date: 2024-11-19
tags: ['kibana', 'dev', 'contributor', 'shared_ux', 'eslint', 'eui']
---

# Summary

`@kbn/eslint-plugin-css` is an ESLint plugin providing custom ESLint rules to help setup guardrails for using eui in the Kibana repo especially around styling.

The aim of this package is to help engineers to modify EUI components in a much complaint way.

If a rule does not behave as you expect or you have an idea of how these rules can be improved, please reach out to the Shared UX team.

# Rules

## `@kbn/css/no_css_color`

This rule warns engineers to not use literal css color in the codebase, particularly for CSS properties that apply color to
either the html element or text nodes, but rather urge users to defer to using the color tokens provided by EUI.

This rule kicks in on the following JSXAttributes; `style`, `className` and `css` and supports various approaches to providing styling declarations.

### Example

The following code:

```
// Filename: /x-pack/plugins/observability_solution/observability/public/my_component.tsx

import React from 'react';
import { EuiText } from '@elastic/eui';

function MyComponent() {
return (
<EuiText style={{ color: 'red' }}>You know, for search</EuiText>
)
}
```

```
// Filename: /x-pack/plugins/observability_solution/observability/public/my_component.tsx

import React from 'react';
import { EuiText } from '@elastic/eui';

function MyComponent() {

const style = {
color: 'red'
}

return (
<EuiText style={{ color: style.color }}>You know, for search</EuiText>
)
}
```

```
// Filename: /x-pack/plugins/observability_solution/observability/public/my_component.tsx

import React from 'react';
import { EuiText } from '@elastic/eui';

function MyComponent() {
const colorValue = '#dd4040';

return (
<EuiText style={{ color: colorValue }}>You know, for search</EuiText>
)
}
```

will all raise an eslint report with an appropriate message of severity that matches the configuration of the rule, further more all the examples above
will also match for when the attribute in question is `css`. The `css` attribute will also raise a report the following cases below;

```
// Filename: /x-pack/plugins/observability_solution/observability/public/my_component.tsx

import React from 'react';
import { css } from '@emotion/css';
import { EuiText } from '@elastic/eui';

function MyComponent() {
return (
<EuiText css={css`color: '#dd4040' `}>You know, for search</EuiText>
)
}
```

```
// Filename: /x-pack/plugins/observability_solution/observability/public/my_component.tsx

import React from 'react';
import { EuiText } from '@elastic/eui';

function MyComponent() {
return (
<EuiText css={() => ({ color: '#dd4040' })}>You know, for search</EuiText>
)
}
```

A special case is also covered for the `className` attribute, where the rule will also raise a report for the following case below;


```
// Filename: /x-pack/plugins/observability_solution/observability/public/my_component.tsx

import React from 'react';
import { css } from '@emotion/css';
import { EuiText } from '@elastic/eui';

function MyComponent() {
return (
<EuiText className={css`color: '#dd4040'`}>You know, for search</EuiText>
)
}
```

it's worth pointing out that although the examples provided are specific to EUI components, this rule applies to all JSX elements.

## `@kbn/css/prefer_css_attributes_for_eui_components`

This rule warns engineers to use the `css` attribute for EUI components instead of the `style` attribute.

20 changes: 20 additions & 0 deletions packages/kbn-eslint-plugin-css/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { NoCssColor } from './src/rules/no_css_color';
import { PreferCSSAttributeForEuiComponents } from './src/rules/prefer_css_attribute_for_eui_components';

/**
* Custom ESLint rules, included as `'@kbn/eslint-plugin-design-tokens'` in the kibana eslint config
* @internal
*/
export const rules = {
no_css_color: NoCssColor,
prefer_css_attributes_for_eui_components: PreferCSSAttributeForEuiComponents,
};
14 changes: 14 additions & 0 deletions packages/kbn-eslint-plugin-css/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

module.exports = {
preset: '@kbn/test',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-eslint-plugin-css'],
};
6 changes: 6 additions & 0 deletions packages/kbn-eslint-plugin-css/kibana.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "shared-common",
"id": "@kbn/eslint-plugin-css",
"devOnly": true,
"owner": "@elastic/appex-sharedux"
}
6 changes: 6 additions & 0 deletions packages/kbn-eslint-plugin-css/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "@kbn/eslint-plugin-css",
"version": "1.0.0",
"private": true,
"license": "Elastic License 2.0 OR AGPL-3.0-only OR SSPL-1.0"
}
Loading