Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat(eslint-plugin): add extension rule
no-loss-of-precision
(#2196)
Co-authored-by: Brad Zacher <brad.zacher@gmail.com>
- Loading branch information
1 parent
5e4dda2
commit 535b0f2
Showing
9 changed files
with
154 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Disallow literal numbers that lose precision (`no-loss-of-precision`) | ||
|
||
## Rule Details | ||
|
||
This rule extends the base [`eslint/no-loss-of-precision`](https://eslint.org/docs/rules/no-loss-of-precision) rule. | ||
It adds support for [numeric separators](https://github.com/tc39/proposal-numeric-separator). | ||
Note that this rule requires ESLint v7. | ||
|
||
## How to use | ||
|
||
```jsonc | ||
{ | ||
// note you must disable the base rule as it can report incorrect errors | ||
"no-loss-of-precision": "off", | ||
"@typescript-eslint/no-loss-of-precision": ["error"] | ||
} | ||
``` | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/no-loss-of-precision.md)</sup> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 isSeperatedNumeric(node: TSESTree.Literal): boolean { | ||
return typeof node.value === 'number' && node.raw.includes('_'); | ||
} | ||
return { | ||
Literal(node: TSESTree.Literal): void { | ||
rules.Literal({ | ||
...node, | ||
raw: isSeperatedNumeric(node) ? node.raw.replace(/_/g, '') : node.raw, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
36 changes: 36 additions & 0 deletions
36
packages/eslint-plugin/tests/rules/no-loss-of-precision.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import rule from '../../src/rules/no-loss-of-precision'; | ||
import { RuleTester } from '../RuleTester'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('no-loss-of-precision', rule, { | ||
valid: [ | ||
'const x = 12345;', | ||
'const x = 123.456;', | ||
'const x = -123.456;', | ||
'const x = 123_456;', | ||
'const x = 123_00_000_000_000_000_000_000_000;', | ||
'const x = 123.000_000_000_000_000_000_000_0;', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'const x = 9007199254740993;', | ||
errors: [{ messageId: 'noLossOfPrecision' }], | ||
}, | ||
{ | ||
code: 'const x = 9_007_199_254_740_993;', | ||
errors: [{ messageId: 'noLossOfPrecision' }], | ||
}, | ||
{ | ||
code: 'const x = 9_007_199_254_740.993e3;', | ||
errors: [{ messageId: 'noLossOfPrecision' }], | ||
}, | ||
{ | ||
code: | ||
'const x = 0b100_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_001;', | ||
errors: [{ messageId: 'noLossOfPrecision' }], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters