UNPKG

@stylable/core

Version:

CSS for Components

104 lines 5.24 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.hooks = exports.diagnostics = void 0; const feature_1 = require("./feature"); const native_reserved_lists_1 = require("../native-reserved-lists"); const STCustomState = __importStar(require("./st-custom-state")); const CSSType = __importStar(require("./css-type")); const diagnostics_1 = require("../diagnostics"); const is_vendor_prefixed_1 = __importDefault(require("is-vendor-prefixed")); exports.diagnostics = { UNKNOWN_STATE_USAGE: (0, diagnostics_1.createDiagnosticReporter)('08001', 'error', (name) => `unknown pseudo-class "${name}"`), }; // HOOKS exports.hooks = (0, feature_1.createFeature)({ transformSelectorNode({ context, selectorContext }) { const { inferredSelector, node, ruleOrAtRule, scopeSelectorAst } = selectorContext; if (node.type !== 'pseudo_class') { return; } // find matching custom state const name = node.value; const inferredState = inferredSelector.getPseudoClasses({ name })[name]; const foundCustomState = !!inferredState; if (inferredState) { if (selectorContext.transform) { STCustomState.transformPseudoClassToCustomState(inferredState.state, inferredState.meta, node.value, node, inferredState.meta.namespace, context.resolver, context.diagnostics, ruleOrAtRule); } } // handle nested pseudo classes if (node.nodes && !foundCustomState) { if (node.value === 'global') { // ignore `:st-global` since it is handled after the mixin transformation if (selectorContext.experimentalSelectorInference) { selectorContext.setNextSelectorScope([ { _kind: 'css', meta: context.meta, symbol: CSSType.createSymbol({ name: '*' }), }, ], node); } return; } else { const hasSubSelectors = node.value.match(/not|any|-\w+?-any|matches|is|where|has|local|nth-child|nth-last-child/); // pickup all nested selectors except nth initial selector const innerSelectors = (node.nodes[0] && node.nodes[0].type === `nth` ? node.nodes.slice(1) : node.nodes); const nestedContext = selectorContext.createNestedContext(innerSelectors, selectorContext.inferredSelector); scopeSelectorAst(nestedContext); // change selector inference if (hasSubSelectors && innerSelectors.length) { if (selectorContext.experimentalSelectorInference && !node.value.match(/not|has/)) { // set inferred to subject of nested selectors + prev compound const prevNode = selectorContext.lastInferredSelectorNode; if (prevNode && prevNode.type !== 'combinator') { nestedContext.inferredMultipleSelectors.add(selectorContext.inferredSelector); } selectorContext.setNextSelectorScope(nestedContext.inferredMultipleSelectors, node); } // legacy: delegate elements of first selector selectorContext.elements[selectorContext.selectorIndex].push(...nestedContext.elements[0]); } } } // warn unknown state if (!foundCustomState && !native_reserved_lists_1.nativePseudoClasses.includes(node.value) && !(0, is_vendor_prefixed_1.default)(node.value) && !selectorContext.isDuplicateStScopeDiagnostic()) { context.diagnostics.report(exports.diagnostics.UNKNOWN_STATE_USAGE(node.value), { node: ruleOrAtRule, word: node.value, }); } }, }); //# sourceMappingURL=css-pseudo-class.js.map