Skip to content

Commit

Permalink
chore: add version checking to some test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
lozinsky committed Dec 5, 2024
1 parent 644f97d commit 4d51667
Showing 1 changed file with 110 additions and 33 deletions.
143 changes: 110 additions & 33 deletions tests/lib/rules/no-implicit-coercion.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*/
'use strict'

const RuleTester = require('../../eslint-compat').RuleTester
const { RuleTester, ESLint } = require('../../eslint-compat')
const semver = require('semver')
const rule = require('../../../lib/rules/no-implicit-coercion')

const tester = new RuleTester({
Expand Down Expand Up @@ -38,7 +39,9 @@ tester.run('no-implicit-coercion', rule, {
]
},
`<template><div :data-foo="Number(foo)" /></template>`,
`<template><div :data-foo="foo * 1/4" /></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>`,
Expand Down Expand Up @@ -103,7 +106,9 @@ tester.run('no-implicit-coercion', rule, {
output: `<template><div :data-foo="Boolean(foo)" /></template>`,
errors: [
{
message: 'use `Boolean(foo)` instead.',
message: semver.gte(ESLint.version, '9.0.0')
? 'Unexpected implicit coercion encountered. Use `Boolean(foo)` instead.'
: 'use `Boolean(foo)` instead.',
line: 1,
column: 27
}
Expand All @@ -115,7 +120,9 @@ tester.run('no-implicit-coercion', rule, {
output: null,
errors: [
{
message: "use `foo.indexOf('.') !== -1` instead.",
message: semver.gte(ESLint.version, '9.0.0')
? "Unexpected implicit coercion encountered. Use `foo.indexOf('.') !== -1` instead."
: "use `foo.indexOf('.') !== -1` instead.",
line: 1,
column: 27
}
Expand All @@ -124,67 +131,137 @@ tester.run('no-implicit-coercion', rule, {
{
filename: 'test.vue',
code: `<template><div :data-foo="+foo" /></template>`,
output: `<template><div :data-foo="Number(foo)" /></template>`,
output: semver.gte(ESLint.version, '9.0.0')
? null
: `<template><div :data-foo="Number(foo)" /></template>`,
errors: [
{
message: 'use `Number(foo)` instead.',
message: semver.gte(ESLint.version, '9.0.0')
? 'Unexpected implicit coercion encountered. Use `Number(foo)` instead.'
: 'use `Number(foo)` instead.',
line: 1,
column: 27
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: `<template><div :data-foo="Number(foo)" /></template>`,
output: semver.gte(ESLint.version, '9.0.0')
? null
: `<template><div :data-foo="Number(foo)" /></template>`,
errors: [
{
message: 'use `Number(foo)` instead.',
message: semver.gte(ESLint.version, '9.0.0')
? 'Unexpected implicit coercion encountered. Use `Number(foo)` instead.'
: 'use `Number(foo)` instead.',
line: 1,
column: 27
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: `<template><div :data-foo="String(foo)" /></template>`,
output: semver.gte(ESLint.version, '9.0.0')
? null
: `<template><div :data-foo="String(foo)" /></template>`,
errors: [
{
message: 'use `String(foo)` instead.',
message: semver.gte(ESLint.version, '9.0.0')
? 'Unexpected implicit coercion encountered. Use `String(foo)` instead.'
: 'use `String(foo)` instead.',
line: 1,
column: 27
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: `<template><div :data-foo="String(foo)" /></template>`,
output: semver.gte(ESLint.version, '9.0.0')
? null
: `<template><div :data-foo="String(foo)" /></template>`,
errors: [
{
message: 'use `String(foo)` instead.',
message: semver.gte(ESLint.version, '9.0.0')
? 'Unexpected implicit coercion encountered. Use `String(foo)` instead.'
: 'use `String(foo)` instead.',
line: 1,
column: 27
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: `<template><div :data-foo="String(foo)" /></template>`,
options: [
{
disallowTemplateShorthand: true
}
],
errors: [
{
message: 'use `String(foo)` instead.',
line: 1,
column: 27
}
]
}
...(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: semver.gte(ESLint.version, '9.0.0')
? 'Unexpected implicit coercion encountered. Use `String(foo)` instead.'
: 'use `String(foo)` instead.',
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>'
}
]
: []
}
]
}
]
: [])
]
})

0 comments on commit 4d51667

Please sign in to comment.