Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update painless autocomplete definitions #85464

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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