Skip to content

Instantly share code, notes, and snippets.

@lahmatiy
lahmatiy / concat-ast.js
Created April 11, 2018 16:07
Concat AST with CSSTree. Note that CSSTree lost an original formatting on parsing.
const csstree = require('css-tree');
const ast1 = csstree.parse('.a { color: red }', {
filename: './a.css',
positions: true
});
const ast2 = csstree.parse('.b { color: red }', {
filename: './b.css',
positions: true
});
@lahmatiy
lahmatiy / prop-types-patch.js
Last active July 9, 2018 20:01
webpack loader to patch the prop-types to get an information about the propTypes definition in a runtime (UPD: you can use ready to use package https://github.com/avito-tech/prop-types-definition)
// UPDATE: you can use ready to use package prop-types-definition
// https://github.com/avito-tech/prop-types-definition
module.exports = function(content) {
return content + `
(function(ReactPropTypes) {
function unwrapValueItem(value) {
if (value) {
if (typeof value.getTypeDefinition === 'function') {
return value.getTypeDefinition();
const cssTree = require("[email protected]");
function getSelectorParent(selector) {
const selectorAst = cssTree.parse(selector, { context: 'selector' });
// solution #1
// selectorAst.children.prevUntil(selectorAst.children.tail, (node, item, list) => {
// list.remove(item);
// return node.type === 'Combinator' || node.type === 'WhiteSpace';
// });
const cssTree = require("[email protected]");
function getSelectorParent(selector) {
const selectorAst = cssTree.parse(selector, { context: 'selector' });
// solution #1
// selectorAst.children.prevUntil(selectorAst.children.tail, (node, item, list) => {
// list.remove(item);
// return node.type === 'Combinator' || node.type === 'WhiteSpace';
// });
@lahmatiy
lahmatiy / get-selector-parent.js
Created January 1, 2019 21:41
Get a selector parent selector with csstree (an issue https://twitter.com/peterbe/status/1079854825880326144)
const cssTree = require("[email protected]");
function getSelectorParent(selector) {
const selectorAst = cssTree.parse(selector, { context: 'selector' });
// solution #1
selectorAst.children.prevUntil(selectorAst.children.tail, (node, item, list) => {
list.remove(item);
return node.type === 'Combinator' || node.type === 'WhiteSpace';
});
const csstree = require("css-tree");
const cssDefinitions = ["color"];
const ast = csstree.parse(`
.some-selector {
color: black;
border: 1px solid #fff;
}
`, { parseValue: false });
csstree.walk(ast, {