[eslint] add rule for validating cross-boundary imports (#137116)

This commit is contained in:
Spencer 2022-07-25 18:49:17 -05:00 committed by GitHub
parent 88bb91f3a0
commit 20f9cf9fd4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
44 changed files with 1672 additions and 64 deletions

View file

@ -0,0 +1,128 @@
load("@npm//@bazel/typescript:index.bzl", "ts_config")
load("@build_bazel_rules_nodejs//:index.bzl", "js_library")
load("//src/dev/bazel:index.bzl", "jsts_transpiler", "pkg_npm", "pkg_npm_types", "ts_project")
PKG_DIRNAME = "kbn-repo-source-classifier-cli"
PKG_REQUIRE_NAME = "@kbn/repo-source-classifier-cli"
SOURCE_FILES = glob(
[
"src/**/*.ts",
],
exclude = [
"**/*.test.*",
"**/*.stories.*",
],
)
SRCS = SOURCE_FILES
filegroup(
name = "srcs",
srcs = SRCS,
)
NPM_MODULE_EXTRA_FILES = [
"package.json",
]
# In this array place runtime dependencies, including other packages and NPM packages
# which must be available for this code to run.
#
# To reference other packages use:
# "//repo/relative/path/to/package"
# eg. "//packages/kbn-utils"
#
# To reference a NPM package use:
# "@npm//name-of-package"
# eg. "@npm//lodash"
RUNTIME_DEPS = [
"//packages/kbn-dev-cli-runner",
"//packages/kbn-dev-cli-errors",
"//packages/kbn-import-resolver",
"//packages/kbn-repo-source-classifier",
"//packages/kbn-get-repo-files",
"//packages/kbn-utils",
]
# In this array place dependencies necessary to build the types, which will include the
# :npm_module_types target of other packages and packages from NPM, including @types/*
# packages.
#
# To reference the types for another package use:
# "//repo/relative/path/to/package:npm_module_types"
# eg. "//packages/kbn-utils:npm_module_types"
#
# References to NPM packages work the same as RUNTIME_DEPS
TYPES_DEPS = [
"@npm//@types/node",
"@npm//@types/jest",
"//packages/kbn-dev-cli-runner:npm_module_types",
"//packages/kbn-dev-cli-errors:npm_module_types",
"//packages/kbn-import-resolver:npm_module_types",
"//packages/kbn-repo-source-classifier:npm_module_types",
"//packages/kbn-get-repo-files:npm_module_types",
"//packages/kbn-utils:npm_module_types",
]
jsts_transpiler(
name = "target_node",
srcs = SRCS,
build_pkg_name = package_name(),
)
ts_config(
name = "tsconfig",
src = "tsconfig.json",
deps = [
"//:tsconfig.base.json",
"//:tsconfig.bazel.json",
],
)
ts_project(
name = "tsc_types",
args = ['--pretty'],
srcs = SRCS,
deps = TYPES_DEPS,
declaration = True,
declaration_map = True,
emit_declaration_only = True,
out_dir = "target_types",
root_dir = "src",
tsconfig = ":tsconfig",
)
js_library(
name = PKG_DIRNAME,
srcs = NPM_MODULE_EXTRA_FILES,
deps = RUNTIME_DEPS + [":target_node"],
package_name = PKG_REQUIRE_NAME,
visibility = ["//visibility:public"],
)
pkg_npm(
name = "npm_module",
deps = [":" + PKG_DIRNAME],
)
filegroup(
name = "build",
srcs = [":npm_module"],
visibility = ["//visibility:public"],
)
pkg_npm_types(
name = "npm_module_types",
srcs = SRCS,
deps = [":tsc_types"],
package_name = PKG_REQUIRE_NAME,
tsconfig = ":tsconfig",
visibility = ["//visibility:public"],
)
filegroup(
name = "build_types",
srcs = [":npm_module_types"],
visibility = ["//visibility:public"],
)

View file

@ -0,0 +1,3 @@
# @kbn/repo-source-classifier-cli
CLI for debugging the repo source classifier, run to see how the classifier identifies your files. Use `node scripts/classify_source --help` for more info.

View file

@ -0,0 +1,13 @@
/*
* 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.
*/
module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-repo-source-classifier-cli'],
};

View file

@ -0,0 +1,10 @@
{
"name": "@kbn/repo-source-classifier-cli",
"private": true,
"version": "1.0.0",
"main": "./target_node/index.js",
"license": "SSPL-1.0 OR Elastic License 2.0",
"kibana": {
"devOnly": true
}
}

View file

