Skip to content

Commit 1c856af

Browse files
authored
Merge pull request #23 from ember-template-lint/new-formatter-api
2 parents 8a86083 + 2734de6 commit 1c856af

5 files changed

Lines changed: 17210 additions & 93 deletions

File tree

__tests__/index-test.js

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,53 @@
1-
const execa = require("execa");
2-
const stripAnsi = require("strip-ansi");
3-
const Project = require("./__utils__/fake-project");
4-
const SonarQubeFormatter = require("../index");
1+
const execa = require('execa');
2+
const Project = require('./__utils__/fake-project');
53

6-
class MockConsole {
7-
buffer = [];
8-
9-
get lines() {
10-
return this.buffer.map((message) => {
11-
return stripAnsi(message).trim();
12-
});
13-
}
14-
15-
log(message) {
16-
this.buffer.push(message);
17-
}
18-
19-
toString() {
20-
return stripAnsi(this.buffer.join("\n"));
21-
}
22-
}
23-
24-
describe("SonarQube Formatter", () => {
4+
describe('SonarQube Formatter', () => {
255
let project;
266

277
beforeEach(() => {
28-
project = new Project("fake-project");
8+
project = new Project('fake-project');
299
});
3010

3111
afterEach(() => {
3212
// project.dispose();
3313
});
3414

35-
it("can format output from no results", () => {
36-
let mockConsole = new MockConsole();
37-
let formatter = new SonarQubeFormatter({
38-
console: mockConsole,
15+
it('can format output from no results', async () => {
16+
project.setConfig({
17+
rules: {
18+
'no-bare-strings': 'warn',
19+
},
20+
});
21+
project.write({
22+
app: {
23+
templates: {
24+
'application.hbs': '<div></div>',
25+
},
26+
},
3927
});
4028

41-
formatter.print({});
29+
let result = await emberTemplateLint();
4230

43-
expect(mockConsole.toString()).toMatchInlineSnapshot(`
31+
expect(result.stdout).toMatchInlineSnapshot(`
4432
"{
4533
\\"issues\\": []
4634
}"
4735
`);
4836
});
4937

50-
it("can format output when there are warnings", async () => {
38+
it('can format output when there are warnings', async () => {
5139
project.setConfig({
5240
rules: {
53-
"no-bare-strings": "warn",
41+
'no-bare-strings': 'warn',
5442
},
5543
});
5644
project.write({
5745
app: {
5846
templates: {
59-
"application.hbs":
60-
"<h2>Here too!!</h2> <div>Bare strings are bad...</div>",
47+
'application.hbs':
48+
'<h2>Here too!!</h2> <div>Bare strings are bad...</div>',
6149
components: {
62-
"foo.hbs": "{{fooData}}",
50+
'foo.hbs': '{{fooData}}',
6351
},
6452
},
6553
},
@@ -107,19 +95,19 @@ describe("SonarQube Formatter", () => {
10795
`);
10896
});
10997

110-
it("can format output when there are errors", async () => {
98+
it('can format output when there are errors', async () => {
11199
project.setConfig({
112100
rules: {
113-
"no-bare-strings": "error",
101+
'no-bare-strings': 'error',
114102
},
115103
});
116104
project.write({
117105
app: {
118106
templates: {
119-
"application.hbs":
120-
"<h2>Here too!!</h2> <div>Bare strings are bad...</div>",
107+
'application.hbs':
108+
'<h2>Here too!!</h2> <div>Bare strings are bad...</div>',
121109
components: {
122-
"foo.hbs": "{{fooData}}",
110+
'foo.hbs': '{{fooData}}',
123111
},
124112
},
125113
},
@@ -189,11 +177,11 @@ describe("SonarQube Formatter", () => {
189177
process.execPath,
190178
[
191179
require.resolve(
192-
"../node_modules/ember-template-lint/bin/ember-template-lint.js"
180+
'../node_modules/ember-template-lint/bin/ember-template-lint.js'
193181
),
194-
".",
195-
"--format",
196-
require.resolve(".."),
182+
'.',
183+
'--format',
184+
require.resolve('..'),
197185
...argumentsOrOptions,
198186
],
199187
mergedOptions

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@ const SONARQUBE_TYPE = {
1313
};
1414

1515
module.exports = class SonarQubeFormatter {
16+
defaultFileExtension = 'json';
17+
1618
constructor(options = {}) {
1719
this.options = options;
18-
this.console = options.console || console;
1920
}
2021

21-
print(results) {
22+
format(results) {
2223
const issues = [];
2324

2425
if (this.options.hasResultData) {
2526
for (const filePath of Object.keys(results.files)) {
2627
let result = results.files[filePath];
2728
let relativePath = path.relative(
28-
this.options.workingDir,
29+
this.options.workingDirectory,
2930
result.filePath
3031
);
3132

@@ -51,6 +52,6 @@ module.exports = class SonarQubeFormatter {
5152
}
5253

5354
// eslint-disable-next-line unicorn/no-null
54-
this.console.log(JSON.stringify({ issues }, null, 2));
55+
return JSON.stringify({ issues }, null, 2);
5556
}
5657
};

0 commit comments

Comments
 (0)