-
Notifications
You must be signed in to change notification settings - Fork 648
Expand file tree
/
Copy pathenforce-custom-element-prefix.ts
More file actions
76 lines (67 loc) · 2.05 KB
/
enforce-custom-element-prefix.ts
File metadata and controls
76 lines (67 loc) · 2.05 KB
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
75
76
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import type {TSESTree} from '@typescript-eslint/types';
import {createRule} from './utils/ruleCreator.ts';
function getTextValue(node: TSESTree.Node): string|undefined {
if (node.type === 'Literal') {
return node.value?.toString();
}
if (node.type === 'TemplateLiteral') {
if (node.quasis.length === 0) {
return;
}
return node.quasis[0].value.cooked ?? undefined;
}
return;
}
export default createRule({
name: 'enforce-custom-element-prefix',
meta: {
type: 'problem',
docs: {
description: 'Enforce that all customElements.define() calls use a "devtools-" prefix for the tag name.',
category: 'Possible Errors',
},
messages: {
onlyStatic: 'Custom element tag name should be called with static string.',
missingPrefix: 'Custom element tag name \'{{tagName}}\' must be prefixed with \'devtools-\'.',
},
schema: [],
},
defaultOptions: [],
create: function(context) {
return {
CallExpression(node: TSESTree.CallExpression) {
const callee = node.callee;
// customElements.define(<string>, <class>);
if (callee.type !== 'MemberExpression' || callee.object.type !== 'Identifier' ||
callee.object.name !== 'customElements' || callee.property.type !== 'Identifier' ||
callee.property.name !== 'define') {
return;
}
const firstArg = node.arguments[0];
if (!firstArg) {
return;
}
const tagName = getTextValue(firstArg);
if (typeof tagName !== 'string') {
context.report({
node: firstArg,
messageId: 'onlyStatic',
});
return;
}
if (!tagName.startsWith('devtools-')) {
context.report({
node: firstArg,
messageId: 'missingPrefix',
data: {
tagName,
},
});
}
},
};
},
});