-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathno-duplicate-template-bindings.ts
64 lines (55 loc) · 1.92 KB
/
no-duplicate-template-bindings.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
/**
* @fileoverview Disallows duplicate names in template bindings
* @author James Garbutt <https://github.com/43081j>
*/
import {Rule} from 'eslint';
import * as ESTree from 'estree';
import {TemplateAnalyzer} from '../template-analyzer.js';
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
export const rule: Rule.RuleModule = {
meta: {
docs: {
description: 'Disallows duplicate names in template bindings',
recommended: false,
url: 'https://github.com/43081j/eslint-plugin-lit/blob/master/docs/rules/no-duplicate-template-bindings.md'
},
schema: [],
messages: {
duplicateBinding: 'Duplicate bindings are not allowed.'
}
},
create(context): Rule.RuleListener {
const source = context.getSourceCode();
//----------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------
//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
return {
TaggedTemplateExpression: (node: ESTree.Node): void => {
if (
node.type === 'TaggedTemplateExpression' &&
node.tag.type === 'Identifier' &&
node.tag.name === 'html'
) {
const analyzer = TemplateAnalyzer.create(node);
const dupeErrors = analyzer.errors.filter(
(err): boolean => err.code === 'duplicate-attribute'
);
for (const err of dupeErrors) {
const loc = analyzer.resolveLocation(err, source);
if (loc) {
context.report({
loc: loc,
messageId: 'duplicateBinding'
});
}
}
}
}
};
}
};