All files / src/rules no-dup-class.ts

100% Statements 11/11
100% Branches 4/4
100% Functions 3/3
100% Lines 11/11
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  1x     1x           6x 3x 1x     2x 2x 2x 5x 1x   5x        
import { Rule, RuleReport, RuleParserProxy } from '../rule';
import { DOMTokenList } from '../dom';
import { AttributeEvent } from '../event';
 
export = {
	name: 'no-dup-class',
	init,
} as Rule;
 
function init(parser: RuleParserProxy){
	parser.on('attr', (event: AttributeEvent, report: RuleReport) => {
		if (event.key.toLowerCase() !== 'class'){
			return;
		}
 
		const classes = new DOMTokenList(event.value);
		const unique: Set<string> = new Set();
		classes.forEach(cur => {
			if (unique.has(cur)) {
				report(event.target, `Class "${cur}" duplicated`, event.location);
			}
			unique.add(cur);
		});
	});
}