Skip to content

Commit 45158a0

Browse files
committed
update tests
1 parent e44d324 commit 45158a0

File tree

2 files changed

+102
-33
lines changed

2 files changed

+102
-33
lines changed

src/template-linter.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,22 +144,11 @@ export default class TemplateLinter {
144144
});
145145
}
146146
}
147-
async lint(textDocument: TextDocument): Promise<Diagnostic[] | undefined> {
148-
if (this._isEnabled === false) {
149-
return;
150-
}
151-
152-
const cwd = process.cwd();
153-
const project = this.getProjectForDocument(textDocument);
154-
155-
if (!project) {
156-
return;
157-
}
158-
147+
getSourcesForDocument(textDocument: TextDocument, project: Project): string[] {
159148
const linterMeta = project.dependencyMap.get('ember-template-lint');
160149

161150
if (!linterMeta) {
162-
return;
151+
return [];
163152
}
164153

165154
let sources = [];
@@ -176,9 +165,26 @@ export default class TemplateLinter {
176165

177166
sources = this.sourcesForDocument(textDocument, linterVersion);
178167
} catch (e) {
168+
return [];
169+
}
170+
171+
return sources;
172+
}
173+
async lint(textDocument: TextDocument): Promise<Diagnostic[] | undefined> {
174+
if (this._isEnabled === false) {
175+
return [];
176+
}
177+
178+
const cwd = process.cwd();
179+
180+
const project = this.getProjectForDocument(textDocument);
181+
182+
if (!project) {
179183
return;
180184
}
181185

186+
const sources = this.getSourcesForDocument(textDocument, project);
187+
182188
if (!sources.length) {
183189
return;
184190
}

test/template-linter-test.ts

Lines changed: 83 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,122 @@
1-
import * as semver from 'semver';
2-
31
import TemplateLinter from '../src/template-linter';
4-
import { type Server } from '../src';
5-
import { TextDocument } from 'vscode-languageserver-textdocument';
2+
import { type Project, type Server } from '../src';
3+
import { type TextDocument } from 'vscode-languageserver-textdocument';
64

7-
describe('template-linter', function () {
8-
describe('sourcesForDocument', function () {
9-
const linter = new TemplateLinter({
10-
options: {
11-
type: 'node',
5+
function getLinterInstance(depName?: string, depVersion?: string): [TemplateLinter, Project] {
6+
const linter = new TemplateLinter({
7+
projectRoots: {
8+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
9+
projectForUri(_url: string) {
10+
return {
11+
dependencyMap: new Map(depName ? [[depName, { package: { name: depName, version: depVersion } }]] : []),
12+
} as Project;
1213
},
13-
} as Server);
14+
},
15+
options: {
16+
type: 'node',
17+
},
18+
} as Server);
1419

20+
return [linter, linter['server'].projectRoots.projectForUri('') as Project];
21+
}
22+
23+
describe('template-linter', function () {
24+
describe('sourcesForDocument', function () {
1525
it('supports empty template-lint version', function () {
26+
const [linter, project] = getLinterInstance();
27+
28+
const doc: TextDocument = {
29+
uri: 'test.gjs',
30+
getText() {
31+
return 'let a = 12;<template>1</template>';
32+
},
33+
} as TextDocument;
34+
35+
expect(linter.getSourcesForDocument(doc, project)).toEqual([]);
36+
});
37+
it('supports incorrect template-lint version [foo-bar]', function () {
38+
const [linter, project] = getLinterInstance('ember-template-lint', 'foo-bar');
39+
40+
const doc: TextDocument = {
41+
uri: 'test.gjs',
42+
getText() {
43+
return 'let a = 12;<template>1</template>';
44+
},
45+
} as TextDocument;
46+
47+
expect(linter.getSourcesForDocument(doc, project)).toEqual([doc.getText()]);
48+
});
49+
it('supports incorrect template-lint version [*]', function () {
50+
const [linter, project] = getLinterInstance('ember-template-lint', '*');
51+
1652
const doc: TextDocument = {
1753
uri: 'test.gjs',
1854
getText() {
1955
return 'let a = 12;<template>1</template>';
2056
},
2157
} as TextDocument;
2258

23-
expect(linter['sourcesForDocument'](doc, null)).toEqual(['let a = 12;<template>1</template>']);
59+
expect(linter.getSourcesForDocument(doc, project)).toEqual([doc.getText()]);
2460
});
25-
it('process gjs for template-lint v4 with ~', function () {
61+
it('process gjs for template-lint v2 with', function () {
62+
const [linter, project] = getLinterInstance('ember-template-lint', '2.0.0');
63+
2664
const doc: TextDocument = {
2765
uri: 'test.gjs',
2866
getText() {
2967
return 'let a = 12;<template>1</template>';
3068
},
3169
} as TextDocument;
3270

33-
console.log(semver.parse('~4.3.1')); // should be counted as 4.3.1
34-
expect(linter['sourcesForDocument'](doc, semver.parse('~4.3.1'))).toEqual([' 1']);
71+
expect(linter.getSourcesForDocument(doc, project)).toEqual([' 1']);
3572
});
36-
it('process gjs for template-lint v4 with ^', function () {
73+
it('process gjs for template-lint v3 with', function () {
74+
const [linter, project] = getLinterInstance('ember-template-lint', '3.3.1');
75+
3776
const doc: TextDocument = {
3877
uri: 'test.gjs',
3978
getText() {
4079
return 'let a = 12;<template>1</template>';
4180
},
4281
} as TextDocument;
4382

44-
console.log(semver.parse('^4.3.1')); // should be counted as 4.3.1
45-
expect(linter['sourcesForDocument'](doc, semver.parse('^4.3.1'))).toEqual([' 1']);
83+
expect(linter.getSourcesForDocument(doc, project)).toEqual([' 1']);
4684
});
47-
it('process gjs for template-lint v4 with strict dependency', function () {
85+
it('process gjs for template-lint v4 with', function () {
86+
const [linter, project] = getLinterInstance('ember-template-lint', '4.3.1');
87+
88+
const doc: TextDocument = {
89+
uri: 'test.gjs',
90+
getText() {
91+
return 'let a = 12;<template>1</template>';
92+
},
93+
} as TextDocument;
94+
95+
expect(linter.getSourcesForDocument(doc, project)).toEqual([' 1']);
96+
});
97+
it('skip gjs processing for template-lint v5', function () {
98+
const [linter, project] = getLinterInstance('ember-template-lint', '5.0.0');
99+
100+
const doc: TextDocument = {
101+
uri: 'test.gjs',
102+
getText() {
103+
return 'let a = 12;<template>1</template>';
104+
},
105+
} as TextDocument;
106+
107+
expect(linter.getSourcesForDocument(doc, project)).toEqual([doc.getText()]);
108+
});
109+
it('skip gjs processing for template-lint v6', function () {
110+
const [linter, project] = getLinterInstance('ember-template-lint', '6.0.0');
111+
48112
const doc: TextDocument = {
49113
uri: 'test.gjs',
50114
getText() {
51115
return 'let a = 12;<template>1</template>';
52116
},
53117
} as TextDocument;
54118

55-
console.log(semver.parse('4.3.1')); // should be counted as 4.3.1
56-
expect(linter['sourcesForDocument'](doc, semver.parse('4.3.1'))).toEqual([' 1']);
119+
expect(linter.getSourcesForDocument(doc, project)).toEqual([doc.getText()]);
57120
});
58121
});
59122
});

0 commit comments

Comments
 (0)