mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
Generate a static parser, move tests to vis_type_timelion (#55299)
* Use generated parser, move tests to vis_type_timelion * Remove legacy tests * Create a grunt task for generating a parser
This commit is contained in:
parent
d3cef4791e
commit
6feabcd533
6 changed files with 1844 additions and 138 deletions
|
@ -9,6 +9,7 @@ bower_components
|
|||
/built_assets
|
||||
/html_docs
|
||||
/src/plugins/data/common/es_query/kuery/ast/_generated_/**
|
||||
/src/legacy/core_plugins/vis_type_timelion/public/_generated_/**
|
||||
src/legacy/core_plugins/vis_type_vislib/public/vislib/__tests__/lib/fixtures/mock_data
|
||||
/src/legacy/ui/public/angular-bootstrap
|
||||
/src/legacy/ui/public/flot-charts
|
||||
|
|
10
src/legacy/core_plugins/vis_type_timelion/README.md
Normal file
10
src/legacy/core_plugins/vis_type_timelion/README.md
Normal file
|
@ -0,0 +1,10 @@
|
|||
# Vis type Timelion
|
||||
|
||||
# Generate a parser
|
||||
If your grammar was changed in `public/chain.peg` you need to re-generate the static parser. You could use a grunt task:
|
||||
|
||||
```
|
||||
grunt peg:timelion_chain
|
||||
```
|
||||
|
||||
The generated parser will be appeared at `public/_generated_` folder, which is included in `.eslintignore`
|
File diff suppressed because it is too large
Load diff
|
@ -17,19 +17,16 @@
|
|||
* under the License.
|
||||
*/
|
||||
|
||||
import expect from '@kbn/expect';
|
||||
import PEG from 'pegjs';
|
||||
import grammar from 'raw-loader!../../../../vis_type_timelion/public/chain.peg';
|
||||
import { SUGGESTION_TYPE, suggest } from '../timelion_expression_input_helpers';
|
||||
import { getArgValueSuggestions } from '../../../../vis_type_timelion/public/helpers/arg_value_suggestions';
|
||||
import {
|
||||
setIndexPatterns,
|
||||
setSavedObjectsClient,
|
||||
} from '../../../../vis_type_timelion/public/helpers/plugin_services';
|
||||
import { SUGGESTION_TYPE, suggest } from './timelion_expression_input_helpers';
|
||||
import { getArgValueSuggestions } from '../helpers/arg_value_suggestions';
|
||||
import { setIndexPatterns, setSavedObjectsClient } from '../helpers/plugin_services';
|
||||
import { IndexPatterns } from 'src/plugins/data/public';
|
||||
import { SavedObjectsClient } from 'kibana/public';
|
||||
import { ITimelionFunction } from '../../common/types';
|
||||
|
||||
describe('Timelion expression suggestions', () => {
|
||||
setIndexPatterns({});
|
||||
setSavedObjectsClient({});
|
||||
setIndexPatterns({} as IndexPatterns);
|
||||
setSavedObjectsClient({} as SavedObjectsClient);
|
||||
|
||||
const argValueSuggestions = getArgValueSuggestions();
|
||||
|
||||
|
@ -45,17 +42,13 @@ describe('Timelion expression suggestions', () => {
|
|||
suggestions: [{ name: 'value1' }],
|
||||
},
|
||||
],
|
||||
};
|
||||
} as ITimelionFunction;
|
||||
const myFunc2 = {
|
||||
name: 'myFunc2',
|
||||
chainable: false,
|
||||
args: [{ name: 'argA' }, { name: 'argAB' }, { name: 'argABC' }],
|
||||
};
|
||||
} as ITimelionFunction;
|
||||
const functionList = [func1, myFunc2];
|
||||
let Parser;
|
||||
beforeEach(function() {
|
||||
Parser = PEG.generate(grammar);
|
||||
});
|
||||
|
||||
describe('parse exception', () => {
|
||||
describe('incompleteFunction', () => {
|
||||
|
@ -65,17 +58,12 @@ describe('Timelion expression suggestions', () => {
|
|||
const suggestions = await suggest(
|
||||
expression,
|
||||
functionList,
|
||||
Parser,
|
||||
cursorPosition,
|
||||
argValueSuggestions
|
||||
);
|
||||
expect(suggestions).to.eql({
|
||||
expect(suggestions).toEqual({
|
||||
list: [func1, myFunc2],
|
||||
location: {
|
||||
min: 0,
|
||||
max: 1,
|
||||
},
|
||||
type: 'functions',
|
||||
type: SUGGESTION_TYPE.FUNCTIONS,
|
||||
});
|
||||
});
|
||||
it('should filter function suggestions by function name', async () => {
|
||||
|
@ -84,17 +72,12 @@ describe('Timelion expression suggestions', () => {
|
|||
const suggestions = await suggest(
|
||||
expression,
|
||||
functionList,
|
||||
Parser,
|
||||
cursorPosition,
|
||||
argValueSuggestions
|
||||
);
|
||||
expect(suggestions).to.eql({
|
||||
expect(suggestions).toEqual({
|
||||
list: [myFunc2],
|
||||
location: {
|
||||
min: 0,
|
||||
max: 4,
|
||||
},
|
||||
type: 'functions',
|
||||
type: SUGGESTION_TYPE.FUNCTIONS,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -106,17 +89,12 @@ describe('Timelion expression suggestions', () => {
|
|||
const suggestions = await suggest(
|
||||
expression,
|
||||
functionList,
|
||||
Parser,
|
||||
cursorPosition,
|
||||
argValueSuggestions
|
||||
);
|
||||
expect(suggestions).to.eql({
|
||||
expect(suggestions).toEqual({
|
||||
list: [],
|
||||
location: {
|
||||
min: 11,
|
||||
max: 12,
|
||||
},
|
||||
type: 'arguments',
|
||||
type: SUGGESTION_TYPE.ARGUMENTS,
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -126,17 +104,12 @@ describe('Timelion expression suggestions', () => {
|
|||
const suggestions = await suggest(
|
||||
expression,
|
||||
functionList,
|
||||
Parser,
|
||||
cursorPosition,
|
||||
argValueSuggestions
|
||||
);
|
||||
expect(suggestions).to.eql({
|
||||
expect(suggestions).toEqual({
|
||||
list: myFunc2.args,
|
||||
location: {
|
||||
min: 9,
|
||||
max: 10,
|
||||
},
|
||||
type: 'arguments',
|
||||
type: SUGGESTION_TYPE.ARGUMENTS,
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -146,17 +119,12 @@ describe('Timelion expression suggestions', () => {
|
|||
const suggestions = await suggest(
|
||||
expression,
|
||||
functionList,
|
||||
Parser,
|
||||
cursorPosition,
|
||||
argValueSuggestions
|
||||
);
|
||||
expect(suggestions).to.eql({
|
||||
expect(suggestions).toEqual({
|
||||
list: myFunc2.args,
|
||||
location: {
|
||||
min: 9,
|
||||
max: 25,
|
||||
},
|
||||
type: 'arguments',
|
||||
type: SUGGESTION_TYPE.ARGUMENTS,
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -166,17 +134,12 @@ describe('Timelion expression suggestions', () => {
|
|||
const suggestions = await suggest(
|
||||
expression,
|
||||
functionList,
|
||||
Parser,
|
||||
cursorPosition,
|
||||
argValueSuggestions
|
||||
);
|
||||
expect(suggestions).to.eql({
|
||||
expect(suggestions).toEqual({
|
||||
list: [{ name: 'argA' }, { name: 'argAB', suggestions: [{ name: 'value1' }] }],
|
||||
location: {
|
||||
min: 7,
|
||||
max: 8,
|
||||
},
|
||||
type: 'arguments',
|
||||
type: SUGGESTION_TYPE.ARGUMENTS,
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -186,17 +149,12 @@ describe('Timelion expression suggestions', () => {
|
|||
const suggestions = await suggest(
|
||||
expression,
|
||||
functionList,
|
||||
Parser,
|
||||
cursorPosition,
|
||||
argValueSuggestions
|
||||
);
|
||||
expect(suggestions).to.eql({
|
||||
expect(suggestions).toEqual({
|
||||
list: [{ name: 'argA' }, { name: 'argABC' }],
|
||||
location: {
|
||||
min: 24,
|
||||
max: 25,
|
||||
},
|
||||
type: 'arguments',
|
||||
type: SUGGESTION_TYPE.ARGUMENTS,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -208,17 +166,12 @@ describe('Timelion expression suggestions', () => {
|
|||
const suggestions = await suggest(
|
||||
expression,
|
||||
functionList,
|
||||
Parser,
|
||||
cursorPosition,
|
||||
argValueSuggestions
|
||||
);
|
||||
expect(suggestions).to.eql({
|
||||
expect(suggestions).toEqual({
|
||||
list: [],
|
||||
location: {
|
||||
min: 11,
|
||||
max: 11,
|
||||
},
|
||||
type: 'argument_value',
|
||||
type: SUGGESTION_TYPE.ARGUMENT_VALUE,
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -228,17 +181,12 @@ describe('Timelion expression suggestions', () => {
|
|||
const suggestions = await suggest(
|
||||
expression,
|
||||
functionList,
|
||||
Parser,
|
||||
cursorPosition,
|
||||
argValueSuggestions
|
||||
);
|
||||
expect(suggestions).to.eql({
|
||||
expect(suggestions).toEqual({
|
||||
list: [{ name: 'value1' }],
|
||||
location: {
|
||||
min: 11,
|
||||
max: 11,
|
||||
},
|
||||
type: 'argument_value',
|
||||
type: SUGGESTION_TYPE.ARGUMENT_VALUE,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -252,17 +200,12 @@ describe('Timelion expression suggestions', () => {
|
|||
const suggestions = await suggest(
|
||||
expression,
|
||||
functionList,
|
||||
Parser,
|
||||
cursorPosition,
|
||||
argValueSuggestions
|
||||
);
|
||||
expect(suggestions).to.eql({
|
||||
expect(suggestions).toEqual({
|
||||
list: [func1],
|
||||
location: {
|
||||
min: 0,
|
||||
max: 8,
|
||||
},
|
||||
type: 'functions',
|
||||
type: SUGGESTION_TYPE.FUNCTIONS,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -275,17 +218,12 @@ describe('Timelion expression suggestions', () => {
|
|||
const suggestions = await suggest(
|
||||
expression,
|
||||
functionList,
|
||||
Parser,
|
||||
cursorPosition,
|
||||
argValueSuggestions
|
||||
);
|
||||
expect(suggestions).to.eql({
|
||||
expect(suggestions).toEqual({
|
||||
list: myFunc2.args,
|
||||
location: {
|
||||
min: 9,
|
||||
max: 9,
|
||||
},
|
||||
type: 'arguments',
|
||||
type: SUGGESTION_TYPE.ARGUMENTS,
|
||||
});
|
||||
});
|
||||
it('should not provide argument suggestions for argument that is all ready set in function def', async () => {
|
||||
|
@ -294,18 +232,12 @@ describe('Timelion expression suggestions', () => {
|
|||
const suggestions = await suggest(
|
||||
expression,
|
||||
functionList,
|
||||
Parser,
|
||||
cursorPosition,
|
||||
argValueSuggestions
|
||||
);
|
||||
expect(suggestions.type).to.equal(SUGGESTION_TYPE.ARGUMENTS);
|
||||
expect(suggestions).to.eql({
|
||||
expect(suggestions).toEqual({
|
||||
list: [{ name: 'argA' }, { name: 'argABC' }],
|
||||
location: {
|
||||
min: 24,
|
||||
max: 24,
|
||||
},
|
||||
type: 'arguments',
|
||||
type: SUGGESTION_TYPE.ARGUMENTS,
|
||||
});
|
||||
});
|
||||
it('should filter argument suggestions by argument name', async () => {
|
||||
|
@ -314,17 +246,12 @@ describe('Timelion expression suggestions', () => {
|
|||
const suggestions = await suggest(
|
||||
expression,
|
||||
functionList,
|
||||
Parser,
|
||||
cursorPosition,
|
||||
argValueSuggestions
|
||||
);
|
||||
expect(suggestions).to.eql({
|
||||
expect(suggestions).toEqual({
|
||||
list: [{ name: 'argAB' }, { name: 'argABC' }],
|
||||
location: {
|
||||
min: 9,
|
||||
max: 14,
|
||||
},
|
||||
type: 'arguments',
|
||||
type: SUGGESTION_TYPE.ARGUMENTS,
|
||||
});
|
||||
});
|
||||
it('should not show first argument for chainable functions', async () => {
|
||||
|
@ -333,17 +260,12 @@ describe('Timelion expression suggestions', () => {
|
|||
const suggestions = await suggest(
|
||||
expression,
|
||||
functionList,
|
||||
Parser,
|
||||
cursorPosition,
|
||||
argValueSuggestions
|
||||
);
|
||||
expect(suggestions).to.eql({
|
||||
expect(suggestions).toEqual({
|
||||
list: [{ name: 'argA' }, { name: 'argAB', suggestions: [{ name: 'value1' }] }],
|
||||
location: {
|
||||
min: 7,
|
||||
max: 7,
|
||||
},
|
||||
type: 'arguments',
|
||||
type: SUGGESTION_TYPE.ARGUMENTS,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -354,17 +276,12 @@ describe('Timelion expression suggestions', () => {
|
|||
const suggestions = await suggest(
|
||||
expression,
|
||||
functionList,
|
||||
Parser,
|
||||
cursorPosition,
|
||||
argValueSuggestions
|
||||
);
|
||||
expect(suggestions).to.eql({
|
||||
expect(suggestions).toEqual({
|
||||
list: [],
|
||||
location: {
|
||||
min: 14,
|
||||
max: 16,
|
||||
},
|
||||
type: 'argument_value',
|
||||
type: SUGGESTION_TYPE.ARGUMENT_VALUE,
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -374,17 +291,12 @@ describe('Timelion expression suggestions', () => {
|
|||
const suggestions = await suggest(
|
||||
expression,
|
||||
functionList,
|
||||
Parser,
|
||||
cursorPosition,
|
||||
argValueSuggestions
|
||||
);
|
||||
expect(suggestions).to.eql({
|
||||
expect(suggestions).toEqual({
|
||||
list: [{ name: 'value1' }],
|
||||
location: {
|
||||
min: 13,
|
||||
max: 16,
|
||||
},
|
||||
type: 'argument_value',
|
||||
type: SUGGESTION_TYPE.ARGUMENT_VALUE,
|
||||
});
|
||||
});
|
||||
});
|
|
@ -18,18 +18,17 @@
|
|||
*/
|
||||
|
||||
import { get, startsWith } from 'lodash';
|
||||
import PEG from 'pegjs';
|
||||
import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api';
|
||||
|
||||
// @ts-ignore
|
||||
import grammar from 'raw-loader!../chain.peg';
|
||||
|
||||
import { i18n } from '@kbn/i18n';
|
||||
import { Parser } from 'pegjs';
|
||||
|
||||
// @ts-ignore
|
||||
import { parse } from '../_generated_/chain';
|
||||
|
||||
import { ITimelionFunction, TimelionFunctionArgs } from '../../common/types';
|
||||
import { ArgValueSuggestions, FunctionArg, Location } from '../helpers/arg_value_suggestions';
|
||||
|
||||
const Parser = PEG.generate(grammar);
|
||||
|
||||
export enum SUGGESTION_TYPE {
|
||||
ARGUMENTS = 'arguments',
|
||||
ARGUMENT_VALUE = 'argument_value',
|
||||
|
@ -57,7 +56,7 @@ function getArgumentsHelp(
|
|||
}
|
||||
|
||||
async function extractSuggestionsFromParsedResult(
|
||||
result: ReturnType<typeof Parser.parse>,
|
||||
result: ReturnType<Parser['parse']>,
|
||||
cursorPosition: number,
|
||||
functionList: ITimelionFunction[],
|
||||
argValueSuggestions: ArgValueSuggestions
|
||||
|
@ -141,7 +140,7 @@ export async function suggest(
|
|||
argValueSuggestions: ArgValueSuggestions
|
||||
) {
|
||||
try {
|
||||
const result = await Parser.parse(expression);
|
||||
const result = await parse(expression);
|
||||
|
||||
return await extractSuggestionsFromParsedResult(
|
||||
result,
|
||||
|
|
|
@ -25,4 +25,8 @@ module.exports = {
|
|||
allowedStartRules: ['start', 'Literal'],
|
||||
},
|
||||
},
|
||||
timelion_chain: {
|
||||
src: 'src/legacy/core_plugins/vis_type_timelion/public/chain.peg',
|
||||
dest: 'src/legacy/core_plugins/vis_type_timelion/public/_generated_/chain.js',
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue