Skip to content

Commit 4e60eb5

Browse files
authored
fix: enhance annotation mach on decimal numbers (#22)
1 parent 6904e61 commit 4e60eb5

5 files changed

Lines changed: 48 additions & 15 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "DNB Eufemia Tools",
44
"description": "DNB Eufemia Design System Extension",
55
"categories": [],
6-
"version": "1.3.5",
6+
"version": "1.3.6",
77
"publisher": "dnbexperience",
88
"author": "Tobias Høegh <tobias.hoegh@dnb.no>",
99
"license": "SEE LICENSE IN LICENSE",

src/extension/__tests__/helpers.test.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,50 @@ beforeAll(() => {
2222

2323
describe('matchLineWhen', () => {
2424
it('should match on px', () => {
25-
expect(matchLineWhen('12.5px')).toBeTruthy()
25+
const match = matchLineWhen('12.5px')
26+
expect(match).toBeTruthy()
27+
expect(match).toEqual(['12.5px'])
2628
})
2729

2830
it('should match on rem', () => {
29-
expect(matchLineWhen('12.5rem')).toBeTruthy()
31+
const match = matchLineWhen('12.5rem')
32+
expect(match).toBeTruthy()
33+
expect(match).toEqual(['12.5rem'])
34+
})
35+
36+
it('should match wtih leading zero', () => {
37+
const match = matchLineWhen('0.5rem')
38+
expect(match).toBeTruthy()
39+
expect(match).toEqual(['0.5rem'])
3040
})
3141

3242
it('should match on var', () => {
33-
expect(matchLineWhen('var(--spacing-large)')).toBeTruthy()
43+
const match = matchLineWhen('var(--spacing-large)')
44+
expect(match).toBeTruthy()
45+
expect(match).toEqual(['var(--spacing-large)'])
3446
})
3547

3648
it('should not match on var only', () => {
37-
expect(matchLineWhen('var')).toBeFalsy()
49+
const match = matchLineWhen('var')
50+
expect(match).toBeFalsy()
3851
})
3952

4053
it('should not match on calc only', () => {
41-
expect(matchLineWhen('calc')).toBeFalsy()
54+
const match = matchLineWhen('calc')
55+
expect(match).toBeFalsy()
4256
})
4357

4458
it('should match on calc', () => {
45-
expect(
46-
matchLineWhen("margin-bottom: ${calc('small', 'large')};")
47-
).toBeTruthy()
59+
const match = matchLineWhen(
60+
"margin-bottom: ${calc('small', 'large')};"
61+
)
62+
expect(match).toBeTruthy()
63+
expect(match).toEqual(["calc('small', 'large')"])
4864
})
4965

5066
it('should not match when no number was given', () => {
51-
expect(matchLineWhen('document.body.removeListener')).toBeFalsy()
67+
const match = matchLineWhen('document.body.removeListener')
68+
expect(match).toBeFalsy()
5269
})
5370
})
5471

src/extension/helpers.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,11 @@ export function getConfig() {
161161

162162
export function matchLineWhen(line: Line) {
163163
return line.match(
164-
// 1. Match px/rem values, but do skip support for comments, like // 3rem
165-
// 2. Match CSS var(--*)
166-
// 3. Match JS calc('*')
167-
/(?<!\/\/.*)(\d+(px|rem))|var\(--(.*)\)|calc\(['"\`](.*)\)/g
164+
// 1. Do skip support for comments, like // 3rem
165+
// 2. Match px/rem values like "5rem"
166+
// 3. Match px/rem values, like "0.5rem" or ".5rem"
167+
// 4. Match CSS var(--*)
168+
// 5. Match JS calc('*')
169+
/(?<!\/\/.*)(\d+(px|rem))|(\d{0,}.\d+(px|rem))|var\(--(.*)\)|calc\(['"\`](.*)\)/g
168170
)
169171
}

src/extension/hover.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default class implements HoverProvider {
1515
const point = pos.character
1616
let text = ''
1717

18-
line.replace(/[.0-9]+(px|rem)/g, (a, _, idx) => {
18+
line.replace(/[.\d]+(px|rem)/g, (a, _, idx) => {
1919
const start = idx + 1
2020
const end = idx + a.length + 1
2121
if (!text && point >= start && point <= end) {

src/rules/__tests__/handleValues.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,20 @@ describe('hover', () => {
157157
})
158158
})
159159

160+
it('should show rem to px with leading zero', () => {
161+
const rule = handleValues()
162+
const text = '-0.5rem'
163+
const line = `margin-top: ${text};`
164+
const result = rule.hover?.hoverHandler?.(text, line)
165+
166+
expect(result).toEqual({
167+
documentation: 'Equivalent to `-8px`',
168+
from: '-0.5rem',
169+
to: '-8px',
170+
type: 'handleValues',
171+
})
172+
})
173+
160174
it('should not include comments (//)', () => {
161175
const rule = handleValues()
162176
const text = '-10.5rem'

0 commit comments

Comments
 (0)