-
Notifications
You must be signed in to change notification settings - Fork 840
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add no-restricted-eui-imports rule
- Loading branch information
1 parent
c8522d9
commit 6a751f9
Showing
10 changed files
with
4,659 additions
and
973 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package.tgz |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
rules/*.test.js | ||
package.tgz |
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,2 @@ | ||
{ | ||
} |
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,65 @@ | ||
const DEFAULT_RESTRICTED_IMPORT_PATTERNS = [ | ||
{ | ||
pattern: '@elastic/eui/dist/eui_theme_*.json', | ||
message: | ||
'For client-side, please use `useEuiTheme` instead. Direct JSON token imports will be removed as per the EUI Deprecation schedule: https://github.com/elastic/eui/issues/1469.', | ||
}, | ||
]; | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallow deprecated EUI imports.', | ||
category: 'Possible Errors', | ||
recommended: false, | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
patterns: { | ||
type: 'array', | ||
items: { | ||
type: 'object', | ||
properties: { | ||
pattern: { type: 'string' }, | ||
message: { type: 'string' }, | ||
}, | ||
required: ['pattern'], | ||
additionalProperties: false, | ||
}, | ||
uniqueItems: true, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}, | ||
|
||
create(context) { | ||
const options = context.options[0] || {}; | ||
const userPatterns = options.patterns || []; | ||
|
||
// Combine the default patterns with the user-defined patterns | ||
const allPatterns = [ | ||
...DEFAULT_RESTRICTED_IMPORT_PATTERNS, | ||
...userPatterns, | ||
]; | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
allPatterns.forEach(({ pattern, message }) => { | ||
const regex = new RegExp(pattern.replace('*', '.*')); | ||
if (regex.test(node.source.value)) { | ||
context.report({ | ||
node, | ||
message: | ||
message || `Importing "${node.source.value}" is restricted.`, | ||
}); | ||
} | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
52 changes: 52 additions & 0 deletions
52
packages/eslint-plugin/rules/no_restricted_eui_imports.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,52 @@ | ||
const { RuleTester } = require('eslint'); | ||
const rule = require('./no_restricted_eui_imports'); | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: require.resolve('babel-eslint'), | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
}, | ||
}); | ||
|
||
ruleTester.run('@elastic/eui/no-restricted-eui-imports', rule, { | ||
valid: [ | ||
{ | ||
code: "import { EuiButton } from '@elastic/eui';", | ||
}, | ||
{ | ||
code: "import theme from '@kbn/ui-theme';", | ||
}, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: "import theme from '@elastic/eui/dist/eui_theme_light.json';", | ||
errors: [ | ||
{ | ||
message: | ||
'For client-side, please use `useEuiTheme` instead. Direct JSON token imports will be removed as per the EUI Deprecation schedule: https://github.com/elastic/eui/issues/1469.', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: "import theme from '@kbn/ui-theme';", | ||
options: [ | ||
{ | ||
patterns: [ | ||
{ | ||
pattern: '@kbn/ui-theme', | ||
message: | ||
'For client-side, please use `useEuiTheme` from `@elastic/eui` instead.', | ||
}, | ||
], | ||
}, | ||
], | ||
errors: [ | ||
{ | ||
message: | ||
'For client-side, please use `useEuiTheme` from `@elastic/eui` instead.', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
Oops, something went wrong.