Skip to content

Commit 0c1fb73

Browse files
committed
feat(css-blocks): Added BlockPath parser
1 parent 382afc7 commit 0c1fb73

19 files changed

+627
-24
lines changed

packages/css-blocks/.vscode/cSpell.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"longhands",
1616
"truthy",
1717
"ruleset",
18-
"combinator"
18+
"combinator",
19+
"regexpu"
1920
],
2021
// flagWords - list of words to be always considered incorrect
2122
// This is useful for offensive words and common spelling errors.

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { LocalScopedContext, HasLocalScope, HasScopeLookup } from "../util/Local
88
import { unionInto } from '../util/unionInto';
99
import { ObjectDictionary, MultiMap, assertNever, objectValues } from "@opticss/util";
1010
import { RulesetContainer } from './RulesetContainer';
11+
import { CLASS_NAME_IDENT } from "../BlockSyntax";
1112
import {
1213
SelectorFactory,
1314
parseSelector,
@@ -27,8 +28,7 @@ import {
2728
isClassNode,
2829
isStateNode,
2930
NodeAndType,
30-
BlockType,
31-
CLASS_NAME_IDENT
31+
BlockType
3232
} from "../BlockParser";
3333

3434
export const OBJ_REF_SPLITTER = (s: string): [string, string] | undefined => {
@@ -485,6 +485,7 @@ export class Block
485485
}
486486

487487
getReferencedBlock(localName: string): Block | null {
488+
if (localName === "") { return this; }
488489
return this._blockReferences[localName] || null;
489490
}
490491

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as postcss from 'postcss';
22
import * as propParser from 'css-property-parser';
33
import { MultiMap, TwoKeyMultiMap } from "@opticss/util";
44
import { Style } from './Block';
5-
import { BLOCK_PROP_NAMES, RESOLVE_RE, SELF_SELECTOR } from "../blockSyntax";
5+
import { BLOCK_PROP_NAMES, RESOLVE_RE, SELF_SELECTOR, BlockPath } from "../BlockSyntax";
66
import * as errors from '../errors';
77
import { sourceLocation } from "../SourceLocation";
88

@@ -110,11 +110,20 @@ export class RulesetContainer {
110110
// If this is a resolution, track that this property has been resolved
111111
if (referenceStr) {
112112

113+
let blockPath = new BlockPath(referenceStr);
113114
let other = style.block.lookup(referenceStr);
115+
let otherBlock = style.block.getReferencedBlock(blockPath.block);
116+
117+
if (!otherBlock) {
118+
throw new errors.InvalidBlockSyntax(
119+
`No Block named "${blockPath.block}" found in scope.`,
120+
sourceLocation(file, decl)
121+
);
122+
}
114123

115124
if (!other) {
116125
throw new errors.InvalidBlockSyntax(
117-
`Cannot resolve Style at Block path "${referenceStr}".`,
126+
`No Style "${blockPath.path}" found on Block "${otherBlock.name}".`,
118127
sourceLocation(file, decl)
119128
);
120129
}

packages/css-blocks/src/BlockCompiler/ConflictResolver.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { parseSelector, ParsedSelector, CompoundSelector } from "opticss";
88
import { QueryKeySelector } from "../query";
99
import { SourceLocation, sourceLocation } from "../SourceLocation";
1010
import * as conflictDetection from "./conflictDetection";
11-
import { RESOLVE_RE } from "../blockSyntax";
11+
import { RESOLVE_RE } from "../BlockSyntax";
1212

1313
enum ConflictType {
1414
conflict,

packages/css-blocks/src/BlockCompiler/conflictDetection.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Style } from "../Block";
22
import { MultiMap } from "@opticss/util";
3-
import { SELF_SELECTOR } from "../blockSyntax";
3+
import { SELF_SELECTOR } from "../BlockSyntax";
44
import { shorthandsFor, longhandsFor } from "../shortHandProps";
55

66
export type Conflict = [string, string];

packages/css-blocks/src/BlockParser/block-intermediates.ts

-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
import regexpu = require('regexpu-core');
21
import selectorParser = require('postcss-selector-parser');
32
import { CompoundSelector } from "opticss";
43

5-
/**
6-
* CSS <ident-token> RegExp.
7-
* Defined: https://www.w3.org/TR/css-syntax-3/#typedef-ident-token
8-
*/
9-
export const CLASS_NAME_IDENT = new RegExp(regexpu("^(-?(?:\\\\.|[A-Za-z_\\u{0080}-\\u{10ffff}])(?:\\\\.|[A-Za-z0-9_\\-\\u{0080}-\\u{10ffff}])*)$", "u"));
10-
114
/**
125
* Holds state values to be passed to the StateContainer.
136
*/

packages/css-blocks/src/BlockParser/features/discover-name.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as postcss from 'postcss';
22
import * as errors from '../../errors';
3-
import { CLASS_NAME_IDENT } from "../block-intermediates";
43
import { sourceLocation } from "../../SourceLocation";
5-
import { BLOCK_NAME } from "../../blockSyntax";
4+
import { BLOCK_NAME, CLASS_NAME_IDENT } from "../../BlockSyntax";
65

76
export default async function discoverName(root: postcss.Root, defaultName: string, file: string): Promise<string> {
87

packages/css-blocks/src/BlockParser/features/extend-block.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as postcss from 'postcss';
22
import * as errors from '../../errors';
33
import { sourceLocation } from "../../SourceLocation";
44
import { Block } from "../../Block";
5-
import { EXTENDS } from "../../blockSyntax";
5+
import { EXTENDS } from "../../BlockSyntax";
66

77
/**
88
* For each `extends` property found in the passed ruleset, set the block's base

packages/css-blocks/src/BlockParser/features/global-states.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { parseSelector } from "opticss";
55
import { stateParser } from "../block-intermediates";
66
import { sourceLocation as loc } from "../../SourceLocation";
77
import { Block } from "../../Block";
8-
import { BLOCK_GLOBAL } from "../../blockSyntax";
8+
import { BLOCK_GLOBAL } from "../../BlockSyntax";
99

1010
export default async function globalStates(root: postcss.Root, block: Block, file: string): Promise<Block> {
1111
root.walkAtRules(BLOCK_GLOBAL, (atRule) => {

packages/css-blocks/src/BlockParser/features/implement-block.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as postcss from 'postcss';
22
import * as errors from '../../errors';
33
import { sourceLocation } from "../../SourceLocation";
44
import { Block } from "../../Block";
5-
import { IMPLEMENTS } from "../../blockSyntax";
5+
import { IMPLEMENTS } from "../../BlockSyntax";
66

77
/**
88
* For each `implements` property found in the passed ruleset, track the foreign

packages/css-blocks/src/BlockParser/features/process-debug-statements.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import parseBlockDebug from "../../parseBlockDebug";
22
import * as postcss from 'postcss';
33
import { Block } from "../../Block";
44
import { OptionsReader } from "../../OptionsReader";
5-
import { BLOCK_DEBUG } from "../../blockSyntax";
5+
import { BLOCK_DEBUG } from "../../BlockSyntax";
66

77
/**
88
* Process all `@block-debug` statements, output debug statement to console or in comment as requested.

packages/css-blocks/src/BlockParser/features/resolve-references.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import * as errors from '../../errors';
33
import { IBlockFactory } from "../../BlockFactory/IBlockFactory";
44
import { sourceLocation } from "../../SourceLocation";
55
import { Block } from "../../Block";
6-
import { CLASS_NAME_IDENT } from "../block-intermediates";
7-
import { BLOCK_REFERENCE } from "../../blockSyntax";
6+
import { BLOCK_REFERENCE, CLASS_NAME_IDENT } from "../../BlockSyntax";
87

98
/**
109
* Resolve all block references for a given block.

packages/css-blocks/src/BlockParser/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import extendBlock from "./features/extend-block";
1919
import implementBlock from "./features/implement-block";
2020

2121
export {
22-
CLASS_NAME_IDENT,
2322
stateParser,
2423
BlockType,
2524
NodeAndType,

0 commit comments

Comments
 (0)