Skip to content

Commit

Permalink
yet another linter rule
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp committed Jan 27, 2025
1 parent 110aa81 commit fbcace1
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 10 deletions.
27 changes: 26 additions & 1 deletion eslint/src/rules/outOfLineRule.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { RuleModule } from 'eslint-plugin-yml/lib/types.js';
import { createRule } from 'eslint-plugin-yml/lib/utils';

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

export function createOutOfLineRule({
property,
Expand All @@ -24,6 +24,8 @@ export function createOutOfLineRule({
},
messages: {
[messageId]: message,
nullDescription: 'description must not be present for `null` type',
descriptionLevel: 'description must not be next to the property',
},
type: 'layout',
schema: [],
Expand All @@ -38,6 +40,29 @@ export function createOutOfLineRule({
if (!isPairWithKey(node, property)) {
return;
}

// the 'null' must not have a description otherwise it will generate a model for it
if (
property === 'oneOf' &&
isNullable(node.value) &&
node.value.entries.some(
(entry) =>
isMapping(entry) &&
isPairWithKey(entry.pairs[0], 'type') &&
isScalar(entry.pairs[0].value) &&
!isBlockScalar(entry.pairs[0].value) &&
entry.pairs[0].value.raw === "'null'" &&
entry.pairs.length > 1,
)
) {
context.report({
node: node.value,
messageId: 'nullDescription',
});

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;
Expand Down
2 changes: 1 addition & 1 deletion eslint/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function isPairWithValue(node: AST.YAMLNode | null, value: string): node
return isScalar(node.value) && node.value.value === value;
}

export function isNullable(node: AST.YAMLNode | null): boolean {
export function isNullable(node: AST.YAMLNode | null): node is AST.YAMLSequence {
return (
isSequence(node) &&
node.entries.some(
Expand Down
26 changes: 26 additions & 0 deletions eslint/tests/outOfLineRule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,32 @@ simple:
`,
errors: [{ messageId: 'oneOfNotOutOfLine' }],
},
{
code: `
simple:
type: object
properties:
name:
oneOf:
- type: string
description: bla bla bla
- type: 'null'
description: bla bla bla
`,
errors: [{ messageId: 'nullDescription' }],
},
{
code: `
root:
oneOf:
oneOf:
- type: string
description: bla bla bla
- type: 'null'
description: bla bla bla
`,
errors: [{ messageId: 'nullDescription' }],
},
],
},
{
Expand Down
2 changes: 2 additions & 0 deletions mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
node = "22.13.1"
3 changes: 1 addition & 2 deletions specs/abtesting/common/schemas/ABTest.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
ABTests:
oneOf:
- type: array
description: A/B tests.
description: The list of A/B tests, null if no A/B tests are configured for this application.
items:
$ref: '#/ABTest'
- type: 'null'
description: No A/B tests are configured for this application.

ABTest:
type: object
Expand Down
6 changes: 2 additions & 4 deletions specs/common/responses/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ updatedAt:
description: Date and time when the object was updated, in RFC 3339 format.

updatedAtNullable:
default: null
oneOf:
- type: string
default: null
description: Date and time when the object was updated, in RFC 3339 format.
example: 2023-07-04T12:49:15Z
description: |
Date and time when the object was updated, in RFC 3339 format.
- type: 'null'
description: If null, this object wasn't updated yet.

deletedAt:
type: string
Expand Down
2 changes: 0 additions & 2 deletions specs/crawler/common/schemas/getCrawlerResponse.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ BaseResponse:
description: Date and time when the last crawl started, in RFC 3339 format.
example: 2024-04-07T09:16:04Z
- type: 'null'
description: If null, this crawler hasn't indexed anything yet.
lastReindexEndedAt:
default: null
oneOf:
- type: string
description: Date and time when the last crawl finished, in RFC 3339 format.
- type: 'null'
description: If null, this crawler hasn't indexed anything yet.
required:
- name
- createdAt
Expand Down

0 comments on commit fbcace1

Please sign in to comment.