-
Notifications
You must be signed in to change notification settings - Fork 2
/
no-invalid-template-interpolation.js
73 lines (69 loc) · 2.19 KB
/
no-invalid-template-interpolation.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"use strict";
const { getSourceCode } = require("eslint-compat-utils");
module.exports = {
meta: {
docs: {
description:
"disallow other than expression in micro-template interpolation. (ex. :ng: `<%= if (test) { %>`)",
category: "best-practices",
url: "https://ota-meshi.github.io/eslint-plugin-lodash-template/rules/no-invalid-template-interpolation.html",
},
fixable: null,
messages: {
missingStatement: "Empty statement.",
unexpected: "Expected an expression, but a not expressions.",
},
schema: [],
type: "problem",
},
create(context) {
const sourceCode = getSourceCode(context);
if (!sourceCode.parserServices.getMicroTemplateService) {
return {};
}
const microTemplateService =
sourceCode.parserServices.getMicroTemplateService();
/**
* process micro-template interpolation node
* @param {ASTNode} node The AST node.
* @returns {void} undefined.
*/
function processNode(node) {
if (!node.code.trim()) {
// empty
return;
}
const innerTokens =
microTemplateService.getMicroTemplateTokensInfo(
node,
).innerTokens;
if (!innerTokens.length) {
context.report({
node,
messageId: "missingStatement",
});
return;
}
const statement =
microTemplateService.getMicroTemplateExpressionStatement(
node,
sourceCode,
);
if (statement) {
return;
}
context.report({
node,
messageId: "unexpected",
});
}
return {
"Program:exit"() {
microTemplateService.traverseMicroTemplates({
MicroTemplateInterpolate: processNode,
MicroTemplateEscape: processNode,
});
},
};
},
};