|
| 1 | +import { |
| 2 | + getStyleContexts, |
| 3 | + getCommentDirectivesReporter, |
| 4 | + StyleContext, |
| 5 | + ValidStyleContext, |
| 6 | +} from "../styles/context" |
| 7 | +import type { VCSSSelectorNode } from "../styles/ast" |
| 8 | +import type { RuleContext, Rule } from "../types" |
| 9 | +import { isVGlobalPseudo } from "../styles/utils/selectors" |
| 10 | + |
| 11 | +declare const module: { |
| 12 | + exports: Rule |
| 13 | +} |
| 14 | + |
| 15 | +module.exports = { |
| 16 | + meta: { |
| 17 | + docs: { |
| 18 | + description: |
| 19 | + "disallow parent selector for `::v-global` pseudo-element", |
| 20 | + categories: ["vue3-recommended"], |
| 21 | + default: "warn", |
| 22 | + url: |
| 23 | + "https://future-architect.github.io/eslint-plugin-vue-scoped-css/rules/no-parent-of-v-global.html", |
| 24 | + }, |
| 25 | + fixable: null, |
| 26 | + messages: { |
| 27 | + unexpected: |
| 28 | + "The parent selector of the `::v-global()` pseudo-element is useless.", |
| 29 | + }, |
| 30 | + schema: [], |
| 31 | + type: "suggestion", // "problem", |
| 32 | + }, |
| 33 | + create(context: RuleContext) { |
| 34 | + const styles = getStyleContexts(context) |
| 35 | + .filter(StyleContext.isValid) |
| 36 | + .filter((style) => style.scoped) |
| 37 | + if (!styles.length) { |
| 38 | + return {} |
| 39 | + } |
| 40 | + const reporter = getCommentDirectivesReporter(context) |
| 41 | + |
| 42 | + /** |
| 43 | + * Reports the given node |
| 44 | + * @param {ASTNode} node node to report |
| 45 | + */ |
| 46 | + function report(node: VCSSSelectorNode) { |
| 47 | + reporter.report({ |
| 48 | + node, |
| 49 | + loc: node.loc, |
| 50 | + messageId: "unexpected", |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Verify the style |
| 56 | + */ |
| 57 | + function verify(style: ValidStyleContext) { |
| 58 | + style.traverseSelectorNodes({ |
| 59 | + enterNode(node) { |
| 60 | + if (!isVGlobalPseudo(node)) { |
| 61 | + return |
| 62 | + } |
| 63 | + const nodes = node.parent.nodes |
| 64 | + const selectorIndex = nodes.indexOf(node) |
| 65 | + if (selectorIndex > 0) { |
| 66 | + report(node) |
| 67 | + } |
| 68 | + }, |
| 69 | + }) |
| 70 | + } |
| 71 | + |
| 72 | + return { |
| 73 | + "Program:exit"() { |
| 74 | + for (const style of styles) { |
| 75 | + verify(style) |
| 76 | + } |
| 77 | + }, |
| 78 | + } |
| 79 | + }, |
| 80 | +} |
0 commit comments