🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

postcss-custom-selectors

Package Overview
Dependencies
Maintainers
4
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postcss-custom-selectors - npm Package Compare versions

Comparing version

to
6.0.0

6

CHANGELOG.md
# Changes to PostCSS Custom Selectors
### 6.0.0 (January 12, 2021)
- Added: Support for PostCSS v8
- Added: Support for Node v10+
- Fixed: importing from multiple sources ([#42](https://github.com/postcss/postcss-custom-selectors/pull/42))
### 5.1.2 (September 20, 2018)

@@ -4,0 +10,0 @@

504

index.cjs.js
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var parser = require('postcss-selector-parser');
var fs = require('fs');
var path = require('path');
var postcss = require('postcss');
var parser = _interopDefault(require('postcss-selector-parser'));
var fs = _interopDefault(require('fs'));
var path = _interopDefault(require('path'));
var postcss = _interopDefault(require('postcss'));
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
try {
var info = gen[key](arg);
var value = info.value;
} catch (error) {
reject(error);
return;
}
if (info.done) {
resolve(value);
} else {
Promise.resolve(value).then(_next, _throw);
}
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
}
});
}
n['default'] = e;
return Object.freeze(n);
}
function _asyncToGenerator(fn) {
return function () {
var self = this,
args = arguments;
return new Promise(function (resolve, reject) {
var gen = fn.apply(self, args);
var parser__default = /*#__PURE__*/_interopDefaultLegacy(parser);
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
var postcss__default = /*#__PURE__*/_interopDefaultLegacy(postcss);
function _next(value) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
}
function _throw(err) {
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
}
_next(undefined);
});
};
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === 'function') {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function (key) {
_defineProperty(target, key, source[key]);
});
}
return target;
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
/* Return a Selectors AST from a Selectors String

@@ -123,3 +40,3 @@ /* ========================================================================== */

