Skip to content

Commit bc75a98

Browse files
committed
fix: support parsing of dynamic styles using square bracket notation
1 parent 86a7fda commit bc75a98

File tree

3 files changed

+90
-3
lines changed

3 files changed

+90
-3
lines changed

src/language-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ export class TwindTemplateLanguageService implements TemplateLanguageService {
321321
? vscode.CompletionItemKind.Module
322322
: completion.interpolation
323323
? vscode.CompletionItemKind.Variable
324-
: vscode.CompletionItemKind.Constant,
324+
: vscode.CompletionItemKind.Property,
325325
data: completion.kind,
326326
label:
327327
rule.prefix && completion.label !== '&' && completion.kind == 'utility'

src/parser.test.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,87 @@ const test = suite('Parser')
230230
},
231231
],
232232
],
233+
[
234+
'top-[-123px] grid-cols-[minmax(100px,max-content)repeat(auto-fill,200px)20%] grid-rows-auto-1fr-auto bg-[#1da1f2]',
235+
[
236+
{
237+
raw: 'top-[-123px]',
238+
value: 'top-[-123px]',
239+
name: 'top-[-123px]',
240+
prefix: '',
241+
important: false,
242+
negated: false,
243+
loc: {
244+
start: 0,
245+
end: 12,
246+
},
247+
spans: [
248+
{
249+
start: 0,
250+
end: 12,
251+
},
252+
],
253+
variants: [],
254+
},
255+
{
256+
raw: 'grid-cols-[minmax(100px,max-content)repeat(auto-fill,200px)20%]',
257+
value: 'grid-cols-[minmax(100px,max-content)repeat(auto-fill,200px)20%]',
258+
name: 'grid-cols-[minmax(100px,max-content)repeat(auto-fill,200px)20%]',
259+
prefix: '',
260+
important: false,
261+
negated: false,
262+
loc: {
263+
start: 13,
264+
end: 76,
265+
},
266+
spans: [
267+
{
268+
start: 13,
269+
end: 76,
270+
},
271+
],
272+
variants: [],
273+
},
274+
{
275+
raw: 'grid-rows-auto-1fr-auto',
276+
value: 'grid-rows-auto-1fr-auto',
277+
name: 'grid-rows-auto-1fr-auto',
278+
prefix: '',
279+
important: false,
280+
negated: false,
281+
loc: {
282+
start: 77,
283+
end: 100,
284+
},
285+
spans: [
286+
{
287+
start: 77,
288+
end: 100,
289+
},
290+
],
291+
variants: [],
292+
},
293+
{
294+
raw: 'bg-[#1da1f2]',
295+
value: 'bg-[#1da1f2]',
296+
name: 'bg-[#1da1f2]',
297+
prefix: '',
298+
important: false,
299+
negated: false,
300+
loc: {
301+
start: 101,
302+
end: 113,
303+
},
304+
spans: [
305+
{
306+
start: 101,
307+
end: 113,
308+
},
309+
],
310+
variants: [],
311+
},
312+
],
313+
],
233314
] as const).forEach(([input, expected]) => {
234315
test(`parse: ${JSON.stringify(input)}`, () => {
235316
// console.log(JSON.stringify(parse(input)))

src/parser.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,20 @@ export function astish(text: string, atPosition = Infinity): Group {
279279
let parent: Exclude<Node, null> = root
280280
let node: Exclude<Node, null> = root
281281

282-
for (let char: string, position = 0; position < text.length; position++) {
282+
for (let char: string, dynamic = false, position = 0; (char = text[position]); position++) {
283283
if (position >= atPosition) {
284284
node.next = createIdentifier(node, parent, buffer, start)
285285

286286
return root
287287
}
288288

289-
switch ((char = text[position])) {
289+
if (dynamic || char == '[') {
290+
buffer += char
291+
dynamic = char != ']'
292+
continue
293+
}
294+
295+
switch (char) {
290296
case ':':
291297
if (buffer) {
292298
buffer += char

0 commit comments

Comments
 (0)