UNPKG

stylelint-value-no-exposed-custom-properties

Version:

A stylelint rule to disallow usage of exposed custom properties

1 lines 11.3 kB
{"version":3,"file":"index.es.mjs","sources":["lib/get-custom-properties-from-root.js","lib/get-custom-properties-from-imports.js","index.js"],"sourcesContent":["// return custom selectors from the css root, conditionally removing them\nexport default (root) => {\n // initialize custom selectors\n const customProperties = {}\n\n // for each custom property declaration\n root.walkDecls(customPropertyRegExp, (decl) => {\n const { prop } = decl\n\n // write the parsed value to the custom property\n customProperties[prop] = decl.value\n })\n\n // return all custom properties, preferring :root properties over html properties\n return customProperties\n}\n\n// match custom properties\nconst customPropertyRegExp = /^--[A-z][\\w-]*$/\n","import fs from 'fs'\nimport path from 'path'\nimport postcss from 'postcss'\nimport getCustomPropertiesFromRoot from './get-custom-properties-from-root'\n\n/* Get Custom Properties from CSS File\n/* ========================================================================== */\n\nasync function getCustomPropertiesFromCSSFile(from) {\n const css = await readFile(from)\n const root = postcss.parse(css, { from })\n\n return getCustomPropertiesFromRoot(root)\n}\n\n/* Get Custom Properties from Object\n/* ========================================================================== */\n\nfunction getCustomPropertiesFromObject(object) {\n const customProperties = Object.assign(\n {},\n Object(object).customProperties,\n Object(object)['custom-properties']\n )\n\n return customProperties\n}\n\n/* Get Custom Properties from JSON file\n/* ========================================================================== */\n\nasync function getCustomPropertiesFromJSONFile(from) {\n const object = await readJSON(from)\n\n return getCustomPropertiesFromObject(object)\n}\n\n/* Get Custom Properties from JS file\n/* ========================================================================== */\n\nasync function getCustomPropertiesFromJSFile(from) {\n const object = await import(from)\n\n return getCustomPropertiesFromObject(object)\n}\n\n/* Get Custom Properties from Sources\n/* ========================================================================== */\n\nexport default function getCustomPropertiesFromSources(sources) {\n return sources\n .map((source) => {\n if (source instanceof Promise) {\n return source\n } else if (source instanceof Function) {\n return source()\n }\n\n // read the source as an object\n const opts = source === Object(source) ? source : { from: String(source) }\n\n // skip objects with Custom Properties\n if (opts.customProperties || opts['custom-properties']) {\n return opts\n }\n\n // source pathname\n const from = path.resolve(String(opts.from || ''))\n\n // type of file being read from\n const type = (opts.type || path.extname(from).slice(1)).toLowerCase()\n\n return { type, from }\n })\n .reduce(async (customProperties, source) => {\n const { type, from } = await source\n\n if (type === 'css') {\n return Object.assign(\n await customProperties,\n await getCustomPropertiesFromCSSFile(from)\n )\n }\n\n if (type === 'js') {\n return Object.assign(\n await customProperties,\n await getCustomPropertiesFromJSFile(from)\n )\n }\n\n if (type === 'json') {\n return Object.assign(\n await customProperties,\n await getCustomPropertiesFromJSONFile(from)\n )\n }\n\n return Object.assign(\n await customProperties,\n await getCustomPropertiesFromObject(await source)\n )\n }, {})\n}\n\n/* Promise-ified utilities\n/* ========================================================================== */\n\nconst readFile = (from) =>\n new Promise((resolve, reject) => {\n fs.readFile(from, 'utf8', (error, result) => {\n if (error) {\n reject(error)\n } else {\n resolve(result)\n }\n })\n })\n\nconst readJSON = async (from) => JSON.parse(await readFile(from))\n","import stylelint from 'stylelint'\nimport getCustomPropertiesFromRoot from './lib/get-custom-properties-from-root'\nimport getCustomPropertiesFromImports from './lib/get-custom-properties-from-imports'\n\nconst ruleName = 'plugin/value-no-exposed-custom-properties'\n\nfunction isCustomPropertyDecl(prop) {\n return prop.includes('--')\n}\n\nfunction prepareValue(value) {\n if (typeof value === 'string') {\n value = value.toUpperCase()\n }\n\n return value\n}\n\nfunction foundCustomPropertyInValue(properies, value) {\n value = prepareValue(value)\n\n for (let i in properies) {\n const property = prepareValue(properies[i])\n\n if (value.includes(property)) return i\n }\n}\n\nasync function getCustomProperties(root, importFrom) {\n // all custom properties from the file and imports\n return Object.assign(\n {},\n await getCustomPropertiesFromImports(importFrom),\n getCustomPropertiesFromRoot(root)\n )\n}\n\nfunction checkDeclOnCustomProperty(decl, customProperties, result) {\n const foundVariable = foundCustomPropertyInValue(customProperties, decl.value)\n\n if (foundVariable) {\n stylelint.utils.report({\n ruleName,\n result,\n node: decl,\n message:\n 'The value (or a part of it) should be presented as a custom property: \"' +\n decl.value +\n '\" is \"' +\n foundVariable +\n '\" (' +\n ruleName +\n ')',\n })\n }\n}\n\nmodule.exports = stylelint.createPlugin(ruleName, function (method, options) {\n return async function (root, result) {\n if (!method) return\n\n const importFrom = options && options.importFrom ? options.importFrom : []\n const customProperties = await getCustomProperties(root, importFrom)\n\n root.walkRules(function (rule) {\n rule.walkDecls(function (decl) {\n // Skip custom properties declarations\n if (!isCustomPropertyDecl(decl.prop)) {\n checkDeclOnCustomProperty(decl, customProperties, result)\n }\n })\n })\n }\n})\n"],"names":["root","customProperties","walkDecls","customPropertyRegExp","decl","prop","value","getCustomPropertiesFromCSSFile","from","css","readFile","postcss","parse","getCustomPropertiesFromRoot","getCustomPropertiesFromObject","object","Object","assign","getCustomPropertiesFromJSONFile","readJSON","getCustomPropertiesFromJSFile","getCustomPropertiesFromSources","sources","map","source","Promise","Function","opts","String","path","resolve","type","extname","slice","toLowerCase","reduce","reject","fs","error","result","JSON","ruleName","isCustomPropertyDecl","includes","prepareValue","toUpperCase","foundCustomPropertyInValue","properies","i","property","getCustomProperties","importFrom","getCustomPropertiesFromImports","checkDeclOnCustomProperty","foundVariable","stylelint","utils","report","node","message","module","exports","createPlugin","method","options","walkRules","rule"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AACA,mCAAgBA,IAAD,IAAU;;EAEvB,MAAMC,gBAAgB,GAAG,EAAzB,CAFuB;;EAKvBD,IAAI,CAACE,SAAL,CAAeC,oBAAf,EAAsCC,IAAD,IAAU;IAC7C,MAAQC,IAAR,GAAiBD,IAAjB,CAAQC,IAAR,CAD6C;;IAI7CJ,gBAAgB,CAACI,IAAD,CAAhB,GAAyBD,IAAI,CAACE,KAA9B;GAJF,EALuB;;EAavB,OAAOL,gBAAP;CAbF;;AAiBA,MAAME,oBAAoB,GAAG,iBAA7B;;ACbA;;;SAGeI;;;;;;;;sDAAf,WAA8CC,IAA9C,EAAoD;IAClD,MAAMC,GAAG,SAASC,QAAQ,CAACF,IAAD,CAA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