Skip to content

Commit 258970e

Browse files
committedMar 2, 2018
fix(css-blocks): Pull in new linting rules from master.
Fixed linting errors. Plugged typing holes in BlockTree. Air tight now!
1 parent 63e4a45 commit 258970e

24 files changed

+933
-219
lines changed
 

Diff for: ‎packages/css-blocks/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"css-property-parser": "^1.0.6",
7070
"debug": "^2.6.8",
7171
"inline-source-map-comment": "^1.0.5",
72+
"json-parse-better-errors": "^1.0.1",
7273
"object.values": "^1.0.4",
7374
"opticss": "^0.2.0",
7475
"postcss-selector-parser": "3.1.1",

Diff for: ‎packages/css-blocks/src/Block/Block.ts

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
1-
import * as postcss from 'postcss';
2-
import { ObjectDictionary, MultiMap, assertNever } from "@opticss/util";
3-
import selectorParser = require('postcss-selector-parser');
4-
5-
import { FileIdentifier } from "../importing";
6-
import { LocalScopedContext } from "../util/LocalScope";
7-
import { CssBlockError } from "../errors";
8-
import { OptionsReader } from "../OptionsReader";
9-
import { CLASS_NAME_IDENT, ROOT_CLASS } from "../BlockSyntax";
10-
11-
import { Source } from "./BlockTree";
12-
import { BlockClass } from "./BlockClass";
13-
import { State } from "./State";
14-
import { BlockPath } from "../BlockSyntax";
15-
1+
import { assertNever, MultiMap, ObjectDictionary } from "@opticss/util";
2+
import { whatever } from "@opticss/util";
163
import {
17-
SelectorFactory,
18-
parseSelector,
4+
CompoundSelector,
195
ParsedSelector,
20-
CompoundSelector
6+
parseSelector,
7+
SelectorFactory,
218
} from "opticss";
9+
import * as postcss from "postcss";
10+
import selectorParser = require("postcss-selector-parser");
11+
2212
import {
23-
stateParser,
13+
BlockType,
2414
isClassNode,
2515
isStateNode,
2616
NodeAndType,
27-
BlockType
17+
stateParser,
2818
} from "../BlockParser";
19+
import { CLASS_NAME_IDENT, ROOT_CLASS } from "../BlockSyntax";
20+
import { BlockPath } from "../BlockSyntax";
21+
import { OptionsReader } from "../OptionsReader";
22+
import { CssBlockError } from "../errors";
23+
import { FileIdentifier } from "../importing";
24+
import { LocalScopedContext } from "../util/LocalScope";
25+
26+
import { BlockClass } from "./BlockClass";
27+
import { Source } from "./BlockTree";
28+
import { State } from "./State";
2929

3030
export type Style = BlockClass | State;
3131

3232
export const OBJ_REF_SPLITTER = (s: string): [string, string] | undefined => {
33-
let index = s.indexOf('.');
34-
if (index < 0) index = s.indexOf('[');
33+
let index = s.indexOf(".");
34+
if (index < 0) index = s.indexOf("[");
3535
if (index >= 0) {
3636
return [s.substr(0, index), s.substring(index)];
3737
}
@@ -70,7 +70,7 @@ export class Block
7070
get name() { return this._name; }
7171
set name(name: string) {
7272
if (this.hasHadNameReset) {
73-
throw new CssBlockError('Can not set block name more than once.');
73+
throw new CssBlockError("Can not set block name more than once.");
7474
}
7575
this._name = name;
7676
this.hasHadNameReset = true;
@@ -201,7 +201,7 @@ export class Block
201201
let missing: Style[] = this.checkImplementation(b);
202202
let paths = missing.map(o => o.asSource()).join(", ");
203203
if (missing.length > 0) {
204-
let s = missing.length > 1 ? 's' : '';
204+
let s = missing.length > 1 ? "s" : "";
205205
throw new CssBlockError(`Missing implementation${s} for: ${paths} from ${b.identifier}`);
206206
}
207207
}
@@ -231,7 +231,7 @@ export class Block
231231
return this.all().find(e => e.asSource() === sourceName);
232232
}
233233