@ -0,0 +1,90 @@
/*
* 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 Path from 'path';
import { RepoSourceClassifier } from '@kbn/repo-source-classifier';
import { ImportResolver } from '@kbn/import-resolver';
import { REPO_ROOT } from '@kbn/utils';
import { getRepoFiles } from '@kbn/get-repo-files';
import { run } from '@kbn/dev-cli-runner';
import { createFlagError } from '@kbn/dev-cli-errors';
import { TypeTree } from './type_tree';
run(
async ({ flags }) => {
const resolver = ImportResolver.create(REPO_ROOT);
const classifier = new RepoSourceClassifier(resolver);
const include = flags._.length ? flags._ : [process.cwd()];
let exclude;
if (flags.exclude) {
if (Array.isArray(flags.exclude)) {
exclude = flags.exclude;
} else if (typeof flags.exclude === 'string') {
exclude = [flags.exclude];
} else {
throw createFlagError('expected --exclude value to be a string');
}
}
const typeFlags = String(flags.types)
.split(',')
.map((f) => f.trim())
.filter(Boolean);
const includeTypes: string[] = [];
const excludeTypes: string[] = [];
for (const type of typeFlags) {
if (type.startsWith('!')) {
excludeTypes.push(type.slice(1));
} else {
includeTypes.push(type);
}
}
const tree = new TypeTree();
const cwd = process.cwd();
for (const { abs } of await getRepoFiles(include, exclude)) {
const { type } = classifier.classify(abs);
if ((includeTypes.length && !includeTypes.includes(type)) || excludeTypes.includes(type)) {
continue;
}
tree.add(type, Path.relative(cwd, abs));
}
if (!!flags.flat) {
for (const file of tree.toList()) {
process.stdout.write(`${file}\n`);
}
} else {
process.stdout.write(tree.print({ expand: !!flags.expand }));
}
},
{
description: 'run the repo-source-classifier on the source files and produce a report',
usage: `node scripts/classify_source <...paths>`,
flags: {
string: ['exclude', 'types'],
boolean: ['expand', 'flat'],
help: `
<...paths> include paths to select specific files which should be reported
by default all files in the cwd are classified. Can be specified
multiple times
--exclude exclude specific paths from the classification. Can be specified
multiple times
--types limit the types reported to the types in this comma separated list
to exclude a type prefix it with !
--expand prevent collapsing entries that are of the same type
--flat just print file names
`,
},
}
);

View file

@ -0,0 +1,104 @@
/*
* 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 { ModuleType } from '@kbn/repo-source-classifier';
import normalizePath from 'normalize-path';
type RecursiveTypes = Map<string, ModuleType | RecursiveTypes>;
interface PrintOpts {
expand: boolean;
}
export class TypeTree {
private dirs = new Map<string, TypeTree>();
private files = new Map<string, ModuleType>();
constructor(public readonly path: string[] = []) {}
add(type: ModuleType, rel: string) {
const segs = normalizePath(rel).split('/').filter(Boolean);
let node: TypeTree = this;
const path = [];
for (const dirSeg of segs.slice(0, -1)) {
path.push(dirSeg);
const existing = node.dirs.get(dirSeg);
if (existing) {
node = existing;
} else {
const newDir = new TypeTree([...node.path, dirSeg]);
node.dirs.set(dirSeg, newDir);
node = newDir;
}
}
const filename = segs.at(-1);
if (!filename) {
throw new Error(`invalid rel path [${rel}]`);
}
node.files.set(filename, type);
}
flatten(options: PrintOpts): ModuleType | RecursiveTypes {
const entries: RecursiveTypes = new Map([
...[...this.dirs].map(([name, dir]) => [name, dir.flatten(options)] as const),
...this.files,
]);
if (!options.expand) {
const types = new Set(entries.values());
const [firstType] = types;
if (types.size === 1 && typeof firstType === 'string') {
return firstType;
}
}
return entries;
}
print(options: PrintOpts) {
const tree = this.flatten(options);
if (typeof tree === 'string') {
return `${this.path.join('/')}: ${tree}`;
}
const lines: string[] = [];
const print = (prefix: string, types: RecursiveTypes) => {
for (const [name, childTypes] of types) {
if (typeof childTypes === 'string') {
lines.push(`${prefix}${name}: ${childTypes}`);
} else {
lines.push(`${prefix}${name}/`);
print(` ${prefix}`, childTypes);
}
}
};
print('', tree);
return lines.join('\n') + '\n';
}
toList() {
const files: string[] = [];
const getFiles = (tree: TypeTree) => {
for (const dir of tree.dirs.values()) {
getFiles(dir);
}
for (const filename of tree.files.keys()) {
files.push([...tree.path, filename].join('/'));
}
};
getFiles(this);
return files.sort((a, b) => a.localeCompare(b));
}
}

View file

@ -0,0 +1,18 @@
{
"extends": "../../tsconfig.bazel.json",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"emitDeclarationOnly": true,
"outDir": "target_types",
"rootDir": "src",
"stripInternal": false,
"types": [
"jest",
"node"
]
},
"include": [
"src/**/*"
]
}