Skip to content

Commit 94a25c7

Browse files
authored
Fixed false positives when there is no template block in no-unused-selector and require-selector-used-inside. (#22)
1 parent dd6852e commit 94a25c7

File tree

7 files changed

+36
-3
lines changed

7 files changed

+36
-3
lines changed

lib/rules/no-unused-selector.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
StyleContext,
2222
getCommentDirectivesReporter,
2323
} from "../styles/context"
24+
import { hasTemplateBlock } from "../utils/utils"
2425

2526
/**
2627
* Gets scoped selectors.
@@ -100,6 +101,9 @@ module.exports = {
100101
type: "suggestion", // "problem",
101102
},
102103
create(context: RuleContext) {
104+
if (!hasTemplateBlock(context)) {
105+
return {}
106+
}
103107
const styles = getStyleContexts(context)
104108
.filter(StyleContext.isValid)
105109
.filter(style => style.scoped)

lib/rules/require-selector-used-inside.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
StyleContext,
1818
getCommentDirectivesReporter,
1919
} from "../styles/context"
20+
import { hasTemplateBlock } from "../utils/utils"
2021

2122
/**
2223
* Gets scoped selectors.
@@ -79,6 +80,9 @@ module.exports = {
7980
type: "suggestion",
8081
},
8182
create(context: RuleContext) {
83+
if (!hasTemplateBlock(context)) {
84+
return {}
85+
}
8286
const styles = getStyleContexts(context)
8387
.filter(StyleContext.isValid)
8488
.filter(style => style.scoped)

lib/utils/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { RuleContext } from "../types"
2+
3+
/**
4+
* Checks whether the given context has template block
5+
*/
6+
export function hasTemplateBlock(context: RuleContext): boolean {
7+
const sourceCode = context.getSourceCode()
8+
const { ast } = sourceCode
9+
return Boolean(ast.templateBody)
10+
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-plugin-vue-scoped-css",
3-
"version": "0.3.2",
3+
"version": "0.3.3",
44
"description": "ESLint plugin for Scoped CSS in Vue.js",
55
"main": "dist/index.js",
66
"scripts": {

tests/lib/rules/no-unused-selector.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,14 @@ tester.run("no-unused-selector", rule, {
253253
.foo-leave-active {}
254254
</style>
255255
`,
256-
256+
`
257+
<script></script>
258+
<style scoped lang="scss">
259+
.no-template {
260+
color: red;
261+
}
262+
</style>
263+
`,
257264
// options
258265
// ignoreBEMModifier
259266
{

tests/lib/rules/require-selector-used-inside.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,14 @@ tester.run("require-selector-used-inside", rule, {
215215
.foo-leave-active {}
216216
</style>
217217
`,
218+
`
219+
<script></script>
220+
<style scoped lang="scss">
221+
.no-template {
222+
color: red;
223+
}
224+
</style>
225+
`,
218226
// options
219227
// ignoreBEMModifier
220228
{

0 commit comments

Comments
 (0)