eslint-plugin-vue-scoped-css
Version:
ESLint plugin for Scoped CSS in Vue.js
100 lines (99 loc) • 2.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getElements = getElements;
exports.isVElement = isVElement;
exports.isTransitionElement = isTransitionElement;
exports.isTransitionGroupElement = isTransitionGroupElement;
exports.findAttribute = findAttribute;
exports.isVDirective = isVDirective;
exports.isVBind = isVBind;
exports.getArgument = getArgument;
exports.isVDirectiveKeyV6 = isVDirectiveKeyV6;
const compat_1 = require("./compat");
function getElements(context, predicate) {
const node = (0, compat_1.getSourceCode)(context).ast;
const body = node.templateBody;
if (!body) {
return [];
}
return [...iterate(body)];
function* iterate(element) {
if (predicate(element)) {
yield element;
}
for (const child of element.children) {
if (isVElement(child)) {
yield* iterate(child);
}
}
}
}
function isVElement(node) {
return (node === null || node === void 0 ? void 0 : node.type) === "VElement";
}
function isTransitionElement(element) {
return (element.type === "VElement" &&
(element.name === "transition" || element.rawName === "Transition"));
}
function isTransitionGroupElement(element) {
return (element.type === "VElement" &&
(element.name === "transition-group" ||
element.rawName === "TransitionGroup"));
}
function findAttribute(node, name) {
if (node.type === "VElement") {
return findAttribute(node.startTag, name);
}
return (node.attributes.find((attr) => {
if (isVDirective(attr)) {
if (isVBind(attr.key)) {
return getArgument(attr.key) === name;
}
}
else {
return attr.key.name === name;
}
return false;
}) || null);
}
function isVDirective(node) {
return node.type === "VAttribute" && node.directive;
}
function isVBind(key) {
if (isVDirectiveKeyV6(key)) {
if (key.name.name !== "bind") {
return false;
}
return true;
}
if (key.name !== "bind") {
return false;
}
return true;
}
function getArgument(key) {
if (isVDirectiveKeyV6(key)) {
const argument = key.argument;
if (argument == null) {
return null;
}
if (argument.type === "VExpressionContainer") {
return null;
}
if (argument.type === "VIdentifier") {
return argument.name;
}
return null;
}
const argument = key.argument;
if (argument == null) {
return null;
}
if (/^\[.*\]$/u.test(argument)) {
return null;
}
return argument || "";
}
function isVDirectiveKeyV6(node) {
return typeof node.name !== "string";
}