let selectorAST;
parser(selectors => {
parser__default['default'](selectors => {
selectorAST = selectors;

@@ -137,8 +54,4 @@ }).processSync(selectorString);

// extract the name and selectors from the params of the custom selector
const _node$params$match = node.params.match(customSelectorParamsRegExp),
_node$params$match2 = _slicedToArray(_node$params$match, 3),
name = _node$params$match2[1],
selectors = _node$params$match2[2]; // write the parsed selectors to the custom selector
const [, name, selectors] = node.params.match(customSelectorParamsRegExp); // write the parsed selectors to the custom selector
customSelectors[name] = getSelectorsAstFromSelectorsString(selectors); // conditionally remove the custom selector atrule

@@ -181,42 +94,24 @@

for (const index in selector.nodes) {
const _selector$nodes$index = selector.nodes[index],
value = _selector$nodes$index.value,
nodes = _selector$nodes$index.nodes;
const {
value,
nodes
} = selector.nodes[index];
if (value in customSelectors) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
for (const replacementSelector of customSelectors[value].nodes) {
const selectorClone = selector.clone();
selectorClone.nodes.splice(index, 1, ...replacementSelector.clone().nodes.map(node => {
// use spacing from the current usage
node.spaces = { ...selector.nodes[index].spaces
};
return node;
}));
const retranspiledSelectors = transformSelector(selectorClone, customSelectors);
adjustNodesBySelectorEnds(selectorClone.nodes, Number(index));
try {
for (var _iterator = customSelectors[value].nodes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
const replacementSelector = _step.value;
const selectorClone = selector.clone();
selectorClone.nodes.splice(index, 1, ...replacementSelector.clone().nodes.map(node => {
// use spacing from the current usage
node.spaces = _objectSpread({}, selector.nodes[index].spaces);
return node;
}));
const retranspiledSelectors = transformSelector(selectorClone, customSelectors);
adjustNodesBySelectorEnds(selectorClone.nodes, Number(index));
if (retranspiledSelectors.length) {
transpiledSelectors.push(...retranspiledSelectors);
} else {
transpiledSelectors.push(selectorClone);
}
if (retranspiledSelectors.length) {
transpiledSelectors.push(...retranspiledSelectors);
} else {
transpiledSelectors.push(selectorClone);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}

@@ -266,4 +161,4 @@

root.walkRules(customPseudoRegExp, rule => {
const selector = parser(selectors => {
transformSelectorList(selectors, customSelectors, opts);
const selector = parser__default['default'](selectors => {
transformSelectorList(selectors, customSelectors);
}).processSync(rule.selector);

@@ -292,4 +187,8 @@

function importCustomSelectorsFromCSSFile(_x) {
return _importCustomSelectorsFromCSSFile.apply(this, arguments);
async function importCustomSelectorsFromCSSFile(from) {
const css = await readFile(path__default['default'].resolve(from));
const root = postcss__default['default'].parse(css, {
from: path__default['default'].resolve(from)
});
return importCustomSelectorsFromCSSAST(root);
}

@@ -300,13 +199,2 @@ /* Import Custom Selectors from Object

function _importCustomSelectorsFromCSSFile() {
_importCustomSelectorsFromCSSFile = _asyncToGenerator(function* (from) {
const css = yield readFile(path.resolve(from));
const root = postcss.parse(css, {
from: path.resolve(from)
});
return importCustomSelectorsFromCSSAST(root);
});
return _importCustomSelectorsFromCSSFile.apply(this, arguments);
}
function importCustomSelectorsFromObject(object) {

@@ -325,4 +213,5 @@ const customSelectors = Object.assign({}, Object(object).customSelectors || Object(object)['custom-selectors']);

function importCustomSelectorsFromJSONFile(_x2) {
return _importCustomSelectorsFromJSONFile.apply(this, arguments);
async function importCustomSelectorsFromJSONFile(from) {
const object = await readJSON(path__default['default'].resolve(from));
return importCustomSelectorsFromObject(object);
}

@@ -333,13 +222,6 @@ /* Import Custom Selectors from JS file

function _importCustomSelectorsFromJSONFile() {
_importCustomSelectorsFromJSONFile = _asyncToGenerator(function* (from) {
const object = yield readJSON(path.resolve(from));
return importCustomSelectorsFromObject(object);
});
return _importCustomSelectorsFromJSONFile.apply(this, arguments);
async function importCustomSelectorsFromJSFile(from) {
const object = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require(path__default['default'].resolve(from))); });
return importCustomSelectorsFromObject(object);
}
function importCustomSelectorsFromJSFile(_x3) {
return _importCustomSelectorsFromJSFile.apply(this, arguments);
}
/* Import Custom Selectors from Sources

@@ -349,10 +231,2 @@ /* ========================================================================== */

function _importCustomSelectorsFromJSFile() {
_importCustomSelectorsFromJSFile = _asyncToGenerator(function* (from) {
const object = yield Promise.resolve(require(path.resolve(from)));
return importCustomSelectorsFromObject(object);
});
return _importCustomSelectorsFromJSFile.apply(this, arguments);
}
function importCustomSelectorsFromSources(sources) {

@@ -378,3 +252,3 @@ return sources.map(source => {

const type = (opts.type || path.extname(from).slice(1)).toLowerCase();
const type = (opts.type || path__default['default'].extname(from).slice(1)).toLowerCase();
return {

@@ -384,33 +258,27 @@ type,

};
}).reduce(
/*#__PURE__*/
function () {
var _ref = _asyncToGenerator(function* (customSelectors, source) {
const _ref2 = yield source,
type = _ref2.type,
from = _ref2.from;
}).reduce(async (customSelectorsPromise, source) => {
const customSelectors = await customSelectorsPromise;
const {
type,
from
} = await source;
if (type === 'ast') {
return Object.assign(customSelectors, importCustomSelectorsFromCSSAST(from));
}
if (type === 'ast') {
return Object.assign(customSelectors, importCustomSelectorsFromCSSAST(from));
}
if (type === 'css') {
return Object.assign(customSelectors, (yield importCustomSelectorsFromCSSFile(from)));
}
if (type === 'css') {
return Object.assign(customSelectors, await importCustomSelectorsFromCSSFile(from));
}
if (type === 'js') {
return Object.assign(customSelectors, (yield importCustomSelectorsFromJSFile(from)));
}
if (type === 'js') {
return Object.assign(customSelectors, await importCustomSelectorsFromJSFile(from));
}
if (type === 'json') {
return Object.assign(customSelectors, (yield importCustomSelectorsFromJSONFile(from)));
}
if (type === 'json') {
return Object.assign(customSelectors, await importCustomSelectorsFromJSONFile(from));
}
return Object.assign(customSelectors, importCustomSelectorsFromObject((yield source)));
});
return function (_x4, _x5) {
return _ref.apply(this, arguments);
};
}(), {});
return Object.assign(customSelectors, importCustomSelectorsFromObject(await source));
}, Promise.resolve({}));
}

@@ -421,3 +289,3 @@ /* Helper utilities

const readFile = from => new Promise((resolve, reject) => {
fs.readFile(from, 'utf8', (error, result) => {
fs__default['default'].readFile(from, 'utf8', (error, result) => {
if (error) {

@@ -431,19 +299,14 @@ reject(error);

const readJSON =
/*#__PURE__*/
function () {
var _ref3 = _asyncToGenerator(function* (from) {
return JSON.parse((yield readFile(from)));
});
const readJSON = async from => JSON.parse(await readFile(from));
return function readJSON(_x6) {
return _ref3.apply(this, arguments);
};
}();
/* Import Custom Selectors from CSS File
/* ========================================================================== */
function exportCustomSelectorsToCssFile(_x, _x2) {
return _exportCustomSelectorsToCssFile.apply(this, arguments);
async function exportCustomSelectorsToCssFile(to, customSelectors) {
const cssContent = Object.keys(customSelectors).reduce((cssLines, name) => {
cssLines.push(`@custom-selector ${name} ${customSelectors[name]};`);
return cssLines;
}, []).join('\n');
const css = `${cssContent}\n`;
await writeFile(to, css);
}

@@ -454,17 +317,9 @@ /* Import Custom Selectors from JSON file

function _exportCustomSelectorsToCssFile() {
_exportCustomSelectorsToCssFile = _asyncToGenerator(function* (to, customSelectors) {
const cssContent = Object.keys(customSelectors).reduce((cssLines, name) => {
cssLines.push(`@custom-selector ${name} ${customSelectors[name]};`);
return cssLines;
}, []).join('\n');
const css = `${cssContent}\n`;
yield writeFile(to, css);
});
return _exportCustomSelectorsToCssFile.apply(this, arguments);
async function exportCustomSelectorsToJsonFile(to, customSelectors) {
const jsonContent = JSON.stringify({
'custom-selectors': customSelectors
}, null, ' ');
const json = `${jsonContent}\n`;
await writeFile(to, json);
}
function exportCustomSelectorsToJsonFile(_x3, _x4) {
return _exportCustomSelectorsToJsonFile.apply(this, arguments);
}
/* Import Custom Selectors from Common JS file

@@ -474,16 +329,10 @@ /* ========================================================================== */

function _exportCustomSelectorsToJsonFile() {
_exportCustomSelectorsToJsonFile = _asyncToGenerator(function* (to, customSelectors) {
const jsonContent = JSON.stringify({
'custom-selectors': customSelectors
}, null, ' ');
const json = `${jsonContent}\n`;
yield writeFile(to, json);
});
return _exportCustomSelectorsToJsonFile.apply(this, arguments);
async function exportCustomSelectorsToCjsFile(to, customSelectors) {
const jsContents = Object.keys(customSelectors).reduce((jsLines, name) => {
jsLines.push(`\t\t'${escapeForJS(name)}': '${escapeForJS(customSelectors[name])}'`);
return jsLines;
}, []).join(',\n');
const js = `module.exports = {\n\tcustomSelectors: {\n${jsContents}\n\t}\n};\n`;
await writeFile(to, js);
}
function exportCustomSelectorsToCjsFile(_x5, _x6) {
return _exportCustomSelectorsToCjsFile.apply(this, arguments);
}
/* Import Custom Selectors from Module JS file

@@ -493,17 +342,10 @@ /* ========================================================================== */

function _exportCustomSelectorsToCjsFile() {
_exportCustomSelectorsToCjsFile = _asyncToGenerator(function* (to, customSelectors) {
const jsContents = Object.keys(customSelectors).reduce((jsLines, name) => {
jsLines.push(`\t\t'${escapeForJS(name)}': '${escapeForJS(customSelectors[name])}'`);
return jsLines;
}, []).join(',\n');
const js = `module.exports = {\n\tcustomSelectors: {\n${jsContents}\n\t}\n};\n`;
yield writeFile(to, js);
});
return _exportCustomSelectorsToCjsFile.apply(this, arguments);
async function exportCustomSelectorsToMjsFile(to, customSelectors) {
const mjsContents = Object.keys(customSelectors).reduce((mjsLines, name) => {
mjsLines.push(`\t'${escapeForJS(name)}': '${escapeForJS(customSelectors[name])}'`);
return mjsLines;
}, []).join(',\n');
const mjs = `export const customSelectors = {\n${mjsContents}\n};\n`;
await writeFile(to, mjs);
}
function exportCustomSelectorsToMjsFile(_x7, _x8) {
return _exportCustomSelectorsToMjsFile.apply(this, arguments);
}
/* Export Custom Selectors to Destinations

@@ -513,66 +355,46 @@ /* ========================================================================== */

function _exportCustomSelectorsToMjsFile() {
_exportCustomSelectorsToMjsFile = _asyncToGenerator(function* (to, customSelectors) {
const mjsContents = Object.keys(customSelectors).reduce((mjsLines, name) => {
mjsLines.push(`\t'${escapeForJS(name)}': '${escapeForJS(customSelectors[name])}'`);
return mjsLines;
}, []).join(',\n');
const mjs = `export const customSelectors = {\n${mjsContents}\n};\n`;
yield writeFile(to, mjs);
});
return _exportCustomSelectorsToMjsFile.apply(this, arguments);
}
function exportCustomSelectorsToDestinations(customSelectors, destinations) {
return Promise.all(destinations.map(
/*#__PURE__*/
function () {
var _ref = _asyncToGenerator(function* (destination) {
if (destination instanceof Function) {
yield destination(defaultCustomSelectorsToJSON(customSelectors));
} else {
// read the destination as an object
const opts = destination === Object(destination) ? destination : {
to: String(destination)
}; // transformer for custom selectors into a JSON-compatible object
return Promise.all(destinations.map(async destination => {
if (destination instanceof Function) {
await destination(defaultCustomSelectorsToJSON(customSelectors));
} else {
// read the destination as an object
const opts = destination === Object(destination) ? destination : {
to: String(destination)
}; // transformer for custom selectors into a JSON-compatible object
const toJSON = opts.toJSON || defaultCustomSelectorsToJSON;
const toJSON = opts.toJSON || defaultCustomSelectorsToJSON;
if ('customSelectors' in opts) {
// write directly to an object as customSelectors
opts.customSelectors = toJSON(customSelectors);
} else if ('custom-selectors' in opts) {
// write directly to an object as custom-selectors
opts['custom-selectors'] = toJSON(customSelectors);
} else {
// destination pathname
const to = String(opts.to || ''); // type of file being written to
if ('customSelectors' in opts) {
// write directly to an object as customSelectors
opts.customSelectors = toJSON(customSelectors);
} else if ('custom-selectors' in opts) {
// write directly to an object as custom-selectors
opts['custom-selectors'] = toJSON(customSelectors);
} else {
// destination pathname
const to = String(opts.to || ''); // type of file being written to
const type = (opts.type || path.extname(opts.to).slice(1)).toLowerCase(); // transformed custom selectors
const type = (opts.type || path__default['default'].extname(opts.to).slice(1)).toLowerCase(); // transformed custom selectors
const customSelectorsJSON = toJSON(customSelectors);
const customSelectorsJSON = toJSON(customSelectors);
if (type === 'css') {
yield exportCustomSelectorsToCssFile(to, customSelectorsJSON);
}
if (type === 'css') {
await exportCustomSelectorsToCssFile(to, customSelectorsJSON);
}
if (type === 'js') {
yield exportCustomSelectorsToCjsFile(to, customSelectorsJSON);
}
if (type === 'js') {
await exportCustomSelectorsToCjsFile(to, customSelectorsJSON);
}
if (type === 'json') {
yield exportCustomSelectorsToJsonFile(to, customSelectorsJSON);
}
if (type === 'json') {
await exportCustomSelectorsToJsonFile(to, customSelectorsJSON);
}
if (type === 'mjs') {
yield exportCustomSelectorsToMjsFile(to, customSelectorsJSON);
}
if (type === 'mjs') {
await exportCustomSelectorsToMjsFile(to, customSelectorsJSON);
}
}
});
return function (_x9) {
return _ref.apply(this, arguments);
};
}()));
}
}));
}

@@ -590,3 +412,3 @@ /* Helper utilities

const writeFile = (to, text) => new Promise((resolve, reject) => {
fs.writeFile(to, text, error => {
fs__default['default'].writeFile(to, text, error => {
if (error) {

@@ -602,3 +424,3 @@ reject(error);

var index = postcss.plugin('postcss-custom-selectors', opts => {
const postcssCustomSelectors = opts => {
// whether to preserve custom selectors and rules using them

@@ -612,23 +434,21 @@ const preserve = Boolean(Object(opts).preserve); // sources to import custom selectors from

const customSelectorsPromise = importCustomSelectorsFromSources(importFrom);
return (
/*#__PURE__*/
function () {
var _ref = _asyncToGenerator(function* (root) {
const customProperties = Object.assign((yield customSelectorsPromise), getCustomSelectors(root, {
preserve
}));
yield exportCustomSelectorsToDestinations(customProperties, exportTo);
transformRules(root, customProperties, {
preserve
});
return {
postcssPlugin: 'postcss-custom-selectors',
async Once(root) {
const customProperties = Object.assign({}, await customSelectorsPromise, getCustomSelectors(root, {
preserve
}));
await exportCustomSelectorsToDestinations(customProperties, exportTo);
transformRules(root, customProperties, {
preserve
});
}
return function (_x) {
return _ref.apply(this, arguments);
};
}()
);
});
};
};
module.exports = index;
postcssCustomSelectors.postcss = true;
module.exports = postcssCustomSelectors;
//# sourceMappingURL=index.cjs.js.map
{
"name": "postcss-custom-selectors",
"version": "5.1.2",
"version": "6.0.0",
"description": "Use Custom Selectors in CSS",

@@ -30,7 +30,6 @@ "author": "Jonathan Neal <jonathantneal@hotmail.com>",

"engines": {
"node": ">=6.0.0"
"node": ">=10.0.0"
},
"dependencies": {
"postcss": "^7.0.2",
"postcss-selector-parser": "^5.0.0-rc.3"
"postcss-selector-parser": "^6.0.4"
},

@@ -41,10 +40,14 @@ "devDependencies": {

"@babel/preset-env": "^7.1.0",
"babel-eslint": "^9.0.0",
"eslint": "^5.6.0",
"@rollup/plugin-babel": "^5.2.1",
"babel-eslint": "^10.0.0",
"eslint": "^7.0.0",
"eslint-config-dev": "^2.0.0",
"postcss-tape": "^2.2.0",
"postcss": "^8.1.2",
"postcss-tape": "^6.0.0",
"pre-commit": "^1.2.2",
"rollup": "^0.66.1",
"rollup-plugin-babel": "^4.0.3"
"rollup": "^2.0.0"
},
"peerDependencies": {
"postcss": "^8.1.2"
},
"eslintConfig": {

@@ -51,0 +54,0 @@ "extends": "dev",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet