|
| 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 { RuleTester } from '@typescript-eslint/rule-tester' |
| 20 | +import noAllStringLiteralUnions from '../rules/no-all-string-literal-unions.js' |
| 21 | + |
| 22 | +const ruleTester = new RuleTester({ |
| 23 | + languageOptions: { |
| 24 | + parserOptions: { |
| 25 | + projectService: { |
| 26 | + allowDefaultProject: ['*.ts*'] |
| 27 | + }, |
| 28 | + tsconfigRootDir: import.meta.dirname |
| 29 | + } |
| 30 | + } |
| 31 | +}) |
| 32 | + |
| 33 | +const rule = noAllStringLiteralUnions |
| 34 | + |
| 35 | +ruleTester.run('no-all-string-literal-unions', rule, { |
| 36 | + valid: [ |
| 37 | + { |
| 38 | + name: 'enum', |
| 39 | + code: `enum MyEnum { foo, bar, baz } |
| 40 | + type MyDict = Dictionary<MyEnum, object>` |
| 41 | + }, |
| 42 | + { |
| 43 | + name: 'type', |
| 44 | + code: `type MyType = "foo" | int` |
| 45 | + }, |
| 46 | + { |
| 47 | + name: 'single string literal (not a union)', |
| 48 | + code: `type SingleValue = "foo"` |
| 49 | + }, |
| 50 | + { |
| 51 | + name: 'union with null/undefined', |
| 52 | + code: `type MaybeString = "active" | null` |
| 53 | + }, |
| 54 | + { |
| 55 | + name: 'union with number', |
| 56 | + code: `type StringOrNumber = "default" | number` |
| 57 | + }, |
| 58 | + { |
| 59 | + name: 'number literal unions (should only catch string literals)', |
| 60 | + code: `type NumericUnion = 1 | 2 | 3` |
| 61 | + }, |
| 62 | + { |
| 63 | + name: 'union with type reference', |
| 64 | + code: `type MyType = string; type Mixed = "literal" | MyType` |
| 65 | + } |
| 66 | + ], |
| 67 | + invalid: [ |
| 68 | + { |
| 69 | + name: 'all string literal union', |
| 70 | + code: `type MyType = "foo" | "bar" | "baz"`, |
| 71 | + errors: [{ messageId: 'noAllStringLiteralUnions' }] |
| 72 | + }, |
| 73 | + { |
| 74 | + name: 'interface with string literal union', |
| 75 | + code: `export interface MyInterface { |
| 76 | + some?: "foo" | "bar" | "baz" |
| 77 | + other?: 'foo' | 'bar' | 'baz' |
| 78 | + }`, |
| 79 | + errors: [ |
| 80 | + { messageId: 'noAllStringLiteralUnions' }, |
| 81 | + { messageId: 'noAllStringLiteralUnions' } |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + name: 'function with string literal union', |
| 86 | + code: `function getStatus(): "pending" | "complete" { return "pending" }`, |
| 87 | + errors: [{ messageId: 'noAllStringLiteralUnions' }] |
| 88 | + }, |
| 89 | + { |
| 90 | + name: 'class with string literal union', |
| 91 | + code: `class Config { status: "active" | "inactive" }`, |
| 92 | + errors: [{ messageId: 'noAllStringLiteralUnions' }] |
| 93 | + } |
| 94 | + ] |
| 95 | +}) |
0 commit comments