Skip to content

Commit 5ba5bd2

Browse files
committed
fix(autofix): 🐛 write autofixes to disk for eslint
1 parent 006b7c0 commit 5ba5bd2

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

lib/builder/src/builder.spec.ts

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { Architect } from '@angular-devkit/architect';
44
import { TestingArchitectHost } from '@angular-devkit/architect/testing';
55
import { schema } from '@angular-devkit/core';
66
import { Logger } from '@angular-devkit/core/src/logger';
7+
import { exec } from 'child_process';
78

89
describe('Lint', () => {
910
let architect: Architect;
1011
let architectHost: TestingArchitectHost;
11-
const logger = new Logger('test');
12+
let logger: Logger;
1213

1314
beforeEach(async () => {
1415
const registry = new schema.CoreSchemaRegistry();
@@ -18,6 +19,7 @@ describe('Lint', () => {
1819
// Since we don't use those, both are the same in this case.
1920
architectHost = new TestingArchitectHost('./test', './test');
2021
architect = new Architect(architectHost, registry);
22+
logger = new Logger('test');
2123

2224
// This will either take a Node package name, or a path to the directory
2325
// for the package.json file.
@@ -26,6 +28,10 @@ describe('Lint', () => {
2628
console.info('#', Array.from(architectHost._builderMap.keys()));
2729
});
2830

31+
afterEach(async () => {
32+
exec('git restore test/src/');
33+
});
34+
2935
it('has created the correct linting results', async () => {
3036
// A "run" can have multiple outputs, and contains progress information.
3137
const run = await architect.scheduleBuilder(
@@ -94,4 +100,66 @@ describe('Lint', () => {
94100
},
95101
]);
96102
});
103+
104+
it('autofix lint issues when using fix option', async () => {
105+
// A "run" can have multiple outputs, and contains progress information.
106+
const run = await architect.scheduleBuilder(
107+
'@krema/angular-eslint-stylelint-builder:lint',
108+
{
109+
eslintFilePatterns: ['src/**/*.ts'],
110+
stylelintFilePatterns: ['src/**/*.css'],
111+
fix: true,
112+
},
113+
{ logger }
114+
);
115+
const loggerPromise = logger
116+
.pipe(
117+
toArray(),
118+
map(messages =>
119+
messages.map(y => {
120+
console.log('>>', y.message);
121+
return { level: y.level, message: y.message };
122+
})
123+
)
124+
)
125+
.toPromise();
126+
127+
// The "result" member (of type BuilderOutput) is the next output.
128+
await run.result;
129+
130+
// Stop the builder from running. This stops Architect from keeping
131+
// the builder-associated states in memory, since builders keep waiting
132+
// to be scheduled.
133+
await run.stop();
134+
logger.complete();
135+
136+
expect(loggerPromise).resolves.toEqual([
137+
{
138+
level: 'info',
139+
message: '\nLinting "<???>"...',
140+
},
141+
{
142+
level: 'debug',
143+
message: 'Running eslint...',
144+
},
145+
{
146+
level: 'debug',
147+
message: 'Running stylelint...',
148+
},
149+
{
150+
level: 'info',
151+
// @ts-ignore
152+
message: expect.toIncludeMultiple([
153+
//file.ts
154+
'file.ts\n' + " 3:3 error Unexpected 'debugger' statement eslint\tno-debugger\n",
155+
// info messages
156+
'✖ 1 problem (1 error, 0 warnings)\n',
157+
]),
158+
},
159+
{
160+
level: 'error',
161+
message: 'Lint errors found in the listed files.\n',
162+
},
163+
]);
164+
});
97165
});

lib/builder/src/eslint/eslint-linter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Schema } from '../schema';
22
import type { ESLint } from 'eslint';
33
import { join, resolve } from 'path';
4-
import { checkESlintVersion, loadESLint } from './eslint-utils';
4+
import { checkESlintVersion, loadESLint, outputFixes } from './eslint-utils';
55

66
export async function lint(
77
linterInstance: ESLint,
@@ -13,6 +13,9 @@ export async function lint(
1313
options.eslintFilePatterns.map(p => join(workspaceRoot, p))
1414
);
1515

16+
// Modify the files with the fixed code.
17+
await outputFixes(lintResults);
18+
1619
return lintResults.map(result => ({
1720
...result,
1821
messages: result.messages.map(message => ({

0 commit comments

Comments
 (0)