Skip to content

Commit 6fc3675

Browse files
committed
fix: Don't allow blocks to be imported with a well-known namespace.
1 parent 9e80277 commit 6fc3675

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ObjectDictionary } from "@opticss/util";
22
import { postcss } from "opticss";
33

4-
import { BLOCK_IMPORT, CLASS_NAME_IDENT, DEFAULT_EXPORT } from "../../BlockSyntax";
4+
import { BLOCK_IMPORT, CLASS_NAME_IDENT, DEFAULT_EXPORT, RESERVED_BLOCK_NAMES } from "../../BlockSyntax";
55
import { Block } from "../../BlockTree";
66
import * as errors from "../../errors";
77
import { sourceRange } from "../../SourceLocation";
@@ -73,9 +73,9 @@ export async function importBlocks(block: Block, factory: BlockFactory, file: st
7373
sourceRange(factory.configuration, block.stylesheet, file, atRule),
7474
);
7575
}
76-
if (localName === DEFAULT_EXPORT) {
76+
if (RESERVED_BLOCK_NAMES.has(localName)) {
7777
throw new errors.InvalidBlockSyntax(
78-
`Can not import "${remoteName}" as reserved word "${DEFAULT_EXPORT}"`,
78+
`Can not import "${remoteName}" as reserved word "${localName}"`,
7979
sourceRange(factory.configuration, block.stylesheet, file, atRule),
8080
);
8181
}

packages/@css-blocks/core/src/BlockSyntax/BlockSyntax.ts

+6
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,9 @@ export const ROOT_CLASS = ":scope";
6666

6767
// The string `default` is used throughout the system to represent the default export of a block file.
6868
export const DEFAULT_EXPORT = "default";
69+
70+
/**
71+
* Names that a block cannot have lest it collides with other syntax.
72+
*/
73+
export const RESERVED_BLOCK_NAMES = new Set<string>([DEFAULT_EXPORT, "html", "svg", "math"]);
74+
Object.freeze(RESERVED_BLOCK_NAMES);

packages/@css-blocks/core/test/BlockParser/import-export-test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,38 @@ export class BlockImportExport extends BEMProcessor {
563563
);
564564
}
565565

566+
@test async "html is a reserved word – named imports"() {
567+
let imports = new MockImportRegistry();
568+
imports.registerSource(
569+
"a.css",
570+
`:scope { block-name: block-a; }`,
571+
);
572+
573+
let inputCSS = `@block ( a as html ) from "./a.css";`;
574+
575+
return assertError(
576+
InvalidBlockSyntax,
577+
`Can not import "a" as reserved word "html" (test.css:1:1)`,
578+
this.process("test.css", inputCSS, {importer: imports.importer()}),
579+
);
580+
}
581+
582+
@test async "html filename is a reserved word – named imports"() {
583+
let imports = new MockImportRegistry();
584+
imports.registerSource(
585+
"html.block.css",
586+
`:scope { color: red; }`,
587+
);
588+
589+
let inputCSS = `@block html from "./html.block.css";`;
590+
591+
return assertError(
592+
InvalidBlockSyntax,
593+
`Can not import "default" as reserved word "html" (test.css:1:1)`,
594+
this.process("test.css", inputCSS, {importer: imports.importer()}),
595+
);
596+
}
597+
566598
@test async "default is a reserved word – bare exports"() {
567599
let imports = new MockImportRegistry();
568600

0 commit comments

Comments
 (0)