UNPKG

eslint-plugin-vue-scoped-css

Version:
210 lines (209 loc) 6.72 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Template = exports.Interpolation = void 0; const utils_1 = require("../utils"); const interpolation_1 = require("./interpolation"); Object.defineProperty(exports, "Interpolation", { enumerable: true, get: function () { return interpolation_1.Interpolation; } }); const selector_1 = __importDefault(require("./selector")); const at_rule_params_1 = __importDefault(require("./at-rule-params")); const decl_value_1 = __importDefault(require("./decl-value")); const utils_2 = require("../../utils/utils"); class Template { constructor(elements) { this.string = null; this._text = null; this._regexp = null; this.elements = elements .filter(utils_2.isDefined) .filter((e) => e !== "") .reduce((l, e) => { if (l.length) { const lastIndex = l.length - 1; const last = l[lastIndex]; if (typeof e === "string" && typeof last === "string") { l[lastIndex] = last + e; return l; } if (typeof e !== "string" && typeof last !== "string") { l[lastIndex] = new interpolation_1.Interpolation(last.text + e.text); return l; } } l.push(e); return l; }, []); if (this.elements.length === 1) { const element = this.elements[0]; if (typeof element === "string") { this.string = element; } } else if (this.elements.length === 0) { this.string = ""; } } static of(value) { return new Template([value]); } static ofSelector(node) { return new Template((0, selector_1.default)(node)); } static ofParams(node) { return new Template((0, at_rule_params_1.default)(node.paramsText.trim(), node.lang)); } static ofDeclValue(nodeOrText, lang) { if (typeof nodeOrText === "string") { return new Template((0, decl_value_1.default)(nodeOrText, lang || "")); } return new Template((0, decl_value_1.default)(nodeOrText.value, nodeOrText.lang)); } static ofNode(node) { if (node.type === "VLiteral") { return Template.of(node.value); } if (node.type === "Literal") { return Template.of(`${node.value}`); } if (node.type === "TemplateLiteral") { const elements = []; for (const element of node.quasis) { elements.push(element.value.cooked || element.value.raw); elements.push(new interpolation_1.Interpolation("${}")); } elements.pop(); return new Template(elements); } if (node.type === "BinaryExpression" && node.operator === "+") { const left = Template.ofNode(node.left); const right = Template.ofNode(node.right); if (left && right) { return left.concat(right); } else if (left) { return left.concat(Template.interpolationTemplate); } else if (right) { return Template.interpolationTemplate.concat(right); } } return null; } match(o) { if (this.string != null && o.string != null) { return this.string === o.string; } if (this.regexp.test(o.text)) { return true; } if (o.regexp.test(this.text)) { return true; } return false; } matchString(s) { if (this.string != null) { return this.string === s; } if (this.regexp.test(s)) { return true; } return false; } endsWith(s) { if (this.string != null) { return this.string.endsWith(s); } const last = this.elements[this.elements.length - 1]; if (typeof last === "string") { if (last.endsWith(s)) { return true; } } return true; } concat(o) { if (typeof o === "string") { return new Template([...this.elements, o]); } return new Template([...this.elements, ...o.elements]); } hasString(s) { return this.elements.some((e) => { if (typeof e === "string") { return e.includes(s); } return false; }); } divide(s) { const results = []; let elements = []; for (const e of this.elements) { if (typeof e === "string") { if (e.search(s) >= 0) { const list = e.split(s); elements.push(list.shift()); results.push(new Template(elements)); while (list.length > 1) { results.push(new Template([list.shift()])); } elements = [list.shift()]; } else { elements.push(e); } } else { elements.push(e); } } if (elements.length) { results.push(new Template(elements)); } return results; } toLowerCase() { return new Template(this.elements.map((e) => { if (typeof e === "string") { return e.toLowerCase(); } return e; })); } get text() { return this._text || (this._text = this.buildText()); } get regexp() { return this._regexp || (this._regexp = this.buildRegexp()); } buildRegexp() { let expr = "^"; for (const e of this.elements) { if (typeof e === "string") { expr += (0, utils_1.escapeRegExp)(e); } else { expr += "[\\s\\S]*"; } } expr += "$"; return new RegExp(expr, "u"); } buildText() { let text = ""; for (const e of this.elements) { if (typeof e === "string") { text += e; } else { text += "\ue000"; } } return text; } } exports.Template = Template; Template.interpolationTemplate = new Template([new interpolation_1.Interpolation("?")]);