Skip to content

Commit f14ca9b

Browse files
authored
chore: throw when macros are called not as a macro (#7441)
1 parent c266dc2 commit f14ca9b

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

packages/@react-spectrum/s2/style/style-macro.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function createSizingProperty<T extends CSSValue>(values: PropertyValueMa
5757
return [{[property]: value}, valueMap.get(value)!];
5858
});
5959
}
60-
60+
6161
if (typeof value === 'number') {
6262
let cssValue = value === 0 ? '0px' : fn(value);
6363
return {default: [{[property]: cssValue}, generateName(value + valueMap.size)]};
@@ -630,6 +630,14 @@ function getStaticClassName(rules: Rule[]): string {
630630
}
631631

632632
export function raw(this: MacroContext | void, css: string, layer = '_.a') {
633+
// Check if `this` is undefined, which means style was not called as a macro but as a normal function.
634+
// We also check if this is globalThis, which happens in non-strict mode bundles.
635+
// Also allow style to be called as a normal function in tests.
636+
// @ts-ignore
637+
// eslint-disable-next-line
638+
if ((this == null || this === globalThis) && process.env.NODE_ENV !== 'test') {
639+
throw new Error('The raw macro must be imported with {type: "macro"}.');
640+
}
633641
let className = generateArbitraryValueSelector(css, true);
634642
css = `@layer ${layer} {
635643
.${className} {
@@ -646,6 +654,14 @@ export function raw(this: MacroContext | void, css: string, layer = '_.a') {
646654
}
647655

648656
export function keyframes(this: MacroContext | void, css: string) {
657+
// Check if `this` is undefined, which means style was not called as a macro but as a normal function.
658+
// We also check if this is globalThis, which happens in non-strict mode bundles.
659+
// Also allow style to be called as a normal function in tests.
660+
// @ts-ignore
661+
// eslint-disable-next-line
662+
if ((this == null || this === globalThis) && process.env.NODE_ENV !== 'test') {
663+
throw new Error('The keyframes macro must be imported with {type: "macro"}.');
664+
}
649665
let name = generateArbitraryValueSelector(css, true);
650666
css = `@keyframes ${name} {
651667
${css}

0 commit comments

Comments
 (0)