-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(plugin-stylelint): change getSeverityFromRuleConfig logic pl…
…us add test
- Loading branch information
Showing
5 changed files
with
118 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
import type { ConfigRuleSettings, Primary, Severity } from 'stylelint'; | ||
|
||
// Typing resource https://stylelint.io/user-guide/configure/ | ||
/** Config rule setting of Stylelint excluding null and undefined values */ | ||
export type ActiveConfigRuleSetting = Exclude< | ||
ConfigRuleSettings<Primary, Record<string, unknown>>, | ||
null | undefined | ||
>; | ||
|
||
/** Output of the `getNormalizedConfigForFile` function. Config file of Stylelint */ | ||
export type NormalizedStyleLintConfig = { | ||
config: { rules: Record<string, unknown[]> }; | ||
config: { | ||
rules: Record<string, ConfigRuleSettings<Primary, Record<string, any>>>; | ||
defaultSeverity?: Severity; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/plugin-stylelint/src/lib/runner/utils.unit.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import type { ActiveConfigRuleSetting } from './model.js'; | ||
import { getSeverityFromRuleConfig } from './utils.js'; | ||
|
||
describe('getSeverityFromRuleConfig', () => { | ||
it('should respect the default severity when from the default', () => { | ||
expect(getSeverityFromRuleConfig([true])).toBe('error'); | ||
}); | ||
|
||
it('should consider the default severity when its different from the default', () => { | ||
expect(getSeverityFromRuleConfig([true], 'warning')).toBe('warning'); | ||
}); | ||
|
||
it.each([true, 5, 'percentage', ['/\\[.+]/', 'percentage'], { a: 1 }])( | ||
'should return the default severity for a primary value %s', | ||
ruleConfig => { | ||
expect( | ||
getSeverityFromRuleConfig(ruleConfig as ActiveConfigRuleSetting), | ||
).toBe('error'); | ||
}, | ||
); | ||
|
||
it('should return the default severity when the rule config does not have a secondary item', () => { | ||
expect(getSeverityFromRuleConfig([true])).toBe('error'); | ||
}); | ||
|
||
it('should return the default severity when the secondary item is missing the `severity` property', () => { | ||
expect(getSeverityFromRuleConfig([true, {}])).toBe('error'); | ||
}); | ||
|
||
it('should return the default severity when `severity` property is of type function', () => { | ||
expect(getSeverityFromRuleConfig([true, { severity: () => {} }])).toBe( | ||
'error', | ||
); | ||
}); | ||
|
||
it.each([ | ||
{ ruleConfig: [true, { severity: 'warning' }], expected: 'warning' }, | ||
{ ruleConfig: [true, { severity: 'error' }], expected: 'error' }, | ||
])('should return the set severity `%s`', ({ ruleConfig, expected }) => { | ||
expect(getSeverityFromRuleConfig(ruleConfig)).toBe(expected); | ||
}); | ||
|
||
it.each([null, undefined])( | ||
'should return the default severity for disabled rules %s', | ||
ruleConfig => { | ||
expect( | ||
getSeverityFromRuleConfig( | ||
ruleConfig as unknown as ActiveConfigRuleSetting, | ||
), | ||
).toBe('error'); | ||
}, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters