Skip to content

Commit

Permalink
[APM] Remove TS optimization scripts (#153161)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
dgieselaar and kibanamachine authored Mar 16, 2023
1 parent 1f8cda7 commit 4fc365a
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 373 deletions.
2 changes: 1 addition & 1 deletion packages/kbn-ts-projects/ts_projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { TsProject } from './ts_project';

export const TS_PROJECTS = TsProject.loadAll({
/** Array of repo-relative paths to projects which should be ignored and not treated as a TS project in the repo */
ignore: ['x-pack/plugins/apm/scripts/optimize_tsconfig/tsconfig.json', '**/__fixtures__/**/*'],
ignore: ['**/__fixtures__/**/*'],

/** Array of repo-relative paths to projects which should have their type-check disabled */
disableTypeCheck: [
Expand Down
11 changes: 0 additions & 11 deletions x-pack/plugins/apm/dev_docs/typescript.md

This file was deleted.

172 changes: 84 additions & 88 deletions x-pack/plugins/apm/scripts/infer_route_return_types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
import Path from 'path';
import { execSync } from 'child_process';
import { argv } from 'yargs';
import { optimizeTsConfig } from '../optimize_tsconfig/optimize';

// This script adds explicit return types to route handlers,
// for performance reasons. See https://github.com/elastic/kibana/pull/123266
Expand All @@ -33,110 +32,107 @@ type ConvertibleDeclaration =
| ArrowFunction
| MethodDeclaration;

optimizeTsConfig().then(() => {
const project = new Project({
tsConfigFilePath: Path.resolve(__dirname, '../../../../../tsconfig.json'),
});
const project = new Project({
tsConfigFilePath: Path.resolve(__dirname, '../../../../../tsconfig.json'),
});

const glob =
(argv.glob as string | undefined) ||
'x-pack/plugins/apm/server/**/route.ts';
const glob =
(argv.glob as string | undefined) || 'x-pack/plugins/apm/server/**/route.ts';

const files = project.getSourceFiles(glob);
const files = project.getSourceFiles(glob);

const changedFiles: SourceFile[] = [];
const changedFiles: SourceFile[] = [];

files.forEach((file) => {
file.getVariableDeclarations().forEach((declaration) => {
const initializer = declaration.getInitializerIfKind(
SyntaxKind.CallExpression
);
files.forEach((file) => {
file.getVariableDeclarations().forEach((declaration) => {
const initializer = declaration.getInitializerIfKind(
SyntaxKind.CallExpression
);

const argument = initializer?.getArguments()[0];
const argument = initializer?.getArguments()[0];

if (Node.isObjectLiteralExpression(argument)) {
// this gets the `handler` function
const handler = argument.getProperty('handler') as
| PropertyAssignment
| MethodDeclaration
| undefined;
if (Node.isObjectLiteralExpression(argument)) {
// this gets the `handler` function
const handler = argument.getProperty('handler') as
| PropertyAssignment
| MethodDeclaration
| undefined;

if (!handler) {
return;
}
if (!handler) {
return;
}

let fnDeclaration = (
Node.isPropertyAssignment(handler)
? (handler.getInitializer() as ConvertibleDeclaration)
: handler
)
// remove any explicit return type
.removeReturnType();
let fnDeclaration = (
Node.isPropertyAssignment(handler)
? (handler.getInitializer() as ConvertibleDeclaration)
: handler
)
// remove any explicit return type
.removeReturnType();

const signature = fnDeclaration.getSignature();
const signature = fnDeclaration.getSignature();

if (!signature) {
return;
if (!signature) {
return;
}

const returnType = signature.getReturnType();

const txt = returnType.getText(
fnDeclaration,
TypeFormatFlags.NoTruncation
);

fnDeclaration = fnDeclaration.setReturnType(txt);

let hasAny: boolean = false;

fnDeclaration.transform((traversal) => {
const node = traversal.visitChildren();

if (node.kind === SyntaxKind.AnyKeyword) {
hasAny = true;
}

const returnType = signature.getReturnType();

const txt = returnType.getText(
fnDeclaration,
TypeFormatFlags.NoTruncation
);

fnDeclaration = fnDeclaration.setReturnType(txt);

let hasAny: boolean = false;

fnDeclaration.transform((traversal) => {
const node = traversal.visitChildren();

if (node.kind === SyntaxKind.AnyKeyword) {
hasAny = true;
}

if (ts.isImportTypeNode(node)) {
const literal = (node.argument as ts.LiteralTypeNode)
.literal as ts.StringLiteral;

// replace absolute paths with relative paths
return ts.updateImportTypeNode(
node,
ts.createLiteralTypeNode(
ts.createStringLiteral(
`./${Path.relative(
Path.dirname(file.getFilePath()),
literal.text
)}`
)
),
node.qualifier!,
node.typeArguments
);
}

return node;
});

if (hasAny) {
// eslint-disable-next-line no-console
console.warn(`Any type detected in ${file.getFilePath()}: ${txt}`);
if (ts.isImportTypeNode(node)) {
const literal = (node.argument as ts.LiteralTypeNode)
.literal as ts.StringLiteral;

// replace absolute paths with relative paths
return ts.updateImportTypeNode(
node,
ts.createLiteralTypeNode(
ts.createStringLiteral(
`./${Path.relative(
Path.dirname(file.getFilePath()),
literal.text
)}`
)
),
node.qualifier!,
node.typeArguments
);
}

changedFiles.push(file);
return node;
});

if (hasAny) {
// eslint-disable-next-line no-console
console.warn(`Any type detected in ${file.getFilePath()}: ${txt}`);
}
});

changedFiles.push(file);
}
});
});

changedFiles.forEach((file) => file.saveSync());
changedFiles.forEach((file) => file.saveSync());

const root = Path.join(__dirname, '../../../../..');
const root = Path.join(__dirname, '../../../../..');

// run ESLint on the changed files
execSync(`node scripts/eslint ${glob} --fix`, {
cwd: root,
stdio: 'inherit',
});
// run ESLint on the changed files
execSync(`node scripts/eslint ${glob} --fix`, {
cwd: root,
stdio: 'inherit',
});
13 changes: 0 additions & 13 deletions x-pack/plugins/apm/scripts/optimize_tsconfig.js

This file was deleted.

119 changes: 0 additions & 119 deletions x-pack/plugins/apm/scripts/optimize_tsconfig/optimize.js

This file was deleted.

28 changes: 0 additions & 28 deletions x-pack/plugins/apm/scripts/optimize_tsconfig/paths.js

This file was deleted.

Loading

0 comments on commit 4fc365a

Please sign in to comment.