diff --git a/.eslintrc.js b/.eslintrc.js index 1dd164239e..017c5af1d4 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,5 +1,5 @@ module.exports = { - ignorePatterns: ['.eslintrc.js', '**/node_modules', '**/build', '**/dist', '**/target', '**/.yarn'], + ignorePatterns: ['.eslintrc.js', '**/node_modules', '**/build', '**/dist', '**/target', '**/.yarn', 'specs/bundled/*.doc.yml'], overrides: [ { @@ -51,6 +51,10 @@ module.exports = { files: ['!specs/bundled/*.yml'], rules: { "automation-custom/out-of-line-enum": "error", + "automation-custom/out-of-line-one-of": "error", + "automation-custom/out-of-line-all-of": "error", + "automation-custom/out-of-line-any-of": "error", + } } ] diff --git a/eslint/src/index.ts b/eslint/src/index.ts index 8a3bdffe4a..57e777f251 100644 --- a/eslint/src/index.ts +++ b/eslint/src/index.ts @@ -1,10 +1,13 @@ import { endWithDot } from './rules/endWithDot'; -import { outOfLineEnum } from './rules/outOfLineEnum'; +import { createOutOfLineRule } from './rules/outOfLineRule'; import { singleQuoteRef } from './rules/singleQuoteRef'; const rules = { 'end-with-dot': endWithDot, - 'out-of-line-enum': outOfLineEnum, + 'out-of-line-enum': createOutOfLineRule({ property: 'enum' }), + 'out-of-line-one-of': createOutOfLineRule({ property: 'oneOf' }), + 'out-of-line-all-of': createOutOfLineRule({ property: 'allOf' }), + 'out-of-line-any-of': createOutOfLineRule({ property: 'anyOf' }), 'single-quote-ref': singleQuoteRef, }; diff --git a/eslint/src/rules/outOfLineEnum.ts b/eslint/src/rules/outOfLineEnum.ts deleted file mode 100644 index ea7c64a50a..0000000000 --- a/eslint/src/rules/outOfLineEnum.ts +++ /dev/null @@ -1,44 +0,0 @@ -import type { Rule } from 'eslint'; - -import { isPairWithKey } from '../utils'; - -export const outOfLineEnum: Rule.RuleModule = { - meta: { - docs: { - description: 'enum must be out of line', - }, - messages: { - enumNotOutOfLine: 'enum is not out of line', - }, - }, - create(context) { - if (!context.parserServices.isYAML) { - return {}; - } - - return { - YAMLPair(node): void { - if (!isPairWithKey(node, 'enum')) { - return; - } - // parent is mapping, and parent is real parent that must be to the far left - if (node.parent.parent.loc.start.column === 0) { - return; - } - if ( - isPairWithKey( - node.parent.parent.parent.parent?.parent?.parent?.parent ?? null, - 'servers' - ) - ) { - // accept out of line enum if in servers - return; - } - context.report({ - node: node.parent.parent as any, - messageId: 'enumNotOutOfLine', - }); - }, - }; - }, -}; diff --git a/eslint/src/rules/outOfLineRule.ts b/eslint/src/rules/outOfLineRule.ts new file mode 100644 index 0000000000..099423cd47 --- /dev/null +++ b/eslint/src/rules/outOfLineRule.ts @@ -0,0 +1,57 @@ +import type { Rule } from 'eslint'; + +import { isPairWithKey } from '../utils'; + +export function createOutOfLineRule({ + property, + description = `${property} must be out of line, not nested inside properties`, + messageId = `${property}NotOutOfLine`, + message = `${property} must be out of line`, +}: { + property: string; + description?: string; + messageId?: string; + message?: string; +}): Rule.RuleModule { + const rule: Rule.RuleModule = { + meta: { + docs: { + description, + }, + messages: { + [messageId]: message, + }, + }, + create(context) { + if (!context.parserServices.isYAML) { + return {}; + } + + return { + YAMLPair(node): void { + if (!isPairWithKey(node, property)) { + return; + } + // parent is mapping, and parent is real parent that must be to the far left + if (node.parent.parent.loc.start.column === 0) { + return; + } + // accept anything in servers + if ( + isPairWithKey( + node.parent.parent.parent.parent?.parent?.parent?.parent ?? null, + 'servers' + ) + ) { + return; + } + context.report({ + node: node.parent.parent as any, + messageId, + }); + }, + }; + }, + }; + return rule; +} diff --git a/eslint/tests/endWithDot.ts b/eslint/tests/endWithDot.test.ts similarity index 91% rename from eslint/tests/endWithDot.ts rename to eslint/tests/endWithDot.test.ts index a4c0aa77aa..443f3105da 100644 --- a/eslint/tests/endWithDot.ts +++ b/eslint/tests/endWithDot.test.ts @@ -38,7 +38,7 @@ responses: simple: description: a number `, - errors: [{ messageId: 'descriptionNoDot' }], + errors: [{ messageId: 'endWithDot' }], output: ` simple: description: a number. @@ -51,7 +51,7 @@ multi: Multiline comment on description `, - errors: [{ messageId: 'descriptionNoDot' }], + errors: [{ messageId: 'endWithDot' }], output: ` multi: description: > diff --git a/eslint/tests/outOfLineEnum.test.ts b/eslint/tests/outOfLineRule.test.ts similarity index 79% rename from eslint/tests/outOfLineEnum.test.ts rename to eslint/tests/outOfLineRule.test.ts index 22db0e3ef1..9463b413d6 100644 --- a/eslint/tests/outOfLineEnum.test.ts +++ b/eslint/tests/outOfLineRule.test.ts @@ -1,12 +1,13 @@ import { RuleTester } from 'eslint'; -import { outOfLineEnum } from '../src/rules/outOfLineEnum'; +import { createOutOfLineRule } from '../src/rules/outOfLineRule'; const ruleTester = new RuleTester({ parser: require.resolve('yaml-eslint-parser'), }); -ruleTester.run('out-of-line-enum', outOfLineEnum, { +// this test is enough for oneOf, allOf, anyOf, as they use the same rule. +ruleTester.run('out-of-line-enum', createOutOfLineRule({ property: 'enum' }), { valid: [ ` simple: diff --git a/scripts/ci/githubActions/setRunVariables.ts b/scripts/ci/githubActions/setRunVariables.ts index ea7760041a..784a20ba5c 100644 --- a/scripts/ci/githubActions/setRunVariables.ts +++ b/scripts/ci/githubActions/setRunVariables.ts @@ -23,7 +23,7 @@ export const COMMON_DEPENDENCIES = { '.github/workflows', '.github/.cache_version', ], - SCRIPTS_CHANGED: ['scripts'], + SCRIPTS_CHANGED: ['scripts', 'eslint'], COMMON_SPECS_CHANGED: ['specs/common'], }; diff --git a/website/docs/automation/add-new-language.md b/website/docs/automation/add-new-language.md index f49ba1c275..b6aeb9f385 100644 --- a/website/docs/automation/add-new-language.md +++ b/website/docs/automation/add-new-language.md @@ -109,6 +109,8 @@ Algolia for Java (5.0.0); Search (5.0.0); JVM (11.0.14); experimental You can take a look at the Java implementation [here](https://github.com/algolia/api-clients-automation/pull/347). +The function must be named `addAlgoliaAgent` because of JavaScript exception that doesn't allow custom `User-Agent` in the header, and must use `x-algolia-agent`. + ### Dependencies You can use any dependency you want to create the client, it can be Json parser or HTTP client, but it's important to never expose those dependencies through the client, meaning: