Skip to content

Commit

Permalink
[ts] enable sourcemaps in summarized types of @kbn/crypto (elastic#12…
Browse files Browse the repository at this point in the history
…6410)

* [ts] enable sourcemaps in summarized types of @kbn/crypto

* update snapshots

* remove unnecessary exports of @kbn/type-summarizer package

* remove tsc from the build process

* use `@kbn/type-summarizer` to summarize its own types

* add tests for interface and function

* switch to export type where necessary

* ignore __tmp__ in global jest preset

* ignore __tmp__ globally

* remove `@kbn/crypto` types path
  • Loading branch information
Spencer authored and lucasfcosta committed Mar 8, 2022
1 parent b312193 commit ef3eddb
Show file tree
Hide file tree
Showing 62 changed files with 2,783 additions and 276 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
**/*.js.snap
__tmp__
/.es
/.chromium
/build
Expand Down
52 changes: 30 additions & 22 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const DEV_PACKAGES = [
'kbn-storybook',
'kbn-telemetry-tools',
'kbn-test',
'kbn-type-summarizer',
];

/** Directories (at any depth) which include dev-only code. */
Expand Down Expand Up @@ -1632,28 +1633,6 @@ module.exports = {
},
},

/**
* Prettier disables all conflicting rules, listing as last override so it takes precedence
*/
{
files: ['**/*'],
rules: {
...require('eslint-config-prettier').rules,
...require('eslint-config-prettier/react').rules,
...require('eslint-config-prettier/@typescript-eslint').rules,
},
},
/**
* Enterprise Search Prettier override
* Lints unnecessary backticks - @see https://github.com/prettier/eslint-config-prettier/blob/main/README.md#forbid-unnecessary-backticks
*/
{
files: ['x-pack/plugins/enterprise_search/**/*.{ts,tsx}'],
rules: {
quotes: ['error', 'single', { avoidEscape: true, allowTemplateLiterals: false }],
},
},

/**
* Platform Security Team overrides
*/
Expand Down Expand Up @@ -1768,5 +1747,34 @@ module.exports = {
'@kbn/eslint/no_export_all': 'error',
},
},

{
files: ['packages/kbn-type-summarizer/**/*.ts'],
rules: {
'no-bitwise': 'off',
},
},

/**
* Prettier disables all conflicting rules, listing as last override so it takes precedence
*/
{
files: ['**/*'],
rules: {
...require('eslint-config-prettier').rules,
...require('eslint-config-prettier/react').rules,
...require('eslint-config-prettier/@typescript-eslint').rules,
},
},
/**
* Enterprise Search Prettier override
* Lints unnecessary backticks - @see https://github.com/prettier/eslint-config-prettier/blob/main/README.md#forbid-unnecessary-backticks
*/
{
files: ['x-pack/plugins/enterprise_search/**/*.{ts,tsx}'],
rules: {
quotes: ['error', 'single', { avoidEscape: true, allowTemplateLiterals: false }],
},
},
],
};
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ target
*.iml
*.log
types.eslint.config.js
__tmp__

# Ignore example plugin builds
/examples/*/build
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@
"@kbn/test": "link:bazel-bin/packages/kbn-test",
"@kbn/test-jest-helpers": "link:bazel-bin/packages/kbn-test-jest-helpers",
"@kbn/test-subj-selector": "link:bazel-bin/packages/kbn-test-subj-selector",
"@kbn/type-summarizer": "link:bazel-bin/packages/kbn-type-summarizer",
"@loaders.gl/polyfills": "^2.3.5",
"@mapbox/vector-tile": "1.3.1",
"@microsoft/api-documenter": "7.13.68",
Expand Down Expand Up @@ -869,6 +870,7 @@
"simple-git": "1.116.0",
"sinon": "^7.4.2",
"sort-package-json": "^1.53.1",
"source-map": "^0.7.3",
"spawn-sync": "^1.0.15",
"string-replace-loader": "^2.2.0",
"strong-log-transformer": "^2.1.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ filegroup(
"//packages/kbn-test-subj-selector:build",
"//packages/kbn-timelion-grammar:build",
"//packages/kbn-tinymath:build",
"//packages/kbn-type-summarizer:build",
"//packages/kbn-typed-react-router-config:build",
"//packages/kbn-ui-framework:build",
"//packages/kbn-ui-shared-deps-npm:build",
Expand Down Expand Up @@ -132,6 +133,7 @@ filegroup(
"//packages/kbn-telemetry-tools:build_types",
"//packages/kbn-test:build_types",
"//packages/kbn-test-jest-helpers:build_types",
"//packages/kbn-type-summarizer:build_types",
"//packages/kbn-typed-react-router-config:build_types",
"//packages/kbn-ui-shared-deps-npm:build_types",
"//packages/kbn-ui-shared-deps-src:build_types",
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-crypto/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ ts_project(
srcs = SRCS,
deps = TYPES_DEPS,
declaration = True,
declaration_map = True,
emit_declaration_only = True,
out_dir = "target_types",
root_dir = "src",
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-crypto/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "../../tsconfig.bazel.json",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"emitDeclarationOnly": true,
"outDir": "./target_types",
"rootDir": "src",
Expand Down
4 changes: 4 additions & 0 deletions packages/kbn-test/jest-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
// For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html

/** @typedef {import("@jest/types").Config.InitialOptions} JestConfig */
/** @type {JestConfig} */
module.exports = {
// The directory where Jest should output its coverage files
coverageDirectory: '<rootDir>/target/kibana-coverage/jest',
Expand Down Expand Up @@ -128,4 +130,6 @@ module.exports = {

// A custom resolver to preserve symlinks by default
resolver: '<rootDir>/node_modules/@kbn/test/target_node/jest/setup/preserve_symlinks_resolver.js',

watchPathIgnorePatterns: ['.*/__tmp__/.*'],
};
136 changes: 136 additions & 0 deletions packages/kbn-type-summarizer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
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")
load("@build_bazel_rules_nodejs//internal/node:node.bzl", "nodejs_binary")
load("@build_bazel_rules_nodejs//:index.bzl", "directory_file_path")

PKG_BASE_NAME = "kbn-type-summarizer"
PKG_REQUIRE_NAME = "@kbn/type-summarizer"

SOURCE_FILES = glob(
[
"src/**/*.ts",
],
exclude = [
"**/*.test.*"
],
)

SRCS = SOURCE_FILES

filegroup(
name = "srcs",
srcs = SRCS,
)

NPM_MODULE_EXTRA_FILES = [
"package.json",
"README.md",
]

RUNTIME_DEPS = [
"@npm//@babel/runtime",
"@npm//@microsoft/api-extractor",
"@npm//source-map-support",
"@npm//chalk",
"@npm//getopts",
"@npm//is-path-inside",
"@npm//normalize-path",
"@npm//source-map",
"@npm//tslib",
]

TYPES_DEPS = [
"@npm//@microsoft/api-extractor",
"@npm//@types/jest",
"@npm//@types/node",
"@npm//@types/normalize-path",
"@npm//getopts",
"@npm//is-path-inside",
"@npm//normalize-path",
"@npm//source-map",
"@npm//tslib",
]

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",
)

jsts_transpiler(
name = "target_node",
srcs = SRCS,
build_pkg_name = package_name(),
)

js_library(
name = PKG_BASE_NAME,
srcs = NPM_MODULE_EXTRA_FILES,
deps = RUNTIME_DEPS + [":target_node"],
package_name = PKG_REQUIRE_NAME,
visibility = ["//visibility:public"],
)

directory_file_path(
name = "bazel-cli-path",
directory = ":target_node",
path = "bazel_cli.js",
)

nodejs_binary(
name = "bazel-cli",
data = [
":%s" % PKG_BASE_NAME
],
entry_point = ":bazel-cli-path",
visibility = ["//visibility:public"],
)

pkg_npm(
name = "npm_module",
deps = [
":%s" % PKG_BASE_NAME,
],
)

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"],
)
17 changes: 17 additions & 0 deletions packages/kbn-type-summarizer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# @kbn/type-summarizer

Consume the .d.ts files for a package, produced by `tsc`, and generate a single `.d.ts` file of the public types along with a source map that points back to the original source.

## You mean like API Extractor?

Yeah, except with source map support and without all the legacy features and other features we disable to generate our current type summaries.

I first attempted to implement this in api-extractor but I (@spalger) hit a wall when dealing with the `Span` class. This class handles all the text output which ends up becoming source code, and I wasn't able to find a way to associate specific spans with source locations without getting 12 headaches. Instead I decided to try implementing this from scratch, reducing our reliance on the api-extractor project and putting us in control of how we generate type summaries.

This package is missing some critical features for wider adoption, but rather than build the entire product in a branch I decided to implement support for a small number of TS features and put this to use in the `@kbn/crypto` module ASAP.

The plan is to expand to other packages in the Kibana repo, adding support for language features as we go.

## Something isn't working and I'm blocked!

If there's a problem with the implmentation blocking another team at any point we can move the package back to using api-extractor by removing the package from the `TYPE_SUMMARIZER_PACKAGES` list at the top of [packages/kbn-type-summarizer/src/lib/bazel_cli_config.ts](./src/lib/bazel_cli_config.ts).
15 changes: 15 additions & 0 deletions packages/kbn-type-summarizer/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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.
*/

/** @typedef {import("@jest/types").Config.InitialOptions} JestConfig */
/** @type {JestConfig} */
module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-type-summarizer'],
};
15 changes: 15 additions & 0 deletions packages/kbn-type-summarizer/jest.integration.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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.
*/

/** @typedef {import("@jest/types").Config.InitialOptions} JestConfig */
/** @type {JestConfig} */
module.exports = {
preset: '@kbn/test/jest_integration_node',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-type-summarizer'],
};
7 changes: 7 additions & 0 deletions packages/kbn-type-summarizer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@kbn/type-summarizer",
"version": "1.0.0",
"license": "SSPL-1.0 OR Elastic License 2.0",
"main": "./target_node/index.js",
"private": true
}
Loading

0 comments on commit ef3eddb

Please sign in to comment.