Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
vue/no-loss-of-precision
rule (#1680)
- Loading branch information
Showing
7 changed files
with
204 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-loss-of-precision | ||
description: disallow literal numbers that lose precision | ||
--- | ||
# vue/no-loss-of-precision | ||
|
||
> disallow literal numbers that lose precision | ||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
|
||
This rule is the same rule as core [no-loss-of-precision] rule but it applies to the expressions in `<template>`. | ||
|
||
:::warning | ||
You must be using ESLint v7.1.0 or later to use this rule. | ||
::: | ||
|
||
## :books: Further Reading | ||
|
||
- [no-loss-of-precision] | ||
|
||
[no-loss-of-precision]: https://eslint.org/docs/rules/no-loss-of-precision | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-loss-of-precision.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-loss-of-precision.js) | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/rules/no-loss-of-precision)</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('../utils') | ||
|
||
// eslint-disable-next-line no-invalid-meta, no-invalid-meta-docs-categories | ||
module.exports = utils.wrapCoreRule('no-loss-of-precision', { | ||
applyDocument: true | ||
}) |
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,93 @@ | ||
/** | ||
* @author Yosuke Ota | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const { RuleTester, ESLint } = require('../../eslint-compat') | ||
const semver = require('semver') | ||
const rule = require('../../../lib/rules/no-loss-of-precision') | ||
|
||
const tester = new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}) | ||
tester.run('no-loss-of-precision', rule, { | ||
valid: [ | ||
...(semver.gte(ESLint.version, '7.1.0') | ||
? [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
{{12345}} | ||
{{123.45}} | ||
</template> | ||
` | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<MyComponent num="12345678901234567890" /> | ||
</template> | ||
` | ||
} | ||
] | ||
: []) | ||
], | ||
invalid: [ | ||
...(semver.gte(ESLint.version, '7.1.0') | ||
? [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
{{12345678901234567890}} | ||
{{0.12345678901234567890}} | ||
</template> | ||
`, | ||
errors: [ | ||
{ | ||
message: 'This number literal will lose precision at runtime.', | ||
line: 3 | ||
}, | ||
{ | ||
message: 'This number literal will lose precision at runtime.', | ||
line: 4 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<MyComponent :num="12345678901234567890" /> | ||
</template> | ||
`, | ||
errors: [ | ||
{ | ||
message: 'This number literal will lose precision at runtime.', | ||
line: 3 | ||
} | ||
] | ||
} | ||
] | ||
: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<MyComponent :num="12345678901234567890" /> | ||
</template> | ||
`, | ||
errors: [ | ||
'Failed to extend ESLint core rule "no-loss-of-precision". You may be able to use this rule by upgrading the version of ESLint. If you cannot upgrade it, turn off this rule.' | ||
] | ||
} | ||
]) | ||
] | ||
}) |
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,41 @@ | ||
'use strict' | ||
|
||
const RuleTester = require('eslint').RuleTester | ||
const utils = require('../../../../lib/utils/index') | ||
|
||
const rule = utils.wrapCoreRule('foo') | ||
|
||
const tester = new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}) | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
tester.run('wrap-core-rule-with-unknown', rule, { | ||
valid: [ | ||
{ | ||
filename: 'test.js', | ||
code: `var a` | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: `<template></template>`, | ||
errors: [ | ||
{ | ||
message: | ||
'Failed to extend ESLint core rule "foo". You may be able to use this rule by upgrading the version of ESLint. If you cannot upgrade it, turn off this rule.', | ||
line: 1, | ||
column: 1 | ||
} | ||
] | ||
} | ||
] | ||
}) |