-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
no-loss-of-precision.ts
53 lines (48 loc) · 1.55 KB
/
no-loss-of-precision.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { TSESTree } from '@typescript-eslint/experimental-utils';
import BaseRule from 'eslint/lib/rules/no-loss-of-precision';
import * as util from '../util';
const baseRule = ((): typeof BaseRule | null => {
try {
return require('eslint/lib/rules/no-loss-of-precision');
} catch {
/* istanbul ignore next */
return null;
}
})();
type Options = util.InferOptionsTypeFromRule<typeof BaseRule>;
type MessageIds = util.InferMessageIdsTypeFromRule<typeof BaseRule>;
export default util.createRule<Options, MessageIds>({
name: 'no-loss-of-precision',
meta: {
type: 'problem',
docs: {
description: 'Disallow literal numbers that lose precision',
category: 'Possible Errors',
recommended: false,
extendsBaseRule: true,
},
schema: [],
messages: baseRule?.meta.messages ?? { noLossOfPrecision: '' },
},
defaultOptions: [],
create(context) {
/* istanbul ignore if */ if (baseRule === null) {
throw new Error(
'@typescript-eslint/no-loss-of-precision requires at least ESLint v7.1.0',
);
}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const rules = baseRule!.create(context);
function isSeparatedNumeric(node: TSESTree.Literal): boolean {
return typeof node.value === 'number' && node.raw.includes('_');
}
return {
Literal(node: TSESTree.Literal): void {
rules.Literal({
...node,
raw: isSeparatedNumeric(node) ? node.raw.replace(/_/g, '') : node.raw,
} as never);
},
};
},
});