All files / src/rules id-pattern.ts

88.89% Statements 8/9
75% Branches 3/4
100% Functions 2/2
88.89% Lines 8/9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28    1x   1x                   2x 2x   2x 2x       2x 1x        
import { Rule, RuleReport, RuleParserProxy } from '../rule';
import { AttributeEvent } from '../event';
import { parsePattern } from '../pattern';
 
export = {
	name: 'id-pattern',
	init,
 
	defaults: {
		pattern: 'kebabcase',
	},
} as Rule;
 
function init(parser: RuleParserProxy, userOptions: any){
	const options = Object.assign({}, this.defaults, userOptions);
	const pattern = parsePattern(options.pattern);
 
	parser.on('attr', (event: AttributeEvent, report: RuleReport) => {
		Iif (event.key.toLowerCase() !== 'id'){
			return;
		}
 
		if (!event.value.match(pattern)){
			report(event.target, `ID "${event.value}" does not match required pattern "${pattern}"`);
		}
	});
}