[packages] Move @kbn/interpreter to Bazel (#101089)

Co-authored-by: Tiago Costa <tiagoffcc@hotmail.com>
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Jonathan Budzenski 2021-06-22 09:59:20 -05:00 committed by GitHub
parent 46f43784b0
commit 11e68fda87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 191 additions and 1290 deletions

View file

@ -30,8 +30,6 @@ snapshots.js
# package overrides
/packages/elastic-eslint-config-kibana
/packages/kbn-interpreter/src/common/lib/grammar.js
/packages/kbn-tinymath/src/grammar.js
/packages/kbn-plugin-generator/template
/packages/kbn-pm/dist
/packages/kbn-test/src/functional_test_runner/__tests__/fixtures/

View file

@ -80,6 +80,7 @@ yarn kbn watch-bazel
- @kbn/eslint-plugin-eslint
- @kbn/expect
- @kbn/i18n
- @kbn/interpreter
- @kbn/io-ts-utils
- @kbn/legacy-logging
- @kbn/logging

View file

@ -133,7 +133,7 @@
"@kbn/crypto": "link:bazel-bin/packages/kbn-crypto",
"@kbn/mapbox-gl": "link:bazel-bin/packages/kbn-mapbox-gl",
"@kbn/i18n": "link:bazel-bin/packages/kbn-i18n",
"@kbn/interpreter": "link:packages/kbn-interpreter",
"@kbn/interpreter": "link:bazel-bin/packages/kbn-interpreter",
"@kbn/io-ts-utils": "link:bazel-bin/packages/kbn-io-ts-utils",
"@kbn/legacy-logging": "link:bazel-bin/packages/kbn-legacy-logging",
"@kbn/logging": "link:bazel-bin/packages/kbn-logging",

View file

@ -23,6 +23,7 @@ filegroup(
"//packages/kbn-eslint-plugin-eslint:build",
"//packages/kbn-expect:build",
"//packages/kbn-i18n:build",
"//packages/kbn-interpreter:build",
"//packages/kbn-io-ts-utils:build",
"//packages/kbn-legacy-logging:build",
"//packages/kbn-logging:build",

View file

@ -1,9 +0,0 @@
{
"presets": ["@kbn/babel-preset/webpack_preset"],
"plugins": [
"@babel/plugin-transform-modules-commonjs",
["@babel/plugin-transform-runtime", {
"regenerator": true
}]
]
}

View file

@ -1,3 +0,0 @@
src
tasks
.babelrc

View file

@ -0,0 +1,99 @@
load("@npm//@bazel/typescript:index.bzl", "ts_config", "ts_project")
load("@npm//pegjs:index.bzl", "pegjs")
load("@build_bazel_rules_nodejs//:index.bzl", "js_library", "pkg_npm")
PKG_BASE_NAME = "kbn-interpreter"
PKG_REQUIRE_NAME = "@kbn/interpreter"
SOURCE_FILES = glob(
[
"src/**/*",
]
)
TYPE_FILES = []
SRCS = SOURCE_FILES + TYPE_FILES
filegroup(
name = "srcs",
srcs = SRCS,
)
NPM_MODULE_EXTRA_FILES = [
"common/package.json",
"package.json",
]
SRC_DEPS = [
"@npm//lodash",
]
TYPES_DEPS = [
"@npm//@types/jest",
"@npm//@types/lodash",
"@npm//@types/node",
]
DEPS = SRC_DEPS + TYPES_DEPS
pegjs(
name = "grammar",
data = [
":grammar/grammar.pegjs"
],
output_dir = True,
args = [
"--allowed-start-rules",
"expression,argument",
"-o",
"$(@D)/index.js",
"./%s/grammar/grammar.pegjs" % package_name()
],
)
ts_config(
name = "tsconfig",
src = "tsconfig.json",
deps = [
"//:tsconfig.base.json",
],
)
ts_project(
name = "tsc",
args = ['--pretty'],
srcs = SRCS,
deps = DEPS,
allow_js = True,
declaration = True,
declaration_map = True,
incremental = True,
out_dir = "target",
source_map = True,
root_dir = "src",
tsconfig = ":tsconfig",
)
js_library(
name = PKG_BASE_NAME,
srcs = NPM_MODULE_EXTRA_FILES + [":grammar"],
deps = DEPS + [":tsc"],
package_name = PKG_REQUIRE_NAME,
visibility = ["//visibility:public"],
)
pkg_npm(
name = "npm_module",
deps = [
":%s" % PKG_BASE_NAME,
]
)
filegroup(
name = "build",
srcs = [
":npm_module",
],
visibility = ["//visibility:public"],
)

View file

@ -1,6 +1,5 @@
{
"private": true,
"main": "../target/common/index.js",
"types": "../target/common/index.d.ts",
"jsnext:main": "../src/common/index.js"
"types": "../target/common/index.d.ts"
}

View file

@ -2,11 +2,5 @@
"name": "@kbn/interpreter",
"private": "true",
"version": "1.0.0",
"license": "SSPL-1.0 OR Elastic License 2.0",
"scripts": {
"interpreter:peg": "../../node_modules/.bin/pegjs src/common/lib/grammar.peg",
"build": "node scripts/build",
"kbn:bootstrap": "node scripts/build --dev",
"kbn:watch": "node scripts/build --dev --watch"
}
"license": "SSPL-1.0 OR Elastic License 2.0"
}

View file

@ -1,9 +0,0 @@
/*
* 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.
*/
require('../tasks/build/cli');

View file

@ -1,12 +0,0 @@
/*
* 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.
*/
export { Registry } from './lib/registry';
export { fromExpression, toExpression, Ast, ExpressionFunctionAST } from './lib/ast';
export { getType } from './lib/get_type';

View file

@ -6,11 +6,19 @@
* Side Public License, v 1.
*/
export { fromExpression, toExpression, safeElementFromExpression } from './lib/ast';
export {
fromExpression,
toExpression,
safeElementFromExpression,
Ast,
ExpressionFunctionAST,
} from './lib/ast';
export { Fn } from './lib/fn';
export { getType } from './lib/get_type';
export { castProvider } from './lib/cast';
export { parse } from './lib/grammar';
// @ts-expect-error
// @internal
export { parse } from '../../grammar';
export { getByAlias } from './lib/get_by_alias';
export { Registry } from './lib/registry';
export { addRegistries, register, registryFactory } from './registries';

View file

@ -1,25 +0,0 @@
/*
* 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.
*/
export type ExpressionArgAST = string | boolean | number | Ast;
export interface ExpressionFunctionAST {
type: 'function';
function: string;
arguments: {
[key: string]: ExpressionArgAST[];
};
}
export interface Ast {
type: 'expression';
chain: ExpressionFunctionAST[];
}
export declare function fromExpression(expression: string): Ast;
export declare function toExpression(astObj: Ast, type?: string): string;

View file

@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
import { fromExpression } from './ast';
import { fromExpression } from '@kbn/interpreter/target/common/lib/ast';
import { getType } from './get_type';
describe('ast fromExpression', () => {

View file

@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
import { toExpression } from './ast';
import { toExpression } from '@kbn/interpreter/common';
describe('ast toExpression', () => {
describe('single expression', () => {

View file

@ -7,12 +7,35 @@
*/
import { getType } from './get_type';
import { parse } from './grammar';
// @ts-expect-error
import { parse } from '../../../grammar';
function getArgumentString(arg, argKey, level = 0) {
export type ExpressionArgAST = string | boolean | number | Ast;
export interface ExpressionFunctionAST {
type: 'function';
function: string;
arguments: {
[key: string]: ExpressionArgAST[];
};
}
export interface Ast {
/** @internal */
function: any;
/** @internal */
arguments: any;
type: 'expression';
chain: ExpressionFunctionAST[];
/** @internal */
replace(regExp: RegExp, s: string): string;
}
function getArgumentString(arg: Ast, argKey: string | undefined, level = 0) {
const type = getType(arg);
function maybeArgKey(argKey, argString) {
// eslint-disable-next-line @typescript-eslint/no-shadow
function maybeArgKey(argKey: string | null | undefined, argString: string) {
return argKey == null || argKey === '_' ? argString : `${argKey}=${argString}`;
}
@ -36,7 +59,7 @@ function getArgumentString(arg, argKey, level = 0) {
throw new Error(`Invalid argument type in AST: ${type}`);
}
function getExpressionArgs(block, level = 0) {
function getExpressionArgs(block: Ast, level = 0) {
const args = block.arguments;
const hasValidArgs = typeof args === 'object' && args != null && !Array.isArray(args);
@ -45,7 +68,7 @@ function getExpressionArgs(block, level = 0) {
const argKeys = Object.keys(args);
const MAX_LINE_LENGTH = 80; // length before wrapping arguments
return argKeys.map((argKey) =>
args[argKey].reduce((acc, arg) => {
args[argKey].reduce((acc: any, arg: any) => {
const argString = getArgumentString(arg, argKey, level);
const lineLength = acc.split('\n').pop().length;
@ -63,12 +86,12 @@ function getExpressionArgs(block, level = 0) {
);
}
function fnWithArgs(fnName, args) {
function fnWithArgs(fnName: any, args: any[]) {
if (!args || args.length === 0) return fnName;
return `${fnName} ${args.join(' ')}`;
}
function getExpression(chain, level = 0) {
function getExpression(chain: any[], level = 0) {
if (!chain) throw new Error('Expressions must contain a chain');
// break new functions onto new lines if we're not in a nested/sub-expression
@ -90,7 +113,7 @@ function getExpression(chain, level = 0) {
.join(separator);
}
export function fromExpression(expression, type = 'expression') {
export function fromExpression(expression: string, type = 'expression'): Ast {
try {
return parse(String(expression), { startRule: type });
} catch (e) {
@ -99,7 +122,7 @@ export function fromExpression(expression, type = 'expression') {
}
// TODO: OMG This is so bad, we need to talk about the right way to handle bad expressions since some are element based and others not
export function safeElementFromExpression(expression) {
export function safeElementFromExpression(expression: string) {
try {
return fromExpression(expression);
} catch (e) {
@ -116,8 +139,11 @@ Thanks for understanding,
}
// TODO: Respect the user's existing formatting
export function toExpression(astObj, type = 'expression') {
if (type === 'argument') return getArgumentString(astObj);
export function toExpression(astObj: Ast, type = 'expression'): string {
if (type === 'argument') {
// @ts-ignore
return getArgumentString(astObj);
}
const validType = ['expression', 'function'].includes(getType(astObj));
if (!validType) throw new Error('Expression must be an expression or argument function');

View file

@ -1,9 +0,0 @@
/*
* 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.
*/
export declare function getType(node: any): string;

View file

@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
export function getType(node) {
export function getType(node: any): string {
if (node == null) return 'null';
if (typeof node === 'object') {
if (!node.type) throw new Error('Objects must have a type property');

File diff suppressed because it is too large Load diff

View file

@ -1,25 +0,0 @@
/*
* 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.
*/
export class Registry<ItemSpec, Item> {
constructor(prop?: string);
public wrapper(obj: ItemSpec): Item;
public register(fn: () => ItemSpec): void;
public toJS(): { [key: string]: any };
public toArray(): Item[];
public get(name: string): Item;
public getProp(): string;
public reset(): void;
}

View file

@ -8,49 +8,59 @@
import { clone } from 'lodash';
export class Registry {
export class Registry<ItemSpec, Item> {
private readonly _prop: string;
// eslint-disable-next-line @typescript-eslint/ban-types
private _indexed: Object;
constructor(prop = 'name') {
if (typeof prop !== 'string') throw new Error('Registry property name must be a string');
this._prop = prop;
this._indexed = new Object();
}
wrapper(obj) {
wrapper(obj: ItemSpec): Item {
// @ts-ignore
return obj;
}
register(fn) {
register(fn: () => ItemSpec): void {
const obj = typeof fn === 'function' ? fn() : fn;
// @ts-ignore
if (typeof obj !== 'object' || !obj[this._prop]) {
throw new Error(`Registered functions must return an object with a ${this._prop} property`);
}
// @ts-ignore
this._indexed[obj[this._prop].toLowerCase()] = this.wrapper(obj);
}
toJS() {
toJS(): { [key: string]: any } {
return Object.keys(this._indexed).reduce((acc, key) => {
// @ts-ignore
acc[key] = this.get(key);
return acc;
}, {});
}
toArray() {
toArray(): Item[] {
return Object.keys(this._indexed).map((key) => this.get(key));
}
get(name) {
get(name: string): Item {
// @ts-ignore
if (name === undefined) return null;
const lowerCaseName = name.toLowerCase();
// @ts-ignore
return this._indexed[lowerCaseName] ? clone(this._indexed[lowerCaseName]) : null;
}
getProp() {
getProp(): string {
return this._prop;
}
reset() {
reset(): void {
this._indexed = new Object();
}
}

View file

@ -1,3 +0,0 @@
/* eslint-disable */
import util from 'util';
console.log(util.format('hello world'));

View file

@ -1,82 +0,0 @@
/*
* 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.
*/
const { relative } = require('path');
const getopts = require('getopts');
const del = require('del');
const supportsColor = require('supports-color');
const { ToolingLog, withProcRunner, pickLevelFromFlags } = require('@kbn/dev-utils');
const { ROOT_DIR, BUILD_DIR } = require('./paths');
const unknownFlags = [];
const flags = getopts(process.argv, {
boolean: ['watch', 'dev', 'help', 'debug'],
unknown(name) {
unknownFlags.push(name);
},
});
const log = new ToolingLog({
level: pickLevelFromFlags(flags),
writeTo: process.stdout,
});
if (unknownFlags.length) {
log.error(`Unknown flag(s): ${unknownFlags.join(', ')}`);
flags.help = true;
process.exitCode = 1;
}
if (flags.help) {
log.info(`
Simple build tool for @kbn/interpreter package
--dev Build for development, include source maps
--watch Run in watch mode
--debug Turn on debug logging
`);
process.exit();
}
withProcRunner(log, async (proc) => {
log.info('Deleting old output');
await del(BUILD_DIR);
const cwd = ROOT_DIR;
const env = { ...process.env };
if (supportsColor.stdout) {
env.FORCE_COLOR = 'true';
}
log.info(`Starting babel ${flags.watch ? ' in watch mode' : ''}`);
await Promise.all([
proc.run('babel ', {
cmd: 'babel',
args: [
'src',
'--ignore',
`*.test.js`,
'--out-dir',
relative(cwd, BUILD_DIR),
'--copy-files',
...(flags.dev ? ['--source-maps', 'inline'] : []),
...(flags.watch ? ['--watch'] : ['--quiet']),
],
wait: true,
env,
cwd,
}),
]);
log.success('Complete');
}).catch((error) => {
log.error(error);
process.exit(1);
});

View file

@ -1,15 +0,0 @@
/*
* 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.
*/
const { resolve } = require('path');
exports.ROOT_DIR = resolve(__dirname, '../../');
exports.SOURCE_DIR = resolve(exports.ROOT_DIR, 'src');
exports.BUILD_DIR = resolve(exports.ROOT_DIR, 'target');
exports.BABEL_PRESET_PATH = require.resolve('@kbn/babel-preset/webpack_preset');

View file

@ -1,7 +1,21 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"tsBuildInfoFile": "../../build/tsbuildinfo/packages/kbn-interpreter"
"allowJs": true,
"incremental": true,
"outDir": "./target",
"declaration": true,
"declarationMap": true,
"rootDir": "src",
"sourceMap": true,
"sourceRoot": "../../../../packages/kbn-interpreter/src",
"stripInternal": true,
"types": [
"jest",
"node"
]
},
"include": ["index.d.ts", "src/**/*.d.ts"]
"include": [
"src/**/*",
]
}

View file

@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
import { Ast } from '@kbn/interpreter/common';
import { ExpressionAstExpression } from '../../../../expressions/common/ast';
/**
* Information about a series in a chart used to determine its color.
@ -78,7 +78,7 @@ export interface PaletteDefinition<T = unknown> {
* This function should be used to pass the palette to the expression function applying color and other styles
* @param state The internal state of the palette
*/
toExpression: (state?: T) => Ast;
toExpression: (state?: T) => ExpressionAstExpression;
/**
* Color a series according to the internal rules of the palette.
* @param series The current series along with its ancestors.

View file

@ -30,7 +30,6 @@
"@kbn/test": "link:../packages/kbn-test"
},
"dependencies": {
"@kbn/interpreter": "link:../packages/kbn-interpreter",
"@kbn/ui-framework": "link:../packages/kbn-ui-framework"
}
}

View file

@ -5,7 +5,6 @@
* 2.0.
*/
// @ts-expect-error untyped Elastic library
import { castProvider } from '@kbn/interpreter/common';
import { ExpressionFunctionDefinition } from 'src/plugins/expressions/public';
import { getFunctionHelp, getFunctionErrors } from '../../i18n';

View file

@ -5,7 +5,6 @@
* 2.0.
*/
// @ts-expect-error untyped module
import { addRegistries, register } from '@kbn/interpreter/common';
// @ts-expect-error untyped local
import { elementsRegistry } from './lib/elements_registry';

View file

@ -6,7 +6,6 @@
*/
import { get, omit } from 'lodash';
// @ts-expect-error untyped local
import { safeElementFromExpression, fromExpression } from '@kbn/interpreter/common';
import { append } from '../../lib/modify_path';
import { getAssets } from './assets';

View file

@ -2668,7 +2668,7 @@
version "0.0.0"
uid ""
"@kbn/interpreter@link:packages/kbn-interpreter":
"@kbn/interpreter@link:bazel-bin/packages/kbn-interpreter":
version "0.0.0"
uid ""