|
| 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 | +}; |
0 commit comments