Skip to content

Commit

Permalink
fix: Update postcss-selector-parser. Re-export exposed libraries.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseppstein committed Apr 14, 2018
1 parent 86e1348 commit 4e28d92
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/opticss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"debug": "^2.6.8",
"object.values": "^1.0.4",
"postcss": "^6.0.21",
"postcss-selector-parser": "^3.1.1",
"postcss-selector-parser": "^4.0.0-rc.0",
"source-map": "^0.6.1",
"specificity": "^0.3.2",
"typescript-collections": "^1.2.5",
Expand Down
9 changes: 9 additions & 0 deletions packages/opticss/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import * as postcss from "postcss";
import * as postcssSelectorParser from "postcss-selector-parser";
import * as TSCollections from "typescript-collections";

export * from "./CssFile";
export * from "./errors";
export * from "./query";
Expand All @@ -10,3 +14,8 @@ export {
TimingData,
Optimizer,
} from "./Optimizer";
export {
postcss,
postcssSelectorParser,
TSCollections,
};
50 changes: 25 additions & 25 deletions packages/opticss/src/parseSelector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as postcss from "postcss";
import * as selectorParser from "postcss-selector-parser";
import * as postcssSelectorParser from "postcss-selector-parser";

import { isRule } from "./util/cssIntrospection";

Expand All @@ -9,18 +9,18 @@ const {
isPseudoElement,
isRoot,
isSelector,
} = selectorParser;
} = postcssSelectorParser;

export interface CombinatorAndSelector<SelectorType> {
combinator: selectorParser.Combinator;
combinator: postcssSelectorParser.Combinator;
selector: SelectorType;
}

export type CombinatorAndCompoundSelector = CombinatorAndSelector<CompoundSelector>;

