|
| 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 | +import * as path from 'path'; |
| 21 | + |
| 22 | +const createRule = ESLintUtils.RuleCreator(name => `https://example.com/rule/${name}`) |
| 23 | + |
| 24 | +export default createRule({ |
| 25 | + name: 'no-generic-number', |
| 26 | + create(context) { |
| 27 | + return { |
| 28 | + TSNumberKeyword(node) { |
| 29 | + const filename = context.filename || context.getFilename(); |
| 30 | + const normalizedPath = path.normalize(filename); |
| 31 | + |
| 32 | + // allow number only in _types/Numeric.ts |
| 33 | + if (normalizedPath.includes(path.join('_types', 'Numeric.ts'))) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + context.report({ |
| 38 | + node, |
| 39 | + messageId: 'noGenericNumber', |
| 40 | + data: { |
| 41 | + types: 'short, byte, integer, uint, long, ulong, float, or double' |
| 42 | + } |
| 43 | + }) |
| 44 | + }, |
| 45 | + } |
| 46 | + }, |
| 47 | + meta: { |
| 48 | + docs: { |
| 49 | + description: 'Force usage of concrete numeric types instead of generic "number" type', |
| 50 | + }, |
| 51 | + messages: { |
| 52 | + noGenericNumber: 'Generic "number" type is not allowed. Use concrete numeric types instead: {{types}}. See specification/_types/Numeric.ts for available types.' |
| 53 | + }, |
| 54 | + type: 'problem', |
| 55 | + schema: [] |
| 56 | + }, |
| 57 | + defaultOptions: [] |
| 58 | +}) |
| 59 | + |
0 commit comments