|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +import { ESLintUtils } from '@typescript-eslint/utils' |
| 20 | + |
| 21 | +export const checkVariants = ESLintUtils.RuleCreator.withoutDocs({ |
| 22 | + name: 'check-variants', |
| 23 | + meta: { |
| 24 | + type: 'problem', |
| 25 | + docs: { |
| 26 | + description: 'Checks that variants are allowed on a given type.', |
| 27 | + recommended: 'error' |
| 28 | + }, |
| 29 | + messages: { |
| 30 | + variantsOnRequestOrResponse: |
| 31 | + 'Variants are not allowed on {{ className }} classes.', |
| 32 | + interfaceWithNonContainerVariants: |
| 33 | + "Interface '{{ interfaceName }}' has '@variants {{ variantValue }}' but only 'container' is allowed for interfaces.", |
| 34 | + invalidVariantsTag: |
| 35 | + "Type alias '{{ typeName }}' has invalid '@variants {{ variantValue }}'. Must start with: {{ allowedValues }}." |
| 36 | + } |
| 37 | + }, |
| 38 | + defaultOptions: [], |
| 39 | + create(context) { |
| 40 | + const sourceCode = context.sourceCode || context.getSourceCode() |
| 41 | + |
| 42 | + const getJsDocTags = (node) => { |
| 43 | + const targetNode = |
| 44 | + node.parent?.type === 'ExportNamedDeclaration' ? node.parent : node |
| 45 | + const comments = sourceCode.getCommentsBefore(targetNode) |
| 46 | + |
| 47 | + const jsDocComment = comments |
| 48 | + ?.filter( |
| 49 | + (comment) => comment.type === 'Block' && comment.value.startsWith('*') |
| 50 | + ) |
| 51 | + .pop() |
| 52 | + |
| 53 | + if (!jsDocComment) return [] |
| 54 | + |
| 55 | + return jsDocComment.value |
| 56 | + .split('\n') |
| 57 | + .map((line) => line.trim().match(/^\*?\s*@(\w+)(?:\s+(.*))?$/)) |
| 58 | + .filter(Boolean) |
| 59 | + .map(([, tag, value]) => ({ tag, value: value?.trim() || '' })) |
| 60 | + } |
| 61 | + |
| 62 | + const isRequestOrResponse = (name) => |
| 63 | + name === 'Request' || name === 'Response' |
| 64 | + |
| 65 | + const hasVariantsTag = (tags) => tags.some(({ tag }) => tag === 'variants') |
| 66 | + |
| 67 | + return { |
| 68 | + 'TSInterfaceDeclaration, ClassDeclaration'(node) { |
| 69 | + const jsDocTags = getJsDocTags(node) |
| 70 | + if (isRequestOrResponse(node.id.name)) { |
| 71 | + if (hasVariantsTag(jsDocTags)) { |
| 72 | + context.report({ |
| 73 | + node, |
| 74 | + messageId: 'variantsOnRequestOrResponse', |
| 75 | + data: { className: node.id.name } |
| 76 | + }) |
| 77 | + } |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + const nonContainerVariant = jsDocTags.find( |
| 82 | + ({ tag, value }) => tag === 'variants' && value !== 'container' |
| 83 | + ) |
| 84 | + if (nonContainerVariant) { |
| 85 | + context.report({ |
| 86 | + node, |
| 87 | + messageId: 'interfaceWithNonContainerVariants', |
| 88 | + data: { |
| 89 | + interfaceName: node.id.name, |
| 90 | + variantValue: nonContainerVariant.value |
| 91 | + } |
| 92 | + }) |
| 93 | + return |
| 94 | + } |
| 95 | + }, |
| 96 | + TSTypeAliasDeclaration(node) { |
| 97 | + const jsDocTags = getJsDocTags(node) |
| 98 | + const allowedVariants = ['internal', 'typed_keys_quirk', 'untagged'] |
| 99 | + |
| 100 | + const invalidVariant = jsDocTags.find( |
| 101 | + ({ tag, value }) => |
| 102 | + tag === 'variants' && |
| 103 | + !allowedVariants.some((allowed) => value.startsWith(allowed)) |
| 104 | + ) |
| 105 | + |
| 106 | + if (invalidVariant) { |
| 107 | + context.report({ |
| 108 | + node, |
| 109 | + messageId: 'invalidVariantsTag', |
| 110 | + data: { |
| 111 | + typeName: node.id.name, |
| 112 | + variantValue: invalidVariant.value, |
| 113 | + allowedValues: allowedVariants.join(', ') |
| 114 | + } |
| 115 | + }) |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +}) |
| 121 | + |
| 122 | +export default checkVariants |
0 commit comments