Skip to content

Commit 6904e61

Browse files
authored
fix: make annotation mach only when number was given (#21)
1 parent 7338478 commit 6904e61

6 files changed

Lines changed: 113 additions & 14 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.4",
6+
"version": "1.3.5",
77
"publisher": "dnbexperience",
88
"author": "Tobias Høegh <tobias.hoegh@dnb.no>",
99
"license": "SEE LICENSE IN LICENSE",
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { beforeAll, describe, expect, it, vi } from 'vitest'
2+
import {
3+
getConfig,
4+
loadConfig,
5+
matchLineWhen,
6+
cleanZero,
7+
isSpacing,
8+
findNearestType,
9+
} from '../helpers'
10+
11+
vi.mock('vscode', () => {
12+
const workspace = {
13+
getConfiguration: () => getConfig(),
14+
}
15+
16+
return { workspace }
17+
})
18+
19+
beforeAll(() => {
20+
loadConfig()
21+
})
22+
23+
describe('matchLineWhen', () => {
24+
it('should match on px', () => {
25+
expect(matchLineWhen('12.5px')).toBeTruthy()
26+
})
27+
28+
it('should match on rem', () => {
29+
expect(matchLineWhen('12.5rem')).toBeTruthy()
30+
})
31+
32+
it('should match on var', () => {
33+
expect(matchLineWhen('var(--spacing-large)')).toBeTruthy()
34+
})
35+
36+
it('should not match on var only', () => {
37+
expect(matchLineWhen('var')).toBeFalsy()
38+
})
39+
40+
it('should not match on calc only', () => {
41+
expect(matchLineWhen('calc')).toBeFalsy()
42+
})
43+
44+
it('should match on calc', () => {
45+
expect(
46+
matchLineWhen("margin-bottom: ${calc('small', 'large')};")
47+
).toBeTruthy()
48+
})
49+
50+
it('should not match when no number was given', () => {
51+
expect(matchLineWhen('document.body.removeListener')).toBeFalsy()
52+
})
53+
})
54+
55+
describe('isSpacing', () => {
56+
it('should return true on condition', () => {
57+
expect(isSpacing('margin')).toBeTruthy()
58+
expect(isSpacing('padding')).toBeTruthy()
59+
expect(isSpacing('top')).toBeTruthy()
60+
expect(isSpacing('bottom')).toBeTruthy()
61+
expect(isSpacing('left')).toBeTruthy()
62+
expect(isSpacing('right')).toBeTruthy()
63+
expect(isSpacing('inset')).toBeTruthy()
64+
})
65+
})
66+
67+
describe('cleanZero', () => {
68+
it('should remove leading zero', () => {
69+
expect(cleanZero(0.5)).toBe('.5')
70+
})
71+
})
72+
73+
describe('findNearestType', () => {
74+
const list = { small: '8', basis: '16', large: '24' }
75+
76+
it('should find large', () => {
77+
expect(findNearestType(17, list)).toBe('large')
78+
})
79+
80+
it('should find small', () => {
81+
expect(findNearestType(7, list)).toBe('small')
82+
})
83+
84+
it('should find basis', () => {
85+
expect(findNearestType(15, list)).toBe('basis')
86+
})
87+
88+
it('should find large', () => {
89+
expect(findNearestType(100, list)).toBe('large')
90+
})
91+
92+
it('should return basis if empty list was given', () => {
93+
expect(findNearestType(1, {})).toBe('basis')
94+
})
95+
})

src/extension/annotation.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
ThemeColor,
1111
window,
1212
} from 'vscode'
13-
import { conf, isIngore } from './helpers'
13+
import { conf, isIngore, matchLineWhen } from './helpers'
1414
import type { HoverResult, Line } from './types'
1515
import { RULES } from '../rules'
1616

@@ -132,12 +132,7 @@ export class LineAnnotation implements Disposable {
132132
return null
133133
}
134134

135-
const values = line.match(
136-
// 1. Match px/rem values, but do skip support for comments, like // 3rem
137-
// 2. Match CSS var(--*)
138-
// 3. Match JS calc('*')
139-
/(?<!\/\/.*)([.0-9]+(px|rem))|var\(--(.*)\)|calc\(['"\`](.*)\)/g
140-
)
135+
const values = matchLineWhen(line)
141136

142137
if (!values) {
143138
return null

src/extension/helpers.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { existsSync, readFileSync } from 'fs'
33
import { parse } from 'jsonc-parser'
44
import { join, resolve } from 'path'
55
import { Uri, workspace } from 'vscode'
6-
import type { Config } from './types'
6+
import type { Config, Line } from './types'
77

88
export let conf!: Config
99
export const eufemiaConfigFileName = '.vscode-eufemia.json'
@@ -102,7 +102,7 @@ export function cleanZero(val: number) {
102102
return val + ''
103103
}
104104

105-
export function findNearestTypes(
105+
export function findNearestType(
106106
size: number,
107107
list: Record<string, string>,
108108
initialValue = 'basis'
@@ -158,3 +158,12 @@ export function getConfig() {
158158
return acc
159159
}, {} as Accumulator) as Config
160160
}
161+
162+
export function matchLineWhen(line: Line) {
163+
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
168+
)
169+
}

src/rules/handleFontSize.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import properties from '@dnb/eufemia/style/properties'
22
import {
33
conf,
4-
findNearestTypes,
4+
findNearestType,
55
localize,
66
cleanProperties,
77
} from '../extension/helpers'
@@ -24,7 +24,7 @@ export const handleFontSize = (): Rule => {
2424
const resultValue = +(
2525
isPx ? fromValue / conf.rootFontSize : fromValue
2626
).toFixed(conf.fixedDigits)
27-
const size = findNearestTypes(resultValue, sizes, 'basis')
27+
const size = findNearestType(resultValue, sizes, 'basis')
2828
const toValue = `var(${varId}${size})`
2929
const unit = isPx ? 'px' : 'rem'
3030
const label = `${fromValue}${unit} 👉 ${toValue}`

src/rules/handleLineHeight.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import properties from '@dnb/eufemia/style/properties'
22
import {
33
conf,
4-
findNearestTypes,
4+
findNearestType,
55
localize,
66
cleanProperties,
77
} from '../extension/helpers'
@@ -24,7 +24,7 @@ export const handleLineHeight = (): Rule => {
2424
const resultValue = +(
2525
isPx ? fromValue / conf.rootFontSize : fromValue
2626
).toFixed(conf.fixedDigits)
27-
const size = findNearestTypes(resultValue, sizes, 'basis')
27+
const size = findNearestType(resultValue, sizes, 'basis')
2828
const toValue = `var(${varId}${size})`
2929
const unit = isPx ? 'px' : 'rem'
3030
const label = `${fromValue}${unit} 👉 ${toValue}`

0 commit comments

Comments
 (0)