Skip to content

Commit af14bc8

Browse files
authored
Add autofix to no-deprecated-deep-combinator rule. (#40)
1 parent d257150 commit af14bc8

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

lib/rules/no-deprecated-deep-combinator.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
ValidStyleContext,
66
} from "../styles/context"
77
import type { VCSSSelectorCombinator } from "../styles/ast"
8-
import type { RuleContext, Rule } from "../types"
8+
import type { RuleContext, Rule, Range } from "../types"
99
import { isDeepCombinator } from "../styles/utils/selectors"
1010

1111
declare const module: {
@@ -21,7 +21,7 @@ module.exports = {
2121
url:
2222
"https://future-architect.github.io/eslint-plugin-vue-scoped-css/rules/no-deprecated-deep-combinator.html",
2323
},
24-
fixable: null,
24+
fixable: "code",
2525
messages: {
2626
deprecated: "The deep combinator `{{value}}` is deprecated.",
2727
},
@@ -49,6 +49,19 @@ module.exports = {
4949
data: {
5050
value: node.value.trim(),
5151
},
52+
fix(fixer) {
53+
const sourceCodeText = context.getSourceCode().text
54+
const range = [...node.range] as Range
55+
let newText = "::v-deep"
56+
if (sourceCodeText[range[0] - 1]?.trim()) {
57+
newText = ` ${newText}`
58+
}
59+
if (sourceCodeText[range[1]]?.trim()) {
60+
newText = `${newText} `
61+
}
62+
63+
return fixer.replaceTextRange(range, newText)
64+
},
5265
})
5366
}
5467

tests/lib/rules/no-deprecated-deep-combinator.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ tester.run("no-deprecated-deep-combinator", rule, {
3030
a /deep/ .b {}
3131
</style>
3232
`,
33+
output: `
34+
<template><div class="item">sample</div></template>
35+
<style scoped>
36+
a ::v-deep .b {}
37+
a ::v-deep .b {}
38+
</style>
39+
`,
3340
errors: [
3441
{
3542
message: "The deep combinator `>>>` is deprecated.",
@@ -47,5 +54,26 @@ tester.run("no-deprecated-deep-combinator", rule, {
4754
},
4855
],
4956
},
57+
58+
{
59+
code: `
60+
<template><div class="item">sample</div></template>
61+
<style scoped>
62+
a>>>.b {}
63+
a/deep/.b {}
64+
</style>
65+
`,
66+
output: `
67+
<template><div class="item">sample</div></template>
68+
<style scoped>
69+
a ::v-deep .b {}
70+
a ::v-deep .b {}
71+
</style>
72+
`,
73+
errors: [
74+
"The deep combinator `>>>` is deprecated.",
75+
"The deep combinator `/deep/` is deprecated.",
76+
],
77+
},
5078
],
5179
})

0 commit comments

Comments
 (0)