export class CombinedSelector<T> {
next?: CombinatorAndSelector<T>;
setNext(combinator: selectorParser.Combinator, selector: T) {
setNext(combinator: postcssSelectorParser.Combinator, selector: T) {
this.next = {
combinator: combinator,
selector: selector,
Expand All @@ -33,8 +33,8 @@ export class CombinedSelector<T> {
* `CompoundSelector` in the chan, and the combinator that connects them.
*/
export class CompoundSelector extends CombinedSelector<CompoundSelector> {
nodes: selectorParser.Node[];
pseudoelement?: selectorParser.Pseudo;
nodes: postcssSelectorParser.Node[];
pseudoelement?: postcssSelectorParser.Pseudo;
next?: CombinatorAndCompoundSelector;

constructor() {
Expand All @@ -58,15 +58,15 @@ export class CompoundSelector extends CombinedSelector<CompoundSelector> {
* Add a node to `CompoundSelector`.
* @param Simple selector node to add.
*/
addNode(node: selectorParser.Node) {
addNode(node: postcssSelectorParser.Node) {
this.nodes.push(node);
}

/**
* Set pseudo element type on this `CompoundSelector`.
* @param The `selectorParser.Pseudo` to assign.
*/
setPseudoelement(pseudo: selectorParser.Pseudo) {
setPseudoelement(pseudo: postcssSelectorParser.Pseudo) {
this.pseudoelement = pseudo;
}

Expand All @@ -76,7 +76,7 @@ export class CompoundSelector extends CombinedSelector<CompoundSelector> {
* @param combinator The combinator connecting these two selectors.
* @param reference The `CompoundSelector` to insert this selector before.
*/
insertBefore(other: CompoundSelector, combinator: selectorParser.Combinator, reference: CompoundSelector): boolean {
insertBefore(other: CompoundSelector, combinator: postcssSelectorParser.Combinator, reference: CompoundSelector): boolean {
if (this.next && this.next.selector === reference) {
let otherEnd = other.lastSibling;
otherEnd.next = {
Expand All @@ -98,7 +98,7 @@ export class CompoundSelector extends CombinedSelector<CompoundSelector> {
* @param selector The `CompoundSelector` to append.
* @return This `CompoundSelector`.
*/
append(combinator: selectorParser.Combinator, selector: CompoundSelector): CompoundSelector {
append(combinator: postcssSelectorParser.Combinator, selector: CompoundSelector): CompoundSelector {
this.lastSibling.next = {
combinator: combinator,
selector: selector,
Expand All @@ -113,9 +113,9 @@ export class CompoundSelector extends CombinedSelector<CompoundSelector> {
*/
mergeNodes(other: CompoundSelector): CompoundSelector {
let foundNodes = new Set<string>();
let pseudos: selectorParser.Pseudo[] = [];
let nodes: selectorParser.Node[] = [];
let filterNodes = function(node: selectorParser.Node) {
let pseudos: postcssSelectorParser.Pseudo[] = [];
let nodes: postcssSelectorParser.Node[] = [];
let filterNodes = function(node: postcssSelectorParser.Node) {
let nodeStr = node.toString();
if (!foundNodes.has(nodeStr)) {
foundNodes.add(nodeStr);
Expand Down Expand Up @@ -225,7 +225,7 @@ export class ParsedSelector {
return earlyReturn;
}

eachSelectorNode<EarlyReturnType>(callback: (node: selectorParser.Node) => EarlyReturnType | undefined): EarlyReturnType | undefined {
eachSelectorNode<EarlyReturnType>(callback: (node: postcssSelectorParser.Node) => EarlyReturnType | undefined): EarlyReturnType | undefined {
return this.eachCompoundSelector((sel) => {
for (let node of sel.nodes) {
let earlyReturn = callback(node);
Expand Down Expand Up @@ -310,14 +310,14 @@ export class ParsedSelector {
return this.selector.toString();
}

toContext(...keyNodes: Array<selectorParser.Node>): ParsedSelector {
toContext(...keyNodes: Array<postcssSelectorParser.Node>): ParsedSelector {
let context = this.clone();
let key = context.key;
key.nodes = key.nodes.filter(node => isPseudo(node)).sort((a, b) => a.value!.localeCompare(b.value!));
if (keyNodes.length > 0) {
key.nodes.unshift(...keyNodes);
} else {
key.nodes.unshift(selectorParser.universal());
key.nodes.unshift(postcssSelectorParser.universal());
}
return context;
}
Expand Down Expand Up @@ -351,16 +351,16 @@ export class ParsedSelector {
/**
* All valid selector-like inputs to the `parseSelector` helper methods.
*/
export type Selectorish = string | selectorParser.Root | selectorParser.Node[] | selectorParser.Node[][] | postcss.Rule;
export type Selectorish = string | postcssSelectorParser.Root | postcssSelectorParser.Node[] | postcssSelectorParser.Node[][] | postcss.Rule;

/**
* Coerce a `selectorParser.Root` object to `selectorParser.Node[][]`.
*
* @param root `selectorParser.Root` object
* @return Array of `selectorParser.Node` arrays.
*/
function coerceRootToNodeList(root: selectorParser.Root): selectorParser.Node[][] {
return root.nodes.map(n => (<selectorParser.Container>n).nodes);
function coerceRootToNodeList(root: postcssSelectorParser.Root): postcssSelectorParser.Node[][] {
return root.nodes.map(n => (<postcssSelectorParser.Container>n).nodes);
}

/**
Expand All @@ -369,14 +369,14 @@ function coerceRootToNodeList(root: selectorParser.Root): selectorParser.Node[][
* @param selector Selector like object: including `string`, `selectorParser.Root`, `selectorParser.Selector`, or `selectorParser.Node`
* @return Array of `selectorParser.Node` arrays.
*/
function toNodes(selector: Selectorish): selectorParser.Node[][] {
function toNodes(selector: Selectorish): postcssSelectorParser.Node[][] {

// If input is already an array of Nodes, return.
if (Array.isArray(selector)) {
if (Array.isArray(selector[0])) {
return <selectorParser.Node[][]>selector;
return <postcssSelectorParser.Node[][]>selector;
} else {
return [<selectorParser.Node[]>selector];
return [<postcssSelectorParser.Node[]>selector];
}
}

Expand All @@ -390,7 +390,7 @@ function toNodes(selector: Selectorish): selectorParser.Node[][] {
return [selector.nodes];
}

let res: selectorParser.Root = selectorParser().astSync(selector, {updateSelector: false});
let res: postcssSelectorParser.Root = postcssSelectorParser().astSync(selector, {updateSelector: false});
return coerceRootToNodeList(res);
}

Expand All @@ -410,7 +410,7 @@ export function parseCompoundSelectors(selector: Selectorish): CompoundSelector[
nodes.forEach((n) => {

// If a combinator is encountered, start a new `CompoundSelector` and link to the previous.
if (n.type === selectorParser.COMBINATOR) {
if (n.type === postcssSelectorParser.COMBINATOR) {
let lastCompoundSel = compoundSel;
compoundSel = new CompoundSelector();
lastCompoundSel.setNext(n, compoundSel);
Expand All @@ -419,7 +419,7 @@ export function parseCompoundSelectors(selector: Selectorish): CompoundSelector[
// Normalize :before and :after to always use double colons and save.
else if (isPseudoElement(n)) {
if (compoundSel.nodes.length === 0) {
let universal = selectorParser.universal();
let universal = postcssSelectorParser.universal();
n.parent!.insertBefore(n, universal);
compoundSel.addNode(universal);
}
Expand Down

0 comments on commit 4e28d92

Please sign in to comment.