Skip to content

Commit 9617fc5

Browse files
Timothy Lindvalltimlindvall
Timothy Lindvall
authored andcommitted
fix: Allow quotes surrounding block-name.
Definition file output wraps the block-name in quotes, but there are existing rules in CSS Blocks disallowing this. This removes this rule, using stripQuotes() to unwrap the block-name value. With this change, block-name, block-id, and block-class will all behave in the same way.
1 parent 6613edb commit 9617fc5

File tree

3 files changed

+41
-40
lines changed

3 files changed

+41
-40
lines changed

packages/@css-blocks/core/src/BlockParser/features/add-preset-selectors.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function addPresetSelectors(configuration: Configuration, root: postcss.R
3030

3131
// Find the block-class declaration...
3232
rule.walkDecls("block-class", decl => {
33-
const val = stripQuotes(decl.value);
33+
const val = stripQuotes(decl.value.trim());
3434

3535
// Test that this actually could be a class name.
3636
if (!CLASS_NAME_IDENT.test(val)) {

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { BLOCK_NAME, CLASS_NAME_IDENT } from "../../BlockSyntax";
44
import { Configuration } from "../../configuration";
55
import * as errors from "../../errors";
66
import { sourceRange } from "../../SourceLocation";
7+
import { stripQuotes } from "../utils";
78

89
export async function discoverName(configuration: Configuration, root: postcss.Root, file: string, isDfnFile: boolean, defaultName: string): Promise<string> {
910
let foundName: string | undefined;
@@ -13,14 +14,15 @@ export async function discoverName(configuration: Configuration, root: postcss.R
1314
root.walkRules(":scope", (rule) => {
1415
scopeRule = rule;
1516
rule.walkDecls(BLOCK_NAME, (decl) => {
16-
if (!CLASS_NAME_IDENT.test(decl.value)) {
17+
const unquotedVal = stripQuotes(decl.value.trim());
18+
if (!CLASS_NAME_IDENT.test(unquotedVal)) {
1719
throw new errors.InvalidBlockSyntax(
1820
`Illegal block name. '${decl.value}' is not a legal CSS identifier.`,
1921
sourceRange(configuration, root, file, decl),
2022
);
2123
}
2224

23-
foundName = decl.value.trim();
25+
foundName = unquotedVal;
2426

2527
});
2628
});

packages/@css-blocks/core/test/BlockParser/block-name-test.ts

+36-37
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { assert } from "chai";
22
import { skip, suite, test } from "mocha-typescript";
33
import { postcss } from "opticss";
4+
import { outdent } from "outdent";
45

56
import { CascadingError } from "../../src/errors";
67
import { assertMultipleErrorsRejection } from "../util/assertError";
@@ -13,60 +14,58 @@ const { InvalidBlockSyntax } = require("../util/postcss-helper");
1314
@suite("Block Names")
1415
export class BlockNames extends BEMProcessor {
1516

16-
@test "block names in double quotes fail parse with helpful error"(): Promise<postcss.Result | void> {
17+
@test "block names in double quotes work"(): Promise<postcss.Result | void> {
1718
let imports = new MockImportRegistry();
1819
imports.registerSource(
1920
"foo/bar/imported.css",
20-
`:scope { block-name: "snow-flake"; }`,
21+
outdent`
22+
:scope { block-name: "snow-flake"; }
23+
`,
2124
);
2225

2326
let filename = "foo/bar/test-block.css";
24-
let inputCSS = `@block a-block from "./imported.css";
25-
@block-debug a-block to comment;`;
27+
let inputCSS = outdent`
28+
@block a-block from "./imported.css";
29+
@block-debug a-block to comment;
30+
`;
2631

27-
return assertMultipleErrorsRejection(
28-
[
29-
{
30-
type: CascadingError,
31-
message: "Error in imported block. (foo/bar/test-block.css:1:1)",
32-
cause: {
33-
type: InvalidBlockSyntax,
34-
message: "Illegal block name. '\"snow-flake\"' is not a legal CSS identifier. (foo/bar/imported.css:1:10)",
35-
},
36-
}
37-
,
38-
{
39-
type: InvalidBlockSyntax,
40-
message: `Invalid block debug: No Block named "a-block" found in scope. (foo/bar/test-block.css:2:21)`,
41-
}],
42-
this.process(filename, inputCSS, {importer: imports.importer()}));
32+
return this.process(filename, inputCSS, {importer: imports.importer()}).then((result) => {
33+
assert.deepEqual(
34+
result.css.toString(),
35+
outdent`
36+
/* Source: foo/bar/imported.css
37+
* :scope (.snow-flake)
38+
*/
39+
40+
`,
41+
);
42+
});
4343
}
4444

45-
@test "block names in single quotes fail parse with helpful error"(): Promise<postcss.Result | void> {
45+
@test "block names in single quotes work"(): Promise<postcss.Result | void> {
4646
let imports = new MockImportRegistry();
4747
imports.registerSource(
4848
"foo/bar/imported.css",
4949
`:scope { block-name: 'snow-flake'; }`,
5050
);
5151

5252
let filename = "foo/bar/test-block.css";
53-
let inputCSS = `@block imported from "./imported.css";
54-
@block-debug snow-flake to comment;`;
53+
let inputCSS = outdent`
54+
@block snow-flake from "./imported.css";
55+
@block-debug snow-flake to comment;
56+
`;
5557

56-
return assertMultipleErrorsRejection(
57-
[{
58-
type: CascadingError,
59-
message: "Error in imported block. (foo/bar/test-block.css:1:1)",
60-
cause: {
61-
type: InvalidBlockSyntax,
62-
message: `Illegal block name. \'\'snow-flake\'\' is not a legal CSS identifier. (foo/bar/imported.css:1:10)`,
63-
},
64-
},
65-
{
66-
type: InvalidBlockSyntax,
67-
message: `Invalid block debug: No Block named "snow-flake" found in scope. (foo/bar/test-block.css:2:21)`,
68-
}],
69-
this.process(filename, inputCSS, {importer: imports.importer()}));
58+
return this.process(filename, inputCSS, {importer: imports.importer()}).then((result) => {
59+
assert.deepEqual(
60+
result.css.toString(),
61+
outdent`
62+
/* Source: foo/bar/imported.css
63+
* :scope (.snow-flake)
64+
*/
65+
66+
`,
67+
);
68+
});
7069
}
7170

7271
@test "block-name property only works in the root ruleset"() {

0 commit comments

Comments
 (0)