-
Notifications
You must be signed in to change notification settings - Fork 842
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
@elastic/eslint-plugin-eui
package (#2218)
* add `@elastic/eslint-plugin-eui` package * Check for all relevant component names * link to @elastic/eslint-plugin-eui readme from release docs * fix link to readme * add git repo and homepage to package.json
- Loading branch information
Spencer
authored
Aug 15, 2019
1 parent
b01ef08
commit 336666a
Showing
11 changed files
with
214 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 @@ | ||
rules/*.test.js |
This file contains 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,25 @@ | ||
# `@elastic/eslint-plugin-eui` | ||
|
||
This package contains an eslint plugin that enforces some default rules for using EUI. | ||
|
||
## Setup | ||
|
||
1. install `@elastic/eslint-plugin-eui` as a dev dependency | ||
2. extend `plugin:@elastic/eui/recommended` in your eslint config | ||
|
||
## Rules | ||
|
||
### `@elastic/eui/href-or-on-click` | ||
|
||
`<EuiButton />` should either be a button or a link, for a11y purposes. When given an `href` the button behaves as a link, otherwise an `onClick` handler is expected and it will behave as a button. | ||
|
||
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. | ||
|
||
## 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 |
This file contains 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,32 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
module.exports = { | ||
rules: { | ||
'href-or-on-click': require('./rules/href_or_on_click'), | ||
}, | ||
configs: { | ||
recommended: { | ||
plugins: ['@elastic/eslint-plugin-eui'], | ||
rules: { | ||
'@elastic/eui/href-or-on-click': 'error', | ||
}, | ||
}, | ||
}, | ||
}; |
This file contains 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,13 @@ | ||
{ | ||
"name": "@elastic/eslint-plugin-eui", | ||
"version": "0.0.1", | ||
"license": "Apache-2.0", | ||
"repository": { | ||
"type" : "git", | ||
"url" : "https://github.com/elastic/eui.git" | ||
}, | ||
"homepage": "https://github.com/elastic/eui/blob/master/packages/eslint-plugin", | ||
"peerDependencies": { | ||
"eslint": "^5.0.0" | ||
} | ||
} |
This file contains 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,35 @@ | ||
const componentNames = ['EuiButton', 'EuiButtonEmpty', 'EuiLink']; | ||
|
||
module.exports = { | ||
meta: { | ||
fixable: null, | ||
}, | ||
create(context) { | ||
return { | ||
JSXOpeningElement(node) { | ||
if ( | ||
node.name.type !== 'JSXIdentifier' || | ||
!componentNames.includes(node.name.name) | ||
) { | ||
return; | ||
} | ||
|
||
const hasHref = node.attributes.some( | ||
attr => attr.type === 'JSXAttribute' && attr.name.name === 'href' | ||
); | ||
const hasOnClick = node.attributes.some( | ||
attr => attr.type === 'JSXAttribute' && attr.name.name === 'onClick' | ||
); | ||
|
||
if (hasHref && hasOnClick) { | ||
context.report( | ||
node, | ||
`<${ | ||
node.name.name | ||
}> accepts either \`href\` or \`onClick\`, not both.` | ||
); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains 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,95 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
|
||
const { RuleTester } = require('eslint'); | ||
const rule = require('./href_or_on_click'); | ||
const dedent = require('dedent'); | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: 'babel-eslint', | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
}, | ||
}); | ||
|
||
ruleTester.run('@elastic/eui/href-or-on-click', rule, { | ||
valid: [ | ||
{ | ||
code: dedent(` | ||
module.export = () => ( | ||
<EuiButton /> | ||
) | ||
`), | ||
}, | ||
{ | ||
code: dedent(` | ||
module.export = () => ( | ||
<EuiButton href="/" /> | ||
) | ||
`), | ||
}, | ||
{ | ||
code: dedent(` | ||
module.export = () => ( | ||
<EuiButton href={'/' + 'home'} /> | ||
) | ||
`), | ||
}, | ||
{ | ||
code: dedent(` | ||
module.export = () => ( | ||
<EuiButton onClick={executeAction} /> | ||
) | ||
`), | ||
}, | ||
{ | ||
code: dedent(` | ||
module.export = () => ( | ||
<EuiButton onClick={() => executeAction()} /> | ||
) | ||
`), | ||
}, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: dedent(` | ||
module.export = () => ( | ||
<EuiButton href="/" onClick={fooBar} /> | ||
) | ||
`), | ||
|
||
errors: [ | ||
{ | ||
message: '<EuiButton> accepts either `href` or `onClick`, not both.', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent(` | ||
module.export = () => ( | ||
<EuiButtonEmpty href="/" onClick={fooBar} /> | ||
) | ||
`), | ||
|
||
errors: [ | ||
{ | ||
message: | ||
'<EuiButtonEmpty> accepts either `href` or `onClick`, not both.', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: dedent(` | ||
module.export = () => ( | ||
<EuiLink href="/" onClick={fooBar} /> | ||
) | ||
`), | ||
|
||
errors: [ | ||
{ | ||
message: '<EuiLink> accepts either `href` or `onClick`, not both.', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
This file contains 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 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 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