|
| 1 | +import { |
| 2 | + getStyleContexts, |
| 3 | + getCommentDirectivesReporter, |
| 4 | + StyleContext, |
| 5 | + ValidStyleContext, |
| 6 | +} from "../styles/context" |
| 7 | +import type { RuleContext, Rule, Range } from "../types" |
| 8 | +import { |
| 9 | + isVDeepPseudoV2, |
| 10 | + isVDeepPseudo, |
| 11 | + VDeepPseudo, |
| 12 | + isPseudoEmptyArguments, |
| 13 | +} from "../styles/utils/selectors" |
| 14 | +import type { VCSSSelectorNode, VCSSAtRule, VCSSStyleRule } from "../styles/ast" |
| 15 | +import { |
| 16 | + hasSelectorNodes, |
| 17 | + isVCSSAtRule, |
| 18 | + isVCSSDeclarationProperty, |
| 19 | + isVCSSComment, |
| 20 | +} from "../styles/utils/css-nodes" |
| 21 | + |
| 22 | +declare const module: { |
| 23 | + exports: Rule |
| 24 | +} |
| 25 | + |
| 26 | +module.exports = { |
| 27 | + meta: { |
| 28 | + docs: { |
| 29 | + description: |
| 30 | + "require selector argument to be passed to `::v-deep()`.", |
| 31 | + categories: ["vue3-recommended"], |
| 32 | + default: "warn", |
| 33 | + url: |
| 34 | + "https://future-architect.github.io/eslint-plugin-vue-scoped-css/rules/require-v-deep-argument.html", |
| 35 | + }, |
| 36 | + fixable: "code", |
| 37 | + messages: { |
| 38 | + missingArguments: |
| 39 | + "Need to pass argument to the `::v-deep` pseudo-element.", |
| 40 | + }, |
| 41 | + schema: [], |
| 42 | + type: "suggestion", // "problem", |
| 43 | + }, |
| 44 | + create(context: RuleContext) { |
| 45 | + const styles = getStyleContexts(context) |
| 46 | + .filter(StyleContext.isValid) |
| 47 | + .filter((style) => style.scoped) |
| 48 | + if (!styles.length) { |
| 49 | + return {} |
| 50 | + } |
| 51 | + const reporter = getCommentDirectivesReporter(context) |
| 52 | + |
| 53 | + /** |
| 54 | + * Find VCSSStyleRule or nest VCSSAtRule |
| 55 | + */ |
| 56 | + function findHasSelectorsNode( |
| 57 | + node: VCSSSelectorNode, |
| 58 | + ): |
| 59 | + | (VCSSAtRule & { name: "nest"; selectors: VCSSSelectorNode[] }) |
| 60 | + | VCSSStyleRule |
| 61 | + | null { |
| 62 | + if (hasSelectorNodes(node.parent)) { |
| 63 | + return node.parent |
| 64 | + } |
| 65 | + if (isVCSSAtRule(node.parent)) { |
| 66 | + return null |
| 67 | + } |
| 68 | + return findHasSelectorsNode(node.parent) |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Reports the given node |
| 73 | + * @param {ASTNode} node node to report |
| 74 | + */ |
| 75 | + function report(node: VDeepPseudo) { |
| 76 | + reporter.report({ |
| 77 | + node, |
| 78 | + loc: node.loc, |
| 79 | + messageId: "missingArguments", |
| 80 | + fix(fixer) { |
| 81 | + if (!isVDeepPseudoV2(node)) { |
| 82 | + return null |
| 83 | + } |
| 84 | + const nodes = node.parent.nodes |
| 85 | + const selectorIndex = nodes.indexOf(node) |
| 86 | + const nextNode = nodes[selectorIndex + 1] |
| 87 | + if (!nextNode) { |
| 88 | + return null |
| 89 | + } |
| 90 | + const betweenRange: Range = [ |
| 91 | + node.range[0] + node.value.length, |
| 92 | + nextNode.range[0], |
| 93 | + ] |
| 94 | + if ( |
| 95 | + context |
| 96 | + .getSourceCode() |
| 97 | + .text.slice(...betweenRange) |
| 98 | + .trim() |
| 99 | + ) { |
| 100 | + // ::v-deep /* comment */ .foo |
| 101 | + return null |
| 102 | + } |
| 103 | + |
| 104 | + const ruleNode = findHasSelectorsNode(node) |
| 105 | + if ( |
| 106 | + !ruleNode?.nodes.every( |
| 107 | + (n) => |
| 108 | + isVCSSDeclarationProperty(n) || |
| 109 | + isVCSSComment(n), |
| 110 | + ) |
| 111 | + ) { |
| 112 | + // Maybe includes nesting |
| 113 | + return null |
| 114 | + } |
| 115 | + |
| 116 | + const last = nodes[nodes.length - 1] |
| 117 | + return [ |
| 118 | + fixer.removeRange(betweenRange), |
| 119 | + fixer.insertTextAfterRange(betweenRange, "("), |
| 120 | + fixer.insertTextAfterRange(last.range, ")"), |
| 121 | + ] |
| 122 | + }, |
| 123 | + }) |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Verify the style |
| 128 | + */ |
| 129 | + function verify(style: ValidStyleContext) { |
| 130 | + style.traverseSelectorNodes({ |
| 131 | + enterNode(node) { |
| 132 | + if (isVDeepPseudoV2(node)) { |
| 133 | + report(node) |
| 134 | + } else if ( |
| 135 | + isVDeepPseudo(node) && |
| 136 | + isPseudoEmptyArguments(node) |
| 137 | + ) { |
| 138 | + report(node) |
| 139 | + } |
| 140 | + }, |
| 141 | + }) |
| 142 | + } |
| 143 | + |
| 144 | + return { |
| 145 | + "Program:exit"() { |
| 146 | + for (const style of styles) { |
| 147 | + verify(style) |
| 148 | + } |
| 149 | + }, |
| 150 | + } |
| 151 | + }, |
| 152 | +} |
0 commit comments