All files / src/rules attr-quotes.ts

87.5% Statements 14/16
81.82% Branches 9/11
100% Functions 3/3
87.5% Lines 14/16
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45      1x                     11x 11x 11x       11x 9x 2x             9x       9x 2x 1x   2x     7x 2x      
import { Rule, RuleReport, RuleParserProxy } from '../rule';
import { AttributeEvent } from '../event';
 
export = {
	name: 'attr-quotes',
	init,
 
	defaults: {
		style: 'double',
		unquoted: false,
	},
} as Rule;
 
function init(parser: RuleParserProxy, options: any){
	parser.on('attr', validate);
	this.options = Object.assign(this.defaults, options);
	this.expected = parseStyle(this.options.style);
}
 
function parseStyle(style: string){
	switch (style.toLowerCase()){
	case 'double': return '"';
	case 'single': return "'";
	default: return '"';
	}
}
 
function validate(event: AttributeEvent, report: RuleReport){
	/* ignore attributes with not value */
	Iif (typeof event.value === 'undefined'){
		return;
	}
 
	if (typeof event.quote === 'undefined'){
		if (this.options.unquoted === false){
			report(event.target, `Attribute "${event.key}" using unquoted value`);
		}
		return;
	}
 
	if (event.quote !== this.expected){
		report(event.target, `Attribute "${event.key}" used ${event.quote} instead of expected ${this.expected}`);
	}
}