Skip to content

Commit de910d7

Browse files
committed
refactor: πŸ’‘ convert Interpreter .js -> .ts (#44545)
* refactor: πŸ’‘ convert Interpreter .js -> .ts * fix: πŸ› fix TypeScript type errors * test: πŸ’ remove old snapshot
1 parent ace757c commit de910d7

23 files changed

Lines changed: 118 additions & 105 deletions

File tree

src/legacy/core_plugins/interpreter/public/functions/__snapshots__/kibana.test.js.snap renamed to src/legacy/core_plugins/interpreter/public/functions/__snapshots__/kibana.test.ts.snap

File renamed without changes.

src/legacy/core_plugins/interpreter/public/functions/__tests__/font.js renamed to src/legacy/core_plugins/interpreter/public/functions/__tests__/font.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ import { font } from '../font';
2323
import { functionWrapper } from '../../../test_helpers';
2424

2525
describe('font', () => {
26-
const fn = functionWrapper(font);
26+
const fn: any = functionWrapper(font);
2727

2828
describe('default output', () => {
2929
const result = fn(null);
3030

3131
it('returns a style', () => {
32-
expect(result)
32+
(expect as any)(result)
3333
.to.have.property('type', 'style')
3434
.and.to.have.property('spec')
3535
.and.to.have.property('css');
@@ -40,8 +40,8 @@ describe('font', () => {
4040
describe('size', () => {
4141
it('sets font size', () => {
4242
const result = fn(null, { size: 20 });
43-
expect(result.spec).to.have.property('fontSize', '20px');
44-
expect(result.css).to.contain('font-size:20px');
43+
(expect as any)(result.spec).to.have.property('fontSize', '20px');
44+
(expect as any)(result.css).to.contain('font-size:20px');
4545
});
4646

4747
it('defaults to 14px', () => {
@@ -110,7 +110,7 @@ describe('font', () => {
110110
expect(result.css).to.contain('font-weight:400');
111111
});
112112

113-
it('defaults to \'normal\'', () => {
113+
it("defaults to 'normal'", () => {
114114
const result = fn(null);
115115
expect(result.spec).to.have.property('fontWeight', 'normal');
116116
expect(result.css).to.contain('font-weight:normal');

src/legacy/core_plugins/interpreter/public/functions/clog.js renamed to src/legacy/core_plugins/interpreter/public/functions/clog.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
export const clog = () => ({
2121
name: 'clog',
2222
help: 'Outputs the context to the console',
23-
fn: context => {
24-
console.log(context); //eslint-disable-line no-console
23+
fn: (context: any) => {
24+
console.log(context); // eslint-disable-line no-console
2525
return context;
2626
},
2727
});

src/legacy/core_plugins/interpreter/public/functions/index.js renamed to src/legacy/core_plugins/interpreter/public/functions/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,12 @@ import { visualization } from './visualization';
2727
import { visDimension } from './vis_dimension';
2828

2929
export const functions = [
30-
clog, esaggs, font, kibana, kibanaContext, range, visualization, visDimension,
30+
clog,
31+
esaggs,
32+
font,
33+
kibana,
34+
kibanaContext,
35+
range,
36+
visualization,
37+
visDimension,
3138
];

src/legacy/core_plugins/interpreter/public/functions/kibana.test.js renamed to src/legacy/core_plugins/interpreter/public/functions/kibana.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import { kibana } from './kibana';
2222

2323
describe('interpreter/functions#kibana', () => {
2424
const fn = functionWrapper(kibana);
25-
let context;
26-
let initialContext;
27-
let handlers;
25+
let context: any;
26+
let initialContext: any;
27+
let handlers: any;
2828

2929
beforeEach(() => {
3030
context = { timeRange: { from: '0', to: '1' } };

src/legacy/core_plugins/interpreter/public/functions/kibana.js renamed to src/legacy/core_plugins/interpreter/public/functions/kibana.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ export const kibana = () => ({
2424
type: 'kibana_context',
2525
context: {},
2626
help: i18n.translate('interpreter.functions.kibana.help', {
27-
defaultMessage: 'Gets kibana global context'
27+
defaultMessage: 'Gets kibana global context',
2828
}),
2929
args: {},
30-
fn(context, args, handlers) {
30+
fn(context: any, args: any, handlers: any) {
3131
const initialContext = handlers.getInitialContext ? handlers.getInitialContext() : {};
3232

3333
if (context.query) {
@@ -45,7 +45,7 @@ export const kibana = () => ({
4545
type: 'kibana_context',
4646
query: initialContext.query,
4747
filters: initialContext.filters,
48-
timeRange: timeRange,
48+
timeRange,
4949
};
5050
},
5151
});

src/legacy/core_plugins/interpreter/public/functions/kibana_context.js renamed to src/legacy/core_plugins/interpreter/public/functions/kibana_context.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,10 @@ export const kibanaContext = () => ({
2424
name: 'kibana_context',
2525
type: 'kibana_context',
2626
context: {
27-
types: [
28-
'kibana_context',
29-
'null',
30-
],
27+
types: ['kibana_context', 'null'],
3128
},
3229
help: i18n.translate('interpreter.functions.kibana_context.help', {
33-
defaultMessage: 'Updates kibana global context'
30+
defaultMessage: 'Updates kibana global context',
3431
}),
3532
args: {
3633
q: {
@@ -49,11 +46,11 @@ export const kibanaContext = () => ({
4946
savedSearchId: {
5047
types: ['string', 'null'],
5148
default: null,
52-
}
49+
},
5350
},
54-
async fn(context, args) {
51+
async fn(context: any, args: any) {
5552
const $injector = await chrome.dangerouslyGetActiveInjector();
56-
const savedSearches = $injector.get('savedSearches');
53+
const savedSearches = $injector.get('savedSearches') as any;
5754
const queryArg = args.q ? JSON.parse(args.q) : [];
5855
let queries = Array.isArray(queryArg) ? queryArg : [queryArg];
5956
let filters = args.filters ? JSON.parse(args.filters) : [];
@@ -71,16 +68,16 @@ export const kibanaContext = () => ({
7168
}
7269

7370
if (context.filters) {
74-
filters = filters.concat(context.filters).filter(f => !f.meta.disabled);
71+
filters = filters.concat(context.filters).filter((f: any) => !f.meta.disabled);
7572
}
7673

7774
const timeRange = args.timeRange ? JSON.parse(args.timeRange) : context.timeRange;
7875

7976
return {
8077
type: 'kibana_context',
8178
query: queries,
82-
filters: filters,
83-
timeRange: timeRange,
79+
filters,
80+
timeRange,
8481
};
8582
},
8683
});

src/legacy/core_plugins/interpreter/public/functions/vis_dimension.js renamed to src/legacy/core_plugins/interpreter/public/functions/vis_dimension.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,48 +22,48 @@ import { i18n } from '@kbn/i18n';
2222
export const visDimension = () => ({
2323
name: 'visdimension',
2424
help: i18n.translate('interpreter.function.visDimension.help', {
25-
defaultMessage: 'Generates visConfig dimension object'
25+
defaultMessage: 'Generates visConfig dimension object',
2626
}),
2727
type: 'vis_dimension',
2828
context: {
29-
types: [
30-
'kibana_datatable'
31-
],
29+
types: ['kibana_datatable'],
3230
},
3331
args: {
3432
accessor: {
3533
types: ['string', 'number'],
3634
aliases: ['_'],
3735
help: i18n.translate('interpreter.function.visDimension.accessor.help', {
38-
defaultMessage: 'Column in your dataset to use (either column index or column name)'
36+
defaultMessage: 'Column in your dataset to use (either column index or column name)',
3937
}),
4038
},
4139
format: {
4240
types: ['string'],
43-
default: 'string'
41+
default: 'string',
4442
},
4543
formatParams: {
4644
types: ['string'],
4745
default: '"{}"',
48-
}
46+
},
4947
},
50-
fn: (context, args) => {
51-
const accessor = Number.isInteger(args.accessor) ?
52-
args.accessor :
53-
context.columns.find(c => c.id === args.accessor);
48+
fn: (context: any, args: any) => {
49+
const accessor = Number.isInteger(args.accessor)
50+
? args.accessor
51+
: context.columns.find((c: any) => c.id === args.accessor);
5452
if (accessor === undefined) {
55-
throw new Error(i18n.translate('interpreter.function.visDimension.error.accessor', {
56-
defaultMessage: 'Column name provided is invalid'
57-
}));
53+
throw new Error(
54+
i18n.translate('interpreter.function.visDimension.error.accessor', {
55+
defaultMessage: 'Column name provided is invalid',
56+
})
57+
);
5858
}
5959

6060
return {
6161
type: 'vis_dimension',
62-
accessor: accessor,
62+
accessor,
6363
format: {
6464
id: args.format,
6565
params: JSON.parse(args.formatParams),
66-
}
66+
},
6767
};
6868
},
6969
});

src/legacy/core_plugins/interpreter/public/functions/visualization.js renamed to src/legacy/core_plugins/interpreter/public/functions/visualization.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@
2020
import { get } from 'lodash';
2121
import { i18n } from '@kbn/i18n';
2222
import chrome from 'ui/chrome';
23-
import { setup as data } from '../../../data/public/legacy';
24-
import { start as visualizations } from '../../../visualizations/public/legacy';
25-
2623
import { FilterBarQueryFilterProvider } from 'ui/filter_manager/query_filter';
2724
import { PersistedState } from 'ui/persisted_state';
25+
import { setup as data } from '../../../data/public/legacy';
26+
import { start as visualizations } from '../../../visualizations/public/legacy';
2827

2928
export const visualization = () => ({
3029
name: 'visualization',
3130
type: 'render',
3231
help: i18n.translate('interpreter.functions.visualization.help', {
33-
defaultMessage: 'A simple visualization'
32+
defaultMessage: 'A simple visualization',
3433
}),
3534
args: {
3635
index: {
@@ -60,17 +59,17 @@ export const visualization = () => ({
6059
uiState: {
6160
types: ['string'],
6261
default: '"{}"',
63-
}
62+
},
6463
},
65-
async fn(context, args, handlers) {
64+
async fn(context: any, args: any, handlers: any) {
6665
const $injector = await chrome.dangerouslyGetActiveInjector();
67-
const Private = $injector.get('Private');
66+
const Private = $injector.get('Private') as any;
6867
const { indexPatterns } = data.indexPatterns;
6968
const queryFilter = Private(FilterBarQueryFilterProvider);
7069

7170
const visConfigParams = JSON.parse(args.visConfig);
7271
const schemas = JSON.parse(args.schemas);
73-
const visType = visualizations.types.get(args.type || 'histogram');
72+
const visType = visualizations.types.get(args.type || 'histogram') as any;
7473
const indexPattern = args.index ? await indexPatterns.get(args.index) : null;
7574

7675
const uiStateParams = JSON.parse(args.uiState);
@@ -85,7 +84,7 @@ export const visualization = () => ({
8584
timeRange: get(context, 'timeRange', null),
8685
query: get(context, 'query', null),
8786
filters: get(context, 'filters', null),
88-
uiState: uiState,
87+
uiState,
8988
inspectorAdapters: handlers.inspectorAdapters,
9089
queryFilter,
9190
forceFetch: true,
@@ -95,14 +94,14 @@ export const visualization = () => ({
9594
if (typeof visType.responseHandler === 'function') {
9695
if (context.columns) {
9796
// assign schemas to aggConfigs
98-
context.columns.forEach(column => {
97+
context.columns.forEach((column: any) => {
9998
if (column.aggConfig) {
10099
column.aggConfig.aggConfigs.schemas = visType.schemas.all;
101100
}
102101
});
103102

104103
Object.keys(schemas).forEach(key => {
105-
schemas[key].forEach(i => {
104+
schemas[key].forEach((i: any) => {
106105
if (context.columns[i] && context.columns[i].aggConfig) {
107106
context.columns[i].aggConfig.schema = key;
108107
}
@@ -119,8 +118,8 @@ export const visualization = () => ({
119118
value: {
120119
visData: context,
121120
visType: args.type,
122-
visConfig: visConfigParams
123-
}
121+
visConfig: visConfigParams,
122+
},
124123
};
125-
}
124+
},
126125
});

src/legacy/core_plugins/interpreter/public/interpreter.test.js renamed to src/legacy/core_plugins/interpreter/public/interpreter.test.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ jest.mock('ui/new_platform', () => ({
2424
injectedMetadata: {
2525
getKibanaVersion: () => '8.0.0',
2626
getBasePath: () => '/lol',
27-
}
28-
}
29-
}
27+
},
28+
},
29+
},
3030
}));
3131
jest.mock('uiExports/interpreter');
3232

@@ -38,7 +38,7 @@ jest.mock('@kbn/interpreter/common', () => ({
3838
const mockInterpreter = {
3939
interpreter: {
4040
interpretAst: jest.fn(),
41-
}
41+
},
4242
};
4343
jest.mock('./lib/interpreter', () => ({
4444
initializeInterpreter: jest.fn().mockReturnValue(Promise.resolve(mockInterpreter)),
@@ -57,9 +57,9 @@ jest.mock('./functions', () => ({ functions: [{}, {}, {}] }));
5757
jest.mock('./renderers/visualization', () => ({ visualization: {} }));
5858

5959
describe('interpreter/interpreter', () => {
60-
let getInterpreter;
61-
let interpretAst;
62-
let initializeInterpreter;
60+
let getInterpreter: any;
61+
let interpretAst: any;
62+
let initializeInterpreter: any;
6363

6464
beforeEach(() => {
6565
jest.clearAllMocks();
@@ -117,5 +117,4 @@ describe('interpreter/interpreter', () => {
117117
expect(mockInterpreter.interpreter.interpretAst).toHaveBeenCalledTimes(2);
118118
});
119119
});
120-
121120
});

0 commit comments

Comments
Β (0)