Skip to content

Commit 03dba9b

Browse files
chriseppsteinamiller-gh
authored andcommitted
feat(css-blocks): Don't expose generic base class methods as public.
1 parent 3404e25 commit 03dba9b

File tree

3 files changed

+74
-45
lines changed

3 files changed

+74
-45
lines changed

packages/css-blocks/src/Block/BlockClass.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class BlockClass extends StyleNode<BlockClass, Block, Block, StateGroup>
4040
public stateGroups(): StateGroup[] { return this.children(); }
4141
public resolveState(groupName: string, stateName = UNIVERSAL_STATE): State | null {
4242
let parent = this.resolveChild(groupName);
43-
if (parent) { return parent.resolveChild(stateName); }
43+
if (parent) { return parent.resolveState(stateName); }
4444
return null;
4545
}
4646

packages/css-blocks/src/Block/BlockTree/Inheritable.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export abstract class Inheritable<
9999

100100
/**
101101
* Sets the base node that this node inherits from. Must be of same type.
102-
* @prop base Self The new base node.
102+
* @prop base The new base node.
103103
*/
104104
public setBase(base: Self) {
105105
this._base = base;
@@ -129,7 +129,7 @@ export abstract class Inheritable<
129129
* @param name The name of the child to resolve.
130130
* @returns The child node, or `null`
131131
*/
132-
public resolveChild(name: string): Child | null {
132+
protected resolveChild(name: string): Child | null {
133133
let state: Child | null = this.getChild(name);
134134
let container: Self | undefined = this.base;
135135
while (!state && container) {
@@ -144,7 +144,7 @@ export abstract class Inheritable<
144144
* @param key string The key to fetch the child object from.
145145
* @returns The child node.
146146
*/
147-
public getChild(key: string): Child | null {
147+
protected getChild(key: string): Child | null {
148148
return this._children.get(key) || null;
149149
}
150150

@@ -153,7 +153,7 @@ export abstract class Inheritable<
153153
* @param key string The key to set the child object to.
154154
* @returns The child node.
155155
*/
156-
public setChild(key: string, value: Child): Child {
156+
protected setChild(key: string, value: Child): Child {
157157
this._children.set(key, value);
158158
return value;
159159
}
@@ -165,7 +165,7 @@ export abstract class Inheritable<
165165
* @param key string The key at which this child object should be (optional)
166166
* @returns The child node.
167167
*/
168-
public ensureChild(name: string, key?: string): Child {
168+
protected ensureChild(name: string, key?: string): Child {
169169
key = key !== undefined ? key : name;
170170
if (!this._children.has(name)) {
171171
this.setChild(key, this.newChild(name));
@@ -177,15 +177,15 @@ export abstract class Inheritable<
177177
* Returns an array of all children nodes in the order they were added.
178178
* @returns The children array.
179179
*/
180-
public children(): Child[] {
180+
protected children(): Child[] {
181181
return [...this._children.values()];
182182
}
183183

184184
/**
185185
* Returns a map of all children nodes at the keys they are stored..
186186
* @returns The children map.
187187
*/
188-
public childrenMap(): Map<string, Child> {
188+
protected childrenMap(): Map<string, Child> {
189189
return new Map(this._children);
190190
}
191191

@@ -194,7 +194,7 @@ export abstract class Inheritable<
194194
* TODO: Cache this maybe? Convert entire model to only use hash?...
195195
* @returns The children hash.
196196
*/
197-
public childrenHash(): ObjectDictionary<Child> {
197+
protected childrenHash(): ObjectDictionary<Child> {
198198
let out = {};
199199
for (let [key, value] of this._children.entries()) {
200200
out[key] = value;

packages/css-blocks/test/Block/BlockTree/block-tree-test.ts

+65-36
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,31 @@ class TestSource extends SourceContainer<
1111
TestSource, // Self
1212
TestNode // Children
1313
> {
14-
newChild(name: string) { return new TestNode(name, this); }
14+
protected newChild(name: string) { return new TestNode(name, this); }
1515
lookup(): undefined { return undefined; }
16+
newChildNode: SourceContainer<TestSource, TestNode>['newChild'] =
17+
(name: string) => this.newChild(name)
18+
19+
getChildNode: SourceContainer<TestSource, TestNode>['getChild'] =
20+
(key: string) => this.getChild(key)
21+
22+
resolveChildNode: SourceContainer<TestSource, TestNode>['resolveChild'] =
23+
(key: string) => this.resolveChild(key)
24+
25+
setChildNode: SourceContainer<TestSource, TestNode>['setChild'] =
26+
(key: string, value: TestNode) => this.setChild(key, value)
27+
28+
ensureChildNode: SourceContainer<TestSource, TestNode>['ensureChild'] =
29+
(name: string, key?: string) => this.ensureChild(name, key)
30+
31+
childNodes: SourceContainer<TestSource, TestNode>['children'] =
32+
() => this.children()
33+
34+
childNodeHash: SourceContainer<TestSource, TestNode>['childrenHash'] =
35+
() => this.childrenHash()
36+
37+
childNodeMap: SourceContainer<TestSource, TestNode>['childrenMap'] =
38+
() => this.childrenMap()
1639
}
1740

1841
class TestNode extends Container<
@@ -23,6 +46,12 @@ class TestNode extends Container<
2346
> {
2447
newChild(name: string) { return new TestSink(name, this); }
2548
lookup(): undefined { return undefined; }
49+
ensureSink: Container<TestNode, TestSource, TestSource, TestSink>['ensureChild'] =
50+
(name: string, key?: string) => this.ensureChild(name, key)
51+
getSink: Container<TestNode, TestSource, TestSource, TestSink>['getChild'] =
52+
(name: string) => this.getChild(name)
53+
resolveSink: Container<TestNode, TestSource, TestSource, TestSink>['resolveChild'] =
54+
(name: string) => this.resolveChild(name)
2655
}
2756

2857
class TestSink extends SinkContainer<
@@ -44,58 +73,58 @@ export class BlockTreeTests {
4473

4574
@test "newChild creates new child, does not set it"() { // Note: this is why `newChild` is protected in Blocks
4675
let source = new TestSource("my-source");
47-
let child = source.newChild("child-node");
48-
assert.equal(source.getChild("child-node"), null);
49-
assert.equal(source.resolveChild("child-node"), null);
76+
let child = source.newChildNode("child-node");
77+
assert.equal(source.getChildNode("child-node"), null);
78+
assert.equal(source.resolveChildNode("child-node"), null);
5079
assert.equal(child.parent, source);
5180
assert.equal(child.base, undefined);
5281
assert.equal(child.root, source);
5382
}
5483

5584
@test "setChild adds new child to parent"() {
5685
let source = new TestSource("my-source");
57-
let child = source.newChild("child-node");
58-
source.setChild("child-key", child);
59-
assert.equal(source.getChild("child-key"), child);
60-
assert.equal(source.resolveChild("child-key"), child);
86+
let child = source.newChildNode("child-node");
87+
source.setChildNode("child-key", child);
88+
assert.equal(source.getChildNode("child-key"), child);
89+
assert.equal(source.resolveChildNode("child-key"), child);
6190
}
6291

6392
@test "ensureChild creates and adds new child to parent"() {
6493
let source = new TestSource("my-source");
65-
let child = source.ensureChild("child-node");
66-
assert.equal(source.getChild("child-node"), child);
67-
assert.equal(source.resolveChild("child-node"), child);
94+
let child = source.ensureChildNode("child-node");
95+
assert.equal(source.getChildNode("child-node"), child);
96+
assert.equal(source.resolveChildNode("child-node"), child);
6897
}
6998

7099
@test "ensureChild accepts optional key"() {
71100
let source = new TestSource("my-source");
72-
let child = source.ensureChild("child-node", "child-key");
73-
assert.equal(source.getChild("child-key"), child);
74-
assert.equal(source.resolveChild("child-key"), child);
101+
let child = source.ensureChildNode("child-node", "child-key");
102+
assert.equal(source.getChildNode("child-key"), child);
103+
assert.equal(source.resolveChildNode("child-key"), child);
75104
}
76105

77106
@test "ensureChild will not overwrite existing nodes"() {
78107
let source = new TestSource("my-source");
79-
let child1 = source.ensureChild("child-node");
80-
let child2 = source.ensureChild("child-node");
108+
let child1 = source.ensureChildNode("child-node");
109+
let child2 = source.ensureChildNode("child-node");
81110
assert.equal(child1, child2);
82111
}
83112

84113
@test "children accessor methods work as expected"() {
85114
let source = new TestSource("my-source");
86-
let child1 = source.ensureChild("child1");
87-
let child2 = source.ensureChild("child2");
88-
let child3 = source.ensureChild("child3", "custom-key");
115+
let child1 = source.ensureChildNode("child1");
116+
let child2 = source.ensureChildNode("child2");
117+
let child3 = source.ensureChildNode("child3", "custom-key");
89118

90-
assert.deepEqual(source.children(), [child1, child2, child3]);
91-
assert.deepEqual(source.childrenHash(), {child1, child2, "custom-key": child3});
92-
assert.deepEqual(source.childrenMap(), new Map([["child1", child1], ["child2", child2], ["custom-key", child3]]));
119+
assert.deepEqual(source.childNodes(), [child1, child2, child3]);
120+
assert.deepEqual(source.childNodeHash(), {child1, child2, "custom-key": child3});
121+
assert.deepEqual(source.childNodeMap(), new Map([["child1", child1], ["child2", child2], ["custom-key", child3]]));
93122
}
94123

95124
@test "grandchildren have tree properties set as expected"() {
96125
let source = new TestSource("my-source");
97-
let child = source.ensureChild("child");
98-
let grandchild = child.ensureChild("grandchild");
126+
let child = source.ensureChildNode("child");
127+
let grandchild = child.ensureSink("grandchild");
99128
assert.equal(grandchild.parent, child);
100129
assert.equal(grandchild.base, undefined);
101130
assert.equal(grandchild.root, source);
@@ -113,30 +142,30 @@ export class BlockTreeTests {
113142
@test "setBase creates inheritance tree for children"() {
114143
let base = new TestSource("my-base");
115144
let source = new TestSource("my-source");
116-
let baseChild = base.ensureChild("child");
117-
let child = source.ensureChild("child");
118-
let baseUnique = base.ensureChild("base-only");
145+
let baseChild = base.ensureChildNode("child");
146+
let child = source.ensureChildNode("child");
147+
let baseUnique = base.ensureChildNode("base-only");
119148
source.setBase(base);
120149

121150
assert.equal(child.base, baseChild);
122-
assert.equal(source.getChild("base-only"), null);
123-
assert.equal(source.resolveChild("base-only"), baseUnique);
151+
assert.equal(source.getChildNode("base-only"), null);
152+
assert.equal(source.resolveChildNode("base-only"), baseUnique);
124153
assert.deepEqual(child.resolveInheritance(), [baseChild]);
125154
}
126155

127156
@test "setBase creates inheritance tree for grandchildren"() {
128157
let base = new TestSource("my-base");
129158
let source = new TestSource("my-source");
130-
let baseChild = base.ensureChild("child");
131-
let baseGrandchild = baseChild.ensureChild("grandchild");
132-
let baseChildUnique = baseChild.ensureChild("base-only");
133-
let child = source.ensureChild("child");
134-
let grandchild = child.ensureChild("grandchild");
159+
let baseChild = base.ensureChildNode("child");
160+
let baseGrandchild = baseChild.ensureSink("grandchild");
161+
let baseChildUnique = baseChild.ensureSink("base-only");
162+
let child = source.ensureChildNode("child");
163+
let grandchild = child.ensureSink("grandchild");
135164
source.setBase(base);
136165

137166
assert.equal(grandchild.base, baseGrandchild);
138-
assert.equal(source.getChild("child")!.getChild("base-only"), null);
139-
assert.equal(source.getChild("child")!.resolveChild("base-only"), baseChildUnique);
167+
assert.equal(source.getChildNode("child")!.getSink("base-only"), null);
168+
assert.equal(source.getChildNode("child")!.resolveSink("base-only"), baseChildUnique);
140169
assert.deepEqual(grandchild.resolveInheritance(), [baseGrandchild]);
141170
}
142171
}

0 commit comments

Comments
 (0)