-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
# Backport This will backport the following commits from `main` to `8.12`: - [[Obs AI Assistant] Use Mocha & Synthtrace (#173993)](#173993) <!--- Backport version: 9.4.3 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Dario Gieselaar","email":"dario.gieselaar@elastic.co"},"sourceCommit":{"committedDate":"2023-12-28T15:13:24Z","message":"[Obs AI Assistant] Use Mocha & Synthtrace (#173993)\n\nTwo changes here:\r\n\r\n1. Use Mocha instead of custom test runner. This allows us to use\r\nMocha's much more extensive API to run the tests: skipping tests, before\r\nand after hooks, grep etc.\r\n2. Make Synthtrace available for running tests.\r\n\r\nOne note:\r\n- I've added a rule to automatically inject a type reference for Mocha,\r\nvia `@kbn/ambient-ftr-types`. The reason is Mocha is not typed by\r\ndefault (ie, the types are not available in the global space), because\r\nit conflicts with Jest. The rule enforces that `@kbn/ambient-ftr-types`\r\nis imported so things like `before()` can be used.","sha":"5359ebe9c8838bd5b3fb347d1dbd556fcd88d566","branchLabelMapping":{"^v8.13.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v8.12.0","v8.12.1","v8.13.0"],"title":"[Obs AI Assistant] Use Mocha & Synthtrace for evaluation framework","number":173993,"url":"https://github.com/elastic/kibana/pull/173993","mergeCommit":{"message":"[Obs AI Assistant] Use Mocha & Synthtrace (#173993)\n\nTwo changes here:\r\n\r\n1. Use Mocha instead of custom test runner. This allows us to use\r\nMocha's much more extensive API to run the tests: skipping tests, before\r\nand after hooks, grep etc.\r\n2. Make Synthtrace available for running tests.\r\n\r\nOne note:\r\n- I've added a rule to automatically inject a type reference for Mocha,\r\nvia `@kbn/ambient-ftr-types`. The reason is Mocha is not typed by\r\ndefault (ie, the types are not available in the global space), because\r\nit conflicts with Jest. The rule enforces that `@kbn/ambient-ftr-types`\r\nis imported so things like `before()` can be used.","sha":"5359ebe9c8838bd5b3fb347d1dbd556fcd88d566"}},"sourceBranch":"main","suggestedTargetBranches":["8.12"],"targetPullRequestStates":[{"branch":"8.12","label":"v8.12.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.13.0","branchLabelMappingKey":"^v8.13.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/173993","number":173993,"mergeCommit":{"message":"[Obs AI Assistant] Use Mocha & Synthtrace (#173993)\n\nTwo changes here:\r\n\r\n1. Use Mocha instead of custom test runner. This allows us to use\r\nMocha's much more extensive API to run the tests: skipping tests, before\r\nand after hooks, grep etc.\r\n2. Make Synthtrace available for running tests.\r\n\r\nOne note:\r\n- I've added a rule to automatically inject a type reference for Mocha,\r\nvia `@kbn/ambient-ftr-types`. The reason is Mocha is not typed by\r\ndefault (ie, the types are not available in the global space), because\r\nit conflicts with Jest. The rule enforces that `@kbn/ambient-ftr-types`\r\nis imported so things like `before()` can be used.","sha":"5359ebe9c8838bd5b3fb347d1dbd556fcd88d566"}}]}] BACKPORT--> Co-authored-by: Dario Gieselaar <dario.gieselaar@elastic.co>
- Loading branch information
1 parent
1ac2f8f
commit fabe951
Showing
17 changed files
with
789 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
packages/kbn-eslint-plugin-imports/src/rules/require_import.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { RuleTester } from 'eslint'; | ||
import { RequireImportRule } from './require_import'; | ||
import dedent from 'dedent'; | ||
|
||
const fmt = (str: TemplateStringsArray) => dedent(str) + '\n'; | ||
|
||
const tsTester = [ | ||
'@typescript-eslint/parser', | ||
new RuleTester({ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
parserOptions: { | ||
sourceType: 'module', | ||
ecmaVersion: 2018, | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}), | ||
] as const; | ||
|
||
const babelTester = [ | ||
'@babel/eslint-parser', | ||
new RuleTester({ | ||
parser: require.resolve('@babel/eslint-parser'), | ||
parserOptions: { | ||
sourceType: 'module', | ||
ecmaVersion: 2018, | ||
requireConfigFile: false, | ||
babelOptions: { | ||
presets: ['@kbn/babel-preset/node_preset'], | ||
}, | ||
}, | ||
}), | ||
] as const; | ||
|
||
for (const [name, tester] of [tsTester, babelTester]) { | ||
describe(name, () => { | ||
tester.run('@kbn/imports/require_import', RequireImportRule, { | ||
valid: [ | ||
{ | ||
options: ['mocha'], | ||
filename: 'foo.ts', | ||
code: fmt` | ||
import 'mocha'; | ||
/// <reference types="mocha"/> | ||
describe(( ) => { | ||
before(( ) => { | ||
}); | ||
}); | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
options: ['mocha'], | ||
filename: 'foo.ts', | ||
code: fmt` | ||
describe(( ) => { | ||
before(( ) => { | ||
}); | ||
}); | ||
`, | ||
output: fmt`/// <reference types="mocha"/> | ||
describe(( ) => { | ||
before(( ) => { | ||
}); | ||
});`, | ||
errors: [ | ||
{ | ||
line: 1, | ||
message: `Required module 'mocha' is not imported as a type reference`, | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
}); | ||
} |
105 changes: 105 additions & 0 deletions
105
packages/kbn-eslint-plugin-imports/src/rules/require_import.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { Rule } from 'eslint'; | ||
import { load } from 'cheerio'; | ||
|
||
type StringModuleConfig = string; | ||
|
||
interface ObjectModuleConfig { | ||
module: string; | ||
as: ReferenceModuleAs; | ||
} | ||
|
||
type ModuleConfig = StringModuleConfig | ObjectModuleConfig; | ||
|
||
enum ReferenceModuleAs { | ||
typeReference = 'typeReference', | ||
} | ||
|
||
export const RequireImportRule: Rule.RuleModule = { | ||
meta: { | ||
type: 'problem', | ||
fixable: 'code', | ||
docs: { | ||
url: 'https://github.com/elastic/kibana/blob/main/packages/kbn-eslint-plugin-imports/README.mdx#kbnimportsrequire_import', | ||
}, | ||
schema: { | ||
type: 'array', | ||
items: { | ||
oneOf: [ | ||
{ | ||
type: 'string', | ||
}, | ||
{ | ||
type: 'object', | ||
additionalProperties: false, | ||
additionalItems: false, | ||
properties: { | ||
module: { | ||
type: 'string', | ||
}, | ||
as: { | ||
type: 'string', | ||
}, | ||
}, | ||
required: ['module', 'type'], | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
|
||
create(context) { | ||
const requiredImports: ModuleConfig[] = context.options; | ||
|
||
const mappedOptions: ObjectModuleConfig[] = requiredImports.map((config) => { | ||
if (typeof config === 'string') { | ||
return { | ||
module: config, | ||
as: ReferenceModuleAs.typeReference, | ||
}; | ||
} | ||
return config; | ||
}); | ||
|
||
return { | ||
'Program:exit': (node) => { | ||
mappedOptions.forEach((option) => { | ||
switch (option.as) { | ||
case ReferenceModuleAs.typeReference: | ||
const hasImport = node.comments?.some((comment) => { | ||
const nodeText = comment.value.match(/\/\s*(<.*>)/)?.[1]; | ||
if (nodeText) { | ||
const parsedNode = load(nodeText, { xml: true })()._root?.children()[0]; | ||
return ( | ||
parsedNode && | ||
parsedNode.name === 'reference' && | ||
parsedNode.attribs.types === option.module | ||
); | ||
} | ||
}); | ||
|
||
if (!hasImport) { | ||
context.report({ | ||
node, | ||
message: `Required module '${option.module}' is not imported as a type reference`, | ||
fix(fixer) { | ||
return fixer.insertTextBefore( | ||
node.body[0], | ||
`/// <reference types="${option.module}"/>\n\n` | ||
); | ||
}, | ||
}); | ||
} | ||
} | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
x-pack/plugins/observability_ai_assistant/scripts/evaluation/.eslintrc.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"overrides": [ | ||
{ | ||
"files": [ | ||
"**/*.spec.ts" | ||
], | ||
"rules": { | ||
"@kbn/imports/require_import": [ | ||
"error", | ||
"@kbn/ambient-ftr-types" | ||
], | ||
"@typescript-eslint/triple-slash-reference": [ | ||
"off" | ||
], | ||
"spaced-comment": [ | ||
"off" | ||
] | ||
} | ||
} | ||
] | ||
} |
Oops, something went wrong.