|
| 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 rule from '../rules/no-generic-number.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 | +ruleTester.run('no-generic-number', rule, { |
| 34 | + valid: [ |
| 35 | + `type MyType = { count: integer }`, |
| 36 | + `type MyType = { amount: long }`, |
| 37 | + `type MyType = { price: float }`, |
| 38 | + `type MyType = { value: double }`, |
| 39 | + `type MyType = { small: short }`, |
| 40 | + `type MyType = { tiny: byte }`, |
| 41 | + `type MyType = { unsigned: uint }`, |
| 42 | + `type MyType = { bigUnsigned: ulong }`, |
| 43 | + `class MyClass { score: float; count: integer; }`, |
| 44 | + `interface MyInterface { id: long; ratio: double; }`, |
| 45 | + `type MyType = { value: integer | string }`, |
| 46 | + `type MyType = { id: long | float }`, |
| 47 | + `type MyType = { numbers: integer[] }`, |
| 48 | + `type MyType = { values: Array<long> }`, |
| 49 | + `type MyType = Dictionary<string, integer>`, |
| 50 | + ], |
| 51 | + invalid: [ |
| 52 | + { |
| 53 | + code: `type MyType = { count: number }`, |
| 54 | + errors: [{ messageId: 'noGenericNumber' }] |
| 55 | + }, |
| 56 | + { |
| 57 | + code: `class MyClass { status: number }`, |
| 58 | + errors: [{ messageId: 'noGenericNumber' }] |
| 59 | + }, |
| 60 | + { |
| 61 | + code: `interface MyInterface { id: number }`, |
| 62 | + errors: [{ messageId: 'noGenericNumber' }] |
| 63 | + }, |
| 64 | + { |
| 65 | + code: `type MyType = { value: string | number }`, |
| 66 | + errors: [{ messageId: 'noGenericNumber' }] |
| 67 | + }, |
| 68 | + { |
| 69 | + code: `type MyType = { items: number[] }`, |
| 70 | + errors: [{ messageId: 'noGenericNumber' }] |
| 71 | + }, |
| 72 | + { |
| 73 | + code: `type MyType = { items: Array<number> }`, |
| 74 | + errors: [{ messageId: 'noGenericNumber' }] |
| 75 | + }, |
| 76 | + { |
| 77 | + code: `type MyType = Dictionary<string, number>`, |
| 78 | + errors: [{ messageId: 'noGenericNumber' }] |
| 79 | + }, |
| 80 | + { |
| 81 | + code: `export class Response { body: { count: number } }`, |
| 82 | + errors: [{ messageId: 'noGenericNumber' }] |
| 83 | + } |
| 84 | + ], |
| 85 | +}) |
| 86 | + |
0 commit comments