-
-
Notifications
You must be signed in to change notification settings - Fork 205
/
Copy pathtemplate-no-let-reference.js
50 lines (46 loc) · 1.55 KB
/
template-no-let-reference.js
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
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow referencing let variables in \\<template\\>',
category: 'Ember Octane',
recommendedGjs: true,
recommendedGts: true,
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-let-reference.md',
},
fixable: null,
schema: [],
messages: {
'no-let': 'update-able variables are not supported in templates, reference a const variable',
},
},
create: (context) => {
const sourceCode = context.sourceCode;
const pathname = context.physicalFilename;
function checkIfWritableReferences(node, scope) {
// Return if the pathname starts with '/start/'
if (pathname.startsWith('/tests/')) {
return;
}
const ref = scope.references.find((v) => v.identifier === node);
if (!ref) {
return;
}
if (ref.resolved?.identifiers.some((i) => ['let', 'var'].includes(i.parent.parent.kind))) {
context.report({ node, messageId: 'no-let' });
}
}
return {
GlimmerPathExpression(node) {
checkIfWritableReferences(node.head, sourceCode.getScope(node));
},
GlimmerElementNode(node) {
// glimmer element is in its own scope, need to get upper scope
const scope = sourceCode.getScope(node.parent);
// GlimmerElementNode is not referenced, instead use tag name parts[0]
checkIfWritableReferences(node.parts[0], scope);
},
};
},
};