|
| 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, TSESTree } from '@typescript-eslint/utils'; |
| 20 | + |
| 21 | +const createRule = ESLintUtils.RuleCreator(name => `https://example.com/rule/${name}`) |
| 22 | + |
| 23 | +/** |
| 24 | + * Check if a type node appears to reference a class type |
| 25 | + * This is a heuristic - we check if the type name starts with uppercase |
| 26 | + * (common convention for classes) and is not a known primitive/utility type |
| 27 | + */ |
| 28 | +function isLikelyClassType(typeNode) { |
| 29 | + if (typeNode.type === 'TSTypeReference' && typeNode.typeName.type === 'Identifier') { |
| 30 | + const name = typeNode.typeName.name; |
| 31 | + |
| 32 | + // Known non-class types |
| 33 | + const knownNonClassTypes = [ |
| 34 | + // Primitives and spec aliases |
| 35 | + 'string', 'number', 'boolean', 'null', 'undefined', |
| 36 | + 'integer', 'long', 'short', 'byte', 'float', 'double', 'uint', 'ulong', |
| 37 | + // Utility types |
| 38 | + 'Array', 'Dictionary', 'SingleKeyDictionary', 'UserDefinedValue', |
| 39 | + // Common aliases |
| 40 | + 'Field', 'Id', 'IndexName', 'Name' |
| 41 | + ]; |
| 42 | + |
| 43 | + if (knownNonClassTypes.includes(name)) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + |
| 47 | + // Heuristic: Class types typically start with uppercase and contain at least one lowercase |
| 48 | + // This catches: QueryContainer, BoolQuery, etc. |
| 49 | + // But not: URL, ID, HTTP, etc. (all caps - likely aliases) |
| 50 | + const hasUpperStart = /^[A-Z]/.test(name); |
| 51 | + const hasLowercase = /[a-z]/.test(name); |
| 52 | + |
| 53 | + return hasUpperStart && hasLowercase; |
| 54 | + } |
| 55 | + |
| 56 | + return false; |
| 57 | +} |
| 58 | + |
| 59 | +export default createRule({ |
| 60 | + name: 'prefer-tagged-variants', |
| 61 | + create(context) { |
| 62 | + return { |
| 63 | + TSUnionType(node) { |
| 64 | + const parent = node.parent; |
| 65 | + |
| 66 | + const isPropertyAnnotation = |
| 67 | + parent?.type === 'TSTypeAnnotation' && |
| 68 | + parent.parent?.type === 'PropertyDefinition'; |
| 69 | + |
| 70 | + const isInterfaceProperty = |
| 71 | + parent?.type === 'TSTypeAnnotation' && |
| 72 | + parent.parent?.type === 'TSPropertySignature'; |
| 73 | + |
| 74 | + if (!isPropertyAnnotation && !isInterfaceProperty) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + const allMembersAreClasses = node.types.every(typeNode => { |
| 79 | + if (typeNode.type === 'TSArrayType') { |
| 80 | + return isLikelyClassType(typeNode.elementType); |
| 81 | + } |
| 82 | + return isLikelyClassType(typeNode); |
| 83 | + }); |
| 84 | + |
| 85 | + if (allMembersAreClasses && node.types.length >= 2) { |
| 86 | + context.report({ |
| 87 | + node, |
| 88 | + messageId: 'preferTaggedVariants', |
| 89 | + data: { |
| 90 | + suggestion: 'Use tagged variants with @variants internal or @variants container (external). See modeling guide: https://github.com/elastic/elasticsearch-specification/blob/main/docs/modeling-guide.md#variants' |
| 91 | + } |
| 92 | + }) |
| 93 | + } |
| 94 | + }, |
| 95 | + } |
| 96 | + }, |
| 97 | + meta: { |
| 98 | + docs: { |
| 99 | + description: 'Union of class types should use tagged variants (internal or external) instead of inline unions for better code generation in statically-typed languages.', |
| 100 | + }, |
| 101 | + messages: { |
| 102 | + preferTaggedVariants: 'Union of class types is not allowed. {{suggestion}}.' |
| 103 | + }, |
| 104 | + type: 'problem', |
| 105 | + schema: [] |
| 106 | + }, |
| 107 | + defaultOptions: [] |
| 108 | +}) |
| 109 | + |
0 commit comments