-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ESLINT constraints to detect inter-group dependencies (#194810)
## Summary Addresses elastic/kibana-team#1175 As part of the **Sustainable Kibana Architecture** initiative, this PR sets the foundation to start classifying plugins in isolated groups, matching our current solutions / project types: * It adds support for the following fields in the packages' manifests (kibana.jsonc): * `group?: 'search' | 'security' | 'observability' | 'platform' | 'common'` * `visibility?: 'private' | 'shared'` * It proposes a folder structure to automatically infer groups: ```javascript 'src/platform/plugins/shared': { group: 'platform', visibility: 'shared', }, 'src/platform/plugins/internal': { group: 'platform', visibility: 'private', }, 'x-pack/platform/plugins/shared': { group: 'platform', visibility: 'shared', }, 'x-pack/platform/plugins/internal': { group: 'platform', visibility: 'private', }, 'x-pack/solutions/observability/plugins': { group: 'observability', visibility: 'private', }, 'x-pack/solutions/security/plugins': { group: 'security', visibility: 'private', }, 'x-pack/solutions/search/plugins': { group: 'search', visibility: 'private', }, ``` * If a plugin is moved to one of the specific locations above, the group and visibility in the manifest (if specified) must match those inferred from the path. * Plugins that are not relocated are considered: `group: 'common', visibility: 'shared'` by default. As soon as we specify a custom `group`, the ESLINT rules will check violations against dependencies / dependants. The ESLINT rules are pretty simple: * Plugins can only depend on: * Plugins in the same group * OR plugins with `'shared'` visibility * Plugins in `'observability', 'security', 'search'` groups are mandatorily `'private'`. --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
300678c
commit 2a085e1
Showing
36 changed files
with
1,278 additions
and
49 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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
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
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 @@ | ||
/* | ||
* 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 type { ModuleGroup, ModuleVisibility } from '@kbn/repo-info/types'; | ||
|
||
/** | ||
* Checks whether a given ModuleGroup can import from another one | ||
* @param importerGroup The group of the module that we are checking | ||
* @param importedGroup The group of the imported module | ||
* @param importedVisibility The visibility of the imported module | ||
* @returns true if importerGroup is allowed to import from importedGroup/Visibiliy | ||
*/ | ||
export function isImportableFrom( | ||
importerGroup: ModuleGroup, | ||
importedGroup: ModuleGroup, | ||
importedVisibility: ModuleVisibility | ||
): boolean { | ||
return importerGroup === importedGroup || importedVisibility === 'shared'; | ||
} |
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
155 changes: 155 additions & 0 deletions
155
packages/kbn-eslint-plugin-imports/src/rules/no_group_crossing_imports.test.ts
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,155 @@ | ||
/* | ||
* 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 { RuleTester } from 'eslint'; | ||
import dedent from 'dedent'; | ||
import { NoGroupCrossingImportsRule } from './no_group_crossing_imports'; | ||
import { formatSuggestions } from '../helpers/report'; | ||
import { ModuleGroup, ModuleVisibility } from '@kbn/repo-info/types'; | ||
|
||
const make = ( | ||
fromGroup: ModuleGroup, | ||
fromVisibility: ModuleVisibility, | ||
toGroup: ModuleGroup, | ||
toVisibility: ModuleVisibility, | ||
imp = 'import' | ||
) => ({ | ||
filename: `${fromGroup}.${fromVisibility}.ts`, | ||
code: dedent` | ||
${imp} '${toGroup}.${toVisibility}' | ||
`, | ||
}); | ||
|
||
jest.mock('../get_import_resolver', () => { | ||
return { | ||
getImportResolver() { | ||
return { | ||
resolve(req: string) { | ||
return { | ||
type: 'file', | ||
absolute: req.split('.'), | ||
}; | ||
}, | ||
}; | ||
}, | ||
}; | ||
}); | ||
|
||
jest.mock('../helpers/repo_source_classifier', () => { | ||
return { | ||
getRepoSourceClassifier() { | ||
return { | ||
classify(r: string | [string, string]) { | ||
const [group, visibility] = | ||
typeof r === 'string' ? (r.endsWith('.ts') ? r.slice(0, -3) : r).split('.') : r; | ||
return { | ||
pkgInfo: { | ||
pkgId: 'aPackage', | ||
}, | ||
group, | ||
visibility, | ||
}; | ||
}, | ||
}; | ||
}, | ||
}; | ||
}); | ||
|
||
const tsTester = [ | ||
'@typescript-eslint/parser', | ||
new RuleTester({ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
parserOptions: { | ||
sourceType: 'module', | ||
ecmaVersion: 2018, | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}), | ||
] as const; | ||
|
||
const babelTester = [ | ||
'@babel/eslint-parser', | ||
new RuleTester({ | ||
parser: require.resolve('@babel/eslint-parser'), | ||
parserOptions: { | ||
sourceType: 'module', | ||
ecmaVersion: 2018, | ||
requireConfigFile: false, | ||
babelOptions: { | ||
presets: ['@kbn/babel-preset/node_preset'], | ||
}, | ||
}, | ||
}), | ||
] as const; | ||
|
||
for (const [name, tester] of [tsTester, babelTester]) { | ||
describe(name, () => { | ||
tester.run('@kbn/imports/no_group_crossing_imports', NoGroupCrossingImportsRule, { | ||
valid: [ | ||
make('observability', 'private', 'observability', 'private'), | ||
make('security', 'private', 'security', 'private'), | ||
make('search', 'private', 'search', 'private'), | ||
make('observability', 'private', 'platform', 'shared'), | ||
make('security', 'private', 'common', 'shared'), | ||
make('platform', 'shared', 'platform', 'shared'), | ||
make('platform', 'shared', 'platform', 'private'), | ||
make('common', 'shared', 'common', 'shared'), | ||
], | ||
|
||
invalid: [ | ||
{ | ||
...make('observability', 'private', 'security', 'private'), | ||
errors: [ | ||
{ | ||
line: 1, | ||
messageId: 'ILLEGAL_IMPORT', | ||
data: { | ||
importerPackage: 'aPackage', | ||
importerGroup: 'observability', | ||
importedPackage: 'aPackage', | ||
importedGroup: 'security', | ||
importedVisibility: 'private', | ||
sourcePath: 'observability.private.ts', | ||
suggestion: formatSuggestions([ | ||
`Please review the dependencies in your module's manifest (kibana.jsonc).`, | ||
`Relocate this module to a different group, and/or make sure it has the right 'visibility'.`, | ||
`Address the conflicting dependencies by refactoring the code`, | ||
]), | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
...make('security', 'private', 'platform', 'private'), | ||
errors: [ | ||
{ | ||
line: 1, | ||
messageId: 'ILLEGAL_IMPORT', | ||
data: { | ||
importerPackage: 'aPackage', | ||
importerGroup: 'security', | ||
importedPackage: 'aPackage', | ||
importedGroup: 'platform', | ||
importedVisibility: 'private', | ||
sourcePath: 'security.private.ts', | ||
suggestion: formatSuggestions([ | ||
`Please review the dependencies in your module's manifest (kibana.jsonc).`, | ||
`Relocate this module to a different group, and/or make sure it has the right 'visibility'.`, | ||
`Address the conflicting dependencies by refactoring the code`, | ||
]), | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
}); | ||
} |
Oops, something went wrong.