Skip to content

State containers refactor #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions packages/css-blocks/src/Block/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ import { FileIdentifier } from "../importing";
import { LocalScopedContext } from "../util/LocalScope";

import { BlockClass } from "./BlockClass";
import { SourceContainer } from "./BlockTree";
import { State } from "./State";

export type Style = BlockClass | State;
import { Inheritable } from "./Inheritable";
import { Styles } from "./Styles";

export const OBJ_REF_SPLITTER = (s: string): [string, string] | undefined => {
let index = s.indexOf(".");
Expand All @@ -37,13 +35,13 @@ export const OBJ_REF_SPLITTER = (s: string): [string, string] | undefined => {
return;
};

export class Block extends SourceContainer<Block, BlockClass> {
export class Block extends Inheritable<Block, Block, never, BlockClass> {
private _rootClass: BlockClass;
private _blockReferences: ObjectDictionary<Block> = {};
private _blockReferencesReverseLookup: Map<Block, string> = new Map();
private _identifier: FileIdentifier;
private _implements: Block[] = [];
private _localScope: LocalScopedContext<Block, Style>;
private _localScope: LocalScopedContext<Block, Styles>;
private hasHadNameReset = false;
/**
* array of paths that this block depends on and, if changed, would
Expand All @@ -59,12 +57,14 @@ export class Block extends SourceContainer<Block, BlockClass> {
super(name);
this._identifier = identifier;
this.parsedRuleSelectors = new WeakMap();
this._localScope = new LocalScopedContext<Block, Style>(OBJ_REF_SPLITTER, this);
this._localScope = new LocalScopedContext<Block, Styles>(OBJ_REF_SPLITTER, this);
this._dependencies = new Set<string>();
this._rootClass = new BlockClass(ROOT_CLASS, this);
this.addClass(this._rootClass);
}

get block(): Block { return this.root; }

get name() { return this._name; }
set name(name: string) {
if (this.hasHadNameReset) {
Expand All @@ -79,7 +79,7 @@ export class Block extends SourceContainer<Block, BlockClass> {
}

/// Start of methods to implement LocalScope<Block, Style>
subScope(name: string): LocalScopedContext<Block, Style> | undefined {
subScope(name: string): LocalScopedContext<Block, Styles> | undefined {
let block = this._blockReferences[name];
if (block) {
return block._localScope;
Expand All @@ -88,7 +88,7 @@ export class Block extends SourceContainer<Block, BlockClass> {
}
}

lookupLocal(name: string): Style | undefined {
lookupLocal(name: string): Styles | undefined {
let blockRef = this._blockReferences[name];
if (blockRef) {
return blockRef.rootClass;
Expand Down Expand Up @@ -118,7 +118,7 @@ export class Block extends SourceContainer<Block, BlockClass> {
* A single dot by itself returns the current block.
* @returns The Style referenced at the supplied path.
*/
public lookup(path: string | BlockPath, errLoc?: SourceLocation): Style | undefined {
public lookup(path: string | BlockPath, errLoc?: SourceLocation): Styles | undefined {
path = new BlockPath(path);
let block = this.getReferencedBlock(path.block);
if (!block) {
Expand Down Expand Up @@ -183,8 +183,8 @@ export class Block extends SourceContainer<Block, BlockClass> {
* @param b The block to check implementation against.
* @returns The Styles from b that are missing in the block.
*/
checkImplementation(b: Block): Style[] {
let missing: Style[] = [];
checkImplementation(b: Block): Styles[] {
let missing: Styles[] = [];
for (let o of b.all()) {
if (!this.find(o.asSource())) {
missing.push(o);
Expand All @@ -198,7 +198,7 @@ export class Block extends SourceContainer<Block, BlockClass> {
*/
checkImplementations(): void {
for (let b of this.getImplementedBlocks()) {
let missing: Style[] = this.checkImplementation(b);
let missing: Styles[] = this.checkImplementation(b);
let paths = missing.map(o => o.asSource()).join(", ");
if (missing.length > 0) {
let s = missing.length > 1 ? "s" : "";
Expand All @@ -208,7 +208,7 @@ export class Block extends SourceContainer<Block, BlockClass> {
}

// This is a really dumb impl
find(sourceName: string): Style | undefined {
find(sourceName: string): Styles | undefined {
let blockRefName: string | undefined;
let md = sourceName.match(CLASS_NAME_IDENT);
if (md && md.index === 0) {
Expand Down Expand Up @@ -284,8 +284,8 @@ export class Block extends SourceContainer<Block, BlockClass> {
* @param shallow Pass true to not include inherited objects.
* @returns Array of Styles.
*/
all(shallow?: boolean): Style[] {
let result = new Array<Style>();
all(shallow?: boolean): Styles[] {
let result = new Array<Styles>();
for (let blockClass of this.classes) {
result.push(...blockClass.all());
}
Expand All @@ -295,8 +295,8 @@ export class Block extends SourceContainer<Block, BlockClass> {
return result;
}

merged(): MultiMap<string, Style> {
let map = new MultiMap<string, Style>(false);
merged(): MultiMap<string, Styles> {
let map = new MultiMap<string, Styles>(false);
for (let obj of this.all()) {
map.set(obj.asSource(), obj);
}
Expand All @@ -307,7 +307,7 @@ export class Block extends SourceContainer<Block, BlockClass> {
* Fetch a the cached `Style` from `Block` given `NodeAndType`.
* @param obj The `NodeAndType` object to use for `Style` lookup.
*/
nodeAndTypeToStyle(obj: NodeAndType): Style | null {
nodeAndTypeToStyle(obj: NodeAndType): Styles | null {
switch (obj.blockType) {
case BlockType.root:
return this.rootClass;
Expand All @@ -327,7 +327,7 @@ export class Block extends SourceContainer<Block, BlockClass> {
}
}

nodeAsStyle(node: selectorParser.Node): [Style, number] | null {
nodeAsStyle(node: selectorParser.Node): [Styles, number] | null {
if (node.type === selectorParser.CLASS && node.value === ROOT_CLASS) {
return [this.rootClass, 0];
} else if (node.type === selectorParser.TAG) {
Expand Down Expand Up @@ -444,7 +444,7 @@ export class Block extends SourceContainer<Block, BlockClass> {
let sortedNames = [...sourceNames].sort();
for (let n of sortedNames) {
if (n !== `.${ROOT_CLASS}`) {
let o = this.find(n) as Style;
let o = this.find(n) as Styles;
result.push(o.asDebug(opts));
}
}
Expand Down
20 changes: 14 additions & 6 deletions packages/css-blocks/src/Block/BlockClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { OptionsReader } from "../OptionsReader";
import { OutputMode } from "../OutputMode";

import { Block } from "./Block";
import { StyleNode } from "./BlockTree";
import { RulesetContainer } from "./RulesetContainer";
import { State } from "./State";
import { StateGroup } from "./StateGroup";
import { Style } from "./Style";
import { Styles } from "./Styles";

/**
* Holds state values to be passed to the StateContainer.
Expand All @@ -20,8 +22,14 @@ export interface StateInfo {
/**
* Represents a Class present in the Block.
*/
export class BlockClass extends StyleNode<BlockClass, Block, Block, StateGroup> {
export class BlockClass extends Style<BlockClass, Block, Block, StateGroup> {
private _sourceAttribute: Attribute | undefined;
public readonly rulesets: RulesetContainer<BlockClass>;

constructor(name: string, parent: Block) {
super(name, parent);
this.rulesets = new RulesetContainer(this);
}

protected newChild(name: string): StateGroup { return new StateGroup(name, this); }

Expand All @@ -40,7 +48,7 @@ export class BlockClass extends StyleNode<BlockClass, Block, Block, StateGroup>
public stateGroups(): StateGroup[] { return this.children(); }
public resolveState(groupName: string, stateName = UNIVERSAL_STATE): State | null {
let parent = this.resolveChild(groupName);
if (parent) { return parent.resolveChild(stateName); }
if (parent) { return parent.resolveState(stateName); }
return null;
}

Expand Down Expand Up @@ -116,7 +124,7 @@ export class BlockClass extends StyleNode<BlockClass, Block, Block, StateGroup>
* @param shallow Pass false to not include children.
* @returns Array of Styles.
*/
all(shallow?: boolean): (State | BlockClass)[] {
all(shallow?: boolean): Styles[] {
let result: (State | BlockClass)[] = [this];
if (!shallow) {
result = result.concat(this.allStates());
Expand Down Expand Up @@ -189,8 +197,8 @@ export class BlockClass extends StyleNode<BlockClass, Block, Block, StateGroup>
*/
debug(opts: OptionsReader): string[] {
let result: string[] = [];
for (let state of this.all()) {
result.push(state.asDebug(opts));
for (let style of this.all()) {
result.push(style.asDebug(opts));
}
return result;
}
Expand Down
122 changes: 0 additions & 122 deletions packages/css-blocks/src/Block/BlockTree/index.ts

This file was deleted.

Loading