Skip to content
This repository was archived by the owner on May 23, 2025. It is now read-only.

Commit f548d60

Browse files
author
Robin Löffel
authored
Merge pull request #108 from unic/feat/estatico-eslint-use-eslint-directly
feat: use eslint directly, deprecate gulp-eslint
2 parents 348d4de + a13c35e commit f548d60

File tree

7 files changed

+625
-83
lines changed

7 files changed

+625
-83
lines changed
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
module.exports = {
2-
parser: 'babel-eslint',
2+
parser: '@babel/eslint-parser',
33
extends: 'airbnb-base',
44
rules: {
55
'import/no-extraneous-dependencies': 'off',
6-
'import/no-unresolved': 'off',
7-
'class-methods-use-this': 'off'
6+
'class-methods-use-this': 'off',
7+
'function-paren-newline': 'off',
88
},
99
env: {
1010
node: true,
1111
browser: true,
1212
},
1313
};
14-

packages/estatico-eslint/index.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@ const task = (config, env = {}) => {
5757
const gulp = require('gulp');
5858
const plumber = require('gulp-plumber');
5959
const changed = require('gulp-changed-in-place');
60-
const eslint = require('gulp-eslint');
6160
const through = require('through2');
62-
const chalk = require('chalk');
63-
const path = require('path');
61+
const { ESLint } = require('eslint');
6462

6563
return gulp.src(config.src, {
6664
base: config.srcBase,
@@ -72,33 +70,33 @@ const task = (config, env = {}) => {
7270
// Do not pass unchanged files
7371
.pipe(config.plugins.changed ? changed(config.plugins.changed) : through.obj())
7472

75-
// Lint and log formatted results, optionally auto-fix files
76-
.pipe(eslint(config.plugins.eslint).on('error', err => config.logger.error(err, env.dev)))
77-
.pipe(eslint.formatEach())
78-
.pipe(through.obj((file, enc, done) => {
79-
if (file.eslint && (file.eslint.errorCount > 0 || file.eslint.fixed)) {
80-
const relFilePath = path.relative(config.srcBase, file.path);
73+
// pass files directly to eslint
74+
.pipe(through.obj(async (file, _enc, done) => {
75+
const eslint = new ESLint(config.plugins.eslint);
76+
const results = await eslint.lintFiles(file.path);
77+
const [ result ] = results;
78+
const formatter = await eslint.loadFormatter('stylish');
79+
const output = formatter.format(results);
8180

82-
if (file.eslint.errorCount > 0) {
83-
config.logger.error({
84-
message: 'Linting error (details above)',
85-
fileName: relFilePath,
86-
}, env.dev);
87-
}
81+
if (config.plugins.eslint.fix) {
82+
await ESLint.outputFixes(results);
83+
}
8884

89-
// Only keep file in stream if it was fixed
90-
if (file.eslint.fixed) {
91-
config.logger.info(`Automatically fixed linting issues in ${chalk.yellow(relFilePath)}. Set "plugins.eslint.fix" to false to disable this functionality.`);
85+
if (output.length > 0) {
86+
// eslint-disable-next-line no-console
87+
console.log(output);
88+
}
9289

93-
return done(null, file);
94-
}
90+
if (!env.dev && result.errorCount > 0) {
91+
return done(new Error(`Found ${result.errorCount} error(s) in ${file.relative}! Aborting build!`), file);
9592
}
9693

97-
return done();
98-
}))
94+
if (!env.dev && result.warningCount > 0) {
95+
return done(new Error(`Found ${result.warningCount} warning(s) in ${file.relative}! Aborting build!`), file);
96+
}
9997

100-
// Optionally write back to disc
101-
.pipe(config.plugins.eslint.fix ? gulp.dest(config.dest) : through.obj());
98+
return done(null, file);
99+
}).on('error', error => config.logger.error(error, env.dev)))
102100
};
103101

104102
/**

packages/estatico-eslint/package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@unic/estatico-eslint",
3-
"version": "0.0.13",
3+
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",
66
"scripts": {
@@ -11,26 +11,25 @@
1111
"repository": "https://github.com/unic/estatico-nou/tree/master/packages/estatico-eslint",
1212
"license": "Apache-2.0",
1313
"dependencies": {
14+
"@babel/core": "^7.16.5",
15+
"@babel/eslint-parser": "^7.16.5",
1416
"@unic/estatico-utils": "0.0.10",
15-
"babel-eslint": "^9.0.0",
16-
"chalk": "^2.4.1",
17+
"eslint": "^8.5.0",
1718
"eslint-config-airbnb-base": "^12.1.0",
1819
"eslint-plugin-import": "^2.11.0",
1920
"gulp-changed-in-place": "^2.3.0",
20-
"gulp-eslint": "^4.0.2",
2121
"gulp-plumber": "^1.2.0",
2222
"joi": "^13.2.0",
2323
"through2": "^2.0.3"
2424
},
2525
"engines": {
26-
"node": ">=8"
26+
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
2727
},
2828
"publishConfig": {
2929
"access": "public"
3030
},
3131
"devDependencies": {
3232
"ava": "^0.25.0",
33-
"del": "^3.0.0",
3433
"lodash.merge": "^4.6.1",
3534
"sinon": "^5.0.7"
3635
},
File renamed without changes.
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module.exports = {
22
extends: 'airbnb-base',
3-
rules: {
4-
'import/no-extraneous-dependencies': 'off',
5-
'import/no-unresolved': 'off'
6-
}
3+
parserOptions: {
4+
requireConfigFile: false,
5+
},
76
};

packages/estatico-eslint/test/index.js

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ const test = require('ava');
22
const sinon = require('sinon');
33
const utils = require('@unic/estatico-utils').test;
44
const path = require('path');
5-
const del = require('del');
65
const merge = require('lodash.merge');
76
const task = require('../index.js');
7+
const { readFileSync, writeFileSync } = require('fs');
88

99
const defaults = {
1010
src: [
@@ -14,47 +14,57 @@ const defaults = {
1414
dest: './test/results/',
1515
};
1616

17-
test.cb('with --fix', (t) => {
17+
test.cb('runs just to lint', (t) => {
1818
const spy = sinon.spy(console, 'log');
19+
const options = merge({}, defaults, {
20+
plugins: {
21+
changed: null,
22+
},
23+
});
1924

20-
task(defaults, {
25+
task(options, {
2126
dev: true,
22-
fix: true,
23-
})().on('end', () => {
27+
})().on('finish', () => {
2428
spy.restore();
25-
2629
const log = utils.stripLogs(spy);
2730

28-
t.notRegex(log, /'hello' is never reassigned. Use 'const' instead/);
29-
t.notRegex(log, /estatico-eslint Linting error in file main\.js \(details above\)/);
30-
31-
utils.compareFiles(t, path.join(__dirname, 'expected/default/*'));
31+
// the monitored console output should contain those three errors
32+
t.true(log.includes("'hello' is never reassigned. Use 'const' instead"));
33+
t.true(log.includes('Missing semicolon'));
34+
t.true(log.includes('Unexpected console statement'));
3235

3336
t.end();
3437
});
3538
});
3639

37-
test.cb('without --fix', (t) => {
38-
const options = merge({}, defaults, {
39-
plugins: {
40-
changed: null,
41-
},
42-
});
43-
40+
test.cb('runs and takes care of autofixable errors', (t) => {
4441
const spy = sinon.spy(console, 'log');
4542

46-
task(options, {
43+
// read the contents of the to-be fixed file in its original state
44+
const untouchedFileContents = readFileSync(path.join(__dirname, './fixtures/main.js'), 'utf-8');
45+
// read the fixture file, containing the expected autofix result
46+
const fixtureFileContents = readFileSync(path.join(__dirname, './expected/main.js'), 'utf-8');
47+
48+
task(defaults, {
4749
dev: true,
50+
fix: true,
4851
})().on('finish', () => {
4952
spy.restore();
50-
5153
const log = utils.stripLogs(spy);
5254

53-
t.regex(log, /'hello' is never reassigned. Use 'const' instead/);
54-
t.regex(log, /estatico-eslint main\.js Linting error \(details above\)/);
55+
// read the contents of the file that got autofixed by eslint
56+
const touchedFileContents = readFileSync(path.join(__dirname, './fixtures/main.js'), 'utf-8');
57+
// revert the autofixed file to its original state
58+
writeFileSync(path.join(__dirname, './fixtures/main.js'), untouchedFileContents);
59+
60+
// the autofixed file should contain the same as the expected file
61+
t.true(touchedFileContents === fixtureFileContents);
62+
// should print out the unfixable error
63+
t.true(log.includes('Unexpected console statement'));
64+
// should not print out fixable errors, because eslint already took care of them
65+
t.false(log.includes("'hello' is never reassigned. Use 'const' instead"));
66+
t.false(log.includes('Missing semicolon'));
5567

5668
t.end();
5769
});
5870
});
59-
60-
test.afterEach(() => del(path.join(__dirname, '/results')));

0 commit comments

Comments
 (0)