Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
eeb8278
feat(eslint-plugin): add no-restricted-eui-imports rule
weronikaolejniczak Jan 8, 2025
3649ad9
fix(eslint-config): rephrase description, remove category and escape dot
weronikaolejniczak Jan 27, 2025
a7cdd22
chore(eslint-plugin): remove empty Jest config
weronikaolejniczak Jan 27, 2025
8aee3e9
chore(eslint-plugin): gitignore .yarn folder
weronikaolejniczak Jan 30, 2025
579e220
chore(eslint-plugin): prepend all files with ES license
weronikaolejniczak Jan 30, 2025
6cbe34b
chore(eslint-plugin): use ESM and TypeScript
weronikaolejniczak Jan 30, 2025
e729eef
chore(eslint-plugin): update HrefOnClick rule meta
weronikaolejniczak Jan 30, 2025
1293ec8
chore(eslint-plugin): rewrite no_restricted_eui_imports rule
weronikaolejniczak Feb 3, 2025
f778cbf
chore(eslint-plugin): add an ESLint config
weronikaolejniczak Feb 3, 2025
f938c1a
chore(eslint-plugin): rewrite href_or_on_click rule
weronikaolejniczak Feb 3, 2025
ca66e6e
chore(eslint-plugin): add prefer_css_attribute_for_eui_components rule
weronikaolejniczak Feb 3, 2025
bf0bad6
chore(eslint-plugin): add explanation comment to href_or_on_click
weronikaolejniczak Feb 3, 2025
a04fdcc
chore(eslint-plugin): add no_css_color rule
weronikaolejniczak Feb 3, 2025
3654bbb
chore(eslint-plugin): add README for new rules
weronikaolejniczak Feb 3, 2025
b1df354
chore(eslint-plugin): update yarn.lock file
weronikaolejniczak Feb 3, 2025
af60230
chore(eslint-plugin): re-type "any"
weronikaolejniczak Feb 7, 2025
5a7936e
chore(eslint-plugin): move files to src folder
weronikaolejniczak Feb 25, 2025
8b3b290
chore(eslint-plugin): add lib folder to gitignore file
weronikaolejniczak Feb 25, 2025
0eafa31
chore(eslint-plugin): configure cjs and esm output
weronikaolejniczak Feb 26, 2025
9c5ce87
refactor(eslint-plugin): use tag function syntax with dedent
weronikaolejniczak Feb 28, 2025
40581ac
chore(eslint-plugin): update TS config and use module.exports
weronikaolejniczak Feb 28, 2025
5659611
refactor(eslint-plugin): use message instead of messageId
weronikaolejniczak Mar 3, 2025
c4f363f
fix: resolve type issues and improve package.json
tkajtoch Mar 20, 2025
81d88f7
chore: remove `yarn.lock` from eslint-plugin to make it a yarn workspace
tkajtoch Mar 20, 2025
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
6 changes: 6 additions & 0 deletions packages/eslint-plugin/.babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
],
};
6 changes: 6 additions & 0 deletions packages/eslint-plugin/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
root: true,
};
3 changes: 3 additions & 0 deletions packages/eslint-plugin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package.tgz
.yarn
lib
1 change: 1 addition & 0 deletions packages/eslint-plugin/.npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
rules/*.test.js
package.tgz
138 changes: 133 additions & 5 deletions packages/eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ This package contains an eslint plugin that enforces some default rules for usin

## Setup

1. install `@elastic/eslint-plugin-eui` as a dev dependency
2. extend `plugin:@elastic/eui/recommended` in your eslint config
1. Install `@elastic/eslint-plugin-eui` as a dev dependency.
2. Extend `plugin:@elastic/eui/recommended` in your ESLint config.

## Rules

Expand All @@ -15,11 +15,139 @@ This package contains an eslint plugin that enforces some default rules for usin

In some cases it makes sense to disable this rule locally, such as when <kbd>cmd</kbd>+click should open the link in a new tab, but a standard click should use the `history.pushState()` API to change the URL without triggering a full page load.

### `@elastic/eui/no-restricted-eui-imports`

At times, we deprecate features that may need more highlighting and/or that are not possible to annotate with JSDoc `@deprecated`, e.g. JSON token imports: `@elastic/eui/dist/eui_theme_*.json` (for context: https://github.com/elastic/kibana/issues/199715#json-tokens).

We don't use `no-restricted-imports` because ESLint doesn't allow multiple error levels at once and it may conflict with the consumer's existing ESLint configuration for that rule. We need to assure that our rule will produce a warning (as a recommendation).

All deprecations still must follow our [deprecation process](../../wiki/eui-team-processes/deprecations.md).

### `@elastic/eui/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.

## `@elastic/eui/prefer-css-attributes-for-eui-components`

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

## Testing

### Against an existing package

To test the local changes to the plugin, you must:

1. Run `yarn pack` in the directory.
2. In your project's `package.json`, point `@elastic/eslint-plugin-eui` to `file:/path/to/package.tgz`.
3. Install dependencies: `yarn kbn bootstrap --no-validate`.

## Publishing

This package is published separately from the rest of EUI, as required by eslint. The code is not transpiled, so make sure to use `require()` statements rather than `import`, and once the code is updated run:

1. `npm version patch|minor|major`
2. commit version bump
3. `npm publish` in this directory
4. push the version bump upstream
2. Commit version bump.
3. `npm publish` in this directory.
4. Push the version bump upstream.
55 changes: 49 additions & 6 deletions packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,58 @@
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://github.com/elastic/eui.git"
"url": "https://github.com/elastic/eui.git",
"directory": "packages/eslint-plugin"
},
"homepage": "https://github.com/elastic/eui/blob/main/packages/eslint-plugin",
"exports": {
"require": "./lib/cjs/index.js",
"import": "./lib/esm/index.js"
},
"peerDependencies": {
"eslint": ">=5 <7"
"eslint": "^8.57.0 || ^9.0.0",
"typescript": ">=4.8.4 <6.0.0"
},
"devDependencies": {
"@babel/core": "^7.6.0",
"babel-eslint": "^10.0.3",
"eslint": "^6.4.0"
}
"@babel/cli": "^7.26.4",
"@babel/core": "^7.26.9",
"@babel/preset-env": "^7.26.9",
"@babel/preset-typescript": "^7.26.0",
"@types/babel__core": "^7",
"@types/babel__preset-env": "^7",
"@types/cssstyle": "^2.2.4",
"@types/dedent": "^0.7.2",
"@types/jest": "^29.5.14",
"@types/micromatch": "^4.0.9",
"@typescript-eslint/eslint-plugin": "^8.22.0",
"@typescript-eslint/parser": "^8.22.0",
"@typescript-eslint/rule-tester": "^8.22.0",
"@typescript-eslint/typescript-estree": "^8.22.0",
"@typescript-eslint/utils": "^8.22.0",
"cssstyle": "^4.2.1",
"dedent": "^1.5.3",
"eslint": "^8.57.0",
"jest": "^29.7.0",
"micromatch": "^4.0.8",
"rimraf": "^6.0.1",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.7.3"
},
"scripts": {
"test": "jest src",
"build": "yarn build:clean && yarn build:compile && yarn build:compile:esm && yarn build:types",
"build:clean": "rimraf lib/",
"build:compile:esm": "tsc --project ./tsconfig.esm.json",
"build:compile": "NODE_ENV=production babel src --out-dir=lib/cjs --extensions .js,.ts,.tsx",
"build:types": "NODE_ENV=production tsc --project tsconfig.types.json",
"build:pack": "yarn build && npm pack"
},
"files": [
"lib",
"!**/.tsbuildinfo",
"!**/*.test.js",
"!**/*.test.js.map",
"!**/*.test.d.ts"
]
}
33 changes: 0 additions & 33 deletions packages/eslint-plugin/rules/href_or_on_click.js

This file was deleted.

95 changes: 0 additions & 95 deletions packages/eslint-plugin/rules/href_or_on_click.test.js

This file was deleted.

Loading