Skip to content

Commit

Permalink
Merge branch 'main' into cases_webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Jul 25, 2022
2 parents 0fd363f + bf12ac2 commit 3dc2aaf
Show file tree
Hide file tree
Showing 50 changed files with 1,674 additions and 104 deletions.
6 changes: 6 additions & 0 deletions dev_docs/operations/operations_landing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ layout: landing
<DocRelatedArticles
sectionTitle='Build tooling'
items={[
{ pageId: "kibDevDocsOpsKbnPm" },
{ pageId: "kibDevDocsOpsOptimizer" },
{ pageId: "kibDevDocsOpsBabelPreset" },
{ pageId: "kibDevDocsOpsTypeSummarizer" },
Expand All @@ -36,6 +37,7 @@ layout: landing
sectionTitle='Linting & Validation'
items={[
{ pageId: "kibDevDocsOpsEslintConfig" },
{ pageId: "kibDevDocsOpsEslintPluginEslint" },
{ pageId: "kibDevDocsOpsEslintWithTypes" },
{ pageId: "kibDevDocsOpsEslintPluginImports" },
{ pageId: "kibDevDocsOpsEslintPluginDisable" },
Expand All @@ -55,5 +57,9 @@ layout: landing
{ pageId: "kibDevDocsOpsBazelRunner" },
{ pageId: "kibDevDocsOpsCliDevMode" },
{ pageId: "kibDevDocsOpsEs" },
{ pageId: "kibDevDocsOpsSomeDevLog" },
{ pageId: "kibDevDocsOpsDevCliRunner" },
{ pageId: "kibDevDocsOpsGetRepoFiles" },
{ pageId: "kibDevDocsOpsRepoSourceClassifier" },
]}
/>
6 changes: 6 additions & 0 deletions nav-kibana-dev.docnav.json
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,12 @@
},
{
"id": "kibDevDocsOpsDevCliRunner"
},
{
"id": "kibDevDocsOpsGetRepoFiles"
},
{
"id": "kibDevDocsOpsRepoSourceClassifier"
}
]
}
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -604,13 +604,16 @@
"@kbn/expect": "link:bazel-bin/packages/kbn-expect",
"@kbn/find-used-node-modules": "link:bazel-bin/packages/kbn-find-used-node-modules",
"@kbn/generate": "link:bazel-bin/packages/kbn-generate",
"@kbn/get-repo-files": "link:bazel-bin/packages/kbn-get-repo-files",
"@kbn/import-resolver": "link:bazel-bin/packages/kbn-import-resolver",
"@kbn/jest-serializers": "link:bazel-bin/packages/kbn-jest-serializers",
"@kbn/optimizer": "link:bazel-bin/packages/kbn-optimizer",
"@kbn/optimizer-webpack-helpers": "link:bazel-bin/packages/kbn-optimizer-webpack-helpers",
"@kbn/performance-testing-dataset-extractor": "link:bazel-bin/packages/kbn-performance-testing-dataset-extractor",
"@kbn/plugin-generator": "link:bazel-bin/packages/kbn-plugin-generator",
"@kbn/plugin-helpers": "link:bazel-bin/packages/kbn-plugin-helpers",
"@kbn/repo-source-classifier": "link:bazel-bin/packages/kbn-repo-source-classifier",
"@kbn/repo-source-classifier-cli": "link:bazel-bin/packages/kbn-repo-source-classifier-cli",
"@kbn/scalability-simulation-generator": "link:bazel-bin/packages/kbn-scalability-simulation-generator",
"@kbn/some-dev-log": "link:bazel-bin/packages/kbn-some-dev-log",
"@kbn/sort-package-json": "link:bazel-bin/packages/kbn-sort-package-json",
Expand Down Expand Up @@ -851,6 +854,7 @@
"@types/kbn__field-types": "link:bazel-bin/packages/kbn-field-types/npm_module_types",
"@types/kbn__find-used-node-modules": "link:bazel-bin/packages/kbn-find-used-node-modules/npm_module_types",
"@types/kbn__generate": "link:bazel-bin/packages/kbn-generate/npm_module_types",
"@types/kbn__get-repo-files": "link:bazel-bin/packages/kbn-get-repo-files/npm_module_types",
"@types/kbn__handlebars": "link:bazel-bin/packages/kbn-handlebars/npm_module_types",
"@types/kbn__hapi-mocks": "link:bazel-bin/packages/kbn-hapi-mocks/npm_module_types",
"@types/kbn__home-sample-data-cards": "link:bazel-bin/packages/home/sample_data_cards/npm_module_types",
Expand All @@ -876,6 +880,8 @@
"@types/kbn__plugin-generator": "link:bazel-bin/packages/kbn-plugin-generator/npm_module_types",
"@types/kbn__plugin-helpers": "link:bazel-bin/packages/kbn-plugin-helpers/npm_module_types",
"@types/kbn__react-field": "link:bazel-bin/packages/kbn-react-field/npm_module_types",
"@types/kbn__repo-source-classifier": "link:bazel-bin/packages/kbn-repo-source-classifier/npm_module_types",
"@types/kbn__repo-source-classifier-cli": "link:bazel-bin/packages/kbn-repo-source-classifier-cli/npm_module_types",
"@types/kbn__rule-data-utils": "link:bazel-bin/packages/kbn-rule-data-utils/npm_module_types",
"@types/kbn__scalability-simulation-generator": "link:bazel-bin/packages/kbn-scalability-simulation-generator/npm_module_types",
"@types/kbn__securitysolution-autocomplete": "link:bazel-bin/packages/kbn-securitysolution-autocomplete/npm_module_types",
Expand Down
6 changes: 6 additions & 0 deletions packages/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ filegroup(
"//packages/kbn-find-used-node-modules:build",
"//packages/kbn-flot-charts:build",
"//packages/kbn-generate:build",
"//packages/kbn-get-repo-files:build",
"//packages/kbn-handlebars:build",
"//packages/kbn-hapi-mocks:build",
"//packages/kbn-i18n-react:build",
Expand All @@ -165,6 +166,8 @@ filegroup(
"//packages/kbn-plugin-generator:build",
"//packages/kbn-plugin-helpers:build",
"//packages/kbn-react-field:build",
"//packages/kbn-repo-source-classifier-cli:build",
"//packages/kbn-repo-source-classifier:build",
"//packages/kbn-rule-data-utils:build",
"//packages/kbn-scalability-simulation-generator:build",
"//packages/kbn-securitysolution-autocomplete:build",
Expand Down Expand Up @@ -361,6 +364,7 @@ filegroup(
"//packages/kbn-field-types:build_types",
"//packages/kbn-find-used-node-modules:build_types",
"//packages/kbn-generate:build_types",
"//packages/kbn-get-repo-files:build_types",
"//packages/kbn-handlebars:build_types",
"//packages/kbn-hapi-mocks:build_types",
"//packages/kbn-i18n-react:build_types",
Expand All @@ -381,6 +385,8 @@ filegroup(
"//packages/kbn-plugin-generator:build_types",
"//packages/kbn-plugin-helpers:build_types",
"//packages/kbn-react-field:build_types",
"//packages/kbn-repo-source-classifier-cli:build_types",
"//packages/kbn-repo-source-classifier:build_types",
"//packages/kbn-rule-data-utils:build_types",
"//packages/kbn-scalability-simulation-generator:build_types",
"//packages/kbn-securitysolution-autocomplete:build_types",
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-eslint-plugin-imports/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ TYPES_DEPS = [
"//packages/kbn-utils:npm_module_types",
"//packages/kbn-dev-utils:npm_module_types", # only required for the tests, which are excluded except on windows
"//packages/kbn-import-resolver:npm_module_types",
"//packages/kbn-repo-source-classifier:npm_module_types",
"@npm//dedent", # only required for the tests, which are excluded except on windows
"@npm//@types/eslint",
"@npm//@types/jest",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

import { ImportResolver } from '@kbn/import-resolver';
import { RepoSourceClassifier } from '@kbn/repo-source-classifier';

const cache = new WeakMap<ImportResolver, RepoSourceClassifier>();

/**
* Gets the instance of RepoSourceClassifier that should be used. We cache these instances
* key'd off of ImportResolver instances because the caches maintained by the RepoSourceClassifer
* should live the same amount of time. Both classes assume that the files on disk are
* relatively "stable" for the lifetime of the object and once the files are assumed
* to have change that a new object will be created and the old version with the old
* caches will be thrown away and garbage collected.
*/
export function getRepoSourceClassifier(resolver: ImportResolver) {
const cached = cache.get(resolver);
if (cached) {
return cached;
}

const classifier = new RepoSourceClassifier(resolver);
cache.set(resolver, classifier);
return classifier;
}
24 changes: 24 additions & 0 deletions packages/kbn-eslint-plugin-imports/src/helpers/source.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

import { Rule } from 'eslint';

/**
* Get the path of the sourcefile being linted
*/
export function getSourcePath(context: Rule.RuleContext) {
const sourceFilename = context.getPhysicalFilename
? context.getPhysicalFilename()
: context.getFilename();

if (!sourceFilename) {
throw new Error('unable to determine sourceFilename for file being linted');
}

return sourceFilename;
}
2 changes: 2 additions & 0 deletions packages/kbn-eslint-plugin-imports/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { NoUnresolvableImportsRule } from './rules/no_unresolvable_imports';
import { UniformImportsRule } from './rules/uniform_imports';
import { ExportsMovedPackagesRule } from './rules/exports_moved_packages';
import { NoUnusedImportsRule } from './rules/no_unused_imports';
import { NoBoundaryCrossingRule } from './rules/no_boundary_crossing';

/**
* Custom ESLint rules, add `'@kbn/eslint-plugin-imports'` to your eslint config to use them
Expand All @@ -21,4 +22,5 @@ export const rules = {
uniform_imports: UniformImportsRule,
exports_moved_packages: ExportsMovedPackagesRule,
no_unused_imports: NoUnusedImportsRule,
no_boundary_crossing: NoBoundaryCrossingRule,
};
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export const ExportsMovedPackagesRule: Rule.RuleModule = {
},
],
docs: {
url: 'https://github.com/elastic/kibana/blob/main/packages/kbn-eslint-plugin-imports/README.md#kbnimportsexports_moved_packages',
url: 'https://github.com/elastic/kibana/blob/main/packages/kbn-eslint-plugin-imports/README.mdx#kbnimportsexports_moved_packages',
},
},

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* 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 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 or the Server
* Side Public License, v 1.
*/

import { RuleTester } from 'eslint';
import { NoBoundaryCrossingRule } from './no_boundary_crossing';
import { ModuleType } from '@kbn/repo-source-classifier';
import dedent from 'dedent';

const make = (from: ModuleType, to: ModuleType, imp = 'import') => ({
filename: `${from}.ts`,
code: dedent`
${imp} '${to}'
`,
});

jest.mock('../get_import_resolver', () => {
return {
getImportResolver() {
return {
resolve(req: string) {
return {
type: 'file',
absolute: {
type: req,
},
};
},
};
},
};
});

jest.mock('../helpers/repo_source_classifier', () => {
return {
getRepoSourceClassifier() {
return {
classify(r: string | { type: string }) {
return {
type: typeof r === 'string' ? (r.endsWith('.ts') ? r.slice(0, -3) : r) : r.type,
};
},
};
},
};
});

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_boundary_crossing', NoBoundaryCrossingRule, {
valid: [
make('common package', 'common package'),
make('server package', 'common package'),
make('browser package', 'common package'),
make('server package', 'server package'),
make('browser package', 'browser package'),
make('tests or mocks', 'common package'),
make('tests or mocks', 'browser package'),
make('tests or mocks', 'server package'),
make('tests or mocks', 'tests or mocks'),
make('browser package', 'server package', 'import type { Foo } from'),
make('server package', 'browser package', 'import type { Foo } from'),
make('common package', 'browser package', 'import type { Foo } from'),
],

invalid: [
{
...make('common package', 'server package'),
errors: [
{
line: 1,
messageId: 'TYPE_MISMATCH',
data: {
importedType: 'server package',
ownType: 'common package',
suggestion: ` ${dedent`
Suggestions:
- Remove the import statement.
- Limit your imports to "common package" or "static" code.
- Covert to a type-only import.
- Reach out to #kibana-operations for help.
`}`,
},
},
],
},
{
...make('server package', 'tests or mocks'),
errors: [
{
line: 1,
messageId: 'TYPE_MISMATCH',
},
],
},
{
...make('browser package', 'tests or mocks'),
errors: [
{
line: 1,
messageId: 'TYPE_MISMATCH',
},
],
},
{
...make('common package', 'server package'),
errors: [
{
line: 1,
messageId: 'TYPE_MISMATCH',
},
],
},
{
...make('common package', 'browser package'),
errors: [
{
line: 1,
messageId: 'TYPE_MISMATCH',
},
],
},
],
});
});
}
Loading

0 comments on commit 3dc2aaf

Please sign in to comment.