Skip to content

Commit

Permalink
Fix(Language Service): Class is incorrectly positioned in literals
Browse files Browse the repository at this point in the history
  • Loading branch information
1aron committed Apr 2, 2024
1 parent 15e1b08 commit cab746e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 9 deletions.
5 changes: 4 additions & 1 deletion packages/language-service/playground/suggest-syntax.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import React from 'react'
import styled from '@master/styled.react'
import clsx from 'clsx'

export default () => <div className="">hello world</div>
export const A = () => <div className={`block ${'content:\'123\''}`}>hello world</div>
export const B = () => <div className={clsx('class-a class-b', `class-c class-d
class-d`)}></div>

const Button = styled.button('text:center')
26 changes: 23 additions & 3 deletions packages/language-service/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,30 @@ export default class CSSLanguageService extends EventEmitter {
token: ''
}
}
for (const classExpressionMatch of eachClassPositionExpression.matchAll(/(['"`])([\s\S]*?)\1/g)) {

// 1. "
for (const classExpressionMatch of eachClassPositionExpression.matchAll(/(?<!\\)"([\s\S]*?)(?<!\\)"/g)) {
if (classExpressionMatch.index === undefined) continue
const eachClassName = classExpressionMatch[1]
const classNameStart = eachAttrStart + classExpressionMatch.index + 1 // " length
const classPosition = normalizeClassNamePosition(eachClassName, classNameStart)
if (classPosition) return classPosition
}

// 2. '
for (const classExpressionMatch of eachClassPositionExpression.matchAll(/(?<!\\)'([\s\S]*?)(?<!\\)'/g)) {
if (classExpressionMatch.index === undefined) continue
const eachClassName = classExpressionMatch[1]
const classNameStart = eachAttrStart + classExpressionMatch.index + 1 // ' length
const classPosition = normalizeClassNamePosition(eachClassName, classNameStart)
if (classPosition) return classPosition
}

// 3. `
for (const classExpressionMatch of eachClassPositionExpression.matchAll(/(?<!\\)`([\s\S]*?)(?<!\\)`/g)) {
if (classExpressionMatch.index === undefined) continue
const eachClassName = classExpressionMatch[2]
const classNameStart = eachAttrStart + classExpressionMatch.index + classExpressionMatch[1].length
const eachClassName = classExpressionMatch[1]
const classNameStart = eachAttrStart + classExpressionMatch.index + 1 // ` length
const classPosition = normalizeClassNamePosition(eachClassName, classNameStart)
if (classPosition) return classPosition
}
Expand Down
4 changes: 3 additions & 1 deletion packages/language-service/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const settings: Settings = {
["class=", "\"", "\""],
["class=", "'", "'"],
["className=", "\"", "\""],
["className=", "'", "'"]
["className=", "'", "'"],
["styled(?:\\s+)?(?:\\.\\w+)", "`", "`"]
],
classAssignments: [
// react
Expand All @@ -40,6 +41,7 @@ const settings: Settings = {
// invoke
["clsx", "(", ")"],
["styled", "(", ")"],
["styled(?:\\s+)?(?:\\.\\w+)", "(", ")"],
["cva", "(", ")"]
],
exclude: ["**/.git/**", "**/node_modules/**", "**/.hg/**"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ test('styled', () => {
expectClassPosition(target, contents, 'tsx')
})

// test('escaped single quotes', () => {
// const target = `content:\\'\\'`
// const contents = [`export default () => <div className={'block `, target, '}></div>']
// expectClassPosition(target, contents, 'tsx')
// })
22 changes: 22 additions & 0 deletions packages/language-service/tests/get-class-position/styled.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import dedent from 'ts-dedent'
import { expectClassPosition } from './test'

test('styled template literal', () => {
const target = 'class-b'
const contents = ['const Button = styled.button`class-a ', target, '`']
expectClassPosition(target, contents, 'tsx')
})

test('styled template literal and new lines', () => {
const target = 'class-b'
const contents = [dedent`
const Button = styled
.button\`class-a `, target, '`']
expectClassPosition(target, contents, 'tsx')
})

test('styled invoked', () => {
const target = 'class-b'
const contents = ['const Button = styled.button("class-a ', target, '")']
expectClassPosition(target, contents, 'tsx')
})
6 changes: 5 additions & 1 deletion packages/language-service/tests/get-class-position/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ test('group syntax', () => {
expectClassPosition(target, contents)
})

test.todo('nested `text:center ${"block"}`')
test('nested strings and literals', () => {
const target = `content:\\'\\'`
const contents = [`export default () => <div className={'block `, target, `'}>hello world</div>`]
expectClassPosition(target, contents)
})
7 changes: 4 additions & 3 deletions packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,10 @@
[ "[className]=", "\"", "\"" ],
[ "[ngClass]=", "\"", "\"" ],
[ "class:list=", "{", "}" ],
[ "clsx", "(", ")" ],
[ "styled", "(", ")" ],
[ "cva", "(", ")" ]
["clsx", "(", ")"],
["styled", "(", ")"],
["styled(?:\\s+)?(?:\\.\\w+)", "(", ")"],
["cva", "(", ")"]
]
},
"masterCSS.suggestSyntax": {
Expand Down

0 comments on commit cab746e

Please sign in to comment.