Skip to content

Commit e179701

Browse files
authored
fix(specs): proper title with linter (#3444)
1 parent 3e90919 commit e179701

File tree

74 files changed

+313
-94
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+313
-94
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ module.exports = {
6565
'automation-custom/out-of-line-any-of': 'error',
6666
'automation-custom/valid-acl': 'error',
6767
'automation-custom/ref-common': 'error',
68+
'automation-custom/valid-inline-title': 'error',
6869
},
6970
},
7071
],

eslint/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { createOutOfLineRule } from './rules/outOfLineRule';
55
import { refCommon } from './rules/refCommon';
66
import { singleQuoteRef } from './rules/singleQuoteRef';
77
import { validACL } from './rules/validACL';
8+
import { validInlineTitle } from './rules/validInlineTitle';
89

910
const rules = {
1011
'end-with-dot': endWithDot,
@@ -17,6 +18,7 @@ const rules = {
1718
'valid-acl': validACL,
1819
'no-new-line': noNewLine,
1920
'ref-common': refCommon,
21+
'valid-inline-title': validInlineTitle,
2022
};
2123

2224
// Custom parser for ESLint, to read plain text file like mustache.

eslint/src/rules/outOfLineRule.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import type { Rule } from 'eslint';
2-
import type { AST } from 'yaml-eslint-parser';
32

4-
import {
5-
isBlockScalar,
6-
isMapping,
7-
isPairWithKey,
8-
isScalar,
9-
isSequence,
10-
} from '../utils';
3+
import { isNullable, isPairWithKey } from '../utils';
114

125
export function createOutOfLineRule({
136
property,
@@ -74,17 +67,3 @@ export function createOutOfLineRule({
7467
};
7568
return rule;
7669
}
77-
78-
function isNullable(node: AST.YAMLNode | null): boolean {
79-
return (
80-
isSequence(node) &&
81-
node.entries.some(
82-
(entry) =>
83-
isMapping(entry) &&
84-
isPairWithKey(entry.pairs[0], 'type') &&
85-
isScalar(entry.pairs[0].value) &&
86-
!isBlockScalar(entry.pairs[0].value) &&
87-
entry.pairs[0].value.raw === "'null'"
88-
)
89-
);
90-
}

eslint/src/rules/validInlineTitle.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import type { Rule } from 'eslint';
2+
3+
import { isNullable, isPairWithKey } from '../utils';
4+
5+
export const validInlineTitle: Rule.RuleModule = {
6+
meta: {
7+
docs: {
8+
description:
9+
'title must be set in inline models, should be the first property and start with a lowercase',
10+
},
11+
messages: {
12+
inlineTitleExists: 'title must be set in inline models',
13+
lowercaseTitle: 'title must start with a lowercase',
14+
firstProperty: 'title must be the first property',
15+
noSpaceInTitle: 'title must not contain spaces',
16+
},
17+
},
18+
create(context) {
19+
if (!context.sourceCode.parserServices.isYAML) {
20+
return {};
21+
}
22+
23+
return {
24+
YAMLPair(node): void {
25+
if (
26+
!isPairWithKey(node, 'type') ||
27+
node.value?.type !== 'YAMLScalar' ||
28+
node.value.value !== 'object'
29+
) {
30+
return;
31+
}
32+
33+
// we don't enforce it for root level object
34+
if (node.parent.parent.loc.start.column === 0) {
35+
return;
36+
}
37+
38+
// make sure title starts with a lowercase
39+
const title = node.parent.pairs.find((pair) =>
40+
isPairWithKey(pair, 'title')
41+
);
42+
const titleNode = title?.value;
43+
const titleValue = (titleNode as any)?.value as string;
44+
if (
45+
titleNode &&
46+
(titleNode.type !== 'YAMLScalar' || !/^[a-z]/.test(titleValue))
47+
) {
48+
context.report({
49+
node: title,
50+
messageId: 'lowercaseTitle',
51+
});
52+
}
53+
54+
// make sure title doesn't contain spaces
55+
if (titleValue?.includes(' ')) {
56+
context.report({
57+
node: title,
58+
messageId: 'noSpaceInTitle',
59+
});
60+
}
61+
62+
// if there are no properties, we don't need a title
63+
const properties = node.parent.pairs.find((pair) =>
64+
isPairWithKey(pair, 'properties')
65+
);
66+
if (!properties) {
67+
return;
68+
}
69+
70+
// allow it on nullable objects
71+
if (
72+
isPairWithKey(node.parent.parent.parent, 'oneOf') &&
73+
isNullable(node.parent.parent.parent.value)
74+
) {
75+
return;
76+
}
77+
78+
// allow on allOf too, since they are not generated
79+
if (isPairWithKey(node.parent.parent.parent, 'allOf')) {
80+
return;
81+
}
82+
83+
// make sure the title is set on the same object
84+
if (!title) {
85+
context.report({
86+
node: node.value,
87+
messageId: 'inlineTitleExists',
88+
});
89+
90+
return;
91+
}
92+
93+
// make sure title is the first property
94+
if (!isPairWithKey(node.parent.pairs[0], 'title')) {
95+
context.report({
96+
node: title,
97+
messageId: 'firstProperty',
98+
});
99+
}
100+
},
101+
};
102+
},
103+
};

eslint/src/utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,17 @@ export function isPairWithKey(
2929
return false;
3030
return isScalar(node.key) && node.key.value === key;
3131
}
32+
33+
export function isNullable(node: AST.YAMLNode | null): boolean {
34+
return (
35+
isSequence(node) &&
36+
node.entries.some(
37+
(entry) =>
38+
isMapping(entry) &&
39+
isPairWithKey(entry.pairs[0], 'type') &&
40+
isScalar(entry.pairs[0].value) &&
41+
!isBlockScalar(entry.pairs[0].value) &&
42+
entry.pairs[0].value.raw === "'null'"
43+
)
44+
);
45+
}

eslint/tests/validInlineTitle.test.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { RuleTester } from 'eslint';
2+
3+
import { validInlineTitle } from '../src/rules/validInlineTitle';
4+
5+
const ruleTester = new RuleTester({
6+
parser: require.resolve('yaml-eslint-parser'),
7+
});
8+
9+
ruleTester.run('valid-inline-title', validInlineTitle, {
10+
valid: [
11+
`
12+
currencies:
13+
type: object
14+
properties:
15+
inner:
16+
type: object
17+
`,
18+
`
19+
currencies:
20+
type: object
21+
properties:
22+
inner:
23+
title: currency
24+
type: object
25+
properties:
26+
currency:
27+
type: string
28+
title: Currency
29+
`,
30+
`
31+
dictionaryLanguage:
32+
oneOf:
33+
- type: object
34+
properties:
35+
prop:
36+
type: integer
37+
- type: 'null'
38+
`,
39+
],
40+
invalid: [
41+
{
42+
code: `
43+
currencies:
44+
type: object
45+
properties:
46+
inner:
47+
type: object
48+
properties:
49+
currency:
50+
type: string
51+
title: Currency
52+
`,
53+
errors: [{ messageId: 'inlineTitleExists' }],
54+
},
55+
{
56+
code: `
57+
currencies:
58+
type: object
59+
properties:
60+
inner:
61+
type: object
62+
title: currency
63+
properties:
64+
currency:
65+
type: string
66+
title: Currency
67+
`,
68+
errors: [{ messageId: 'firstProperty' }],
69+
},
70+
{
71+
code: `
72+
currencies:
73+
title: UpperCaseFine
74+
type: object
75+
properties:
76+
inner:
77+
title: UpperCaseNotFine
78+
type: object
79+
`,
80+
errors: [{ messageId: 'lowercaseTitle' }],
81+
},
82+
{
83+
code: `
84+
currencies:
85+
title: spaces are fine
86+
type: object
87+
properties:
88+
inner:
89+
title: spaces are not fine
90+
type: object
91+
`,
92+
errors: [{ messageId: 'noSpaceInTitle' }],
93+
},
94+
],
95+
});

generators/src/main/java/com/algolia/codegen/AlgoliaSwiftGenerator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public class AlgoliaSwiftGenerator extends Swift5ClientCodegen {
6666
"facetordering",
6767
"facets",
6868
"facetsstats",
69+
"forbidden",
6970
"highlightresult",
7071
"highlightresultoption",
7172
"ignoreplurals",
@@ -83,10 +84,11 @@ public class AlgoliaSwiftGenerator extends Swift5ClientCodegen {
8384
"promoteobjectids",
8485
"querysuggestionsconfiguration",
8586
"querytype",
87+
"range",
8688
"rankinginfo",
8789
"redirect",
8890
"redirectruleindexmetadata",
89-
"redirectruleindexmetadatadata",
91+
"redirectruleindexdata",
9092
"redirecturl",
9193
"region",
9294
"removestopwords",

scripts/formatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function formatter(language: string, cwd: string): Promise<void> {
2929
break;
3030
case 'java':
3131
await run(
32-
`find . -type f -name "*.java" | xargs java --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
32+
`find . -path ./.gradle -prune -o -type f -name "*.java" | xargs java --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
3333
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
3434
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
3535
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \

specs/abtesting/common/parameters.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ filterEffects:
9696
description: A/B test filter effects resulting from configuration settings.
9797
properties:
9898
outliers:
99+
title: outliersFilter
99100
type: object
100101
description: Outliers removed from the A/B test as a result of configuration settings.
101102
example:
@@ -111,6 +112,7 @@ filterEffects:
111112
description: Number of tracked searches removed from the A/B test.
112113
example: 237
113114
emptySearch:
115+
title: emptySearchFilter
114116
type: object
115117
description: Empty searches removed from the A/B test as a result of configuration settings.
116118
example:

specs/analytics/common/parameters.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ clickPositions:
117117
minItems: 12
118118
maxItems: 12
119119
items:
120+
title: clickPosition
120121
type: object
121122
description: Click position.
122123
properties:
@@ -242,10 +243,12 @@ purchaseCount:
242243
example: 10
243244

244245
currencies:
246+
title: currenciesValue
245247
type: object
246248
description: Revenue associated with this search, broken-down by currencies.
247249
default: {}
248250
additionalProperties:
251+
title: currencyCode
249252
x-additionalPropertiesName: currency
250253
type: object
251254
description: Currency code.

0 commit comments

Comments
 (0)