Skip to content

Commit 1ef167b

Browse files
committed
fix: ensure component name is validated correctly
1 parent e8022cc commit 1ef167b

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

src/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ describe('rule implementation', () => {
222222
accept: [
223223
{
224224
description: 'works with the default option',
225-
code: `.button { --Component: red; }`,
225+
code: `.button { --Component-something: red; }`,
226226
},
227227
],
228228
})

src/validate.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,28 @@ describe('isValidCustomProperty (default)', () => {
66
expect(isValidCustomProperty('--Button-color', 'Button')).toBe(true)
77
expect(isValidCustomProperty('--Button-Color', 'Button')).toBe(true)
88
expect(isValidCustomProperty('--Button-123', 'Button')).toBe(true)
9-
expect(isValidCustomProperty('--Button', 'Button')).toBe(true)
109
expect(
1110
isValidCustomProperty('--UserProfile-avatar-size', 'UserProfile'),
1211
).toBe(true)
1312
})
1413

14+
test('rejects bare component name without suffix', () => {
15+
expect(isValidCustomProperty('--Button', 'Button')).toBe(false)
16+
expect(isValidCustomProperty('--Button-', 'Button')).toBe(false)
17+
})
18+
19+
test('rejects component name not followed by hyphen', () => {
20+
expect(isValidCustomProperty('--Buttonsdfs-color', 'Button')).toBe(false)
21+
expect(isValidCustomProperty('--Buttonextra', 'Button')).toBe(false)
22+
})
23+
1524
test('rejects wrong component names', () => {
1625
expect(isValidCustomProperty('--Wrong-color', 'Button')).toBe(false)
1726
expect(isValidCustomProperty('--button-color', 'Button')).toBe(false)
1827
})
1928
})
2029

21-
describe('isValidSuitCssProperty (strict SUIT CSS)', () => {
30+
describe('isValidSuitCssProperty', () => {
2231
test('accepts basic valid patterns', () => {
2332
expect(isValidSuitCssProperty('--Button-color', 'Button')).toBe(true)
2433
expect(isValidSuitCssProperty('--Button-icon-size', 'Button')).toBe(true)

src/validate.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ export function isValidCustomProperty(
1212
}
1313

1414
const {pattern} = config
15+
const afterComponent = property.slice(`--${componentName}`.length)
1516

17+
// ensures the component has a hyphen at the end and that we also have
18+
// some text there, just `--Component: red` is not allowed
1619
if (!pattern) {
17-
return true
20+
return afterComponent.startsWith('-') && afterComponent.length > 1
1821
}
1922

20-
const afterComponent = property.slice(`--${componentName}`.length)
21-
2223
return pattern.test(afterComponent)
2324
}
2425

0 commit comments

Comments
 (0)