Skip to content

Commit

Permalink
Fixed false positives when there is no template block in `no-unused-s…
Browse files Browse the repository at this point in the history
…elector` and `require-selector-used-inside`. (#22)
  • Loading branch information
ota-meshi authored Jan 14, 2020
1 parent dd6852e commit 94a25c7
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/rules/no-unused-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
StyleContext,
getCommentDirectivesReporter,
} from "../styles/context"
import { hasTemplateBlock } from "../utils/utils"

/**
* Gets scoped selectors.
Expand Down Expand Up @@ -100,6 +101,9 @@ module.exports = {
type: "suggestion", // "problem",
},
create(context: RuleContext) {
if (!hasTemplateBlock(context)) {
return {}
}
const styles = getStyleContexts(context)
.filter(StyleContext.isValid)
.filter(style => style.scoped)
Expand Down
4 changes: 4 additions & 0 deletions lib/rules/require-selector-used-inside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
StyleContext,
getCommentDirectivesReporter,
} from "../styles/context"
import { hasTemplateBlock } from "../utils/utils"

/**
* Gets scoped selectors.
Expand Down Expand Up @@ -79,6 +80,9 @@ module.exports = {
type: "suggestion",
},
create(context: RuleContext) {
if (!hasTemplateBlock(context)) {
return {}
}
const styles = getStyleContexts(context)
.filter(StyleContext.isValid)
.filter(style => style.scoped)
Expand Down
10 changes: 10 additions & 0 deletions lib/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { RuleContext } from "../types"

/**
* Checks whether the given context has template block
*/
export function hasTemplateBlock(context: RuleContext): boolean {
const sourceCode = context.getSourceCode()
const { ast } = sourceCode
return Boolean(ast.templateBody)
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-vue-scoped-css",
"version": "0.3.2",
"version": "0.3.3",
"description": "ESLint plugin for Scoped CSS in Vue.js",
"main": "dist/index.js",
"scripts": {
Expand Down
9 changes: 8 additions & 1 deletion tests/lib/rules/no-unused-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,14 @@ tester.run("no-unused-selector", rule, {
.foo-leave-active {}
</style>
`,

`
<script></script>
<style scoped lang="scss">
.no-template {
color: red;
}
</style>
`,
// options
// ignoreBEMModifier
{
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/require-selector-used-inside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ tester.run("require-selector-used-inside", rule, {
.foo-leave-active {}
</style>
`,
`
<script></script>
<style scoped lang="scss">
.no-template {
color: red;
}
</style>
`,
// options
// ignoreBEMModifier
{
Expand Down

0 comments on commit 94a25c7

Please sign in to comment.