Skip to content

Commit 6361b60

Browse files
authored
style(specs): add out-of-line-one-of rule (and allOf and anyOf) APIC-418 (#512)
1 parent c971410 commit 6361b60

File tree

8 files changed

+75
-52
lines changed

8 files changed

+75
-52
lines changed

.eslintrc.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
2-
ignorePatterns: ['.eslintrc.js', '**/node_modules', '**/build', '**/dist', '**/target', '**/.yarn'],
2+
ignorePatterns: ['.eslintrc.js', '**/node_modules', '**/build', '**/dist', '**/target', '**/.yarn', 'specs/bundled/*.doc.yml'],
33

44
overrides: [
55
{
@@ -51,6 +51,10 @@ module.exports = {
5151
files: ['!specs/bundled/*.yml'],
5252
rules: {
5353
"automation-custom/out-of-line-enum": "error",
54+
"automation-custom/out-of-line-one-of": "error",
55+
"automation-custom/out-of-line-all-of": "error",
56+
"automation-custom/out-of-line-any-of": "error",
57+
5458
}
5559
}
5660
]

eslint/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { endWithDot } from './rules/endWithDot';
2-
import { outOfLineEnum } from './rules/outOfLineEnum';
2+
import { createOutOfLineRule } from './rules/outOfLineRule';
33
import { singleQuoteRef } from './rules/singleQuoteRef';
44

55
const rules = {
66
'end-with-dot': endWithDot,
7-
'out-of-line-enum': outOfLineEnum,
7+
'out-of-line-enum': createOutOfLineRule({ property: 'enum' }),
8+
'out-of-line-one-of': createOutOfLineRule({ property: 'oneOf' }),
9+
'out-of-line-all-of': createOutOfLineRule({ property: 'allOf' }),
10+
'out-of-line-any-of': createOutOfLineRule({ property: 'anyOf' }),
811
'single-quote-ref': singleQuoteRef,
912
};
1013

eslint/src/rules/outOfLineEnum.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

eslint/src/rules/outOfLineRule.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import type { Rule } from 'eslint';
2+
3+
import { isPairWithKey } from '../utils';
4+
5+
export function createOutOfLineRule({
6+
property,
7+
description = `${property} must be out of line, not nested inside properties`,
8+
messageId = `${property}NotOutOfLine`,
9+
message = `${property} must be out of line`,
10+
}: {
11+
property: string;
12+
description?: string;
13+
messageId?: string;
14+
message?: string;
15+
}): Rule.RuleModule {
16+
const rule: Rule.RuleModule = {
17+
meta: {
18+
docs: {
19+
description,
20+
},
21+
messages: {
22+
[messageId]: message,
23+
},
24+
},
25+
create(context) {
26+
if (!context.parserServices.isYAML) {
27+
return {};
28+
}
29+
30+
return {
31+
YAMLPair(node): void {
32+
if (!isPairWithKey(node, property)) {
33+
return;
34+
}
35+
// parent is mapping, and parent is real parent that must be to the far left
36+
if (node.parent.parent.loc.start.column === 0) {
37+
return;
38+
}
39+
// accept anything in servers
40+
if (
41+
isPairWithKey(
42+
node.parent.parent.parent.parent?.parent?.parent?.parent ?? null,
43+
'servers'
44+
)
45+
) {
46+
return;
47+
}
48+
context.report({
49+
node: node.parent.parent as any,
50+
messageId,
51+
});
52+
},
53+
};
54+
},
55+
};
56+
return rule;
57+
}

eslint/tests/endWithDot.ts renamed to eslint/tests/endWithDot.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ responses:
3838
simple:
3939
description: a number
4040
`,
41-
errors: [{ messageId: 'descriptionNoDot' }],
41+
errors: [{ messageId: 'endWithDot' }],
4242
output: `
4343
simple:
4444
description: a number.
@@ -51,7 +51,7 @@ multi:
5151
Multiline comment
5252
on description
5353
`,
54-
errors: [{ messageId: 'descriptionNoDot' }],
54+
errors: [{ messageId: 'endWithDot' }],
5555
output: `
5656
multi:
5757
description: >

eslint/tests/outOfLineEnum.test.ts renamed to eslint/tests/outOfLineRule.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { RuleTester } from 'eslint';
22

3-
import { outOfLineEnum } from '../src/rules/outOfLineEnum';
3+
import { createOutOfLineRule } from '../src/rules/outOfLineRule';
44

55
const ruleTester = new RuleTester({
66
parser: require.resolve('yaml-eslint-parser'),
77
});
88

9-
ruleTester.run('out-of-line-enum', outOfLineEnum, {
9+
// this test is enough for oneOf, allOf, anyOf, as they use the same rule.
10+
ruleTester.run('out-of-line-enum', createOutOfLineRule({ property: 'enum' }), {
1011
valid: [
1112
`
1213
simple:

scripts/ci/githubActions/setRunVariables.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const COMMON_DEPENDENCIES = {
2323
'.github/workflows',
2424
'.github/.cache_version',
2525
],
26-
SCRIPTS_CHANGED: ['scripts'],
26+
SCRIPTS_CHANGED: ['scripts', 'eslint'],
2727
COMMON_SPECS_CHANGED: ['specs/common'],
2828
};
2929

website/docs/automation/add-new-language.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ Algolia for Java (5.0.0); Search (5.0.0); JVM (11.0.14); experimental
109109

110110
You can take a look at the Java implementation [here](https://github.com/algolia/api-clients-automation/pull/347).
111111

112+
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`.
113+
112114
### Dependencies
113115

114116
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:

0 commit comments

Comments
 (0)