From 4a4e1363b44dad4ccd20a56dc393c84d50dc4dce Mon Sep 17 00:00:00 2001 From: Anton Evzhakov Date: Tue, 21 Feb 2023 16:12:28 +0200 Subject: [PATCH] fix(tags): tags parsing is too aggressive (fixes #1214) --- packages/react/src/processors/styled.ts | 3 +++ packages/tags/src/TaggedTemplateProcessor.ts | 3 +++ packages/tags/src/utils/validateParams.ts | 8 ++++++-- packages/testkit/src/utils/getTagProcessor.test.ts | 13 +++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/react/src/processors/styled.ts b/packages/react/src/processors/styled.ts index b4fdb38c2..f599447b7 100644 --- a/packages/react/src/processors/styled.ts +++ b/packages/react/src/processors/styled.ts @@ -54,6 +54,9 @@ export default class StyledProcessor extends TaggedTemplateProcessor { #variablesCache = new Map(); constructor(params: Params, ...args: TailProcessorParams) { + // If the first param is not a tag, we should skip the expression. + validateParams(params, ['tag', '...'], TaggedTemplateProcessor.SKIP); + validateParams( params, ['tag', ['call', 'member'], ['template', 'call']], diff --git a/packages/tags/src/TaggedTemplateProcessor.ts b/packages/tags/src/TaggedTemplateProcessor.ts index 1346e49a1..6525b676e 100644 --- a/packages/tags/src/TaggedTemplateProcessor.ts +++ b/packages/tags/src/TaggedTemplateProcessor.ts @@ -10,6 +10,9 @@ export default abstract class TaggedTemplateProcessor extends BaseProcessor { #template: (TemplateElement | ExpressionValue)[]; public constructor(params: Params, ...args: TailProcessorParams) { + // If the first param is not a tag, we should skip the expression. + validateParams(params, ['tag', '...'], TaggedTemplateProcessor.SKIP); + validateParams( params, ['tag', 'template'], diff --git a/packages/tags/src/utils/validateParams.ts b/packages/tags/src/utils/validateParams.ts index 36fc639c5..baa077e7d 100644 --- a/packages/tags/src/utils/validateParams.ts +++ b/packages/tags/src/utils/validateParams.ts @@ -53,9 +53,13 @@ export function isValidParams( export function validateParams( params: Params, constraints: T, - message: string + messageOrError: unknown ): asserts params is MapParams { if (!isValidParams(params, constraints)) { - throw new Error(message); + if (typeof messageOrError === 'string') { + throw new Error(messageOrError); + } + + throw messageOrError; } } diff --git a/packages/testkit/src/utils/getTagProcessor.test.ts b/packages/testkit/src/utils/getTagProcessor.test.ts index 60043a461..53f4f245d 100644 --- a/packages/testkit/src/utils/getTagProcessor.test.ts +++ b/packages/testkit/src/utils/getTagProcessor.test.ts @@ -335,6 +335,12 @@ describe('getTagProcessor', () => { expect(runner).toThrow('Invalid usage of template tag'); }); + it('do not throw if css is not a tag', () => { + const runner = () => run(dedent`export { css } from "@linaria/core";`); + + expect(runner).not.toThrow(); + }); + it('styled`` tag', () => { const runner = () => run( @@ -344,6 +350,13 @@ describe('getTagProcessor', () => { expect(runner).toThrow('Invalid usage of `styled` tag'); }); + it('do not throw if styled is not a tag', () => { + const runner = () => + run(dedent`export { styled } from "@linaria/react";`); + + expect(runner).not.toThrow(); + }); + it('styled.div.span`` tag', () => { const runner = () => run(