Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 26 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2d7663f
[packages] Move @kbn/interpreter to Bazel
jbudz Jun 1, 2021
fd5b266
remove dep
jbudz Jun 1, 2021
50a94ba
rm jsnext:main
jbudz Jun 1, 2021
c84a520
Merge branch 'master' into bazel/kbn-interpreter
jbudz Jun 4, 2021
559aee9
wip
jbudz Jun 4, 2021
6a09dd1
Merge branch 'master' into bazel/kbn-interpreter
jbudz Jun 4, 2021
ead7202
wip
jbudz Jun 4, 2021
ba84fe6
Merge branch 'master' into bazel/kbn-interpreter
jbudz Jun 8, 2021
2ff1c09
skip lint error
jbudz Jun 8, 2021
429984c
Merge branch 'master' into bazel/kbn-interpreter
jbudz Jun 9, 2021
40d7093
chore(NA): fix peggy task
mistic Jun 8, 2021
6b922cf
chore(NA): move some files into typescript
mistic Jun 9, 2021
ff2423c
Merge branch 'master' into bazel/kbn-interpreter
kibanamachine Jun 9, 2021
25fb1da
chore(NA): remove peggy
mistic Jun 9, 2021
efdea8e
chore(NA): correct arguments for building kbn interpreter
mistic Jun 10, 2021
d8906bd
Merge branch 'master' into bazel/kbn-interpreter
jbudz Jun 10, 2021
2eba61e
Merge branch 'master' into bazel/kbn-interpreter
jbudz Jun 14, 2021
5f94c44
fix types
jbudz Jun 14, 2021
38e516e
fix import
jbudz Jun 14, 2021
d67f320
fix test
jbudz Jun 14, 2021
851098b
Merge branch 'master' into bazel/kbn-interpreter
jbudz Jun 15, 2021
d57f376
Merge branch 'master' into bazel/kbn-interpreter
jbudz Jun 17, 2021
04b4927
chore(NA): fix last unit test
mistic Jun 17, 2021
d1209fe
chore(NA): fix types for kbn/interpreter migration
mistic Jun 17, 2021
f558973
Merge branch 'master' into bazel/kbn-interpreter
kibanamachine Jun 21, 2021
4a69ae1
Merge branch 'master' into bazel/kbn-interpreter
kibanamachine Jun 22, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
1 change: 1 addition & 0 deletions docs/developer/getting-started/monorepo-packages.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 0 additions & 9 deletions packages/kbn-interpreter/.babelrc

This file was deleted.

3 changes: 0 additions & 3 deletions packages/kbn-interpreter/.npmignore

This file was deleted.

99 changes: 99 additions & 0 deletions packages/kbn-interpreter/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
3 changes: 1 addition & 2 deletions packages/kbn-interpreter/common/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
8 changes: 1 addition & 7 deletions packages/kbn-interpreter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
9 changes: 0 additions & 9 deletions packages/kbn-interpreter/scripts/build.js

This file was deleted.

12 changes: 0 additions & 12 deletions packages/kbn-interpreter/src/common/index.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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';
25 changes: 0 additions & 25 deletions packages/kbn-interpreter/src/common/lib/ast.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}

Expand All @@ -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);

Expand All @@ -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;

Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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');
Expand Down
9 changes: 0 additions & 9 deletions packages/kbn-interpreter/src/common/lib/get_type.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Loading