linaria icon indicating copy to clipboard operation
linaria copied to clipboard

The function cannot be used in a string template

Open afflicted-cat opened this issue 3 years ago • 2 comments

Environment

Linaria version: v4+ Bundler (+ version): Webpack 5+ Node.js version: 16+

Description

Using a function declaration in a string template results in the error "This expression cannot be used in a template because it is a function parameter.":

function getColor() {
  return 'red';
}

const Title = styled.h1`
  color: ${getColor()};
`;

At the same time, the use of function expression is correctly processed:

const getColor = () => {
  return 'red';
}

const Title = styled.h1`
  color: ${getColor()};
`;

Reproducible Demo

https://codesandbox.io/s/jolly-leftpad-6gfqz1?file=/pages/index.js

afflicted-cat avatar Aug 18 '22 14:08 afflicted-cat

Also seeing this issue, it happens when I try to upgrade @linaria/webpack-loader from 3.0.0-beta.16 to 4.1.3

paddyobrien avatar Sep 21 '22 09:09 paddyobrien

The issue appears to happen here: https://github.com/callstack/linaria/blob/master/packages/babel/src/utils/collectTemplateDependencies.ts#L142

binding.kind is set to hoisted and the above error message is generated

paddyobrien avatar Sep 21 '22 13:09 paddyobrien