Skip to content

Commit 5be5080

Browse files
committed
feat(eslint): support template literal interpolation for define-style
1 parent fa71e69 commit 5be5080

File tree

1 file changed

+24
-2
lines changed
  • packages/eslint/src/rules/define-style

1 file changed

+24
-2
lines changed

packages/eslint/src/rules/define-style/index.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,33 @@ const rule: RuleModule<MessageIds, RuleOptions> = {
4949
const arg = node.arguments[0]
5050

5151
if (arg?.type === 'TemplateLiteral') {
52-
const cssRaw = arg.quasis[0].value.raw
52+
let index = 0
53+
const cssRaw = context.sourceCode.text.slice(
54+
arg.range[0] + 1,
55+
arg.range[1] - 1,
56+
)
57+
// .replaceAll('${', '--')
58+
// arg.quasis[0].value.raw.replaceAll('${', '-')
5359

5460
let formattedCss = ''
5561
try {
56-
formattedCss = prettier.format(cssRaw, { parser, tabWidth })
62+
formattedCss = prettier
63+
.format(
64+
arg.quasis
65+
.map(
66+
(i) =>
67+
i.value.raw +
68+
(arg.expressions[index]
69+
? `-MACROS_START-${context.sourceCode.text.slice(
70+
...arg.expressions[index++].range,
71+
)}-MACROS_END-`
72+
: ''),
73+
)
74+
.join(''),
75+
{ parser, tabWidth },
76+
)
77+
.replaceAll('-MACROS_START-', '${')
78+
.replaceAll('-MACROS_END-', '}')
5779
} catch {
5880
return context.report({
5981
node: arg,

0 commit comments

Comments
 (0)