Skip to content

Commit

Permalink
style(specs): add out-of-line-one-of rule (and allOf and anyOf) APIC-418
Browse files Browse the repository at this point in the history
 (#512)
  • Loading branch information
millotp authored May 19, 2022
1 parent c971410 commit 6361b60
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 52 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -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: [
{
Expand Down Expand Up @@ -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",

}
}
]
Expand Down
7 changes: 5 additions & 2 deletions eslint/src/index.ts
Original file line number Diff line number Diff line change
@@ -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,
};

Expand Down
44 changes: 0 additions & 44 deletions eslint/src/rules/outOfLineEnum.ts

This file was deleted.

57 changes: 57 additions & 0 deletions eslint/src/rules/outOfLineRule.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ responses:
simple:
description: a number
`,
errors: [{ messageId: 'descriptionNoDot' }],
errors: [{ messageId: 'endWithDot' }],
output: `
simple:
description: a number.
Expand All @@ -51,7 +51,7 @@ multi:
Multiline comment
on description
`,
errors: [{ messageId: 'descriptionNoDot' }],
errors: [{ messageId: 'endWithDot' }],
output: `
multi:
description: >
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion scripts/ci/githubActions/setRunVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
};

Expand Down
2 changes: 2 additions & 0 deletions website/docs/automation/add-new-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 6361b60

Please sign in to comment.