|
16 | 16 | * specific language governing permissions and limitations |
17 | 17 | * under the License. |
18 | 18 | */ |
19 | | -import { ESLintUtils } from '@typescript-eslint/utils'; |
| 19 | +import { ESLintUtils } from '@typescript-eslint/utils' |
20 | 20 |
|
21 | | -const createRule = ESLintUtils.RuleCreator(name => `https://example.com/rule/${name}`) |
| 21 | +const createRule = ESLintUtils.RuleCreator( |
| 22 | + (name) => `https://example.com/rule/${name}` |
| 23 | +) |
22 | 24 |
|
23 | 25 | export default createRule({ |
24 | 26 | name: 'no-variants-on-responses', |
25 | 27 | create(context) { |
| 28 | + const sourceCode = context.sourceCode || context.getSourceCode() |
| 29 | + |
| 30 | + const getJsDocTags = (node) => { |
| 31 | + const targetNode = |
| 32 | + node.parent?.type === 'ExportNamedDeclaration' ? node.parent : node |
| 33 | + const comments = sourceCode.getCommentsBefore(targetNode) |
| 34 | + |
| 35 | + const jsDocComment = comments |
| 36 | + ?.filter( |
| 37 | + (comment) => comment.type === 'Block' && comment.value.startsWith('*') |
| 38 | + ) |
| 39 | + .pop() |
| 40 | + |
| 41 | + if (!jsDocComment) return [] |
| 42 | + |
| 43 | + return jsDocComment.value |
| 44 | + .split('\n') |
| 45 | + .map((line) => line.trim().match(/^\*?\s*@(\w+)(?:\s+(.*))?$/)) |
| 46 | + .filter(Boolean) |
| 47 | + .map(([, tag, value]) => ({ tag, value: value?.trim() || '' })) |
| 48 | + } |
| 49 | + |
26 | 50 | return { |
27 | | - ClassDeclaration(node) { |
28 | | - const className = node.id?.name; |
29 | | - if (className !== 'Response' && className !== 'Request') { |
30 | | - return; |
| 51 | + 'TSInterfaceDeclaration, ClassDeclaration'(node) { |
| 52 | + const className = node.id?.name |
| 53 | + if (className === 'Response' || className === 'Request') { |
| 54 | + const fullText = sourceCode.text |
| 55 | + |
| 56 | + const nodeStart = node.range[0] |
| 57 | + const textBefore = fullText.substring( |
| 58 | + Math.max(0, nodeStart - 200), |
| 59 | + nodeStart |
| 60 | + ) |
| 61 | + |
| 62 | + const hasVariantsTag = |
| 63 | + /@variants\s+(container|internal|external|untagged)/.test( |
| 64 | + textBefore |
| 65 | + ) |
| 66 | + |
| 67 | + if (hasVariantsTag) { |
| 68 | + context.report({ |
| 69 | + node, |
| 70 | + messageId: 'noVariantsOnResponses', |
| 71 | + data: { |
| 72 | + className, |
| 73 | + suggestion: |
| 74 | + 'Move @variants to a separate body class and use value_body pattern with @codegen_name. See SearchResponse for an example.' |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | + return |
31 | 79 | } |
32 | | - |
33 | | - const sourceCode = context.sourceCode || context.getSourceCode(); |
34 | | - const fullText = sourceCode.text; |
35 | | - |
36 | | - const nodeStart = node.range[0]; |
37 | | - const textBefore = fullText.substring(Math.max(0, nodeStart - 200), nodeStart); |
38 | | - |
39 | | - const hasVariantsTag = /@variants\s+(container|internal|external|untagged)/.test(textBefore); |
40 | | - |
41 | | - if (hasVariantsTag) { |
| 80 | + |
| 81 | + const jsDocTags = getJsDocTags(node) |
| 82 | + |
| 83 | + const nonContainerVariant = jsDocTags.find( |
| 84 | + ({ tag, value }) => tag === 'variants' && value !== 'container' |
| 85 | + ) |
| 86 | + if (nonContainerVariant) { |
42 | 87 | context.report({ |
43 | 88 | node, |
44 | | - messageId: 'noVariantsOnResponses', |
| 89 | + messageId: 'interfaceWithNonContainerVariants', |
45 | 90 | data: { |
46 | | - className, |
47 | | - suggestion: 'Move @variants to a separate body class and use value_body pattern with @codegen_name. See SearchResponse for an example.' |
| 91 | + interfaceName: node.id.name, |
| 92 | + variantValue: nonContainerVariant.value |
48 | 93 | } |
49 | | - }); |
| 94 | + }) |
| 95 | + return |
50 | 96 | } |
51 | 97 | }, |
| 98 | + TSTypeAliasDeclaration(node) { |
| 99 | + const jsDocTags = getJsDocTags(node) |
| 100 | + const allowedVariants = ['internal', 'typed_keys_quirk', 'untagged'] |
| 101 | + |
| 102 | + const invalidVariant = jsDocTags.find( |
| 103 | + ({ tag, value }) => |
| 104 | + tag === 'variants' && |
| 105 | + !allowedVariants.some((allowed) => value.startsWith(allowed)) |
| 106 | + ) |
| 107 | + |
| 108 | + if (invalidVariant) { |
| 109 | + context.report({ |
| 110 | + node, |
| 111 | + messageId: 'invalidVariantsTag', |
| 112 | + data: { |
| 113 | + typeName: node.id.name, |
| 114 | + variantValue: invalidVariant.value, |
| 115 | + allowedValues: allowedVariants.join(', ') |
| 116 | + } |
| 117 | + }) |
| 118 | + } |
| 119 | + } |
52 | 120 | } |
53 | 121 | }, |
54 | 122 | meta: { |
55 | 123 | docs: { |
56 | | - description: '@variants is only supported on Interface types, not on Request or Response classes. Use value_body pattern instead.', |
| 124 | + description: |
| 125 | + '@variants is only supported on Interface types, not on Request or Response classes. Use value_body pattern instead.' |
57 | 126 | }, |
58 | 127 | messages: { |
59 | | - noVariantsOnResponses: '@variants on {{className}} is not supported in metamodel. {{suggestion}}' |
| 128 | + noVariantsOnResponses: |
| 129 | + '@variants on {{className}} is not supported in metamodel. {{suggestion}}', |
| 130 | + interfaceWithNonContainerVariants: |
| 131 | + "Interface '{{ interfaceName }}' has '@variants {{ variantValue }}' but only 'container' is allowed for interfaces.", |
| 132 | + invalidVariantsTag: |
| 133 | + "Type alias '{{ typeName }}' has invalid '@variants {{ variantValue }}'. Must start with: {{ allowedValues }}." |
60 | 134 | }, |
61 | 135 | type: 'problem', |
62 | 136 | schema: [] |
63 | 137 | }, |
64 | 138 | defaultOptions: [] |
65 | 139 | }) |
66 | | - |
|
0 commit comments