-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser.test.js
More file actions
160 lines (135 loc) · 4.69 KB
/
Copy pathparser.test.js
File metadata and controls
160 lines (135 loc) · 4.69 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { describe, expect, it } from "vitest";
import { Linter } from "eslint";
import emberEstreeParser from "./parser.js";
/**
* These tests invoke ESLint's Linter API with our custom parser
* to prove the parser works end-to-end — not just as a function call,
* but as a real ESLint parser plugin that ESLint can traverse and lint.
*/
function createLinter() {
return new Linter({ configType: "flat" });
}
function lint(source, rules = {}) {
const linter = createLinter();
return linter.verify(source, {
languageOptions: {
parser: emberEstreeParser,
},
rules,
});
}
describe("ESLint parser example — invokes ESLint", () => {
it("parses gjs without errors", () => {
const source = `const x = <template><h1>Hello</h1></template>;`;
const messages = lint(source);
// No parse errors
const parseErrors = messages.filter((m) => m.fatal);
expect(parseErrors).toEqual([]);
});
it("ESLint can report lint violations on JS around templates", () => {
const source = `
const x = <template><h1>Hello</h1></template>;
with (x) { console.log(x); }`;
const messages = lint(source, { "no-with": "error" });
expect(messages.length).toBeGreaterThan(0);
expect(messages[0].ruleId).toBe("no-with");
});
it("no-unused-vars works on JS identifiers", () => {
// 'y' is declared but never used
const source = `
const y = 1;
const x = <template><h1>Hello</h1></template>;
export { x };`;
const messages = lint(source, { "no-unused-vars": "error" });
const unusedVars = messages.filter((m) => m.ruleId === "no-unused-vars");
expect(unusedVars.length).toBeGreaterThan(0);
expect(unusedVars[0].message).toContain("y");
});
it("no-undef detects undefined variables referenced within a template", () => {
// 'undefinedVar' is not defined anywhere — used inside {{}} in template
const source = `const x = <template>{{undefinedVar}}</template>;`;
const messages = lint(source, { "no-undef": "error" });
const undefErrors = messages.filter((m) => m.ruleId === "no-undef");
expect(undefErrors.length).toBeGreaterThan(0);
expect(undefErrors[0].message).toContain("undefinedVar");
});
it("ESLint traverses Glimmer nodes without crashing", () => {
// A complex template with nested Glimmer nodes
const source = `
const Greeting = <template>
<div class="wrapper">
<h1>{{@name}}</h1>
<p>Welcome!</p>
</div>
</template>;
export { Greeting };`;
const messages = lint(source);
const parseErrors = messages.filter((m) => m.fatal);
expect(parseErrors).toEqual([]);
});
it("handles multiple templates in one file", () => {
const source = `
const A = <template><h1>A</h1></template>;
const B = <template><h2>B</h2></template>;
export { A, B };`;
const messages = lint(source);
const parseErrors = messages.filter((m) => m.fatal);
expect(parseErrors).toEqual([]);
});
it("handles gts-style class with template", () => {
const source = `
export default class Greeting {
<template><h1>Hello</h1></template>
}`;
const messages = lint(source);
const parseErrors = messages.filter((m) => m.fatal);
expect(parseErrors).toEqual([]);
});
it("no-eval rule works on JS around templates", () => {
const source = `
const x = <template><h1>Hello</h1></template>;
eval("1 + 1");
export { x };`;
const messages = lint(source, { "no-eval": "error" });
const evalErrors = messages.filter((m) => m.ruleId === "no-eval");
expect(evalErrors.length).toBeGreaterThan(0);
});
it("ESLint can use a custom rule that visits Glimmer nodes", () => {
const source = `const x = <template><h1>Hello</h1></template>;`;
const linter = createLinter();
const glimmerNodeTypes = [];
// Define a custom rule plugin that collects Glimmer node types
const plugin = {
rules: {
"collect-glimmer-nodes": {
create(_context) {
// Return visitors for each Glimmer node type
return {
GlimmerTemplate(node) {
glimmerNodeTypes.push(node.type);
},
GlimmerElementNode(node) {
glimmerNodeTypes.push(node.type);
},
GlimmerTextNode(node) {
glimmerNodeTypes.push(node.type);
},
};
},
},
},
};
linter.verify(source, {
plugins: { custom: plugin },
languageOptions: {
parser: emberEstreeParser,
},
rules: {
"custom/collect-glimmer-nodes": "error",
},
});
expect(glimmerNodeTypes).toContain("GlimmerTemplate");
expect(glimmerNodeTypes).toContain("GlimmerElementNode");
expect(glimmerNodeTypes).toContain("GlimmerTextNode");
});
});