Skip to content

Commit

Permalink
fix: space inserted after function interpolation (fixes #1141 #1368) (#…
Browse files Browse the repository at this point in the history
…1374)

* fix: space inserted after function interpolation (fixes #1141 #1368)

This fixes the failing test that was added in #1151.

* chore: changeset for #1374

---------

Co-authored-by: Anton Evzhakov <[email protected]>
  • Loading branch information
agriffis and Anber authored Nov 19, 2023
1 parent 60e6b7e commit faf6579
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changeset/seven-insects-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@linaria/tags': patch
'@linaria/testkit': patch
---

Fix for space inserted after function interpolation. Fixes #1141 and #1368.
12 changes: 3 additions & 9 deletions packages/tags/src/utils/templateProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* It uses CSS code from template literals and evaluated values of lazy dependencies stored in ValueCache.
*/

import type { TemplateElement, SourceLocation } from '@babel/types';
import type { TemplateElement } from '@babel/types';

import type { ExpressionValue, Replacements } from '@linaria/utils';
import { hasMeta, ValueType } from '@linaria/utils';
Expand Down Expand Up @@ -37,30 +37,24 @@ export default function templateProcessor(
let cssText = '';

let item: TemplateElement | ExpressionValue | undefined;
let lastTemplateElementLocation: SourceLocation | null | undefined;
// eslint-disable-next-line no-cond-assign
while ((item = template.shift())) {
if ('type' in item) {
// It's a template element
cssText += item.value.cooked;
lastTemplateElementLocation = item.loc;
continue;
}

// It's an expression
const { ex } = item;

const { end } = ex.loc!;
const { start, end } = ex.loc!;
const beforeLength = cssText.length;

// The location will be end of the current string to start of next string
const next = template[0] as TemplateElement; // template[0] is the next template element
const loc = {
// +1 because an expression location always shows 1 column before
start: {
line: lastTemplateElementLocation!.end.line,
column: lastTemplateElementLocation!.end.column + 1,
},
start,
end: next
? { line: next.loc!.start.line, column: next.loc!.start.column }
: { line: end.line, column: end.column + 1 },
Expand Down
4 changes: 2 additions & 2 deletions packages/testkit/src/__snapshots__/babel.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1469,7 +1469,7 @@ exports[`strategy shaker handles interpolation in css function followed by unit
const _exp = /*#__PURE__*/() => props => props.$rotateDeg;
export const Container = /*#__PURE__*/styled('div')({
name: "Container",
class: "atm_tr_18309cv atm_vy_1783zgq Container_c13jq05",
class: "atm_tr_18309cv atm_vy_uuw12j Container_c13jq05",
propsAsIs: false,
vars: {
"y6125t": [_exp(), "deg"]
Expand All @@ -1483,7 +1483,7 @@ exports[`strategy shaker handles interpolation in css function followed by unit
CSS:
.atm_tr_18309cv{transform:rotate(var(--y6125t));}
.atm_vy_1783zgq{width:200 px;}
.atm_vy_uuw12j{width:200px;}
Dependencies: NA
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit faf6579

Please sign in to comment.