UNPKG

eslint-plugin-clsx

Version:
105 lines (104 loc) 4.18 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var utils_exports = {}; __export(utils_exports, { chunkBy: () => chunkBy, extractClsxOptions: () => extractClsxOptions, findClsxImport: () => findClsxImport, getClsxUsages: () => getClsxUsages }); module.exports = __toCommonJS(utils_exports); var import_types = require("@typescript-eslint/types"); var R = __toESM(require("remeda")); function chunkBy(collection, chunker) { const res = []; const temp = [collection[0]]; let lastChunkMarker = chunker(collection[0]); for (const el of collection.slice(1)) { const currentChunkMarker = chunker(el); if (currentChunkMarker === lastChunkMarker) { temp.push(el); continue; } if (temp.length !== 0) { res.push([...temp]); temp.length = 0; temp.push(el); } lastChunkMarker = currentChunkMarker; } if (temp.length) res.push(temp); return res; } function findClsxImport(importNode, clsxOptions) { if (typeof importNode.source.value !== "string") { throw new Error("import source value is not a string"); } const names = clsxOptions[importNode.source.value]; const importNames = typeof names === "string" ? [names] : names; return importNames == null ? void 0 : importNames.map((name) => { if (name === "default") { const defaultSpecifier = importNode.specifiers.find( (s) => s.type === import_types.TSESTree.AST_NODE_TYPES.ImportDefaultSpecifier ); return defaultSpecifier == null ? void 0 : defaultSpecifier.local.name; } const named = importNode.specifiers.find( (s) => s.type === import_types.TSESTree.AST_NODE_TYPES.ImportSpecifier && "name" in s.imported && s.imported.name === name ); return named == null ? void 0 : named.local.name; }).filter(R.isDefined); } function isCallExpressionWithName(name) { return (node) => !!node && node.type === import_types.TSESTree.AST_NODE_TYPES.CallExpression && "name" in node.callee && node.callee.name === name; } function getClsxUsages(importNode, sourceCode, assignedClsxNames) { return assignedClsxNames.flatMap( (assignedClsxName) => { var _a, _b; return (_b = (_a = sourceCode.scopeManager) == null ? void 0 : _a.getDeclaredVariables(importNode).find((variable) => variable.name === assignedClsxName)) == null ? void 0 : _b.references.map( (ref) => ref.identifier.parent ).filter(R.isDefined).filter(isCallExpressionWithName(assignedClsxName)); } ).filter(R.isDefined); } function extractClsxOptions(context) { var _a; return (_a = context.settings.clsxOptions) != null ? _a : { clsx: ["default", "clsx"], classnames: "default" }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { chunkBy, extractClsxOptions, findClsxImport, getClsxUsages });