-
-
Notifications
You must be signed in to change notification settings - Fork 668
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-implicit-coercion
rule (#2639)
Co-authored-by: Yosuke Ota <otameshiyo23@gmail.com>
- Loading branch information
Showing
4 changed files
with
307 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-implicit-coercion | ||
description: Disallow shorthand type conversions in `<template>` | ||
--- | ||
|
||
# vue/no-implicit-coercion | ||
|
||
> Disallow shorthand type conversions in `<template>` | ||
- :wrench: The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule. | ||
|
||
This rule is the same rule as core [no-implicit-coercion] rule but it applies to the expressions in `<template>`. | ||
|
||
## :books: Further Reading | ||
|
||
- [no-implicit-coercion] | ||
|
||
[no-implicit-coercion]: https://eslint.org/docs/rules/no-implicit-coercion | ||
|
||
## :rocket: Version | ||
|
||
This rule was introduced in eslint-plugin-vue v9.33.0 | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-implicit-coercion.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-implicit-coercion.js) | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://eslint.org/docs/latest/rules/no-implicit-coercion)</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,12 @@ | ||
/** | ||
* @author lozinsky <https://github.com/lozinsky> | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
// eslint-disable-next-line internal/no-invalid-meta | ||
module.exports = utils.wrapCoreRule('no-implicit-coercion', { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,263 @@ | ||
/** | ||
* @author lozinsky <https://github.com/lozinsky> | ||
* 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-implicit-coercion') | ||
|
||
/** | ||
* @param {string} suggestedCode | ||
* @returns {string} | ||
*/ | ||
function getExpectedErrorMessage(suggestedCode) { | ||
return semver.gte(ESLint.version, '9.0.0') | ||
? `Unexpected implicit coercion encountered. Use \`${suggestedCode}\` instead.` | ||
: `use \`${suggestedCode}\` instead.` | ||
} | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
parser: require('vue-eslint-parser'), | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}) | ||
|
||
tester.run('no-implicit-coercion', rule, { | ||
valid: [ | ||
`<template><div :data-foo="Boolean(foo)" /></template>`, | ||
`<template><div :data-foo="foo.indexOf('.') !== -1" /></template>`, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div :data-foo="!!foo" /></template>`, | ||
options: [ | ||
{ | ||
boolean: false | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div :data-foo="~foo.indexOf('.')" /></template>`, | ||
options: [ | ||
{ | ||
boolean: false | ||
} | ||
] | ||
}, | ||
`<template><div :data-foo="Number(foo)" /></template>`, | ||
...(semver.gte(ESLint.version, '8.28.0') | ||
? [`<template><div :data-foo="foo * 1/4" /></template>`] | ||
: []), | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div :data-foo="+foo" /></template>`, | ||
options: [ | ||
{ | ||
number: false | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div :data-foo="1 * foo" /></template>`, | ||
options: [ | ||
{ | ||
number: false | ||
} | ||
] | ||
}, | ||
`<template><div :data-foo="String(foo)" /></template>`, | ||
`<template><div :data-foo="\`\${foo}\`" /></template>`, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div :data-foo="'' + foo" /></template>`, | ||
options: [ | ||
{ | ||
string: false | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div :data-foo="\`\` + foo" /></template>`, | ||
options: [ | ||
{ | ||
string: false | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div :data-foo="!!foo" /></template>`, | ||
options: [ | ||
{ | ||
allow: ['!!'] | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div :data-foo="~foo.indexOf('.')" /></template>`, | ||
options: [ | ||
{ | ||
allow: ['~'] | ||
} | ||
] | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div :data-foo="!!foo" /></template>`, | ||
output: `<template><div :data-foo="Boolean(foo)" /></template>`, | ||
errors: [ | ||
{ | ||
message: getExpectedErrorMessage('Boolean(foo)'), | ||
line: 1, | ||
column: 27 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div :data-foo="~foo.indexOf('.')" /></template>`, | ||
output: null, | ||
errors: [ | ||
{ | ||
message: getExpectedErrorMessage("foo.indexOf('.') !== -1"), | ||
line: 1, | ||
column: 27 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div :data-foo="+foo" /></template>`, | ||
output: semver.gte(ESLint.version, '9.0.0') | ||
? null | ||
: `<template><div :data-foo="Number(foo)" /></template>`, | ||
errors: [ | ||
{ | ||
message: getExpectedErrorMessage('Number(foo)'), | ||
line: 1, | ||
column: 27, | ||
suggestions: semver.gte(ESLint.version, '9.0.0') | ||
? [ | ||
{ | ||
messageId: 'useRecommendation', | ||
data: { recommendation: 'Number(foo)' }, | ||
output: '<template><div :data-foo="Number(foo)" /></template>' | ||
} | ||
] | ||
: [] | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div :data-foo="1 * foo" /></template>`, | ||
output: semver.gte(ESLint.version, '9.0.0') | ||
? null | ||
: `<template><div :data-foo="Number(foo)" /></template>`, | ||
errors: [ | ||
{ | ||
message: getExpectedErrorMessage('Number(foo)'), | ||
line: 1, | ||
column: 27, | ||
suggestions: semver.gte(ESLint.version, '9.0.0') | ||
? [ | ||
{ | ||
messageId: 'useRecommendation', | ||
data: { recommendation: 'Number(foo)' }, | ||
output: '<template><div :data-foo="Number(foo)" /></template>' | ||
} | ||
] | ||
: [] | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div :data-foo="'' + foo" /></template>`, | ||
output: semver.gte(ESLint.version, '9.0.0') | ||
? null | ||
: `<template><div :data-foo="String(foo)" /></template>`, | ||
errors: [ | ||
{ | ||
message: getExpectedErrorMessage('String(foo)'), | ||
line: 1, | ||
column: 27, | ||
suggestions: semver.gte(ESLint.version, '9.0.0') | ||
? [ | ||
{ | ||
messageId: 'useRecommendation', | ||
data: { recommendation: 'String(foo)' }, | ||
output: '<template><div :data-foo="String(foo)" /></template>' | ||
} | ||
] | ||
: [] | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div :data-foo="\`\` + foo" /></template>`, | ||
output: semver.gte(ESLint.version, '9.0.0') | ||
? null | ||
: `<template><div :data-foo="String(foo)" /></template>`, | ||
errors: [ | ||
{ | ||
message: getExpectedErrorMessage('String(foo)'), | ||
line: 1, | ||
column: 27, | ||
suggestions: semver.gte(ESLint.version, '9.0.0') | ||
? [ | ||
{ | ||
messageId: 'useRecommendation', | ||
data: { recommendation: 'String(foo)' }, | ||
output: '<template><div :data-foo="String(foo)" /></template>' | ||
} | ||
] | ||
: [] | ||
} | ||
] | ||
}, | ||
...(semver.gte(ESLint.version, '7.24.0') | ||
? [ | ||
{ | ||
filename: 'test.vue', | ||
code: `<template><div :data-foo="\`\${foo}\`" /></template>`, | ||
output: semver.gte(ESLint.version, '9.0.0') | ||
? null | ||
: `<template><div :data-foo="String(foo)" /></template>`, | ||
options: [ | ||
{ | ||
disallowTemplateShorthand: true | ||
} | ||
], | ||
errors: [ | ||
{ | ||
message: getExpectedErrorMessage('String(foo)'), | ||
line: 1, | ||
column: 27, | ||
suggestions: semver.gte(ESLint.version, '9.0.0') | ||
? [ | ||
{ | ||
messageId: 'useRecommendation', | ||
data: { recommendation: 'String(foo)' }, | ||
output: | ||
'<template><div :data-foo="String(foo)" /></template>' | ||
} | ||
] | ||
: [] | ||
} | ||
] | ||
} | ||
] | ||
: []) | ||
] | ||
}) |