Skip to content

Commit

Permalink
Update painless autocomplete definitions (#85464) (#85593)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Dec 10, 2020
1 parent 088f301 commit 99f426c
Show file tree
Hide file tree
Showing 19 changed files with 6,399 additions and 388,563 deletions.
2 changes: 1 addition & 1 deletion packages/kbn-monaco/scripts/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = {
'filter',
'ip_script_field_script_field',
'long_script_field_script_field',
'painless_test',
'common',
'processor_conditional',
'score',
'string_script_field_script_field',
Expand Down
7 changes: 4 additions & 3 deletions packages/kbn-monaco/scripts/generate_autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,17 @@ function start(opts) {
// Generate autocomplete definitions
painlessContextFolderContents
.filter((file) => {
// Expected filename format: whitelist-<contextName>.json
const contextName = file.split('.')[0].split('whitelist-').pop();
// Expected filename format: painless-<contextName>.json
const contextName = file.split('.')[0].split('painless-').pop();
return supportedContexts.includes(contextName);
})
.forEach((file) => {
try {
const { name, classes: painlessClasses } = JSON.parse(
readFileSync(join(esPainlessContextFolder, file), 'utf8')
);
const filePath = join(autocompleteOutputFolder, `${name}.json`);
const contextName = name ? name : 'common'; // The common allowlist does not have a name associated to it.
const filePath = join(autocompleteOutputFolder, `${contextName}.json`);
const code = JSON.stringify(
{ suggestions: createAutocompleteDefinitions(painlessClasses) },
null,
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-monaco/scripts/utils/clone_es.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const esPainlessContextFolder = join(
'lang-painless',
'src',
'main',
'generated'
'generated',
'whitelist-json'
);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,6 @@ const getDisplayName = (name, imported) => {
return displayName.replace('$', '.');
};

/**
* Filters the context data by primitives and returns an array of primitive names
* The current data structure from ES does not indicate if a field is
* a primitive or class, so we infer this by checking
* that no methods or fields are defined
* @param {string} contextData
* @returns {Array<String>}
*/
const getPrimitives = (contextData) => {
return contextData
.filter(
({
static_fields: staticFields,
fields,
static_methods: staticMethods,
methods,
constructors,
}) => {
if (
staticMethods.length === 0 &&
methods.length === 0 &&
staticFields.length === 0 &&
fields.length === 0 &&
constructors.length === 0
) {
return true;
}
}
)
.map((type) => type.name);
};

/**
* Given the method name, array of parameters, and return value,
* we create a description of the method that will be
Expand Down Expand Up @@ -286,7 +254,6 @@ const createAutocompleteDefinitions = (painlessClasses) => {
}) => {
// The name is often prefixed by the Java package (e.g., Java.lang.Math) and needs to be removed
const displayName = getDisplayName(name, imported);
const isType = getPrimitives(painlessClasses).includes(name);

const properties = getPainlessClassToAutocomplete({
staticFields,
Expand All @@ -299,8 +266,8 @@ const createAutocompleteDefinitions = (painlessClasses) => {

return {
label: displayName,
kind: isType ? 'type' : 'class',
documentation: isType ? `Primitive: ${displayName}` : `Class: ${displayName}`,
kind: 'class',
documentation: `Class: ${displayName}`,
insertText: displayName,
properties: properties.length ? properties : undefined,
constructorDefinition,
Expand All @@ -313,7 +280,6 @@ const createAutocompleteDefinitions = (painlessClasses) => {

module.exports = {
getMethodDescription,
getPrimitives,
getPainlessClassToAutocomplete,
createAutocompleteDefinitions,
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,13 @@
* under the License.
*/
const {
getPrimitives,
getMethodDescription,
getPainlessClassToAutocomplete,
createAutocompleteDefinitions,
} = require('./create_autocomplete_definitions');

// Snippet of sample data returned from GET _scripts/painless/_context?context=<context>
// Snippet of sample data returned from https://github.com/elastic/elasticsearch/tree/master/modules/lang-painless/src/main/generated/whitelist-json
const testContext = [
{
name: 'boolean',
imported: true,
constructors: [],
static_methods: [],
methods: [],
static_fields: [],
fields: [],
},
{
name: 'int',
imported: true,
constructors: [],
static_methods: [],
methods: [],
static_fields: [],
fields: [],
},
{
name: 'java.lang.Long',
imported: true,
Expand Down Expand Up @@ -103,12 +84,6 @@ const testContext = [
];

describe('Autocomplete utils', () => {
describe('getPrimitives()', () => {
test('returns an array of primitives', () => {
expect(getPrimitives(testContext)).toEqual(['boolean', 'int']);
});
});

describe('getMethodDescription()', () => {
test('returns a string describing the method', () => {
expect(getMethodDescription('pow', [['double', 'double']], ['double'])).toEqual(
Expand All @@ -128,7 +103,7 @@ describe('Autocomplete utils', () => {

describe('getPainlessClassToAutocomplete()', () => {
test('returns the fields and methods associated with a class', () => {
const mathClass = testContext[3];
const mathClass = testContext[1];

const {
static_fields: staticFields,
Expand Down Expand Up @@ -173,7 +148,7 @@ describe('Autocomplete utils', () => {
});

test('removes duplicate methods', () => {
const longClass = testContext[2];
const longClass = testContext[0];

const {
static_fields: staticFields,
Expand Down Expand Up @@ -251,22 +226,6 @@ describe('Autocomplete utils', () => {
describe('createAutocompleteDefinitions()', () => {
test('returns formatted autocomplete definitions', () => {
expect(createAutocompleteDefinitions(testContext)).toEqual([
{
properties: undefined,
constructorDefinition: undefined,
documentation: 'Primitive: boolean',
insertText: 'boolean',
kind: 'type',
label: 'boolean',
},
{
properties: undefined,
constructorDefinition: undefined,
documentation: 'Primitive: int',
insertText: 'int',
kind: 'type',
label: 'int',
},
{
constructorDefinition: undefined,
documentation: 'Class: Long',
Expand Down
Loading

0 comments on commit 99f426c

Please sign in to comment.