Skip to content

Commit fbcace1

Browse files
committed
yet another linter rule
1 parent 110aa81 commit fbcace1

File tree

7 files changed

+58
-10
lines changed

7 files changed

+58
-10
lines changed

eslint/src/rules/outOfLineRule.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { RuleModule } from 'eslint-plugin-yml/lib/types.js';
22
import { createRule } from 'eslint-plugin-yml/lib/utils';
33

4-
import { isNullable, isPairWithKey } from '../utils.js';
4+
import { isBlockScalar, isMapping, isNullable, isPairWithKey, isScalar } from '../utils.js';
55

66
export function createOutOfLineRule({
77
property,
@@ -24,6 +24,8 @@ export function createOutOfLineRule({
2424
},
2525
messages: {
2626
[messageId]: message,
27+
nullDescription: 'description must not be present for `null` type',
28+
descriptionLevel: 'description must not be next to the property',
2729
},
2830
type: 'layout',
2931
schema: [],
@@ -38,6 +40,29 @@ export function createOutOfLineRule({
3840
if (!isPairWithKey(node, property)) {
3941
return;
4042
}
43+
44+
// the 'null' must not have a description otherwise it will generate a model for it
45+
if (
46+
property === 'oneOf' &&
47+
isNullable(node.value) &&
48+
node.value.entries.some(
49+
(entry) =>
50+
isMapping(entry) &&
51+
isPairWithKey(entry.pairs[0], 'type') &&
52+
isScalar(entry.pairs[0].value) &&
53+
!isBlockScalar(entry.pairs[0].value) &&
54+
entry.pairs[0].value.raw === "'null'" &&
55+
entry.pairs.length > 1,
56+
)
57+
) {
58+
context.report({
59+
node: node.value,
60+
messageId: 'nullDescription',
61+
});
62+
63+
return;
64+
}
65+
4166
// parent is mapping, and parent is real parent that must be to the far left
4267
if (node.parent.parent.loc.start.column === 0) {
4368
return;

eslint/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function isPairWithValue(node: AST.YAMLNode | null, value: string): node
3333
return isScalar(node.value) && node.value.value === value;
3434
}
3535

36-
export function isNullable(node: AST.YAMLNode | null): boolean {
36+
export function isNullable(node: AST.YAMLNode | null): node is AST.YAMLSequence {
3737
return (
3838
isSequence(node) &&
3939
node.entries.some(

eslint/tests/outOfLineRule.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,32 @@ simple:
105105
`,
106106
errors: [{ messageId: 'oneOfNotOutOfLine' }],
107107
},
108+
{
109+
code: `
110+
simple:
111+
type: object
112+
properties:
113+
name:
114+
oneOf:
115+
- type: string
116+
description: bla bla bla
117+
- type: 'null'
118+
description: bla bla bla
119+
`,
120+
errors: [{ messageId: 'nullDescription' }],
121+
},
122+
{
123+
code: `
124+
root:
125+
oneOf:
126+
oneOf:
127+
- type: string
128+
description: bla bla bla
129+
- type: 'null'
130+
description: bla bla bla
131+
`,
132+
errors: [{ messageId: 'nullDescription' }],
133+
},
108134
],
109135
},
110136
{

mise.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[tools]
2+
node = "22.13.1"

specs/abtesting/common/schemas/ABTest.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
ABTests:
22
oneOf:
33
- type: array
4-
description: A/B tests.
4+
description: The list of A/B tests, null if no A/B tests are configured for this application.
55
items:
66
$ref: '#/ABTest'
77
- type: 'null'
8-
description: No A/B tests are configured for this application.
98

109
ABTest:
1110
type: object

specs/common/responses/common.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@ updatedAt:
3333
description: Date and time when the object was updated, in RFC 3339 format.
3434

3535
updatedAtNullable:
36-
default: null
3736
oneOf:
3837
- type: string
38+
default: null
39+
description: Date and time when the object was updated, in RFC 3339 format.
3940
example: 2023-07-04T12:49:15Z
40-
description: |
41-
Date and time when the object was updated, in RFC 3339 format.
4241
- type: 'null'
43-
description: If null, this object wasn't updated yet.
4442

4543
deletedAt:
4644
type: string

specs/crawler/common/schemas/getCrawlerResponse.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,12 @@ BaseResponse:
4141
description: Date and time when the last crawl started, in RFC 3339 format.
4242
example: 2024-04-07T09:16:04Z
4343
- type: 'null'
44-
description: If null, this crawler hasn't indexed anything yet.
4544
lastReindexEndedAt:
4645
default: null
4746
oneOf:
4847
- type: string
4948
description: Date and time when the last crawl finished, in RFC 3339 format.
5049
- type: 'null'
51-
description: If null, this crawler hasn't indexed anything yet.
5250
required:
5351
- name
5452
- createdAt

0 commit comments

Comments
 (0)