-
Notifications
You must be signed in to change notification settings - Fork 648
/
hint.ts
74 lines (56 loc) 路 2.28 KB
/
hint.ts
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* @fileoverview Check if button has type attribute set.
*/
import { HintContext } from 'hint/dist/src/lib/hint-context';
import { IHint, ElementFound } from 'hint/dist/src/lib/types';
import { debug as d } from '@hint/utils-debug';
import { Severity } from '@hint/utils-types';
import { HTMLElement } from '@hint/utils-dom';
import meta from './meta';
import { getMessage } from './i18n.import';
const debug: debug.IDebugger = d(__filename);
/*
* ------------------------------------------------------------------------------
* Public
* ------------------------------------------------------------------------------
*/
export default class ButtonTypeHint implements IHint {
public static readonly meta = meta;
public constructor(context: HintContext) {
const inAForm = (element: HTMLElement): boolean => {
const parent = element.parentElement;
if (!parent) {
return false;
}
if (parent.nodeName === 'form') {
return true;
}
return inAForm(parent);
};
const validateElement = (elementFound: ElementFound) => {
const { resource } = elementFound;
const allowedTypes = ['submit', 'reset', 'button'];
debug('Validating hint button-type');
const element = elementFound.element;
const elementType = element.getAttribute('type');
if (element.isAttributeAnExpression('type')) {
// Assume template expressions will map to a valid value.
} else if (elementType === null || elementType === '') {
const severity = inAForm(element) ?
Severity.warning :
Severity.hint;
context.report(
resource,
getMessage('attributeNotSet', context.language),
{ element, severity }
);
} else if (!allowedTypes.includes(elementType.toLowerCase())) {
context.report(
resource,
getMessage('invalidType', context.language, elementType),
{ element, severity: Severity.error });
}
};
context.on('element::button', validateElement);
}
}