234-
eachBlockReference(callback: (name: string, block: Block) => any) {
234+
eachBlockReference(callback: (name: string, block: Block) => whatever) {
235235
for (let name of Object.keys(this._blockReferences)) {
236236
callback(name, this._blockReferences[name]);
237237
}
@@ -326,15 +326,15 @@ export class Block
326326
if (node.type === selectorParser.CLASS && node.value === ROOT_CLASS) {
327327
return [this.rootClass, 0];
328328
} else if (node.type === selectorParser.TAG) {
329-
let otherBlock = this.getReferencedBlock(node.value!);
329+
let otherBlock = this.getReferencedBlock(node.value);
330330
if (otherBlock) {
331331
let next = node.next();
332332
if (next && isClassNode(next)) {
333-
let klass = otherBlock.getClass(next.value!);
333+
let klass = otherBlock.getClass(next.value);
334334
if (klass) {
335335
let another = next.next();
336336
if (another && isStateNode(another)) {
337-
let info = stateParser(<selectorParser.Attribute>another);
337+
let info = stateParser(another);
338338
let state = klass._getState(info);
339339
if (state) {
340340
return [state, 2];
@@ -349,7 +349,7 @@ export class Block
349349
return null;
350350
}
351351
} else if (next && isStateNode(next)) {
352-
let info = stateParser(<selectorParser.Attribute>next);
352+
let info = stateParser(next);
353353
let state = otherBlock.rootClass._getState(info);
354354
if (state) {
355355
return [state, 1];
@@ -363,13 +363,13 @@ export class Block
363363
return null;
364364
}
365365
} else if (node.type === selectorParser.CLASS) {
366-
let klass = this.getClass(node.value!);
366+
let klass = this.getClass(node.value);
367367
if (klass === null) {
368368
return null;
369369
}
370370
let next = node.next();
371371
if (next && isStateNode(next)) {
372-
let info = stateParser(<selectorParser.Attribute>next);
372+
let info = stateParser(next);
373373
let state = klass._getState(info);
374374
if (state === null) {
375375
return null;
@@ -380,7 +380,7 @@ export class Block
380380
return [klass, 0];
381381
}
382382
} else if (isStateNode(node)) {
383-
let info = stateParser(<selectorParser.Attribute>node);
383+
let info = stateParser(node);
384384
let state = this.rootClass._ensureState(info);
385385
if (state) {
386386
return [state, 0];

Diff for: ‎packages/css-blocks/src/Block/BlockClass.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { Block } from "./Block";
2-
import { NodeStyle } from "./BlockTree";
3-
import { StateGroup } from "./StateGroup";
4-
import { State } from "./State";
51
import { Attribute } from "@opticss/element-analysis";
2+
3+
import { UNIVERSAL_STATE } from "../BlockSyntax";
64
import { OptionsReader } from "../OptionsReader";
75
import { OutputMode } from "../OutputMode";
8-
import { UNIVERSAL_STATE } from "../BlockSyntax";
6+
7+
import { Block } from "./Block";
8+
import { NodeStyle } from "./BlockTree";
9+
import { State } from "./State";
10+
import { StateGroup } from "./StateGroup";
911

1012
/**
1113
* Holds state values to be passed to the StateContainer.
@@ -19,7 +21,7 @@ export interface StateInfo {
1921
* Represents a Class present in the Block.
2022
*/
2123
export class BlockClass extends NodeStyle<BlockClass, Block, Block, StateGroup> {
22-
private _sourceAttribute: Attribute;
24+
private _sourceAttribute: Attribute | undefined;
2325

2426
protected newChild(name: string): StateGroup { return new StateGroup(name, this, this.block); }
2527

@@ -48,12 +50,12 @@ export class BlockClass extends NodeStyle<BlockClass, Block, Block, StateGroup>
4850
* @param stateName The name of the sub-state to resolve.
4951
*/
5052
resolveStates(groupName?: string): Map<string, State> {
51-
let resolved: Map<string, State> = new Map;
53+
let resolved: Map<string, State> = new Map();
5254
let chain = this.resolveInheritance();
5355
chain.push(this);
5456
for (let base of chain) {
5557
let groups = !groupName ? base.getGroups() : [base.getGroup(groupName)];
56-
for (let group of groups ) {
58+
for (let group of groups) {
5759
if (group && group.states()) {
5860
resolved = new Map([...resolved, ...group.statesMap()]);
5961
}
@@ -62,7 +64,7 @@ export class BlockClass extends NodeStyle<BlockClass, Block, Block, StateGroup>
6264
return resolved;
6365
}
6466

65-
public booleanStates(): State[]{
67+
public booleanStates(): State[] {
6668
let res: State[] = [];
6769
for (let group of this.getGroups()) {
6870
let state = group.getState(UNIVERSAL_STATE);
@@ -102,7 +104,7 @@ export class BlockClass extends NodeStyle<BlockClass, Block, Block, StateGroup>
102104
return `${this.block.name}__${this.name}`;
103105
}
104106
default:
105-
throw "this never happens";
107+
throw new Error("this never happens");
106108
}
107109
}
108110

@@ -125,7 +127,7 @@ export class BlockClass extends NodeStyle<BlockClass, Block, Block, StateGroup>
125127
*/
126128
allStates(): State[] {
127129
let result: State[] = [];
128-
for (let stateContainer of this.stateGroups()){
130+
for (let stateContainer of this.stateGroups()) {
129131
result.push(...stateContainer.states());
130132
}
131133
return result;
@@ -194,4 +196,4 @@ export class BlockClass extends NodeStyle<BlockClass, Block, Block, StateGroup>
194196

195197
export function isBlockClass(o: object): o is BlockClass {
196198
return o instanceof BlockClass;
197-
}
199+
}

Diff for: ‎packages/css-blocks/src/Block/BlockTree/Container.ts

Whitespace-only changes.

Diff for: ‎packages/css-blocks/src/Block/BlockTree/Inheritable.ts

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import { ObjectDictionary } from "@opticss/util";
2+
import { whatever } from "@opticss/util";
3+
4+
/* tslint:disable:prefer-whatever-to-any */
5+
export type AnyNode = Inheritable<any, any, any, any>;
26

37
export abstract class Inheritable<
48
Self extends Inheritable<Self, Root, Parent, Child>,
5-
Root extends Inheritable<any, Root, null, any> | null,
9+
Root extends Inheritable<any, Root, null, any> | Self,
610
Parent extends Inheritable<any, Root, any, Self> | null,
711
Child extends Inheritable<any, Root, Self, any> | null
812
> {
9-
13+
/* tslint:enable:prefer-whatever-to-any */
1014
protected _name: string;
1115
protected _base: Self | undefined;
16+
protected _baseName: string | undefined;
1217
protected _root: Root | Self;
13-
protected _baseName: string;
14-
protected _parent: Parent | undefined;
15-
protected _children: Map<string, Child> = new Map;
18+
protected _parent: Parent;
19+
protected _children: Map<string, Child> = new Map();
1620

1721
/**
1822
* Given a parent that is a base class of this style, retrieve this style's
@@ -36,15 +40,15 @@ export abstract class Inheritable<
3640
* @param name Name for this Inheritable instance.
3741
* @param parent The parent Inheritable of this node.
3842
*/
39-
constructor(name: string, parent?: Parent, root?: Root) {
43+
constructor(name: string, parent: Parent, root?: Root) {
4044
this._name = name;
4145
this._parent = parent;
42-
this._root = root || this.asSelf();
46+
this._root = root || this.asSelf(); // `Root` is only set to `Self` for `Source` nodes.
4347
}
4448

4549
public get name(): string { return this._name; }
4650
public get baseName(): string | undefined { return this._baseName; }
47-
public get parent(): Parent | undefined { return this._parent; }
51+
public get parent(): Parent { return this._parent; }
4852

4953
/**
5054
* Get the style that this style inherits from, if any.
@@ -75,8 +79,9 @@ export abstract class Inheritable<
7579
* traverse parents and return the base block object.
7680
* @returns The base block in this container tree.
7781
*/
78-
public get block(): Root | Self {
79-
return this._root;
82+
public get block(): Root {
83+
// This is a safe cast because we know root will only be set to `Self` for `Source` nodes.
84+
return this._root as Root;
8085
}
8186

8287
setBase(baseName: string, base: Self) {
@@ -149,7 +154,7 @@ export abstract class Inheritable<
149154
return this._children.get(name) as Child;
150155
}
151156

152-
protected children(): Child[]{
157+
protected children(): Child[] {
153158
return [...this._children.values()];
154159
}
155160

@@ -160,7 +165,7 @@ export abstract class Inheritable<
160165
// TODO: Cache this maybe? Convert entire model to only use hash?...
161166
protected childrenHash(): ObjectDictionary<Child> {
162167
let out = {};
163-
for (let [key, value] of this._children.entries() ) {
168+
for (let [key, value] of this._children.entries()) {
164169
out[key] = value;
165170
}
166171
return out;
@@ -169,7 +174,7 @@ export abstract class Inheritable<
169174
// TypeScript can't figure out that `this` is the `Self` so this private
170175
// method casts it in a few places where it's needed.
171176
private asSelf(): Self {
172-
return <Self><any>this;
177+
return <Self><whatever>this;
173178
}
174179

175180
}

Diff for: ‎packages/css-blocks/src/Block/BlockTree/RulesetContainer.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { MultiMap, TwoKeyMultiMap } from "@opticss/util";
22
import * as propParser from "css-property-parser";
3+
import { ParsedSelector } from "opticss";
34
import * as postcss from "postcss";
45

5-
import { BLOCK_PROP_NAMES, RESOLVE_RE, SELF_SELECTOR, BlockPath } from "../../BlockSyntax";
6-
import { isBlockClass, isState, Style } from '../index';
6+
import { BLOCK_PROP_NAMES, BlockPath, RESOLVE_RE, SELF_SELECTOR } from "../../BlockSyntax";
77
import { sourceLocation } from "../../SourceLocation";
88
import * as errors from "../../errors";
99

10-
import { Style } from "./Block";
10+
import { AnyStyle, isStyle, Style } from "./Style";
11+
export { Style }; // Required for Typescript
1112

1213
// Convenience types to help our code read better.
1314
export type Pseudo = string;
@@ -21,7 +22,7 @@ export type Declaration = {
2122
export type Resolution = {
2223
node: postcss.Rule;
2324
property: string;
24-
resolution: Style;
25+
resolution: AnyStyle;
2526
};
2627

2728
/**
@@ -45,12 +46,12 @@ function expandProp(prop: string, value: string): propParser.Declarations {
4546
export class Ruleset {
4647
file: string;
4748
node: postcss.Rule;
48-
style: Style;
49+
style: AnyStyle;
4950

5051
declarations = new MultiMap<Property, Declaration>();
51-
resolutions = new MultiMap<Property, Style>();
52+
resolutions = new MultiMap<Property, AnyStyle>();
5253

53-
constructor(file: string, node: postcss.Rule, style: Style) {
54+
constructor(file: string, node: postcss.Rule, style: AnyStyle) {
5455
this.file = file;
5556
this.node = node;
5657
this.style = style;
@@ -61,7 +62,7 @@ export class Ruleset {
6162
* @param property A CSS property name.
6263
* @param style The Style object this property should resolve to.
6364
*/
64-
addResolution(property: Property, style: Style): void {
65+
addResolution(property: Property, style: AnyStyle): void {
6566
let expanded = expandProp(property, "inherit");
6667
for (let prop of Object.keys(expanded)) {
6768
this.resolutions.set(prop, style);
@@ -73,7 +74,7 @@ export class Ruleset {
7374
* @param property A CSS property name.
7475
* @param style The Style object this property should resolve to.
7576
*/
76-
hasResolutionFor(property: Property, other: Style): boolean {
77+
hasResolutionFor(property: Property, other: AnyStyle): boolean {
7778
return this.resolutions.hasValue(property, other);
7879
}
7980

@@ -95,8 +96,8 @@ export class RulesetContainer {
9596
* @param rule PostCSS ruleset
9697
* @param block External block
9798
*/
98-
addRuleset(file: string, rule: postcss.Rule, style: Style) {
99-
let selectors = style.block.getParsedSelectors(rule);
99+
addRuleset(file: string, rule: postcss.Rule, style: AnyStyle) {
100+
let selectors: ParsedSelector[] = style.block.getParsedSelectors(rule);
100101
selectors.forEach((selector) => {
101102
let ruleSet = new Ruleset(file, rule, style);
102103
let key = selector.key;
@@ -114,7 +115,6 @@ export class RulesetContainer {
114115
if (referenceStr) {
115116

116117
let blockPath = new BlockPath(referenceStr);
117-
console.log(referenceStr);
118118
let other = style.block.lookup(referenceStr);
119119
let otherBlock = style.block.getReferencedBlock(blockPath.block);
120120

@@ -146,7 +146,7 @@ export class RulesetContainer {
146146
);
147147
}
148148

149-
if (isBlockClass(other) || isState(other)) {
149+
if (isStyle(other)) {
150150
ruleSet.addResolution(decl.prop, other);
151151
}
152152
}

Diff for: ‎packages/css-blocks/src/Block/BlockTree/Style.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
import { Attr } from "@opticss/element-analysis";
2+
import { whatever } from "@opticss/util";
23

3-
import { RulesetContainer } from './RulesetContainer';
44
import { OptionsReader } from "../../OptionsReader";
5-
import { unionInto } from '../../util/unionInto';
5+
import { unionInto } from "../../util/unionInto";
6+
67
import { Inheritable } from "./Inheritable";
8+
import { RulesetContainer } from "./RulesetContainer";
79

810
/**
911
* Abstract class that serves as the base for all Styles. Contains basic
1012
* properties and abstract methods that extenders must implement.
1113
*/
14+
/* tslint:disable:prefer-whatever-to-any */
15+
export type AnyStyle = Style<any, any, any, any>;
16+
1217
export abstract class Style<
1318
Self extends Style<Self, Root, Parent, Child>,
1419
Root extends Inheritable<Root, Root, null, any>,
1520
Parent extends Inheritable<Parent, Root, any, Self>,
1621
Child extends Inheritable<any, Root, Self, any> | null
1722
> extends Inheritable<Self, Root, Parent, Child> {
23+
/* tslint:enable:prefer-whatever-to-any */
1824

1925
public readonly rulesets: RulesetContainer;
2026

@@ -134,10 +140,10 @@ export abstract class Style<
134140
// TypeScript can't figure out that `this` is the `StyleType` so this private
135141
// method casts it in a few places where it's needed.
136142
private asStyle(): Self {
137-
return <Self><any>this;
143+
return <Self><whatever>this;
138144
}
139145
}
140146

141-
export function isStyle(o: any): o is Style<any, any, any, any> {
147+
export function isStyle(o: object): o is AnyStyle {
142148
return o && o instanceof Style;
143-
}
149+
}

Diff for: ‎packages/css-blocks/src/Block/BlockTree/index.ts

+16-34
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,49 @@
1-
import { Inheritable } from "./Inheritable";
1+
import { AnyNode, Inheritable } from "./Inheritable";
2+
export { Ruleset } from "./RulesetContainer";
23
import { Style } from "./Style";
34

4-
export { Ruleset } from "./RulesetContainer";
55
export { Style, isStyle } from "./Style";
66

77
// Its days like these that I wish JavaScript had multiple inheritance.
88
export abstract class Source<
99
Self extends Source<Self, Child>,
10-
Child extends Inheritable<Child, Self, Self, any>
10+
Child extends Inheritable<Child, Self, Self, AnyNode>
1111
> extends Inheritable<Self, Self, null, Child> {
12-
constructor(name: string) { super(name); }
13-
public parent: null;
14-
public block: Self;
15-
protected _children: Map<string, Child>;
16-
protected _root: Self;
12+
constructor(name: string) {
13+
super(name, null);
14+
}
1715
}
1816

1917
export abstract class Node<
2018
Self extends Node<Self, Root, Parent, Child>,
21-
Root extends Source<Root, any>,
22-
Parent extends Inheritable<Parent, Root, any, Self>,
23-
Child extends Inheritable<Child, Root, Self, any>
19+
Root extends Source<Root, AnyNode>,
20+
Parent extends Inheritable<Parent, Root, AnyNode, Self>,
21+
Child extends Inheritable<Child, Root, Self, AnyNode | null>
2422
> extends Inheritable<Self, Root, Parent, Child> {
2523
constructor(name: string, parent: Parent, root: Root) { super(name, parent, root); }
26-
public parent: Parent;
27-
public block: Root;
28-
protected _children: Map<string, Child>;
29-
protected _root: Root;
3024
}
3125

3226
export abstract class Sink<
3327
Self extends Inheritable<Self, Root, Parent, null>,
34-
Root extends Source<Root, any>,
35-
Parent extends Inheritable<Parent, Root, any, Self>
28+
Root extends Source<Root, AnyNode>,
29+
Parent extends Inheritable<Parent, Root, AnyNode, Self>
3630
> extends Inheritable<Self, Root, Parent, null> {
3731
constructor(name: string, parent: Parent, root: Root) { super(name, parent, root); }
38-
public parent: Parent;
39-
public block: Root;
40-
protected _children: Map<string, null>;
41-
protected _root: Root;
4232
}
4333

4434
export abstract class NodeStyle<
4535
Self extends Style<Self, Root, Parent, Child>,
46-
Root extends Source<Root, any>,
47-
Parent extends Inheritable<Parent, Root, any, Self>,
48-
Child extends Inheritable<Child, Root, Self, any>
36+
Root extends Source<Root, AnyNode>,
37+
Parent extends Inheritable<Parent, Root, AnyNode | null, Self>,
38+
Child extends Inheritable<Child, Root, Self, AnyNode | null>
4939
> extends Style<Self, Root, Parent, Child> {
5040
constructor(name: string, parent: Parent, root: Root) { super(name, parent, root); }
51-
public parent: Parent;
52-
public block: Root;
53-
protected _children: Map<string, Child>;
54-
protected _root: Root;
5541
}
5642

5743
export abstract class SinkStyle<
5844
Self extends Style<Self, Root, Parent, null>,
59-
Root extends Source<Root, any>,
60-
Parent extends Inheritable<Parent, Root, any, Self>
45+
Root extends Source<Root, AnyNode>,
46+
Parent extends Inheritable<Parent, Root, AnyNode, Self>
6147
> extends Style<Self, Root, Parent, null> {
6248
constructor(name: string, parent: Parent, root: Root) { super(name, parent, root); }
63-
public parent: Parent;
64-
public block: Root;
65-
protected _children: Map<string, null>;
66-
protected _root: Root;
6749
}

Diff for: ‎packages/css-blocks/src/Block/State.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
import {
2+
Attr,
3+
AttributeNS,
4+
} from "@opticss/element-analysis";
15
import { assertNever } from "@opticss/util";
2-
import { SinkStyle } from "./BlockTree";
3-
import { BlockClass } from "./BlockClass";
4-
import { StateGroup } from "./StateGroup";
6+
7+
import { UNIVERSAL_STATE } from "../BlockSyntax";
58
import { OptionsReader } from "../OptionsReader";
69
import { OutputMode } from "../OutputMode";
7-
import { UNIVERSAL_STATE } from "../BlockSyntax";
10+
811
import { Block } from "./Block";
9-
import {
10-
Attr,
11-
AttributeNS
12-
} from "@opticss/element-analysis";
12+
import { BlockClass } from "./BlockClass";
13+
import { SinkStyle } from "./BlockTree";
14+
import { StateGroup } from "./StateGroup";
1315

1416
/**
1517
* States represent a state attribute selector in a particular Block.
@@ -19,7 +21,7 @@ import {
1921
export class State extends SinkStyle<State, Block, StateGroup> {
2022
isGlobal = false;
2123

22-
private _sourceAttributes: AttributeNS[];
24+
private _sourceAttributes: AttributeNS[] | undefined;
2325

2426
/**
2527
* State constructor. Provide a local name for this State, an optional group name,
@@ -47,7 +49,7 @@ export class State extends SinkStyle<State, Block, StateGroup> {
4749
* @returns The State's local name.
4850
*/
4951
localName(): string {
50-
return `${this.parent.localName()}${this.isUniversal ? '' : `-${this.name}`}`;
52+
return `${this.parent.localName()}${this.isUniversal ? "" : `-${this.name}`}`;
5153
}
5254

5355
asSourceAttributes(): Attr[] {
@@ -71,7 +73,7 @@ export class State extends SinkStyle<State, Block, StateGroup> {
7173
public cssClass(opts: OptionsReader): string {
7274
switch (opts.outputMode) {
7375
case OutputMode.BEM:
74-
return `${this.parent.cssClass(opts)}${ this.isUniversal ? '' : `-${this.name}`}`;
76+
return `${this.parent.cssClass(opts)}${ this.isUniversal ? "" : `-${this.name}`}`;
7577
default:
7678
return assertNever(opts.outputMode);
7779
}

Diff for: ‎packages/css-blocks/src/Block/StateGroup.ts

+18-17
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
import { Node } from "./BlockTree";
2-
import { State } from "./State";
3-
import { BlockClass } from "./BlockClass";
4-
import { ObjectDictionary } from "@opticss/util";
5-
import { UNIVERSAL_STATE } from "../BlockSyntax";
6-
import { OptionsReader } from "../OptionsReader";
7-
import { OutputMode } from "../OutputMode";
8-
import { Block } from "./Block";
9-
101
import {
112
Attr,
12-
ValueAbsent,
133
AttributeNS,
144
AttributeValueChoice,
15-
ValueConstant
5+
ValueAbsent,
6+
ValueConstant,
167
} from "@opticss/element-analysis";
8+
import { ObjectDictionary } from "@opticss/util";
9+
10+
import { UNIVERSAL_STATE } from "../BlockSyntax";
11+
import { OptionsReader } from "../OptionsReader";
12+
import { OutputMode } from "../OutputMode";
13+
14+
import { Block } from "./Block";
15+
import { BlockClass } from "./BlockClass";
16+
import { Node } from "./BlockTree";
17+
import { State } from "./State";
1718

1819
export class StateGroup extends Node<StateGroup, Block, BlockClass, State>
1920
{
2021

2122
private _hasSubStates = false;
22-
private _universalState: State;
23-
private _sourceAttributes: Attr[];
23+
private _universalState: State | undefined;
24+
private _sourceAttributes: Attr[] | undefined;
2425

2526
constructor(name: string, parent: BlockClass, root: Block) {
2627
super(name, parent, root);
@@ -29,7 +30,7 @@ export class StateGroup extends Node<StateGroup, Block, BlockClass, State>
2930
protected newChild(name: string): State { return new State(name, this, this.block); }
3031

3132
get hasSubStates(): boolean { return this._hasSubStates; }
32-
get universalState(): State { return this._universalState; }
33+
get universalState(): State | undefined { return this._universalState; }
3334
get blockClass(): BlockClass { return this.parent; }
3435

3536
states(): State[] { return this.children(); }
@@ -58,7 +59,7 @@ export class StateGroup extends Node<StateGroup, Block, BlockClass, State>
5859
* @param stateName The name of the sub-state to resolve.
5960
*/
6061
resolveStates(): Map<string, State> {
61-
let resolved: Map<string, State> = new Map;
62+
let resolved: Map<string, State> = new Map();
6263
for (let base of this.resolveInheritance()) {
6364
if (base.states()) {
6465
resolved = new Map([...resolved, ...base._children]);
@@ -77,7 +78,7 @@ export class StateGroup extends Node<StateGroup, Block, BlockClass, State>
7778
* @returns The State's attribute selector
7879
*/
7980
asSource(): string {
80-
return (this.blockClass.isRoot ? '' : this.blockClass.asSource()) + this.unqualifiedSource();
81+
return (this.blockClass.isRoot ? "" : this.blockClass.asSource()) + this.unqualifiedSource();
8182
}
8283

8384
asSourceAttributes(): Attr[] {
@@ -122,7 +123,7 @@ export class StateGroup extends Node<StateGroup, Block, BlockClass, State>
122123
let cssClassName = this.blockClass.cssClass(opts);
123124
return `${cssClassName}--${this.name}`;
124125
default:
125-
throw "this never happens";
126+
throw new Error("this never happens");
126127
}
127128
}
128129

Diff for: ‎packages/css-blocks/src/BlockCompiler/ConflictResolver.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ export class ConflictResolver {
9393

9494
// Fetch the associated `Style`. If does not exist (ex: malformed selector), skip.
9595
let blockNode = getBlockNode(key);
96-
if ( !blockNode ) { return; }
96+
if (!blockNode) { return; }
9797
let obj: Style | null = block.nodeAndTypeToStyle(blockNode);
98-
if ( !obj ) { return; }
98+
if (!obj) { return; }
9999

100100
// Fetch the set of Style conflicts. If the Style has already
101101
// been handled, skip.

Diff for: ‎packages/css-blocks/src/BlockSyntax/BlockPath.ts

+16-15
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,14 @@ function stringify(tokens: Token[]): string {
6868
* Simple utility to easily walk over string data one character at a time.
6969
*/
7070
class Walker {
71-
private data: string;
72-
private length: number;
71+
private data = "";
72+
private length = 0;
7373
private idx = 0;
7474

75-
constructor(data: string) {
75+
init(data: string) {
7676
this.data = data;
7777
this.length = data.length;
78+
this.idx = 0;
7879
}
7980

8081
next(): string { return this.data[this.idx++]; }
@@ -101,12 +102,12 @@ class Walker {
101102
* Parser and container object for Block Path data.
102103
*/
103104
export class BlockPath {
104-
private _location: ErrorLocation | undefined;
105-
private _block: BlockToken | undefined;
106-
private _class: ClassToken | undefined;
107-
private _state: StateToken | undefined;
105+
private _location?: ErrorLocation;
106+
private _block?: BlockToken;
107+
private _class?: ClassToken;
108+
private _state?: StateToken;
108109

109-
private walker: Walker;
110+
private walker: Walker = new Walker();
110111
private _tokens: Token[] = [];
111112
private parts: Token[] = [];
112113

@@ -119,7 +120,7 @@ export class BlockPath {
119120
if (this._location) {
120121
location = {
121122
...this._location,
122-
column: (this._location.column || 0) + this.walker!.index() - len,
123+
column: (this._location.column || 0) + this.walker.index() - len,
123124
};
124125
}
125126
throw new BlockPathError(msg, location);
@@ -163,7 +164,7 @@ export class BlockPath {
163164
private tokenize(): void {
164165
let char,
165166
working = "",
166-
walker = this.walker!,
167+
walker = this.walker,
167168
token: Partial<Token> = { type: "block" };
168169

169170
while (char = walker.next()) {
@@ -176,7 +177,7 @@ export class BlockPath {
176177
if (!isIdent(working)) { return this.throw(ERRORS.invalidIdent(working), working.length); }
177178
token.name = working;
178179
this.addToken(token, true);
179-
token = { type: 'class' };
180+
token = { type: "class" };
180181
working = "";
181182
break;
182183

@@ -186,7 +187,7 @@ export class BlockPath {
186187
if (!isIdent(working)) { return this.throw(ERRORS.invalidIdent(working), working.length); }
187188
token.name = working;
188189
this.addToken(token, true);
189-
token = { type: 'state' };
190+
token = { type: "state" };
190191
working = "";
191192
break;
192193

@@ -274,13 +275,13 @@ export class BlockPath {
274275
this.parts = path.parts;
275276
}
276277
else {
277-
this.walker = new Walker(path);
278+
this.walker.init(path);
278279
this.tokenize();
279280
}
280281
}
281282

282283
private static from(tokens: Token[]) {
283-
let path = new BlockPath('');
284+
let path = new BlockPath("");
284285
path.parts = tokens;
285286
return path;
286287
}
@@ -327,7 +328,7 @@ export class BlockPath {
327328
* Return a new BlockPath without the parent-most token.
328329
*/
329330
childPath() {
330-
return BlockPath.from(this.parts.slice(this._block.name ? 1 : 2));
331+
return BlockPath.from(this.parts.slice(this._block && this._block.name ? 1 : 2));
331332
}
332333

333334
/**

Diff for: ‎packages/css-blocks/src/BlockSyntax/BlockSyntax.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ export const SELF_SELECTOR = "::self";
4949
// Internally use the invented `::universal` state name to represent a bare state selector with no value set.
5050
// This way, we can treat the universal state selector as just another `State` object instead of having to
5151
// special case it in the `StateGroup` Block object.
52-
export const UNIVERSAL_STATE = '::universal';
52+
export const UNIVERSAL_STATE = "::universal";
5353

5454
// Internally use the invented `root` class represents the root element styling for a block. By interpreting the
5555
// root selector as just another class we no longer have to store styling information it on the `Block` object..
56-
export const ROOT_CLASS = 'root';
56+
export const ROOT_CLASS = "root";

Diff for: ‎packages/css-blocks/src/TemplateAnalysis/ElementAnalysis.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import {
2626
isBlockClass,
2727
isState,
2828
State,
29-
Style,
3029
StateGroup,
30+
Style,
3131
} from "../Block";
3232
import {
3333
OptionsReader as CssBlocksOptionsReader,
@@ -451,7 +451,7 @@ export class ElementAnalysis<BooleanExpression, StringExpression, TernaryExpress
451451
container,
452452
group: group.statesHash(),
453453
stringExpression,
454-
disallowFalsy
454+
disallowFalsy,
455455
});
456456
}
457457
private _addDynamicGroup(style: ConditionalDependentStateGroup<StringExpression>) {

Diff for: ‎packages/css-blocks/src/TemplateAnalysis/validations/property-conflict-validator.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ import { MultiMap, objectValues, TwoKeyMultiMap } from "@opticss/util";
22
import * as propParser from "css-property-parser";
33
import * as postcss from "postcss";
44

5-
import { Style, State } from "../../Block";
6-
import { Ruleset } from "../../Block/RulesetContainer";
5+
import { Style } from "../../Block";
76
import { Ruleset } from "../../Block/BlockTree";
8-
import { Validator } from "./Validator";
9-
107
import {
118
isBooleanState,
129
isFalseCondition,

Diff for: ‎packages/css-blocks/test/opticss-test.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ import {
1010
import {
1111
assert as typedAssert,
1212
clean,
13-
ObjectDictionary,
1413
whatever,
1514
} from "@opticss/util";
1615
import { assert } from "chai";
1716
import { suite, test } from "mocha-typescript";
1817
import { Optimizer } from "opticss";
1918
import * as postcss from "postcss";
2019

21-
import { Block, BlockClass, State, Style, StateGroup } from "../src/Block";
20+
import { Block, BlockClass, State, Style } from "../src/Block";
2221
import { BlockCompiler } from "../src/BlockCompiler";
2322
import { BlockFactory } from "../src/BlockFactory";
2423
import { BlockParser } from "../src/BlockParser";

Diff for: ‎packages/css-blocks/test/state-container-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
OptionsReader,
1111
} from "../src/OptionsReader";
1212
import cssBlocks = require("../src/cssBlocks");
13-
import { State, StateGroup } from "../src/index";
13+
import { State } from "../src/index";
1414
import {
1515
PluginOptions,
1616
} from "../src/options";

Diff for: ‎packages/css-blocks/test/template-analysis-test.ts

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { POSITION_UNKNOWN } from "@opticss/element-analysis";
22
import { SerializedTemplateAnalysis as SerializedOptimizedAnalysis, Template } from "@opticss/template-api";
3-
import { ObjectDictionary } from "@opticss/util";
43
import { assert } from "chai";
5-
import { suite, test, skip, only } from "mocha-typescript";
4+
import { skip, suite, test } from "mocha-typescript";
65
import * as postcss from "postcss";
76

87
import { Block, BlockClass, State, StateGroup } from "../src/Block";
@@ -252,8 +251,8 @@ export class TemplateAnalysisTests {
252251
analysis.blocks[""] = block;
253252
let element: TestElement = analysis.startElement({ line: 10, column: 32 });
254253
element.addDynamicClasses({condition: null, whenTrue: [block.rootClass]});
255-
element.addDynamicGroup( block.rootClass, block.rootClass.resolveGroup('color') as StateGroup, null );
256-
element.addDynamicState( block.rootClass, block.rootClass.getState('bgcolor')!, null );
254+
element.addDynamicGroup(block.rootClass, block.rootClass.resolveGroup("color") as StateGroup, null);
255+
element.addDynamicState(block.rootClass, block.rootClass.getState("bgcolor")!, null);
257256
analysis.endElement(element);
258257

259258
let result = analysis.serialize();
@@ -267,7 +266,7 @@ export class TemplateAnalysisTests {
267266
staticStyles: [ ],
268267
dynamicClasses: [ {condition: true, whenTrue: [ 0 ] } ],
269268
dynamicStates: [
270-
{ stringExpression: true, group: {'::universal': 2 }, container: 0 },
269+
{ stringExpression: true, group: {"::universal": 2 }, container: 0 },
271270
{ condition: true, state: 1, container: 0 },
272271
],
273272
},
@@ -295,8 +294,8 @@ export class TemplateAnalysisTests {
295294
analysis.blocks[""] = block;
296295
let element: TestElement = analysis.startElement({ line: 10, column: 32 });
297296
element.addStaticClass(block.rootClass);
298-
element.addDynamicGroup( block.rootClass, block.rootClass.resolveGroup('color') as StateGroup, null );
299-
element.addDynamicGroup( block.rootClass, block.rootClass.resolveGroup('color') as StateGroup, null, true );
297+
element.addDynamicGroup(block.rootClass, block.rootClass.resolveGroup("color") as StateGroup, null);
298+
element.addDynamicGroup(block.rootClass, block.rootClass.resolveGroup("color") as StateGroup, null, true);
300299
analysis.endElement(element);
301300

302301
let result = analysis.serialize();
@@ -357,8 +356,8 @@ export class TemplateAnalysisTests {
357356
analysis.blocks[""] = block;
358357
let element: TestElement = analysis.startElement({ line: 10, column: 32 });
359358
element.addStaticClass(block.rootClass);
360-
element.addDynamicGroup( block.rootClass, block.rootClass.resolveGroup('color') as StateGroup, null );
361-
element.addDynamicGroup(block.rootClass, block.rootClass.resolveGroup('bgcolor') as StateGroup, null, true );
359+
element.addDynamicGroup(block.rootClass, block.rootClass.resolveGroup("color") as StateGroup, null);
360+
element.addDynamicGroup(block.rootClass, block.rootClass.resolveGroup("bgcolor") as StateGroup, null, true);
362361
analysis.endElement(element);
363362

364363
let result = analysis.serialize();
@@ -537,7 +536,7 @@ export class TemplateAnalysisTests {
537536
return this.parseBlock(css, "blocks/foo.block.css", reader).then(([block, _]) => {
538537
analysis.blocks[""] = block;
539538
let element: TestElement = analysis.startElement(POSITION_UNKNOWN);
540-
element.addStaticClass( block.rootClass );
539+
element.addStaticClass(block.rootClass);
541540
element.addDynamicGroup(block.rootClass, block.rootClass.resolveGroup("foo") as StateGroup, null, true);
542541
analysis.endElement(element);
543542
let result = analysis.serialize();
@@ -898,9 +897,9 @@ export class TemplateAnalysisTests {
898897
analysis.blocks[""] = block;
899898
analysis.blocks["a"] = aBlock;
900899
let element = analysis.startElement({ line: 10, column: 32 });
901-
let klass = block.getClass('pretty') as BlockClass;
902-
let state = klass.resolveState('color', 'yellow')!;
903-
element.addStaticState(klass, state );
900+
let klass = block.getClass("pretty") as BlockClass;
901+
let state = klass.resolveState("color", "yellow")!;
902+
element.addStaticState(klass, state);
904903
analysis.endElement(element);
905904
assert.deepEqual(1, 1);
906905
}));
@@ -939,9 +938,9 @@ export class TemplateAnalysisTests {
939938
analysis.blocks["a"] = aBlock;
940939
let element = analysis.startElement({ line: 10, column: 32 });
941940

942-
let klass = block.getClass('pretty') as BlockClass;
943-
let state = klass.resolveState('color', 'yellow') as State;
944-
element.addStaticClass( klass );
941+
let klass = block.getClass("pretty") as BlockClass;
942+
let state = klass.resolveState("color", "yellow") as State;
943+
element.addStaticClass(klass);
945944
element.addStaticState(klass, state);
946945
analysis.endElement(element);
947946
assert.deepEqual(1, 1);

Diff for: ‎packages/css-blocks/test/validations/class-pairs-validator-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Template } from "@opticss/template-api";
22
import { suite, test } from "mocha-typescript";
33
import * as postcss from "postcss";
44

5-
import { Block, BlockClass, State } from "../../src/Block";
5+
import { Block } from "../../src/Block";
66
import { BlockFactory } from "../../src/BlockFactory";
77
import { BlockParser } from "../../src/BlockParser";
88
import { OptionsReader } from "../../src/OptionsReader";

Diff for: ‎packages/css-blocks/test/validations/property-conflict-validator-test.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { Template } from "@opticss/template-api";
2-
import { ObjectDictionary } from "@opticss/util";
32
import { assert } from "chai";
43
import { suite, test } from "mocha-typescript";
54
import * as postcss from "postcss";
65

7-
import { Block, BlockClass, State, StateGroup } from "../../src/Block";
6+
import { Block, BlockClass, State } from "../../src/Block";
87
import { BlockFactory } from "../../src/BlockFactory";
98
import { BlockParser } from "../../src/BlockParser";
109
import { OptionsReader } from "../../src/OptionsReader";
@@ -1288,9 +1287,7 @@ function constructElement(block: Block, ...styles: string[]) {
12881287
let element = analysis.startElement({ line: 10, column: 32 });
12891288

12901289
for (let path of styles) {
1291-
console.log(path);
12921290
let style = block.lookup(path);
1293-
console.log(style);
12941291
if (!style) { throw Error(`Error looking up Style ${path} for test.`); }
12951292
if (style instanceof BlockClass) {
12961293
element.addStaticClass(style);

Diff for: ‎packages/css-blocks/test/validations/root-class-validator-test.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import { assert } from "chai";
33
import { suite, test } from "mocha-typescript";
44
import * as postcss from "postcss";
55

6-
import { Block, BlockClass, State } from "../../src/Block";
6+
import { Block } from "../../src/Block";
77
import { BlockFactory } from "../../src/BlockFactory";
88
import { BlockParser } from "../../src/BlockParser";
9-
import { Importer, ImportedFile } from "../../src/importing";
109
import { OptionsReader } from "../../src/OptionsReader";
1110
import { SerializedTemplateAnalysis, TemplateAnalysis } from "../../src/TemplateAnalysis";
1211
import * as cssBlocks from "../../src/errors";

Diff for: ‎packages/css-blocks/test/validations/state-parent-validator-test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { assert } from "chai";
33
import { suite, test } from "mocha-typescript";
44
import * as postcss from "postcss";
55

6-
import { Block, BlockClass, State } from "../../src/Block";
6+
import { Block, BlockClass } from "../../src/Block";
77
import { BlockFactory } from "../../src/BlockFactory";
88
import { BlockParser } from "../../src/BlockParser";
99
import { OptionsReader } from "../../src/OptionsReader";
@@ -113,9 +113,9 @@ export class TemplateAnalysisTests {
113113
analysis.blocks[""] = block;
114114
analysis.blocks["a"] = aBlock;
115115
let element = analysis.startElement({ line: 10, column: 32 });
116-
let klass = block.getClass('pretty') as BlockClass;
117-
let state = klass.resolveState('color', 'yellow')!;
118-
if (!state) { throw new Error('No state group `color` resolved'); }
116+
let klass = block.getClass("pretty") as BlockClass;
117+
let state = klass.resolveState("color", "yellow")!;
118+
if (!state) { throw new Error("No state group `color` resolved"); }
119119
element.addStaticState(klass, state);
120120
analysis.endElement(element);
121121
assert.deepEqual(1, 1);
@@ -155,8 +155,8 @@ export class TemplateAnalysisTests {
155155
analysis.blocks["a"] = aBlock;
156156
let element = analysis.startElement({ line: 10, column: 32 });
157157
let klass = block.getClass("pretty") as BlockClass;
158-
let state = klass.resolveState('color', 'yellow');
159-
if (!state) { throw new Error('No state group `color` resolved'); }
158+
let state = klass.resolveState("color", "yellow");
159+
if (!state) { throw new Error("No state group `color` resolved"); }
160160
element.addStaticClass(klass);
161161
element.addStaticState(klass, state);
162162
analysis.endElement(element);

Diff for: ‎packages/css-blocks/test/validations/validator-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Template } from "@opticss/template-api";
33
import { suite, test } from "mocha-typescript";
44
import * as postcss from "postcss";
55

6-
import { Block, BlockClass, State } from "../../src/Block";
6+
import { Block } from "../../src/Block";
77
import { BlockFactory } from "../../src/BlockFactory";
88
import { BlockParser } from "../../src/BlockParser";
99
import { OptionsReader } from "../../src/OptionsReader";

Diff for: ‎yarn.lock

+747-24
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.