|
| 1 | +import type { ValidStyleContext } from "../styles/context"; |
| 2 | +import { |
| 3 | + getStyleContexts, |
| 4 | + getCommentDirectivesReporter, |
| 5 | + isValidStyleContext, |
| 6 | +} from "../styles/context"; |
| 7 | +import type { RuleContext, RuleListener } from "../types"; |
| 8 | +import type { VGlobalPseudo } from "../styles/utils/selectors"; |
| 9 | +import { |
| 10 | + isVGlobalPseudo, |
| 11 | + isPseudoEmptyArguments, |
| 12 | +} from "../styles/utils/selectors"; |
| 13 | + |
| 14 | +export = { |
| 15 | + meta: { |
| 16 | + docs: { |
| 17 | + description: "enforce `:global()`/`::v-global()` style", |
| 18 | + categories: [ |
| 19 | + // TODO: enable in next major version |
| 20 | + // "vue3-recommended" |
| 21 | + ], |
| 22 | + default: "warn", |
| 23 | + url: "https://future-architect.github.io/eslint-plugin-vue-scoped-css/rules/v-global-pseudo-style.html", |
| 24 | + }, |
| 25 | + fixable: "code", |
| 26 | + messages: { |
| 27 | + expectedGlobal: "Expected ':global()' instead of '::v-global()'.", |
| 28 | + expectedVGlobal: "Expected '::v-global()' instead of ':global()'.", |
| 29 | + }, |
| 30 | + schema: [{ enum: [":global", "::v-global"] }], |
| 31 | + type: "suggestion", |
| 32 | + }, |
| 33 | + create(context: RuleContext): RuleListener { |
| 34 | + const styles = getStyleContexts(context) |
| 35 | + .filter(isValidStyleContext) |
| 36 | + .filter((style) => style.scoped); |
| 37 | + if (!styles.length) { |
| 38 | + return {}; |
| 39 | + } |
| 40 | + const expected = (context.options[0] || ":global") as |
| 41 | + | ":global" |
| 42 | + | "::v-global"; |
| 43 | + const reporter = getCommentDirectivesReporter(context); |
| 44 | + |
| 45 | + /** |
| 46 | + * Reports the given node |
| 47 | + * @param {ASTNode} node node to report |
| 48 | + */ |
| 49 | + function report(node: VGlobalPseudo) { |
| 50 | + reporter.report({ |
| 51 | + node, |
| 52 | + loc: node.loc, |
| 53 | + messageId: |
| 54 | + expected === ":global" ? "expectedGlobal" : "expectedVGlobal", |
| 55 | + fix(fixer) { |
| 56 | + const nodeText = context.getSourceCode().text.slice(...node.range); |
| 57 | + return fixer.replaceTextRange( |
| 58 | + node.range, |
| 59 | + nodeText.replace( |
| 60 | + /^(\s*)(?::global|::v-global)(\s*\()/u, |
| 61 | + (_, prefix: string, suffix: string) => |
| 62 | + `${prefix}${expected}${suffix}` |
| 63 | + ) |
| 64 | + ); |
| 65 | + }, |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Verify the node |
| 71 | + */ |
| 72 | + function verifyNode(node: VGlobalPseudo) { |
| 73 | + if (node.value === expected) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + report(node); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Verify the style |
| 82 | + */ |
| 83 | + function verify(style: ValidStyleContext) { |
| 84 | + style.traverseSelectorNodes({ |
| 85 | + enterNode(node) { |
| 86 | + if (isVGlobalPseudo(node) && !isPseudoEmptyArguments(node)) { |
| 87 | + verifyNode(node); |
| 88 | + } |
| 89 | + }, |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + return { |
| 94 | + "Program:exit"() { |
| 95 | + for (const style of styles) { |
| 96 | + verify(style); |
| 97 | + } |
| 98 | + }, |
| 99 | + }; |
| 100 | + }, |
| 101 | +}; |
0 commit comments